Last active
August 29, 2015 14:04
-
-
Save smaldini/113fdec5f1604dd093db to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def 'GroupBy will re-route N elements to a nested stream based on the mapped key'() { | |
given: | |
'a source and a collected window stream' | |
def source = Streams.<SimplePojo> defer() | |
def result = [:] | |
source.groupBy { pojo -> | |
pojo.id | |
}.consume { stream -> | |
stream.consume { pojo -> | |
if (result[pojo.id]) { | |
result[pojo.id] << pojo.title | |
} else { | |
result[pojo.id] = [pojo.title] | |
} | |
} | |
} | |
when: | |
'some values are accepted' | |
source.broadcastNext(new SimplePojo(id: 1, title: 'Stephane')) | |
source.broadcastNext(new SimplePojo(id: 1, title: 'Jon')) | |
source.broadcastNext(new SimplePojo(id: 1, title: 'Sandrine')) | |
source.broadcastNext(new SimplePojo(id: 2, title: 'Acme')) | |
source.broadcastNext(new SimplePojo(id: 3, title: 'Acme2')) | |
source.broadcastNext(new SimplePojo(id: 3, title: 'Acme3')) | |
source.broadcastComplete() | |
println source.debug() | |
then: | |
'the result should group titles by id' | |
result | |
result == [ | |
1: ['Stephane', 'Jon', 'Sandrine'], | |
2: ['Acme'], | |
3: ['Acme2', 'Acme3'] | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment