Maps in Groovy: convert to spock test framework
This commit is contained in:
parent
46ac54b723
commit
3512ca847f
@ -1,148 +1,160 @@
|
|||||||
package com.baeldung.maps;
|
package com.baeldung.maps
|
||||||
|
|
||||||
import static groovy.test.GroovyAssert.*
|
import spock.lang.Specification
|
||||||
import org.junit.Test
|
|
||||||
|
|
||||||
class MapTest{
|
class MapTest extends Specification {
|
||||||
|
|
||||||
@Test
|
|
||||||
void createMap() {
|
|
||||||
|
|
||||||
|
def "createMap"() {
|
||||||
|
when:
|
||||||
def emptyMap = [:]
|
def emptyMap = [:]
|
||||||
assertNotNull(emptyMap)
|
|
||||||
|
|
||||||
assertTrue(emptyMap instanceof java.util.LinkedHashMap)
|
|
||||||
|
|
||||||
def map = [name: "Jerry", age: 42, city: "New York"]
|
def map = [name: "Jerry", age: 42, city: "New York"]
|
||||||
assertTrue(map.size() == 3)
|
|
||||||
|
then:
|
||||||
|
emptyMap != null
|
||||||
|
emptyMap instanceof java.util.LinkedHashMap
|
||||||
|
map.size() == 3
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
def "addItemsToMap"() {
|
||||||
void addItemsToMap() {
|
given:
|
||||||
|
|
||||||
def map = [name: "Jerry"]
|
def map = [name: "Jerry"]
|
||||||
|
|
||||||
map["age"] = 42
|
|
||||||
|
|
||||||
map.city = "New York"
|
|
||||||
|
|
||||||
def hobbyLiteral = "hobby"
|
def hobbyLiteral = "hobby"
|
||||||
def hobbyMap = [(hobbyLiteral): "Singing"]
|
def hobbyMap = [(hobbyLiteral): "Singing"]
|
||||||
|
def appendToMap = [:]
|
||||||
|
|
||||||
|
when:
|
||||||
|
map["age"] = 42
|
||||||
|
map.city = "New York"
|
||||||
|
|
||||||
map.putAll(hobbyMap)
|
map.putAll(hobbyMap)
|
||||||
|
|
||||||
assertTrue(map == [name:"Jerry", age: 42, city: "New York", hobby:"Singing"])
|
appendToMap.plus([1: 20])
|
||||||
assertTrue(hobbyMap.hobby == "Singing")
|
appendToMap << [2: 30]
|
||||||
assertTrue(hobbyMap[hobbyLiteral] == "Singing")
|
|
||||||
|
|
||||||
map.plus([1:20]) // returns new map
|
then:
|
||||||
|
map == [name: "Jerry", age: 42, city: "New York", hobby: "Singing"]
|
||||||
|
|
||||||
map << [2:30]
|
hobbyMap.hobby == "Singing"
|
||||||
|
hobbyMap[hobbyLiteral] == "Singing"
|
||||||
|
|
||||||
|
appendToMap == [2: 30] // plus(Map) returns new instance of Map
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
def "getItemsFromMap"() {
|
||||||
void getItemsFromMap() {
|
when:
|
||||||
|
|
||||||
def map = [name: "Jerry", age: 42, city: "New York", hobby: "Singing"]
|
def map = [name: "Jerry", age: 42, city: "New York", hobby: "Singing"]
|
||||||
|
|
||||||
assertTrue(map["name"] == "Jerry")
|
|
||||||
|
|
||||||
assertTrue(map.name == "Jerry")
|
|
||||||
|
|
||||||
def propertyAge = "age"
|
def propertyAge = "age"
|
||||||
assertTrue(map[propertyAge] == 42)
|
|
||||||
|
then:
|
||||||
|
map["name"] == "Jerry"
|
||||||
|
map.name == "Jerry"
|
||||||
|
map[propertyAge] == 42
|
||||||
|
map."$propertyAge" == 42
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
def "removeItemsFromMap"() {
|
||||||
void removeItemsFromMap() {
|
given:
|
||||||
|
|
||||||
def map = [1: 20, a: 30, 2: 42, 4: 34, ba: 67, 6: 39, 7: 49]
|
def map = [1: 20, a: 30, 2: 42, 4: 34, ba: 67, 6: 39, 7: 49]
|
||||||
|
def removeAllKeysOfTypeString = [1: 20, a: 30, ba: 67, 6: 39, 7: 49]
|
||||||
|
def retainAllEntriesWhereValueIsEven = [1: 20, a: 30, ba: 67, 6: 39, 7: 49]
|
||||||
|
|
||||||
def minusMap = map.minus([2:42, 4:34]);
|
when:
|
||||||
assertTrue(minusMap == [1:20, a:30, ba:67, 6:39, 7:49])
|
def minusMap = map - [2: 42, 4: 34]
|
||||||
|
removeAllKeysOfTypeString.removeAll { it.key instanceof String }
|
||||||
|
retainAllEntriesWhereValueIsEven.retainAll { it.value % 2 == 0 }
|
||||||
|
|
||||||
minusMap.removeAll{it -> it.key instanceof String}
|
then:
|
||||||
assertTrue( minusMap == [ 1:20, 6:39, 7:49])
|
minusMap == [1: 20, a: 30, ba: 67, 6: 39, 7: 49]
|
||||||
|
removeAllKeysOfTypeString == [1: 20, 6: 39, 7: 49]
|
||||||
minusMap.retainAll{it -> it.value %2 == 0}
|
retainAllEntriesWhereValueIsEven == [1: 20, a: 30]
|
||||||
assertTrue( minusMap == [1:20])
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
def "iteratingOnMaps"() {
|
||||||
void iteratingOnMaps(){
|
when:
|
||||||
def map = [name: "Jerry", age: 42, city: "New York", hobby: "Singing"]
|
def map = [name: "Jerry", age: 42, city: "New York", hobby: "Singing"]
|
||||||
|
|
||||||
|
then:
|
||||||
map.each { entry -> println "$entry.key: $entry.value" }
|
map.each { entry -> println "$entry.key: $entry.value" }
|
||||||
|
|
||||||
map.eachWithIndex { entry, i -> println "$i $entry.key: $entry.value" }
|
map.eachWithIndex { entry, i -> println "$i $entry.key: $entry.value" }
|
||||||
|
|
||||||
map.eachWithIndex { key, value, i -> println "$i $key: $value" }
|
map.eachWithIndex { key, value, i -> println "$i $key: $value" }
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
def "filteringAndSearchingMaps"() {
|
||||||
void filteringAndSearchingMaps(){
|
given:
|
||||||
def map = [name: "Jerry", age: 42, city: "New York", hobby: "Singing"]
|
def map = [name: "Jerry", age: 42, city: "New York", hobby: "Singing"]
|
||||||
|
|
||||||
assertTrue(map.find{ it.value == "New York"}.key == "city")
|
when:
|
||||||
|
def find = map.find { it.value == "New York" }
|
||||||
|
def finaAll = map.findAll { it.value == "New York" }
|
||||||
|
def grep = map.grep { it.value == "New York" }
|
||||||
|
def every = map.every { it -> it.value instanceof String }
|
||||||
|
def any = map.any { it -> it.value instanceof String }
|
||||||
|
|
||||||
assertTrue(map.findAll{ it.value == "New York"} == [city : "New York"])
|
then:
|
||||||
|
find.key == "city"
|
||||||
|
|
||||||
map.grep{it.value == "New York"}.each{ it -> assertTrue(it.key == "city" && it.value == "New York")}
|
finaAll instanceof Map
|
||||||
|
finaAll == [city: "New York"]
|
||||||
|
|
||||||
assertTrue(map.every{it -> it.value instanceof String} == false)
|
grep instanceof Collection
|
||||||
|
grep.each { it -> assert it.key == "city" && it.value == "New York" }
|
||||||
|
|
||||||
assertTrue(map.any{it -> it.value instanceof String} == true)
|
every instanceof Boolean
|
||||||
|
!every
|
||||||
|
|
||||||
|
any instanceof Boolean
|
||||||
|
any
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
def "collect"() {
|
||||||
void collect(){
|
given:
|
||||||
|
def map = [
|
||||||
def map = [1: [name:"Jerry", age: 42, city: "New York"],
|
1: [name: "Jerry", age: 42, city: "New York"],
|
||||||
2: [name: "Long", age: 25, city: "New York"],
|
2: [name: "Long", age: 25, city: "New York"],
|
||||||
3: [name: "Dustin", age: 29, city: "New York"],
|
3: [name: "Dustin", age: 29, city: "New York"],
|
||||||
4: [name:"Dustin", age: 34, city: "New York"]]
|
4: [name: "Dustin", age: 34, city: "New York"]
|
||||||
|
]
|
||||||
|
|
||||||
|
when:
|
||||||
def names = map.collect { entry -> entry.value.name } // returns only list
|
def names = map.collect { entry -> entry.value.name } // returns only list
|
||||||
assertTrue(names == ["Jerry", "Long", "Dustin", "Dustin"])
|
def uniqueNames = map.collect { entry -> entry.value.name }
|
||||||
|
.unique()
|
||||||
def uniqueNames = map.collect([] as HashSet){entry -> entry.value.name}
|
|
||||||
assertTrue(uniqueNames == ["Jerry", "Long", "Dustin"] as Set)
|
|
||||||
|
|
||||||
def idNames = map.collectEntries { key, value -> [key, value.name] }
|
def idNames = map.collectEntries { key, value -> [key, value.name] }
|
||||||
assertTrue(idNames == [1:"Jerry", 2: "Long", 3:"Dustin", 4: "Dustin"])
|
def below30Names = map.findAll { it.value.age < 30 }
|
||||||
|
.collect { key, value -> value.name }
|
||||||
def below30Names = map.findAll{it.value.age < 30}.collect{key, value -> value.name}
|
|
||||||
assertTrue(below30Names == ["Long", "Dustin"])
|
|
||||||
|
|
||||||
|
|
||||||
|
then:
|
||||||
|
names == ["Jerry", "Long", "Dustin", "Dustin"]
|
||||||
|
uniqueNames == ["Jerry", "Long", "Dustin"]
|
||||||
|
idNames == [1: "Jerry", 2: "Long", 3: "Dustin", 4: "Dustin"]
|
||||||
|
below30Names == ["Long", "Dustin"]
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
def "group"() {
|
||||||
void group(){
|
given:
|
||||||
def map = [1: 20, 2: 40, 3: 11, 4: 93]
|
def map = [1: 20, 2: 40, 3: 11, 4: 93]
|
||||||
|
|
||||||
|
when:
|
||||||
def subMap = map.groupBy { it.value % 2 }
|
def subMap = map.groupBy { it.value % 2 }
|
||||||
println subMap
|
|
||||||
assertTrue(subMap == [0:[1:20, 2:40 ], 1:[3:11, 4:93]])
|
|
||||||
|
|
||||||
def keySubMap = map.subMap([1, 2])
|
def keySubMap = map.subMap([1, 2])
|
||||||
assertTrue(keySubMap == [1:20, 2:40])
|
|
||||||
|
|
||||||
|
then:
|
||||||
|
subMap == [0: [1: 20, 2: 40], 1: [3: 11, 4: 93]]
|
||||||
|
keySubMap == [1: 20, 2: 40]
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
def "sorting"() {
|
||||||
void sorting(){
|
given:
|
||||||
def map = [ab: 20, a: 40, cb: 11, ba: 93]
|
def map = [ab: 20, a: 40, cb: 11, ba: 93]
|
||||||
|
|
||||||
|
when:
|
||||||
def naturallyOrderedMap = map.sort()
|
def naturallyOrderedMap = map.sort()
|
||||||
assertTrue([a:40, ab:20, ba:93, cb:11] == naturallyOrderedMap)
|
|
||||||
|
|
||||||
def compSortedMap = map.sort({ k1, k2 -> k1 <=> k2 } as Comparator)
|
def compSortedMap = map.sort({ k1, k2 -> k1 <=> k2 } as Comparator)
|
||||||
assertTrue([a:40, ab:20, ba:93, cb:11] == compSortedMap)
|
|
||||||
|
|
||||||
def cloSortedMap = map.sort({ it1, it2 -> it1.value <=> it1.value })
|
def cloSortedMap = map.sort({ it1, it2 -> it1.value <=> it1.value })
|
||||||
assertTrue([cb:11, ab:20, a:40, ba:93] == cloSortedMap)
|
|
||||||
|
|
||||||
|
then:
|
||||||
|
naturallyOrderedMap == [a: 40, ab: 20, ba: 93, cb: 11]
|
||||||
|
compSortedMap == [a: 40, ab: 20, ba: 93, cb: 11]
|
||||||
|
cloSortedMap == [cb: 11, ab: 20, a: 40, ba: 93]
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user