A Quick Guide to Iterating a Map in Groovy: refactor to use spock testing framework
This commit is contained in:
parent
66feff149a
commit
f6fa7f8909
|
@ -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 = [
|
||||||
|
'FF0000': 'Red',
|
||||||
@Test
|
'00FF00': 'Lime',
|
||||||
void whenUsingEach_thenMapIsIterated() {
|
'0000FF': 'Blue',
|
||||||
def map = [
|
'FFFF00': 'Yellow',
|
||||||
'FF0000' : 'Red',
|
'E6E6FA': 'Lavender',
|
||||||
'00FF00' : 'Lime',
|
'D8BFD8': 'Thistle',
|
||||||
'0000FF' : 'Blue',
|
'DDA0DD': 'Plum',
|
||||||
'FFFF00' : 'Yellow'
|
]
|
||||||
]
|
|
||||||
|
|
||||||
|
def "whenUsingEach_thenMapIsIterated"() {
|
||||||
|
expect:
|
||||||
map.each { println "Hex Code: $it.key = Color Name: $it.value" }
|
map.each { println "Hex Code: $it.key = Color Name: $it.value" }
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
def "whenUsingEachWithEntry_thenMapIsIterated"() {
|
||||||
void whenUsingEachWithEntry_thenMapIsIterated() {
|
expect:
|
||||||
def map = [
|
|
||||||
'E6E6FA' : 'Lavender',
|
|
||||||
'D8BFD8' : 'Thistle',
|
|
||||||
'DDA0DD' : 'Plum',
|
|
||||||
]
|
|
||||||
|
|
||||||
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"
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue