/
EitherMatchersTest.kt
117 lines (96 loc) · 3.63 KB
/
EitherMatchersTest.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package com.sksamuel.kotlintest.assertions.arrow
import arrow.core.Either
import io.kotlintest.assertions.arrow.either.*
import io.kotlintest.should
import io.kotlintest.shouldBe
import io.kotlintest.shouldThrow
import io.kotlintest.specs.WordSpec
class EitherMatchersTest : WordSpec() {
sealed class MyError {
object Foo : MyError()
object Boo : MyError()
}
init {
"Either should beRight()" should {
"test that the either is of type right" {
Either.right("boo").shouldBeRight()
Either.left("boo").shouldNotBeRight()
}
"use contracts to expose Right<*>" {
val e = Either.right("boo")
e.shouldBeRight()
e.b shouldBe "boo"
}
}
"Either should beRight(fn)" should {
"test that the either is of type right" {
data class Person(val name: String, val location: String)
Either.right(Person("sam", "chicago")) shouldBeRight {
it.name shouldBe "sam"
it.location shouldBe "chicago"
}
}
}
"Either should beRight(value)" should {
"test that an either is a right with the given value" {
shouldThrow<AssertionError> {
Either.left("foo") should beRight("boo")
}.message shouldBe "Either should be Right(boo) but was Left(foo)"
shouldThrow<AssertionError> {
Either.right("foo") should beRight("boo")
}.message shouldBe "Either should be Right(boo) but was Right(foo)"
Either.right("foo") shouldNotBeRight "boo"
Either.left("foo") shouldNotBeRight "foo"
Either.right("boo") should beRight("boo")
Either.right("boo") shouldBeRight "boo"
}
}
"Either should beLeft()" should {
"test that the either is of type left" {
Either.left("boo").shouldBeLeft()
Either.right("boo").shouldNotBeLeft()
}
"use contracts to expose Left<*>" {
val e = Either.left("boo")
e.shouldBeLeft()
e.a shouldBe "boo"
}
}
"Either should beLeft(fn)" should {
"test that the either is of type right" {
data class Person(val name: String, val location: String)
Either.left(Person("sam", "chicago")) shouldBeLeft {
it.name shouldBe "sam"
it.location shouldBe "chicago"
}
}
}
"Either should beLeft(value)" should {
"test that an either is a left with the given value" {
shouldThrow<AssertionError> {
Either.right("foo") should beLeft("boo")
}.message shouldBe "Either should be Left(boo) but was Right(foo)"
shouldThrow<AssertionError> {
Either.left("foo") should beLeft("boo")
}.message shouldBe "Either should be Left(boo) but was Left(foo)"
shouldThrow<AssertionError> {
Either.left("foo") shouldNotBeLeft "foo"
}.message shouldBe "Either should not be Left(foo)"
Either.left("boo") should beLeft("boo")
Either.left("boo") shouldBeLeft "boo"
Either.right("boo") shouldNotBeLeft "boo"
}
}
"Either should beLeftOfType" should {
"test that an either is a left have exactly the same type" {
shouldThrow<AssertionError> {
Either.left(MyError.Boo).shouldBeLeftOfType<MyError.Foo>()
}.message shouldBe "Either should be Left<${MyError.Foo::class.qualifiedName}> but was Left<${MyError.Boo::class.qualifiedName}>"
Either.left(MyError.Foo).shouldBeLeftOfType<MyError.Foo>()
Either.left(MyError.Boo).shouldBeLeftOfType<MyError.Boo>()
Either.left(MyError.Boo).shouldNotBeLeftOfType<MyError.Foo>()
Either.right("foo").shouldNotBeLeftOfType<MyError.Foo>()
}
}
}
}