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