Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import static java.util.Collections.singletonList;
import static org.spockframework.compiler.AstUtil.createDirectMethodCall;
import static org.spockframework.compiler.AstUtil.primitiveConstExpression;
import static org.spockframework.compiler.SpecialMethodCall.checkIsConditionMethodCall;

// NOTE: currently some conversions reference old expression objects rather than copying them;
// this can potentially lead to aliasing problems (e.g. for Condition.originalExpression)
Expand Down Expand Up @@ -641,8 +642,7 @@ private Statement rewriteCondition(Expression expr, Expression message, boolean
// method conditions with spread operator are not lifted because MOP doesn't support spreading
if (expr instanceof MethodCallExpression && !((MethodCallExpression) expr).isSpreadSafe()) {
MethodCallExpression methodCallExpression = (MethodCallExpression)expr;
String methodName = AstUtil.getMethodName(methodCallExpression);
if ((Identifiers.CONDITION_METHODS.contains(methodName))) {
if (checkIsConditionMethodCall(methodCallExpression)) {
return surroundSpecialTryCatch(expr);
}
return rewriteMethodCondition(methodCallExpression, message, explicit, optOut);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,17 @@ private boolean handleImplicitCondition(ExpressionStatement stat) {

checkIsValidImplicitCondition(stat, resources.getErrorReporter());

String methodName = AstUtil.getMethodName(stat.getExpression());
boolean isConditionMethodCall = Identifiers.CONDITION_METHODS.contains(methodName);
boolean isConditionMethodCall;
if (stat.getExpression() instanceof MethodCallExpression) {
isConditionMethodCall = SpecialMethodCall.checkIsConditionMethodCall(((MethodCallExpression) stat.getExpression()));
} else {
isConditionMethodCall = false;
}

if (isConditionMethodCall) {
groupConditionFound = currSpecialMethodCall.isGroupConditionBlock();
} else {
}
if (!isConditionMethodCall || currSpecialMethodCall.isConditionMethodCall() || currSpecialMethodCall.isGroupConditionBlock()) {
conditionFound();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,11 @@ public String toString() {
methodName, inferredName, inferredType, methodCallExpr, closureExpr, conditionBlock);
}

static boolean checkIsConditionMethodCall(MethodCallExpression expr) {
if (!AstUtil.isThisOrSuperExpression(expr.getObjectExpression())) return false;
return Identifiers.CONDITION_METHODS.contains(expr.getMethodAsString());
}

private static boolean checkIsBuiltInMethodCall(MethodCallExpression expr) {
if (!AstUtil.isThisOrSuperExpression(expr.getObjectExpression())) return false;
return Identifiers.BUILT_IN_METHODS.contains(expr.getMethodAsString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@

package org.spockframework.smoke

import org.spockframework.EmbeddedSpecification
import org.spockframework.runtime.*
import spock.lang.*

class WithBlockFailingConditions extends Specification {
class WithBlockFailingConditions extends EmbeddedSpecification {
@FailsWith(ConditionNotSatisfiedError)
def "basic usage"() {
def list = [1, 2]
Expand Down Expand Up @@ -155,4 +156,146 @@ class WithBlockFailingConditions extends Specification {
size() == 3
}
}

@FailsWith(ConditionNotSatisfiedError)
def "GDK method that looks like built-in method as implicit condition"() {
expect:
null.with {
false
}
}

@FailsWith(ConditionNotSatisfiedError)
def "GDK method that looks like built-in method as explicit condition"() {
expect:
assert null.with {
false
}
Comment on lines +171 to +173
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 Do we need to test assert in other places (blocks, helper methods, ...)?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally it could make sense for each and every test to have an implicit and an explicit version I guess. :-D

But thanks to Groovy adapting an earlier version of our power assertions, you should even get nice output and failure if the rewriting of Spock does not happen in most cases.

Specifically here was the problem that there was a half rewrite.
The rewriting removed the explicit assert, but did not add the verifyMethodCondition magic.
So we lost the Groovy built-in assertion and also the Spock assertion in this case.

}

def "condition method #conditionMethod within condition method #conditionMethod"() {
when:
runner.runFeatureBody("""
expect:
$conditionMethod(['']) {
$conditionMethod(['']) {
false
}
}
""")

then:
thrown(expectedException)

where:
conditionMethod || expectedException
'with' || ConditionNotSatisfiedError
'verifyAll' || ConditionNotSatisfiedError
'verifyEach' || SpockAssertionError
}

def "condition method #conditionMethod within condition method #conditionMethod with exception"() {
when:
runner.runFeatureBody("""
expect:
$conditionMethod(['']) {
$conditionMethod(['']) {
true
throw new Exception('foo')
}
}
""")

then:
def exception = thrown(expectedException)
exception.message."${expectedException == Exception ? 'equals' : 'contains'}"('foo')

where:
conditionMethod || expectedException
'with' || Exception
'verifyAll' || Exception
'verifyEach' || SpockAssertionError
}

def "condition method #conditionMethod within condition method #conditionMethod with only exception"() {
when:
runner.runFeatureBody("""
expect:
$conditionMethod(['']) {
$conditionMethod(['']) {
throw new Exception('foo')
}
}
""")

then:
def exception = thrown(expectedException)
exception.message."${expectedException == Exception ? 'equals' : 'contains'}"('foo')

where:
conditionMethod || expectedException
'with' || Exception
'verifyAll' || Exception
'verifyEach' || SpockAssertionError
}

def "condition method #conditionMethod"() {
when:
runner.runFeatureBody("""
expect:
$conditionMethod(['']) {
false
}
""")

then:
thrown(expectedException)

where:
conditionMethod || expectedException
'with' || ConditionNotSatisfiedError
'verifyAll' || ConditionNotSatisfiedError
'verifyEach' || SpockAssertionError
}

def "condition method #conditionMethod with exception"() {
when:
runner.runFeatureBody("""
expect:
$conditionMethod(['']) {
true
throw new Exception('foo')
}
""")

then:
def exception = thrown(expectedException)
exception.message."${expectedException == Exception ? 'equals' : 'contains'}"('foo')

where:
conditionMethod || expectedException
'with' || Exception
'verifyAll' || Exception
'verifyEach' || SpockAssertionError
}

def "condition method #conditionMethod with only exception"() {
when:
runner.runFeatureBody("""
expect:
$conditionMethod(['']) {
throw new Exception('foo')
}
""")

then:
def exception = thrown(expectedException)
exception.message."${expectedException == Exception ? 'equals' : 'contains'}"('foo')

where:
conditionMethod || expectedException
'with' || Exception
'verifyAll' || Exception
'verifyEach' || SpockAssertionError
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -387,4 +387,158 @@ class TestSpec extends Specification {
then:
textSnapshotter.assertThat(result.source).matchesSnapshot()
}

Comment thread
Vampire marked this conversation as resolved.
Outdated
def "GDK method that looks like built-in method as implicit condition"() {
when:
def result = compiler.transpileFeatureBody('''
expect:
null.with {
false
}
''')

then:
snapshotter.assertThat(result.source).matchesSnapshot()
}

def "GDK method that looks like built-in method as explicit condition"() {
when:
def result = compiler.transpileFeatureBody('''
expect:
assert null.with {
false
}
''')

then:
snapshotter.assertThat(result.source).matchesSnapshot()
}

def "condition method #conditionMethod within condition method #conditionMethod"() {
when:
def result = compiler.transpileFeatureBody("""
expect:
$conditionMethod(['']) {
$conditionMethod(['']) {
false
}
}
""")

then:
snapshotter.assertThat(result.source).matchesSnapshot()

where:
conditionMethod << [
'with',
'verifyAll',
'verifyEach'
]
}

def "condition method #conditionMethod within condition method #conditionMethod with exception"() {
when:
def result = compiler.transpileFeatureBody("""
expect:
$conditionMethod(['']) {
$conditionMethod(['']) {
true
throw new Exception('foo')
}
}
""")

then:
snapshotter.assertThat(result.source).matchesSnapshot()

where:
conditionMethod << [
'with',
'verifyAll',
'verifyEach'
]
}

def "condition method #conditionMethod within condition method #conditionMethod with only exception"() {
when:
def result = compiler.transpileFeatureBody("""
expect:
$conditionMethod(['']) {
$conditionMethod(['']) {
throw new Exception('foo')
}
}
""")

then:
snapshotter.assertThat(result.source).matchesSnapshot()

where:
conditionMethod << [
'with',
'verifyAll',
'verifyEach'
]
}

def "condition method #conditionMethod"() {
when:
def result = compiler.transpileFeatureBody("""
expect:
$conditionMethod(['']) {
false
}
""")

then:
snapshotter.assertThat(result.source).matchesSnapshot()

where:
conditionMethod << [
'with',
'verifyAll',
'verifyEach'
]
}

def "condition method #conditionMethod with exception"() {
when:
def result = compiler.transpileFeatureBody("""
expect:
$conditionMethod(['']) {
true
throw new Exception('foo')
}
""")

then:
snapshotter.assertThat(result.source).matchesSnapshot()

where:
conditionMethod << [
'with',
'verifyAll',
'verifyEach'
]
}

def "condition method #conditionMethod with only exception"() {
when:
def result = compiler.transpileFeatureBody("""
expect:
$conditionMethod(['']) {
throw new Exception('foo')
}
""")

then:
snapshotter.assertThat(result.source).matchesSnapshot()

where:
conditionMethod << [
'with',
'verifyAll',
'verifyEach'
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
@org.spockframework.runtime.model.FeatureMetadata(name = 'a feature', ordinal = 0, line = 1, blocks = [@org.spockframework.runtime.model.BlockMetadata(kind = org.spockframework.runtime.model.BlockKind.EXPECT, texts = [])], parameterNames = [])
public void $spock_feature_0_0() {
org.spockframework.runtime.ErrorCollector $spock_errorCollector = org.spockframework.runtime.ErrorRethrower.INSTANCE
org.spockframework.runtime.ValueRecorder $spock_valueRecorder = new org.spockframework.runtime.ValueRecorder()
org.spockframework.runtime.SpockRuntime.callBlockEntered(this, 0)
try {
org.spockframework.runtime.SpockRuntime.verifyMethodCondition($spock_errorCollector, $spock_valueRecorder.reset(), 'null.with {\n false\n}', 2, 8, null, $spock_valueRecorder.record($spock_valueRecorder.startRecordingValue(0), null), $spock_valueRecorder.record($spock_valueRecorder.startRecordingValue(1), 'with'), new java.lang.Object[]{$spock_valueRecorder.record($spock_valueRecorder.startRecordingValue(2), { ->
false
})}, $spock_valueRecorder.realizeNas(5, false), true, 4)
}
catch (java.lang.Throwable $spock_condition_throwable) {
org.spockframework.runtime.SpockRuntime.conditionFailedWithException($spock_errorCollector, $spock_valueRecorder, 'null.with {\n false\n}', 2, 8, null, $spock_condition_throwable)}
finally {
}
org.spockframework.runtime.SpockRuntime.callBlockExited(this, 0)
this.getSpecificationContext().getMockController().leaveScope()
}
Loading