-
-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added failing tests for BeforeTry member variables
- Loading branch information
Showing
5 changed files
with
115 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
engine/src/main/java/net/jqwik/engine/hooks/lifecycle/BeforeTryMembersHook.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package net.jqwik.engine.hooks.lifecycle; | ||
|
||
import java.lang.reflect.*; | ||
import java.util.*; | ||
|
||
import org.junit.platform.engine.support.hierarchical.*; | ||
|
||
import net.jqwik.api.lifecycle.*; | ||
import net.jqwik.engine.hooks.*; | ||
import net.jqwik.engine.support.*; | ||
|
||
public class BeforeTryMembersHook implements AroundTryHook { | ||
|
||
private void beforeTry(TryLifecycleContext context) { | ||
System.out.println("initialize before try members"); | ||
// List<Method> beforeTryMethods = LifecycleMethods.findBeforeTryMethods(context.containerClass()); | ||
// callTryMethods(beforeTryMethods, context); | ||
} | ||
|
||
private void callTryMethods(List<Method> methods, TryLifecycleContext context) { | ||
Object testInstance = context.testInstance(); | ||
ThrowableCollector throwableCollector = new ThrowableCollector(ignore -> false); | ||
for (Method method : methods) { | ||
Object[] parameters = MethodParameterResolver.resolveParameters(method, context); | ||
throwableCollector.execute(() -> callMethod(method, testInstance, parameters)); | ||
} | ||
throwableCollector.assertEmpty(); | ||
} | ||
|
||
private void callMethod(Method method, Object target, Object[] parameters) { | ||
JqwikReflectionSupport.invokeMethodPotentiallyOuter(method, target, parameters); | ||
} | ||
|
||
private void afterTry(TryLifecycleContext context) { | ||
List<Method> afterTryMethods = LifecycleMethods.findAfterTryMethods(context.containerClass()); | ||
callTryMethods(afterTryMethods, context); | ||
} | ||
|
||
@Override | ||
public PropagationMode propagateTo() { | ||
return PropagationMode.ALL_DESCENDANTS; | ||
} | ||
|
||
@Override | ||
public boolean appliesTo(Optional<AnnotatedElement> element) { | ||
return element.map(e -> e instanceof Method).orElse(false); | ||
} | ||
|
||
@Override | ||
public int aroundTryProximity() { | ||
return Hooks.AroundTry.BEFORE_TRY_MEMBERS_PROXIMITY; | ||
} | ||
|
||
@Override | ||
public TryExecutionResult aroundTry(TryLifecycleContext context, TryExecutor aTry, List<Object> parameters) { | ||
beforeTry(context); | ||
try { | ||
return aTry.execute(parameters); | ||
} finally { | ||
afterTry(context); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
...ne/src/test/java/net/jqwik/engine/execution/lifecycle/BeforeTryOnMemberVariableTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package net.jqwik.engine.execution.lifecycle; | ||
|
||
import org.assertj.core.api.*; | ||
|
||
import net.jqwik.api.*; | ||
import net.jqwik.api.constraints.*; | ||
import net.jqwik.api.lifecycle.*; | ||
|
||
class BeforeTryOnMemberVariableTests { | ||
|
||
@BeforeTry | ||
private int member = 41; | ||
|
||
@BeforeTry | ||
void testBefore() { | ||
System.out.println("before try outer"); | ||
} | ||
|
||
@Property(tries = 10) | ||
void beforeTryOnMember(@ForAll @IntRange(min = 1, max = 100) int addend) { | ||
Assertions.assertThat(member).isEqualTo(41); | ||
member = member + addend; | ||
} | ||
|
||
@Group | ||
class InnerTests { | ||
|
||
@BeforeTry | ||
private int innerMember = 42; | ||
|
||
@BeforeTry | ||
void testBeforeInner() { | ||
System.out.println("before try inner"); | ||
} | ||
|
||
@Property(tries = 10) | ||
void beforeTryOnMemberInInnerGroup(@ForAll @IntRange(min = 1, max = 100) int addend) { | ||
Assertions.assertThat(member).isEqualTo(41); | ||
member = member + addend; | ||
Assertions.assertThat(innerMember).isEqualTo(42); | ||
innerMember = innerMember + addend; | ||
} | ||
} | ||
} |