-
Notifications
You must be signed in to change notification settings - Fork 437
/
ReflectionTest.kt
55 lines (47 loc) · 1.59 KB
/
ReflectionTest.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package arrow.optics
import arrow.core.test.UnitSpec
import io.kotest.matchers.shouldBe
import io.kotest.property.Arb
import io.kotest.property.arbitrary.int
import io.kotest.property.arbitrary.string
data class Person(val name: String, val friends: List<String>)
sealed interface Cutlery
object Fork: Cutlery
object Spoon: Cutlery
object ReflectionTest: UnitSpec() {
init {
"optional for function" {
checkAll(Arb.list(Arb.int())) { ints ->
val firsty = { it: List<Int> -> it.firstOrNull() }
firsty.ogetter.get(ints) shouldBe ints.firstOrNull()
}
}
"lenses for field, get" {
checkAll(Arb.string(), Arb.list(Arb.string())) { nm, fs ->
val p = Person(nm, fs.toMutableList())
Person::name.lens.get(p) shouldBe nm
}
}
"lenses for field, set" {
checkAll(Arb.string(), Arb.list(Arb.string())) { nm, fs ->
val p = Person(nm, fs.toMutableList())
val m = Person::name.lens.modify(p) { it.capitalize() }
m shouldBe Person(nm.capitalize(), fs)
}
}
"traversal for list, set" {
checkAll(Arb.string(), Arb.list(Arb.string())) { nm, fs ->
val p = Person(nm, fs)
val m = Person::friends.every.modify(p) { it.capitalize() }
m shouldBe Person(nm, fs.map { it.capitalize() })
}
}
"instances" {
val things = listOf(Fork, Spoon, Fork)
val forks = Every.list<Cutlery>() compose instance<Cutlery, Fork>()
val spoons = Every.list<Cutlery>() compose instance<Cutlery, Fork>()
forks.size(things) shouldBe 2
spoons.size(things) shouldBe 1
}
}
}