A Quick Guide to Iterating a Map in Groovy: refactor to use spock testing framework

This commit is contained in:
Alex Golub 2023-01-20 19:25:34 +02:00
parent 66feff149a
commit f6fa7f8909
1 changed files with 25 additions and 56 deletions

View File

@ -1,85 +1,54 @@
package com.baeldung.iteratemap package com.baeldung.iteratemap
import com.baeldung.find.Person import spock.lang.Specification
import org.junit.Test
import static org.junit.Assert.* class IterateMapUnitTest extends Specification {
class IterateMapUnitTest { final Map map = [
@Test
void whenUsingEach_thenMapIsIterated() {
def map = [
'FF0000': 'Red', 'FF0000': 'Red',
'00FF00': 'Lime', '00FF00': 'Lime',
'0000FF': 'Blue', '0000FF': 'Blue',
'FFFF00' : 'Yellow' 'FFFF00': 'Yellow',
]
map.each { println "Hex Code: $it.key = Color Name: $it.value" }
}
@Test
void whenUsingEachWithEntry_thenMapIsIterated() {
def map = [
'E6E6FA': 'Lavender', 'E6E6FA': 'Lavender',
'D8BFD8': 'Thistle', 'D8BFD8': 'Thistle',
'DDA0DD': 'Plum', 'DDA0DD': 'Plum',
] ]
def "whenUsingEach_thenMapIsIterated"() {
expect:
map.each { println "Hex Code: $it.key = Color Name: $it.value" }
}
def "whenUsingEachWithEntry_thenMapIsIterated"() {
expect:
map.each { entry -> println "Hex Code: $entry.key = Color Name: $entry.value" } map.each { entry -> println "Hex Code: $entry.key = Color Name: $entry.value" }
} }
@Test def "whenUsingEachWithKeyAndValue_thenMapIsIterated"() {
void whenUsingEachWithKeyAndValue_thenMapIsIterated() { expect:
def map = [
'000000' : 'Black',
'FFFFFF' : 'White',
'808080' : 'Gray'
]
map.each { key, val -> map.each { key, val ->
println "Hex Code: $key = Color Name $val" println "Hex Code: $key = Color Name $val"
} }
} }
@Test def "whenUsingEachWithIndexAndEntry_thenMapIsIterated"() {
void whenUsingEachWithIndexAndEntry_thenMapIsIterated() { expect:
def map = [
'800080' : 'Purple',
'4B0082' : 'Indigo',
'6A5ACD' : 'Slate Blue'
]
map.eachWithIndex { entry, index -> map.eachWithIndex { entry, index ->
def indent = ((index == 0 || index % 2 == 0) ? " " : "") def indent = index % 2 == 0 ? " " : ""
println "$indent Hex Code: $entry.key = Color Name: $entry.value" println "$indent Hex Code: $entry.key = Color Name: $entry.value"
} }
} }
@Test def "whenUsingEachWithIndexAndKeyAndValue_thenMapIsIterated"() {
void whenUsingEachWithIndexAndKeyAndValue_thenMapIsIterated() { expect:
def map = [
'FFA07A' : 'Light Salmon',
'FF7F50' : 'Coral',
'FF6347' : 'Tomato',
'FF4500' : 'Orange Red'
]
map.eachWithIndex { key, val, index -> map.eachWithIndex { key, val, index ->
def indent = ((index == 0 || index % 2 == 0) ? " " : "") def indent = index % 2 == 0 ? " " : ""
println "$indent Hex Code: $key = Color Name: $val" println "$indent Hex Code: $key = Color Name: $val"
} }
} }
@Test def "whenUsingForLoop_thenMapIsIterated"() {
void whenUsingForLoop_thenMapIsIterated() { expect:
def map = [
'2E8B57' : 'Seagreen',
'228B22' : 'Forest Green',
'008000' : 'Green'
]
for (entry in map) { for (entry in map) {
println "Hex Code: $entry.key = Color Name: $entry.value" println "Hex Code: $entry.key = Color Name: $entry.value"
} }