Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
leonard84 committed Feb 25, 2024
1 parent d216397 commit ae38bda
Show file tree
Hide file tree
Showing 11 changed files with 589 additions and 131 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
Loading

0 comments on commit ae38bda

Please sign in to comment.