From e2e3e0c450a46d87cdd59047c5d97ced15e73b4b Mon Sep 17 00:00:00 2001 From: Johannes Link Date: Sun, 18 Jun 2023 09:46:12 +0200 Subject: [PATCH] Added experimental MethodLifecycleContext interface --- .../api/lifecycle/MethodLifecycleContext.java | 59 +++++++++++++++++++ .../lifecycle/PropertyLifecycleContext.java | 43 +------------- .../api/lifecycle/TryLifecycleContext.java | 37 +----------- .../execution/DefaultTryLifecycleContext.java | 5 ++ .../java/net/jqwik/engine/TestHelper.java | 5 ++ .../lifecycle/AroundTryHookTests.java | 1 + 6 files changed, 72 insertions(+), 78 deletions(-) create mode 100644 api/src/main/java/net/jqwik/api/lifecycle/MethodLifecycleContext.java diff --git a/api/src/main/java/net/jqwik/api/lifecycle/MethodLifecycleContext.java b/api/src/main/java/net/jqwik/api/lifecycle/MethodLifecycleContext.java new file mode 100644 index 000000000..80513ee00 --- /dev/null +++ b/api/src/main/java/net/jqwik/api/lifecycle/MethodLifecycleContext.java @@ -0,0 +1,59 @@ +package net.jqwik.api.lifecycle; + +import java.lang.reflect.*; +import java.util.*; + +import org.apiguardian.api.*; + +import static org.apiguardian.api.API.Status.*; + +/** + * The context information for all method-based lifecyle contexts. + * + * @see PropertyLifecycleContext + * @see TryLifecycleContext + */ +@API(status = EXPERIMENTAL, since = "1.7.4") +public interface MethodLifecycleContext extends LifecycleContext { + + /** + * The method that defines the current property or example. + * + * @return a Method instance + */ + @API(status = MAINTAINED, since = "1.4.0") + Method targetMethod(); + + /** + * The container class in which the current property method is running. + * + *

+ * Most of the time that's also the defining class. It differs when + * running properties that are defined in a super class or an implemented interface. + *

+ * + * @return a Class instance + */ + @API(status = MAINTAINED, since = "1.4.0") + Class containerClass(); + + /** + * The current instance of the property's container class. + * There is exactly one instance per property method. + * + * @return an instance of the container class in which the current property method is running + */ + @API(status = MAINTAINED, since = "1.4.0") + Object testInstance(); + + /** + * The list of the current instance of the property's container class and all its outer objects + * if it is in a nested class. + * The result of {@linkplain #testInstance()} is the last in the list. + * + * @return List of instances starting from outer-most to inner-most class + */ + @API(status = MAINTAINED, since = "1.5.4") + List testInstances(); + +} diff --git a/api/src/main/java/net/jqwik/api/lifecycle/PropertyLifecycleContext.java b/api/src/main/java/net/jqwik/api/lifecycle/PropertyLifecycleContext.java index 0f670b8bd..0c9f545ee 100644 --- a/api/src/main/java/net/jqwik/api/lifecycle/PropertyLifecycleContext.java +++ b/api/src/main/java/net/jqwik/api/lifecycle/PropertyLifecycleContext.java @@ -1,55 +1,14 @@ package net.jqwik.api.lifecycle; -import java.lang.reflect.*; -import java.util.*; - import org.apiguardian.api.*; -import net.jqwik.api.*; - import static org.apiguardian.api.API.Status.*; /** * The context information of a property or example. */ @API(status = MAINTAINED, since = "1.4.0") -public interface PropertyLifecycleContext extends LifecycleContext { - - /** - * The method that defines the current property or example. - * - * @return a Method instance - */ - Method targetMethod(); - - /** - * The container class in which the current property method is running. - * - *

- * Most of the time that's also the defining class. It differs when - * running properties that are defined in a super class or an implemented interface. - *

- * - * @return a Class instance - */ - Class containerClass(); - - /** - * The current instance of the property's container class. - * There is exactly one instance per property method. - * - * @return an instance of the container class in which the current property method is running - */ - Object testInstance(); - - /** - * The list of the current instance of the property's container class and all its outer objects if it has any. - * The result of {@linkplain #testInstance()} is the last in the list. - * - * @return List of instances starting from outer-most to inner-most class - */ - @API(status = MAINTAINED, since = "1.5.4") - List testInstances(); +public interface PropertyLifecycleContext extends MethodLifecycleContext { /** * The extended label contains additional information about the current container class. diff --git a/api/src/main/java/net/jqwik/api/lifecycle/TryLifecycleContext.java b/api/src/main/java/net/jqwik/api/lifecycle/TryLifecycleContext.java index 18a8236b7..b19a6cc9f 100644 --- a/api/src/main/java/net/jqwik/api/lifecycle/TryLifecycleContext.java +++ b/api/src/main/java/net/jqwik/api/lifecycle/TryLifecycleContext.java @@ -1,48 +1,13 @@ package net.jqwik.api.lifecycle; -import java.lang.reflect.*; - import org.apiguardian.api.*; -import net.jqwik.api.*; - import static org.apiguardian.api.API.Status.*; /** * The context information of a single try of a property. */ @API(status = MAINTAINED, since = "1.4.0") -public interface TryLifecycleContext extends LifecycleContext { - - /** - * The method that defines the current property or example. - * - * @return a Method instance - */ - Method targetMethod(); - - /** - * The container class in which the current property method is running. - * - *

- * Most of the time that's also the defining class. It differs when - * running properties that are defined in a super class or an implemented interface. - *

- * - * @return a Class instance - */ - Class containerClass(); - - /** - * The current instance of the property's container class. - * There is exactly one instance per property method. - * - *

- * Mind that all tries of a property share the same test instance. - *

- * - * @return an instance of the container class in which the current property method is running - */ - Object testInstance(); +public interface TryLifecycleContext extends MethodLifecycleContext { } diff --git a/engine/src/main/java/net/jqwik/engine/execution/DefaultTryLifecycleContext.java b/engine/src/main/java/net/jqwik/engine/execution/DefaultTryLifecycleContext.java index 8c8b1957d..88f647e32 100644 --- a/engine/src/main/java/net/jqwik/engine/execution/DefaultTryLifecycleContext.java +++ b/engine/src/main/java/net/jqwik/engine/execution/DefaultTryLifecycleContext.java @@ -32,6 +32,11 @@ public Object testInstance() { return propertyContext.testInstance(); } + @Override + public List testInstances() { + return propertyContext.testInstances(); + } + @Override public String label() { return propertyContext.label(); diff --git a/engine/src/test/java/net/jqwik/engine/TestHelper.java b/engine/src/test/java/net/jqwik/engine/TestHelper.java index ec506514d..fd8c899c2 100644 --- a/engine/src/test/java/net/jqwik/engine/TestHelper.java +++ b/engine/src/test/java/net/jqwik/engine/TestHelper.java @@ -184,6 +184,11 @@ public Object testInstance() { return null; } + @Override + public List testInstances() { + return null; + } + @Override public String label() { return null; diff --git a/engine/src/test/java/net/jqwik/engine/execution/lifecycle/AroundTryHookTests.java b/engine/src/test/java/net/jqwik/engine/execution/lifecycle/AroundTryHookTests.java index 7c32834b7..80ef55cde 100644 --- a/engine/src/test/java/net/jqwik/engine/execution/lifecycle/AroundTryHookTests.java +++ b/engine/src/test/java/net/jqwik/engine/execution/lifecycle/AroundTryHookTests.java @@ -231,6 +231,7 @@ public TryExecutionResult aroundTry(TryLifecycleContext context, TryExecutor aTr assertThat(context.containerClass()).isEqualTo(AroundTryHookTests.class); assertThat(context.targetMethod().getName()).isEqualTo("checkTryLifecycleContextAttributes"); assertThat(context.testInstance()).isInstanceOf(AroundTryHookTests.class); + assertThat(context.testInstances().get(0)).isInstanceOf(AroundTryHookTests.class); return aTry.execute(parameters); } }