Hello everyone,
Tried to upgrade Griffon to the latest Groovy 2.5.0 and found a problem
with Spock (1.1-groovy-2.4). Together with Jochen and Paul (we're at
Gr8Conf right now) we had a look at the problem and reduced it to the
following code (self contained, runnable with GroovyConsole):
----------------------
@Grab("org.spockframework:spock-core:1.2-groovy-2.4-SNAPSHOT")
@Grab("org.codehaus.griffon:griffon-javafx:2.14.0")
@GrabResolver("https://oss.sonatype.org/content/repositories/snapshots/")
import griffon.javafx.beans.binding.*
import groovy.transform.Canonical
import javafx.beans.binding.Binding
import javafx.collections.FXCollections
import javafx.collections.ObservableList
import spock.lang.Specification
import spock.lang.Unroll
import java.util.function.Predicate
@Unroll
class FilteringBindingsSpec extends Specification {
def "Filter then findFirst in list with defaultValue"() {
given:
Box defaultValue = new Box(6)
ObservableList<Box> items = FXCollections.observableArrayList()
Predicate<Box> filter = { it.id % 2 == 0 }
Binding binding = FilteringBindings.filterThenFindFirst(items,
defaultValue, filter)
expect:
defaultValue == binding.get()
when:
items.addAll([new Box(1), new Box(2), new Box(3), new Box(4)])
then:
new Box(2) == binding.get()
}
def "Filter then findFirst #type in list with defaultValue"() {
given:
ObservableList items = FXCollections.observableArrayList()
Binding binding =
FilteringBindings."filterThenFindFirst${type}"(items, defaultValue,
predicate)
expect:
defaultValue == binding.get()
when:
items.addAll(values)
then:
result == binding.get()
where:
type | defaultValue | predicate |
values | result
'Boolean' | true | { it } | [false,
true, false] | true
'Integer' | 6 | { it % 2 == 0 } | [1, 2, 3,
4, 5] | 2
//'Long' | 6L | { it % 2 == 0 } | [1L, 2L,
3L, 4L, 5L] | 2L
//'Float' | 6f | { it % 2 == 0 } | [1f, 2f,
3f, 4f, 5f] | 2f
//'Double' | 6d | { it % 2 == 0 } | [1d, 2d,
3d, 4d, 5d] | 2d
//'String' | '6' | { it.toInteger() % 2 == 0 } | ['1',
'2', '3', '4', '5'] | '2'
}
@Canonical
private static class Box {
int id
}
}
----------------------
The error we get is
JUnit 4 Runner, Tests: 1, Failures: 2, Time: 175
Test Failure: Filter then findFirst #type in list with
defaultValue(FilteringBindingsSpec)
org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast
object 'true' with class 'java.lang.Boolean' to class
'FilteringBindingsSpec$Box'
at FilteringBindingsSpec.Filter then findFirst #type in list with
defaultValue(ConsoleScript0:49)
Test Failure: Filter then findFirst #type in list with
defaultValue(FilteringBindingsSpec)
org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast
object '6' with class 'java.lang.Integer' to class
'FilteringBindingsSpec$Box'
at FilteringBindingsSpec.Filter then findFirst #type in list with
defaultValue(ConsoleScript0:49)
Apparently there's some state left from the first feature method that leaks
into the second and third. This does not occur with Groovy 2.4.15 and Spock
1.1-groovy-2.4
-------------------------------------------
Java Champion; Groovy Enthusiast
JCP EC Associate Seat
http://andresalmiray.com
http://www.linkedin.com/in/aalmiray
--
What goes up, must come down. Ask any system administrator.
There are 10 types of people in the world: Those who understand binary, and
those who don't.
To understand recursion, we must first understand recursion.