Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
@@ -0,0 +1,180 @@
/*
* Copyright 2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* https://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package org.spockframework.smoke.ast.condition


import org.spockframework.EmbeddedSpecification
import org.spockframework.specs.extension.SpockSnapshotter
import spock.lang.Snapshot

class ConditionMethodsAstSpec extends EmbeddedSpecification {
@Snapshot(extension = 'groovy')
SpockSnapshotter snapshotter

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'
]
}
}
Loading