-
Notifications
You must be signed in to change notification settings - Fork 1
/
BottomN.kt
39 lines (32 loc) · 1.34 KB
/
BottomN.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package org.kollektions.examples.consumers
import org.kollektions.consumers.consume
import org.kollektions.consumers.bottomBy
import org.kollektions.consumers.bottomNBy
import kotlin.test.assertEquals
import kotlin.test.Test
class BottomNConsumerTest {
@Test
fun `compare by a projection`() {
val things = listOf(ball, puck, ski, pole, boot)
val projection = { a: Thing -> a.quantity }
val actual = things.consume(bottomBy(projection), bottomNBy(2, projection))
val expected2 = listOf(listOf(ball, puck), listOf(ski, boot))
assertEquals(listOf(ball, puck), actual[0])
assertEquals(expected2, actual[1])
}
@Test
fun `provide a comparator`() {
val things = listOf(ball, puck, ski, pole, boot)
val comparator = { a: Thing, b: Thing -> a.quantity.compareTo(b.quantity) }
val actual = things.consume(bottomBy(comparator), bottomNBy(2, comparator))
val expected2 = listOf(listOf(ball, puck), listOf(ski, boot))
assertEquals(listOf(ball, puck), actual[0])
assertEquals(expected2, actual[1])
}
data class Thing(val name: String, val quantity: Int)
private val ball = Thing("Ball", 1)
private val puck = Thing("Puck", 1)
private val ski = Thing("Ski", 2)
private val pole = Thing("Pole", 3)
private val boot = Thing("Boot", 2)
}