Improved assertion message in "with(object) {}"?
Issue description
def "should keep verbose error message"() {
given:
String foo = "foo message"
expect:
foo.contains("bar")
}
results in very nice error message:
Condition not satisfied:
foo.contains("bar")
| |
| false
foo message
However, when with(object) {} is used with the same assertion:
def "should keep verbose error message"() {
given:
String foo = "foo message"
expect:
with(foo) {
contains("bar")
}
}
it looks worse:
Condition not satisfied:
contains("bar")
|
false
Maybe it would be possible in AST transformation to get the real object value and present it?
Additional Environment information
Spock 2.0-M4-groovy-3.0
Java/JDK
OpenJDK 15
Groovy version
3.0.7
This might not be trivial, as you can't really decide at compile time, whether the method belongs to the delegate or the specification.
def "spec message"() {
given:
String foo = "foo message"
expect:
with(foo) {
cont(it)
}
}
boolean cont(String str) {
return str.contains("bar")
}
@szpak I was thinking about adding new withContext/(verifyAllContext|verifyAllWithContext)? methods, that would just add the target object to the exception message.
A simple workaround is to still use it.contains("bar")