Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
leonard84 committed May 7, 2023
1 parent fad4d1e commit 0f3197e
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,54 +57,6 @@ public DeepBlockRewriter(IRewriteResources resources) {
@Override
public void visit(Block block) {
super.visit(block);
addBlockEnterCall(block);
}

private void addBlockEnterCall(Block block) {
BlockParseInfo blockType = block.getParseInfo();
if (blockType == BlockParseInfo.WHERE
|| blockType == BlockParseInfo.METHOD_END
|| blockType == BlockParseInfo.ANONYMOUS) return;

// SpockRuntime.enterBlock(getSpecificationContext(), new BlockInfo(blockKind, [blockTexts]))
MethodCallExpression enterBlockCall = createBlockListenerCall(block, blockType, resources.getAstNodeCache().SpockRuntime_CallEnterBlock);
// SpockRuntime.exitedBlock(getSpecificationContext(), new BlockInfo(blockKind, [blockTexts]))
MethodCallExpression exitBlockCall = createBlockListenerCall(block, blockType, resources.getAstNodeCache().SpockRuntime_CallExitBlock);

// As the cleanup block finalizes the specification, it would override any previous block in ErrorInfo,
// so we only call enterBlock if there is no error yet.
if (blockType == BlockParseInfo.CLEANUP) {
block.getAst().add(0, new IfStatement(
// if ($spock_feature_throwable == null)
new BooleanExpression(AstUtil.createVariableIsNullExpression(new VariableExpression(SpecRewriter.SPOCK_FEATURE_THROWABLE, resources.getAstNodeCache().Throwable))),
new ExpressionStatement(enterBlockCall),
EmptyStatement.INSTANCE
));
} else {
block.getAst().add(0, new ExpressionStatement(enterBlockCall));
}
block.getAst().add( new ExpressionStatement(exitBlockCall));
}

private MethodCallExpression createBlockListenerCall(Block block, BlockParseInfo blockType, MethodNode blockListenerMethod) {
return createDirectMethodCall(
new ClassExpression(resources.getAstNodeCache().SpockRuntime),
blockListenerMethod,
new ArgumentListExpression(
createDirectMethodCall(VariableExpression.THIS_EXPRESSION,
resources.getAstNodeCache().SpecInternals_GetSpecificationContext,
ArgumentListExpression.EMPTY_ARGUMENTS),
new ConstructorCallExpression(resources.getAstNodeCache().BlockInfo,
new ArgumentListExpression(
new PropertyExpression(
new ClassExpression(resources.getAstNodeCache().BlockKind),
blockType.name()
),
new ListExpression(
block.getDescriptions().stream().map(ConstantExpression::new).collect(Collectors.toList())
)
))
));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import java.lang.reflect.InvocationTargetException;
import java.util.*;
import java.util.stream.Collectors;

import org.codehaus.groovy.ast.*;
import org.codehaus.groovy.ast.expr.*;
Expand Down Expand Up @@ -384,8 +385,10 @@ public void visitMethodAgain(Method method) {
this.block = null;

if (!movedStatsBackToMethod)
for (Block b : method.getBlocks())
for (Block b : method.getBlocks()) {
addBlockListeners(b);
method.getStatements().addAll(b.getAst());
}

// for global required interactions
if (method instanceof FeatureMethod)
Expand All @@ -402,6 +405,54 @@ public void visitMethodAgain(Method method) {
clearCurrentBlockOnExit(method.getStatements());
}


private void addBlockListeners(Block block) {
BlockParseInfo blockType = block.getParseInfo();
if (blockType == BlockParseInfo.WHERE
|| blockType == BlockParseInfo.METHOD_END
|| blockType == BlockParseInfo.ANONYMOUS) return;

// SpockRuntime.enterBlock(getSpecificationContext(), new BlockInfo(blockKind, [blockTexts]))
MethodCallExpression enterBlockCall = createBlockListenerCall(block, blockType, nodeCache.SpockRuntime_CallEnterBlock);
// SpockRuntime.exitedBlock(getSpecificationContext(), new BlockInfo(blockKind, [blockTexts]))
MethodCallExpression exitBlockCall = createBlockListenerCall(block, blockType, nodeCache.SpockRuntime_CallExitBlock);

// As the cleanup block finalizes the specification, it would override any previous block in ErrorInfo,
// so we only call enterBlock if there is no error yet.
if (blockType == BlockParseInfo.CLEANUP) {
block.getAst().add(0, new IfStatement(
// if ($spock_feature_throwable == null)
new BooleanExpression(AstUtil.createVariableIsNullExpression(new VariableExpression(SpecRewriter.SPOCK_FEATURE_THROWABLE, nodeCache.Throwable))),
new ExpressionStatement(enterBlockCall),
EmptyStatement.INSTANCE
));
} else {
block.getAst().add(0, new ExpressionStatement(enterBlockCall));
}
block.getAst().add( new ExpressionStatement(exitBlockCall));
}

private MethodCallExpression createBlockListenerCall(Block block, BlockParseInfo blockType, MethodNode blockListenerMethod) {
return createDirectMethodCall(
new ClassExpression(nodeCache.SpockRuntime),
blockListenerMethod,
new ArgumentListExpression(
createDirectMethodCall(VariableExpression.THIS_EXPRESSION,
nodeCache.SpecInternals_GetSpecificationContext,
ArgumentListExpression.EMPTY_ARGUMENTS),
new ConstructorCallExpression(nodeCache.BlockInfo,
new ArgumentListExpression(
new PropertyExpression(
new ClassExpression(nodeCache.BlockKind),
blockType.name()
),
new ListExpression(
block.getDescriptions().stream().map(ConstantExpression::new).collect(Collectors.toList())
)
))
));
}

@Override
public void visitAnyBlock(Block block) {
this.block = block;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ public class apackage.ASpec extends spock.lang.Specification {
org.spockframework.runtime.SpockRuntime.clearCurrentBlock(this.getSpecificationContext())}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ public class apackage.ASpec extends spock.lang.Specification implements groovy.l
org.spockframework.runtime.SpockRuntime.clearCurrentBlock(this.getSpecificationContext())}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ public void $spock_feature_0_0() {
org.spockframework.runtime.SpockRuntime.clearCurrentBlock(this.getSpecificationContext())}
}
/*--------- end::snapshot[] ---------*/
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,22 @@ public void $spock_feature_0_0() {
java.lang.Object foobar
java.lang.Throwable $spock_feature_throwable
try {
org.spockframework.runtime.SpockRuntime.callEnterBlock(this.getSpecificationContext(), new org.spockframework.runtime.model.BlockInfo(org.spockframework.runtime.model.BlockKind.WHEN, []))
foobar = this.foobar()
org.spockframework.runtime.SpockRuntime.callExitBlock(this.getSpecificationContext(), new org.spockframework.runtime.model.BlockInfo(org.spockframework.runtime.model.BlockKind.WHEN, []))
org.spockframework.runtime.SpockRuntime.callEnterBlock(this.getSpecificationContext(), new org.spockframework.runtime.model.BlockInfo(org.spockframework.runtime.model.BlockKind.THEN, []))
try {
org.spockframework.runtime.SpockRuntime.verifyMethodCondition($spock_errorCollector, $spock_valueRecorder.reset(), 'println(foobar)', 6, 3, null, this, $spock_valueRecorder.record($spock_valueRecorder.startRecordingValue(0), 'println'), new java.lang.Object[]{$spock_valueRecorder.record($spock_valueRecorder.startRecordingValue(1), foobar)}, $spock_valueRecorder.realizeNas(4, false), false, 3)
}
catch (java.lang.Throwable $spock_condition_throwable) {
org.spockframework.runtime.SpockRuntime.conditionFailedWithException($spock_errorCollector, $spock_valueRecorder, 'println(foobar)', 6, 3, null, $spock_condition_throwable)}
finally {
}
org.spockframework.runtime.SpockRuntime.callExitBlock(this.getSpecificationContext(), new org.spockframework.runtime.model.BlockInfo(org.spockframework.runtime.model.BlockKind.THEN, []))
}
catch (java.lang.Throwable $spock_tmp_throwable) {
$spock_feature_throwable = $spock_tmp_throwable
throw $spock_tmp_throwable
}
finally {
try {
if ( $spock_feature_throwable == null) {
org.spockframework.runtime.SpockRuntime.callEnterBlock(this.getSpecificationContext(), new org.spockframework.runtime.model.BlockInfo(org.spockframework.runtime.model.BlockKind.CLEANUP, []))
}
foobar.size()
org.spockframework.runtime.SpockRuntime.callExitBlock(this.getSpecificationContext(), new org.spockframework.runtime.model.BlockInfo(org.spockframework.runtime.model.BlockKind.CLEANUP, []))
}
catch (java.lang.Throwable $spock_tmp_throwable) {
if ( $spock_feature_throwable != null) {
Expand All @@ -55,4 +47,4 @@ public void $spock_feature_0_0() {
org.spockframework.runtime.SpockRuntime.clearCurrentBlock(this.getSpecificationContext())}
}
/*--------- end::snapshot[] ---------*/
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,22 @@ public void $spock_feature_0_0() {
def (java.lang.Object foobar, java.lang.Object b) = [null, null]
java.lang.Throwable $spock_feature_throwable
try {
org.spockframework.runtime.SpockRuntime.callEnterBlock(this.getSpecificationContext(), new org.spockframework.runtime.model.BlockInfo(org.spockframework.runtime.model.BlockKind.WHEN, []))
(foobar, b) = this.foobar()
org.spockframework.runtime.SpockRuntime.callExitBlock(this.getSpecificationContext(), new org.spockframework.runtime.model.BlockInfo(org.spockframework.runtime.model.BlockKind.WHEN, []))
org.spockframework.runtime.SpockRuntime.callEnterBlock(this.getSpecificationContext(), new org.spockframework.runtime.model.BlockInfo(org.spockframework.runtime.model.BlockKind.THEN, []))
try {
org.spockframework.runtime.SpockRuntime.verifyMethodCondition($spock_errorCollector, $spock_valueRecorder.reset(), 'println(foobar)', 6, 3, null, this, $spock_valueRecorder.record($spock_valueRecorder.startRecordingValue(0), 'println'), new java.lang.Object[]{$spock_valueRecorder.record($spock_valueRecorder.startRecordingValue(1), foobar)}, $spock_valueRecorder.realizeNas(4, false), false, 3)
}
catch (java.lang.Throwable $spock_condition_throwable) {
org.spockframework.runtime.SpockRuntime.conditionFailedWithException($spock_errorCollector, $spock_valueRecorder, 'println(foobar)', 6, 3, null, $spock_condition_throwable)}
finally {
}
org.spockframework.runtime.SpockRuntime.callExitBlock(this.getSpecificationContext(), new org.spockframework.runtime.model.BlockInfo(org.spockframework.runtime.model.BlockKind.THEN, []))
}
catch (java.lang.Throwable $spock_tmp_throwable) {
$spock_feature_throwable = $spock_tmp_throwable
throw $spock_tmp_throwable
}
finally {
try {
if ( $spock_feature_throwable == null) {
org.spockframework.runtime.SpockRuntime.callEnterBlock(this.getSpecificationContext(), new org.spockframework.runtime.model.BlockInfo(org.spockframework.runtime.model.BlockKind.CLEANUP, []))
}
foobar.size()
org.spockframework.runtime.SpockRuntime.callExitBlock(this.getSpecificationContext(), new org.spockframework.runtime.model.BlockInfo(org.spockframework.runtime.model.BlockKind.CLEANUP, []))
}
catch (java.lang.Throwable $spock_tmp_throwable) {
if ( $spock_feature_throwable != null) {
Expand All @@ -55,4 +47,4 @@ public void $spock_feature_0_0() {
org.spockframework.runtime.SpockRuntime.clearCurrentBlock(this.getSpecificationContext())}
}
/*--------- end::snapshot[] ---------*/
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ public java.lang.Object foobar() {
public void $spock_feature_0_0() {
try {
java.lang.Object foobar
org.spockframework.runtime.SpockRuntime.callEnterBlock(this.getSpecificationContext(), new org.spockframework.runtime.model.BlockInfo(org.spockframework.runtime.model.BlockKind.WHEN, []))
this.getSpecificationContext().setThrownException(null)
try {
org.spockframework.runtime.SpockRuntime.callEnterBlock(this.getSpecificationContext(), new org.spockframework.runtime.model.BlockInfo(org.spockframework.runtime.model.BlockKind.WHEN, []))
foobar = this.foobar()
org.spockframework.runtime.SpockRuntime.callExitBlock(this.getSpecificationContext(), new org.spockframework.runtime.model.BlockInfo(org.spockframework.runtime.model.BlockKind.WHEN, []))
}
catch (java.lang.Throwable $spock_ex) {
this.getSpecificationContext().setThrownException($spock_ex)
}
finally {
}
org.spockframework.runtime.SpockRuntime.callExitBlock(this.getSpecificationContext(), new org.spockframework.runtime.model.BlockInfo(org.spockframework.runtime.model.BlockKind.WHEN, []))
org.spockframework.runtime.SpockRuntime.callEnterBlock(this.getSpecificationContext(), new org.spockframework.runtime.model.BlockInfo(org.spockframework.runtime.model.BlockKind.THEN, []))
this.thrownImpl(null, null, java.lang.IllegalStateException)
org.spockframework.runtime.SpockRuntime.callExitBlock(this.getSpecificationContext(), new org.spockframework.runtime.model.BlockInfo(org.spockframework.runtime.model.BlockKind.THEN, []))
Expand All @@ -30,4 +30,4 @@ public void $spock_feature_0_0() {
org.spockframework.runtime.SpockRuntime.clearCurrentBlock(this.getSpecificationContext())}
}
/*--------- end::snapshot[] ---------*/
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ public java.lang.Object foobar() {
public void $spock_feature_0_0() {
try {
def (java.lang.Object foobar, java.lang.Object b) = [null, null]
org.spockframework.runtime.SpockRuntime.callEnterBlock(this.getSpecificationContext(), new org.spockframework.runtime.model.BlockInfo(org.spockframework.runtime.model.BlockKind.WHEN, []))
this.getSpecificationContext().setThrownException(null)
try {
org.spockframework.runtime.SpockRuntime.callEnterBlock(this.getSpecificationContext(), new org.spockframework.runtime.model.BlockInfo(org.spockframework.runtime.model.BlockKind.WHEN, []))
(foobar, b) = this.foobar()
org.spockframework.runtime.SpockRuntime.callExitBlock(this.getSpecificationContext(), new org.spockframework.runtime.model.BlockInfo(org.spockframework.runtime.model.BlockKind.WHEN, []))
}
catch (java.lang.Throwable $spock_ex) {
this.getSpecificationContext().setThrownException($spock_ex)
}
finally {
}
org.spockframework.runtime.SpockRuntime.callExitBlock(this.getSpecificationContext(), new org.spockframework.runtime.model.BlockInfo(org.spockframework.runtime.model.BlockKind.WHEN, []))
org.spockframework.runtime.SpockRuntime.callEnterBlock(this.getSpecificationContext(), new org.spockframework.runtime.model.BlockInfo(org.spockframework.runtime.model.BlockKind.THEN, []))
this.thrownImpl(null, null, java.lang.IllegalStateException)
org.spockframework.runtime.SpockRuntime.callExitBlock(this.getSpecificationContext(), new org.spockframework.runtime.model.BlockInfo(org.spockframework.runtime.model.BlockKind.THEN, []))
Expand All @@ -30,4 +30,4 @@ public void $spock_feature_0_0() {
org.spockframework.runtime.SpockRuntime.clearCurrentBlock(this.getSpecificationContext())}
}
/*--------- end::snapshot[] ---------*/
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ public void $spock_feature_0_0() {
try {
org.spockframework.runtime.SpockRuntime.callEnterBlock(this.getSpecificationContext(), new org.spockframework.runtime.model.BlockInfo(org.spockframework.runtime.model.BlockKind.SETUP, []))
java.util.List list = this.MockImpl('list', java.util.List)
org.spockframework.runtime.SpockRuntime.callExitBlock(this.getSpecificationContext(), new org.spockframework.runtime.model.BlockInfo(org.spockframework.runtime.model.BlockKind.SETUP, []))
this.getSpecificationContext().getMockController().enterScope()
this.getSpecificationContext().getMockController().addInteraction(new org.spockframework.mock.runtime.InteractionBuilder(8, 5, '1 * list.add(1)').setFixedCount(1).addEqualTarget(list).addEqualMethodName('add').setArgListKind(true, false).addEqualArg(1).build())
org.spockframework.runtime.SpockRuntime.callExitBlock(this.getSpecificationContext(), new org.spockframework.runtime.model.BlockInfo(org.spockframework.runtime.model.BlockKind.SETUP, []))
org.spockframework.runtime.SpockRuntime.callEnterBlock(this.getSpecificationContext(), new org.spockframework.runtime.model.BlockInfo(org.spockframework.runtime.model.BlockKind.WHEN, []))
list.add(1)
org.spockframework.runtime.SpockRuntime.callExitBlock(this.getSpecificationContext(), new org.spockframework.runtime.model.BlockInfo(org.spockframework.runtime.model.BlockKind.WHEN, []))
this.getSpecificationContext().getMockController().leaveScope()
org.spockframework.runtime.SpockRuntime.callEnterBlock(this.getSpecificationContext(), new org.spockframework.runtime.model.BlockInfo(org.spockframework.runtime.model.BlockKind.THEN, []))
this.getSpecificationContext().getMockController().leaveScope()
org.spockframework.runtime.SpockRuntime.callExitBlock(this.getSpecificationContext(), new org.spockframework.runtime.model.BlockInfo(org.spockframework.runtime.model.BlockKind.THEN, []))
this.getSpecificationContext().getMockController().leaveScope()
}
Expand Down

0 comments on commit 0f3197e

Please sign in to comment.