Maps in Groovy: convert to spock test framework

This commit is contained in:
Alex Golub 2023-01-20 15:01:35 +02:00
parent 46ac54b723
commit 3512ca847f
1 changed files with 112 additions and 100 deletions

View File

@ -1,148 +1,160 @@
package com.baeldung.maps;
package com.baeldung.maps
import static groovy.test.GroovyAssert.*
import org.junit.Test
import spock.lang.Specification
class MapTest{
@Test
void createMap() {
class MapTest extends Specification {
def "createMap"() {
when:
def emptyMap = [:]
assertNotNull(emptyMap)
def map = [name: "Jerry", age: 42, city: "New York"]
assertTrue(emptyMap instanceof java.util.LinkedHashMap)
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
void addItemsToMap() {
def map = [name:"Jerry"]
map["age"] = 42
map.city = "New York"
def "addItemsToMap"() {
given:
def map = [name: "Jerry"]
def hobbyLiteral = "hobby"
def hobbyMap = [(hobbyLiteral): "Singing"]
def appendToMap = [:]
when:
map["age"] = 42
map.city = "New York"
map.putAll(hobbyMap)
assertTrue(map == [name:"Jerry", age: 42, city: "New York", hobby:"Singing"])
assertTrue(hobbyMap.hobby == "Singing")
assertTrue(hobbyMap[hobbyLiteral] == "Singing")
map.plus([1:20]) // returns new map
appendToMap.plus([1: 20])
appendToMap << [2: 30]
map << [2:30]
then:
map == [name: "Jerry", age: 42, city: "New York", hobby: "Singing"]
hobbyMap.hobby == "Singing"
hobbyMap[hobbyLiteral] == "Singing"
appendToMap == [2: 30] // plus(Map) returns new instance of Map
}
@Test
void getItemsFromMap() {
def map = [name:"Jerry", age: 42, city: "New York", hobby:"Singing"]
assertTrue(map["name"] == "Jerry")
assertTrue(map.name == "Jerry")
def "getItemsFromMap"() {
when:
def map = [name: "Jerry", age: 42, city: "New York", hobby: "Singing"]
def propertyAge = "age"
assertTrue(map[propertyAge] == 42)
then:
map["name"] == "Jerry"
map.name == "Jerry"
map[propertyAge] == 42
map."$propertyAge" == 42
}
@Test
void removeItemsFromMap() {
def "removeItemsFromMap"() {
given:
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 map = [1:20, a:30, 2:42, 4:34, ba:67, 6:39, 7:49]
when:
def minusMap = map - [2: 42, 4: 34]
removeAllKeysOfTypeString.removeAll { it.key instanceof String }
retainAllEntriesWhereValueIsEven.retainAll { it.value % 2 == 0 }
def minusMap = map.minus([2:42, 4:34]);
assertTrue(minusMap == [1:20, a:30, ba:67, 6:39, 7:49])
minusMap.removeAll{it -> it.key instanceof String}
assertTrue( minusMap == [ 1:20, 6:39, 7:49])
minusMap.retainAll{it -> it.value %2 == 0}
assertTrue( minusMap == [1:20])
then:
minusMap == [1: 20, a: 30, ba: 67, 6: 39, 7: 49]
removeAllKeysOfTypeString == [1: 20, 6: 39, 7: 49]
retainAllEntriesWhereValueIsEven == [1: 20, a: 30]
}
@Test
void iteratingOnMaps(){
def map = [name:"Jerry", age: 42, city: "New York", hobby:"Singing"]
def "iteratingOnMaps"() {
when:
def map = [name: "Jerry", age: 42, city: "New York", hobby: "Singing"]
map.each{ entry -> println "$entry.key: $entry.value" }
map.eachWithIndex{ entry, i -> println "$i $entry.key: $entry.value" }
map.eachWithIndex{ key, value, i -> println "$i $key: $value" }
then:
map.each { entry -> println "$entry.key: $entry.value" }
map.eachWithIndex { entry, i -> println "$i $entry.key: $entry.value" }
map.eachWithIndex { key, value, i -> println "$i $key: $value" }
}
@Test
void filteringAndSearchingMaps(){
def map = [name:"Jerry", age: 42, city: "New York", hobby:"Singing"]
def "filteringAndSearchingMaps"() {
given:
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
void collect(){
def map = [1: [name:"Jerry", age: 42, city: "New York"],
2: [name:"Long", age: 25, city: "New York"],
3: [name:"Dustin", age: 29, city: "New York"],
4: [name:"Dustin", age: 34, city: "New York"]]
def names = map.collect{entry -> entry.value.name} // returns only list
assertTrue(names == ["Jerry", "Long", "Dustin", "Dustin"])
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]}
assertTrue(idNames == [1:"Jerry", 2: "Long", 3:"Dustin", 4: "Dustin"])
def below30Names = map.findAll{it.value.age < 30}.collect{key, value -> value.name}
assertTrue(below30Names == ["Long", "Dustin"])
def "collect"() {
given:
def map = [
1: [name: "Jerry", age: 42, city: "New York"],
2: [name: "Long", age: 25, city: "New York"],
3: [name: "Dustin", age: 29, city: "New York"],
4: [name: "Dustin", age: 34, city: "New York"]
]
when:
def names = map.collect { entry -> entry.value.name } // returns only list
def uniqueNames = map.collect { entry -> entry.value.name }
.unique()
def idNames = map.collectEntries { key, value -> [key, value.name] }
def below30Names = map.findAll { it.value.age < 30 }
.collect { key, value -> value.name }
then:
names == ["Jerry", "Long", "Dustin", "Dustin"]
uniqueNames == ["Jerry", "Long", "Dustin"]
idNames == [1: "Jerry", 2: "Long", 3: "Dustin", 4: "Dustin"]
below30Names == ["Long", "Dustin"]
}
@Test
void group(){
def map = [1:20, 2: 40, 3: 11, 4: 93]
def subMap = map.groupBy{it.value % 2}
println subMap
assertTrue(subMap == [0:[1:20, 2:40 ], 1:[3:11, 4:93]])
def "group"() {
given:
def map = [1: 20, 2: 40, 3: 11, 4: 93]
when:
def subMap = map.groupBy { it.value % 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
void sorting(){
def map = [ab:20, a: 40, cb: 11, ba: 93]
def "sorting"() {
given:
def map = [ab: 20, a: 40, cb: 11, ba: 93]
when:
def naturallyOrderedMap = map.sort()
assertTrue([a:40, ab:20, ba:93, cb:11] == naturallyOrderedMap)
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 })
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]
}
}