Skip to content

Commit

Permalink
Added experimental MethodLifecycleContext interface
Browse files Browse the repository at this point in the history
  • Loading branch information
jlink committed Jun 18, 2023
1 parent b87a3ed commit e2e3e0c
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 78 deletions.
Original file line number Diff line number Diff line change
@@ -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.
*
* <p>
* 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.
* </p>
*
* @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<Object> testInstances();

}
Original file line number Diff line number Diff line change
@@ -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.
*
* <p>
* 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.
* </p>
*
* @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<Object> testInstances();
public interface PropertyLifecycleContext extends MethodLifecycleContext {

/**
* The extended label contains additional information about the current container class.
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
*
* <p>
* 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.
* </p>
*
* @return a Class instance
*/
Class<?> containerClass();

/**
* The current instance of the property's container class.
* There is exactly one instance per property method.
*
* <p>
* Mind that all tries of a property <em>share the same test instance</em>.
* </p>
*
* @return an instance of the container class in which the current property method is running
*/
Object testInstance();
public interface TryLifecycleContext extends MethodLifecycleContext {

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ public Object testInstance() {
return propertyContext.testInstance();
}

@Override
public List<Object> testInstances() {
return propertyContext.testInstances();
}

@Override
public String label() {
return propertyContext.label();
Expand Down
5 changes: 5 additions & 0 deletions engine/src/test/java/net/jqwik/engine/TestHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,11 @@ public Object testInstance() {
return null;
}

@Override
public List<Object> testInstances() {
return null;
}

@Override
public String label() {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit e2e3e0c

Please sign in to comment.