Skip to content

Commit

Permalink
Clear current block on exit from feature method
Browse files Browse the repository at this point in the history
  • Loading branch information
leonard84 committed Feb 16, 2023
1 parent 9bc3a7a commit e3d2667
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ public class AstNodeCache {
SpockRuntime.getDeclaredMethods(org.spockframework.runtime.SpockRuntime.DESPREAD_LIST).get(0);
public final MethodNode SpockRuntime_CallEnterBlock =
SpockRuntime.getDeclaredMethods(org.spockframework.runtime.SpockRuntime.CALL_ENTER_BLOCK).get(0);
public final MethodNode SpockRuntime_ClearCurrentBlock =
SpockRuntime.getDeclaredMethods(org.spockframework.runtime.SpockRuntime.CLEAR_CURRENT_BLOCK).get(0);

public final MethodNode ValueRecorder_Reset =
ValueRecorder.getDeclaredMethods(org.spockframework.runtime.ValueRecorder.RESET).get(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,9 @@ public void visitMethodAgain(Method method) {
if (methodHasDeepNonGroupedCondition) {
defineErrorRethrower(method.getStatements());
}

if (method instanceof FeatureMethod)
clearCurrentBlockOnExit(method.getStatements());
}

@Override
Expand Down Expand Up @@ -533,6 +536,22 @@ private TryCatchStatement createCleanupTryCatch(CleanupBlock block, VariableExpr
return tryCatchStat;
}

// Wraps the feature method in a try-finally to clear the current block on exit.
private void clearCurrentBlockOnExit(List<Statement> statements) {
MethodCallExpression setCurrentBlockToNull = createDirectMethodCall(new ClassExpression(nodeCache.SpockRuntime),
nodeCache.SpockRuntime_ClearCurrentBlock, new ArgumentListExpression(getSpecificationContext()));

List<Statement> innerStatements = new ArrayList<>(statements);

TryCatchStatement tryCatchStat =
new TryCatchStatement(
new BlockStatement(innerStatements, null),
new ExpressionStatement(setCurrentBlockToNull));

statements.clear();
statements.add(tryCatchStat);
}

private CatchStatement createThrowableAssignmentAndRethrowCatchStatement(VariableExpression assignmentVar) {
Parameter catchParameter = new Parameter(nodeCache.Throwable, SPOCK_TMP_THROWABLE);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,11 @@ public static void callEnterBlock(SpecificationContext context, BlockInfo blockI
}
}

public static final String CLEAR_CURRENT_BLOCK = "clearCurrentBlock";
public static void clearCurrentBlock(SpecificationContext context) {
context.setCurrentBlock(null);
}

private static List<Object> getValues(ValueRecorder recorder) {
return recorder == null ? null : recorder.getValues();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ class ASpec extends Specification {
"feature when" | BlockKind.WHEN | ["when label"]
"feature then" | BlockKind.THEN | ["then label"]
"feature cleanup" | BlockKind.CLEANUP | ["cleanup label"]
"cleanup" | null | [] // TODO the last block leaks into cleanup, this is a bug
"cleanup" | null | []
"cleanupSpec" | null | []
}
}
Expand Down

0 comments on commit e3d2667

Please sign in to comment.