Skip to content

Commit 808616e

Browse files
committed
Signed-off-by: Pavlo Shevchenko <pavel.shevchenko.95@gmail.com>
Transform only top-level implicit conditions in helper methods Signed-off-by: Pavlo Shevchenko <pavel.shevchenko.95@gmail.com>
1 parent 2604e74 commit 808616e

File tree

6 files changed

+23
-26
lines changed

6 files changed

+23
-26
lines changed

spock-core/src/main/java/org/spockframework/compiler/condition/BaseVerifyMethodRewriter.java

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.spockframework.compiler.StatementReplacingVisitorSupport;
2525

2626
import java.util.List;
27+
import java.util.ListIterator;
2728

2829
import static org.spockframework.compiler.condition.ImplicitConditionsUtils.checkIsValidImplicitCondition;
2930
import static org.spockframework.compiler.condition.ImplicitConditionsUtils.isImplicitCondition;
@@ -32,6 +33,7 @@ abstract class BaseVerifyMethodRewriter extends StatementReplacingVisitorSupport
3233

3334
final IRewriteResources resources;
3435
private final MethodNode methodNode;
36+
private Statement currentTopLevelStatement;
3537
private boolean conditionFound = false;
3638

3739
BaseVerifyMethodRewriter(MethodNode methodNode, IRewriteResources resources) {
@@ -42,24 +44,33 @@ abstract class BaseVerifyMethodRewriter extends StatementReplacingVisitorSupport
4244
abstract void defineErrorCollector(List<Statement> statements);
4345

4446
public void rewrite() {
45-
methodNode.getCode().visit(this);
47+
ListIterator<Statement> statements = AstUtil.getStatements(methodNode).listIterator();
48+
while (statements.hasNext()) {
49+
Statement next = statements.next();
50+
currentTopLevelStatement = next;
51+
statements.set(replace(next));
52+
}
4653
defineRecorders();
4754
}
4855

4956
@Override
5057
public void visitAssertStatement(AssertStatement stat) {
5158
super.visitAssertStatement(stat);
52-
conditionFound();
53-
replaceVisitedStatementWith(ConditionRewriter.rewriteExplicitCondition(stat, resources));
59+
if (stat == currentTopLevelStatement) {
60+
conditionFound();
61+
replaceVisitedStatementWith(ConditionRewriter.rewriteExplicitCondition(stat, resources));
62+
}
5463
}
5564

5665
@Override
57-
public void visitExpressionStatement(ExpressionStatement statement) {
58-
super.visitExpressionStatement(statement);
59-
if (isImplicitCondition(statement)) {
60-
checkIsValidImplicitCondition(statement, resources.getErrorReporter());
61-
conditionFound();
62-
replaceVisitedStatementWith(ConditionRewriter.rewriteImplicitCondition(statement, resources));
66+
public void visitExpressionStatement(ExpressionStatement stat) {
67+
super.visitExpressionStatement(stat);
68+
if (isImplicitCondition(stat)) {
69+
checkIsValidImplicitCondition(stat, resources.getErrorReporter());
70+
if (stat == currentTopLevelStatement) {
71+
conditionFound();
72+
replaceVisitedStatementWith(ConditionRewriter.rewriteImplicitCondition(stat, resources));
73+
}
6374
}
6475
}
6576

spock-core/src/main/java/spock/lang/Verify.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
* and void method invocations will not be considered conditions.
2727
* The method can be defined in the {@link Specification} class or in a separate class.
2828
* The method must have a {@code void} return type.
29-
* Conditions on all levels will be transformed into assertions.
3029
* The test will fail on the first failing assertion.
3130
*/
3231
@Target(ElementType.METHOD)

spock-core/src/main/java/spock/lang/VerifyAll.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
* and void method invocations will not be considered conditions.
2727
* The method can be defined inside the {@link Specification} class or in a separate class.
2828
* The method must have a {@code void} return type.
29-
* Conditions on all levels will be transformed into assertions.
3029
* In contrast to {@link Verify}, all conditions will be evaluated before failing the test.
3130
*/
3231
@Target(ElementType.METHOD)

spock-specs/src/test/groovy/org/spockframework/smoke/ast/condition/BaseVerifyMethodsAstSpec.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ class Assertions {
141141
snapshotter.assertThat(result.source).matchesSnapshot()
142142
}
143143

144-
def "transforms nested conditions"() {
144+
def "transforms only top-level conditions"() {
145145
when:
146146
def result = compiler.transpile("""
147147
class Assertions {

spock-specs/src/test/resources/snapshots/org/spockframework/smoke/ast/condition/VerifyAllMethodsAstSpec/transforms_nested_conditions.groovy renamed to spock-specs/src/test/resources/snapshots/org/spockframework/smoke/ast/condition/VerifyAllMethodsAstSpec/transforms_only_top_level_conditions.groovy

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,7 @@ public class Assertions extends java.lang.Object {
1313
finally {
1414
}
1515
if ( a > 0) {
16-
try {
17-
org.spockframework.runtime.SpockRuntime.verifyCondition($spock_errorCollector, $spock_valueRecorder.reset(), 'a % 2 == 0', 7, 13, null, $spock_valueRecorder.record($spock_valueRecorder.startRecordingValue(4), $spock_valueRecorder.record($spock_valueRecorder.startRecordingValue(2), $spock_valueRecorder.record($spock_valueRecorder.startRecordingValue(0), a) % $spock_valueRecorder.record($spock_valueRecorder.startRecordingValue(1), 2)) == $spock_valueRecorder.record($spock_valueRecorder.startRecordingValue(3), 0)))
18-
}
19-
catch (java.lang.Throwable $spock_condition_throwable) {
20-
org.spockframework.runtime.SpockRuntime.conditionFailedWithException($spock_errorCollector, $spock_valueRecorder, 'a % 2 == 0', 7, 13, null, $spock_condition_throwable)}
21-
finally {
22-
}
16+
a % 2 == 0
2317
}
2418
}
2519
finally {

spock-specs/src/test/resources/snapshots/org/spockframework/smoke/ast/condition/VerifyMethodsAstSpec/transforms_nested_conditions.groovy renamed to spock-specs/src/test/resources/snapshots/org/spockframework/smoke/ast/condition/VerifyMethodsAstSpec/transforms_only_top_level_conditions.groovy

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,7 @@ public class Assertions extends java.lang.Object {
1212
finally {
1313
}
1414
if ( a > 0) {
15-
try {
16-
org.spockframework.runtime.SpockRuntime.verifyCondition($spock_errorCollector, $spock_valueRecorder.reset(), 'a % 2 == 0', 7, 13, null, $spock_valueRecorder.record($spock_valueRecorder.startRecordingValue(4), $spock_valueRecorder.record($spock_valueRecorder.startRecordingValue(2), $spock_valueRecorder.record($spock_valueRecorder.startRecordingValue(0), a) % $spock_valueRecorder.record($spock_valueRecorder.startRecordingValue(1), 2)) == $spock_valueRecorder.record($spock_valueRecorder.startRecordingValue(3), 0)))
17-
}
18-
catch (java.lang.Throwable $spock_condition_throwable) {
19-
org.spockframework.runtime.SpockRuntime.conditionFailedWithException($spock_errorCollector, $spock_valueRecorder, 'a % 2 == 0', 7, 13, null, $spock_condition_throwable)}
20-
finally {
21-
}
15+
a % 2 == 0
2216
}
2317
}
2418

0 commit comments

Comments
 (0)