Skip to content

Commit 97c1834

Browse files
committed
Added sample code for eachCount and eachCountTo methods
1 parent d74a21e commit 97c1834

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.baeldung.eachCountMethods
2+
3+
import org.junit.jupiter.api.Test
4+
import kotlin.test.assertEquals
5+
6+
class EachCountMethodsUnitTest {
7+
@Test
8+
fun `given a list of flights eachCount method should create a count per airline`() {
9+
val flights = listOf("EK060", "EK531", "LH7", "LH1030", "DL47", "AI120")
10+
val flightCount = flights.groupingBy { it.take(2) }.eachCount()
11+
val expectedMap = mapOf("EK" to 2, "LH" to 2, "DL" to 1, "AI" to 1)
12+
assertEquals(expectedMap, flightCount)
13+
}
14+
15+
@Test
16+
fun `given a list of flights, use groupBy and count to implement eachCount logic`() {
17+
val flights = listOf("EK060", "EK531", "LH7", "LH1030", "DL47", "AI120")
18+
val flightCount = flights.groupBy { it.take(2) }.mapValues { it.value.count() }
19+
val expectedMap = mapOf("EK" to 2, "LH" to 2, "DL" to 1, "AI" to 1)
20+
assertEquals(expectedMap, flightCount)
21+
}
22+
23+
@Test
24+
fun `given a list of flights eachCountTo method should create a count per airline`() {
25+
val flights = listOf("EK060", "EK531", "LH7", "LH1030", "DL47", "AI120")
26+
val flightCount = flights.groupingBy { it.take(2) }.eachCount().toMutableMap()
27+
val expectedMap = mutableMapOf("EK" to 2, "LH" to 2, "DL" to 1, "AI" to 1)
28+
assertEquals(expectedMap, flightCount)
29+
val moreFlights = listOf("EK061", "AI435")
30+
moreFlights.groupingBy { it.take(2) }.eachCountTo(flightCount)
31+
val expectedMapAfterMoreFlights = mutableMapOf("EK" to 3, "LH" to 2, "DL" to 1, "AI" to 2)
32+
assertEquals(expectedMapAfterMoreFlights, flightCount)
33+
}
34+
}

0 commit comments

Comments
 (0)