|
| 1 | +package katz |
| 2 | + |
| 3 | +import io.kotlintest.KTestJUnitRunner |
| 4 | +import io.kotlintest.matchers.shouldBe |
| 5 | +import io.kotlintest.properties.Gen |
| 6 | +import io.kotlintest.properties.forAll |
| 7 | +import org.junit.runner.RunWith |
| 8 | + |
| 9 | +@RunWith(KTestJUnitRunner::class) |
| 10 | +class EitherTTest : UnitSpec() { |
| 11 | + init { |
| 12 | + "map should modify value" { |
| 13 | + forAll { a: String -> |
| 14 | + val right = EitherT(Id(Either.Right(a))) |
| 15 | + val mapped = right.map({ "$it power" }) |
| 16 | + val expected = EitherT(Id(Either.Right("$a power"))) |
| 17 | + |
| 18 | + mapped == expected |
| 19 | + } |
| 20 | + } |
| 21 | + |
| 22 | + "flatMap should modify entity" { |
| 23 | + forAll { a: String -> |
| 24 | + val right = EitherT(NonEmptyList.of(Either.Right(a))) |
| 25 | + val mapped = right.flatMap { EitherT(NonEmptyList.of(Either.Right(3))) } |
| 26 | + val expected = EitherT.pure<NonEmptyList.F, Int, Int>(3) |
| 27 | + |
| 28 | + mapped == expected |
| 29 | + } |
| 30 | + |
| 31 | + forAll { ignored: String -> |
| 32 | + val right: EitherT<NonEmptyList.F, Int, String> = EitherT(NonEmptyList.of(Either.Right(ignored))) |
| 33 | + val mapped = right.flatMap { EitherT(NonEmptyList.of(Either.Left(3))) } |
| 34 | + val expected = EitherT.left<NonEmptyList.F, Int, Int>(3) |
| 35 | + |
| 36 | + mapped == expected |
| 37 | + } |
| 38 | + |
| 39 | + forAll { _: String -> |
| 40 | + val right = EitherT.left<NonEmptyList.F, Int, Int>(3) |
| 41 | + val mapped = right.flatMap { EitherT(NonEmptyList.of<Either<Int, Int>>(Either.Right(2))) } |
| 42 | + val expected = EitherT(NonEmptyList.of(Either.Left(3))) |
| 43 | + |
| 44 | + mapped == expected |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + "cata should modify the return" { |
| 49 | + forAll { num: Int -> |
| 50 | + val right = EitherT.pure<NonEmptyList.F, Int, Int>(num) |
| 51 | + val expected = NonEmptyList.of(true) |
| 52 | + val result = right.cata({ false }, { true }) |
| 53 | + |
| 54 | + expected == result |
| 55 | + } |
| 56 | + |
| 57 | + forAll { num: Int -> |
| 58 | + val right = EitherT.left<NonEmptyList.F, Int, Int>(num) |
| 59 | + val expected = NonEmptyList.of(true) |
| 60 | + val result = right.cata({ true }, { false }) |
| 61 | + |
| 62 | + expected == result |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + "semiFlatMap should map the right side of the inner either" { |
| 67 | + forAll { num: Int -> |
| 68 | + val right: EitherT<NonEmptyList.F, Int, Int> = EitherT(NonEmptyList.of(Either.Right(num))) |
| 69 | + val calculated = right.semiflatMap { NonEmptyList.of(it > 0) } |
| 70 | + val expected = EitherT(NonEmptyList.of(Either.Right(num > 0))) |
| 71 | + |
| 72 | + calculated == expected |
| 73 | + } |
| 74 | + |
| 75 | + forAll { num: Int -> |
| 76 | + val left: EitherT<NonEmptyList.F, Int, Int> = EitherT(NonEmptyList.of(Either.Left(num))) |
| 77 | + val calculated = left.semiflatMap { NonEmptyList.of(it > 0) } |
| 78 | + val expected = EitherT(NonEmptyList.of(Either.Left(num))) |
| 79 | + |
| 80 | + calculated == expected |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + |
| 85 | + "subFlatMap should map the right side of the Either wrapped by EitherT" { |
| 86 | + forAll { num: Int -> |
| 87 | + val right: EitherT<NonEmptyList.F, Int, Int> = EitherT(NonEmptyList.of(Either.Right(num))) |
| 88 | + val calculated = right.subflatMap { Either.Right(it > 0) } |
| 89 | + val expected = EitherT(NonEmptyList.of(Either.Right(num > 0))) |
| 90 | + |
| 91 | + calculated == expected |
| 92 | + } |
| 93 | + |
| 94 | + forAll { num: Int -> |
| 95 | + val left: EitherT<NonEmptyList.F, Int, Int> = EitherT(NonEmptyList.of(Either.Right(num))) |
| 96 | + val calculated = left.subflatMap { Either.Left(num) } |
| 97 | + val expected = EitherT(NonEmptyList.of(Either.Left(num))) |
| 98 | + |
| 99 | + calculated == expected |
| 100 | + } |
| 101 | + |
| 102 | + forAll { num: Int -> |
| 103 | + val left: EitherT<NonEmptyList.F, Int, Int> = EitherT(NonEmptyList.of(Either.Left(num))) |
| 104 | + val calculated = left.subflatMap { Either.Right(num > 0) } |
| 105 | + val expected = EitherT(NonEmptyList.of(Either.Left(num))) |
| 106 | + |
| 107 | + calculated == expected |
| 108 | + } |
| 109 | + |
| 110 | + forAll { num: Int -> |
| 111 | + val left: EitherT<NonEmptyList.F, Int, Int> = EitherT(NonEmptyList.of(Either.Left(num))) |
| 112 | + val calculated = left.subflatMap { Either.Left(num + 1) } |
| 113 | + val expected = EitherT(NonEmptyList.of(Either.Left(num))) |
| 114 | + |
| 115 | + calculated == expected |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + "exists evaluates a predicate on the right side and lifts it to the wrapped monad" { |
| 120 | + forAll { num: Int -> |
| 121 | + val right: EitherT<NonEmptyList.F, Int, Int> = EitherT(NonEmptyList.of(Either.Right(num))) |
| 122 | + val calculated = right.exists { it > 0 }.ev() |
| 123 | + val expected = NonEmptyList.of(num > 0) |
| 124 | + |
| 125 | + calculated == expected |
| 126 | + } |
| 127 | + |
| 128 | + forAll { num: Int -> |
| 129 | + val left: EitherT<NonEmptyList.F, Int, Int> = EitherT(NonEmptyList.of(Either.Left(num))) |
| 130 | + val calculated = left.exists { true }.ev() |
| 131 | + val expected = NonEmptyList.of(false) |
| 132 | + |
| 133 | + calculated == expected |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + "to OptionT should transform to a correct OptionT" { |
| 138 | + forAll { a: String -> |
| 139 | + val right: EitherT<NonEmptyList.F, String, String> = EitherT(NonEmptyList.of(Either.Right(a))) |
| 140 | + val expected = OptionT.pure<NonEmptyList.F, String>(a) |
| 141 | + val calculated = right.toOptionT() |
| 142 | + |
| 143 | + expected == calculated |
| 144 | + } |
| 145 | + |
| 146 | + forAll { a: String -> |
| 147 | + val left: EitherT<NonEmptyList.F, String, String> = EitherT(NonEmptyList.of(Either.Left(a))) |
| 148 | + val expected = OptionT.none<NonEmptyList.F>() |
| 149 | + val calculated = left.toOptionT() |
| 150 | + |
| 151 | + expected == calculated |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + "from option should build a correct EitherT" { |
| 156 | + forAll { a: String -> |
| 157 | + EitherT.fromEither<NonEmptyList.F, Int, String>(Either.Right(a)) == EitherT.pure<NonEmptyList.F, Int, String>(a) |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + "EitherTMonad#flatMap should be consistent with EitherT#flatMap" { |
| 162 | + forAll { a: Int -> |
| 163 | + val x = { b: Int -> EitherT.pure<Id.F, Int, Int>(b * a) } |
| 164 | + val option = EitherT.pure<Id.F, Int, Int>(a) |
| 165 | + option.flatMap(x) == EitherTMonad<Id.F, Int>(Id).flatMap(option, x) |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + "EitherTMonad#tailRecM should execute and terminate without blowing up the stack" { |
| 170 | + forAll { a: Int -> |
| 171 | + val value: EitherT<Id.F, Int, Int> = EitherTMonad<Id.F, Int>(Id).tailRecM(a) { b -> |
| 172 | + EitherT.pure<Id.F, Int, Either<Int, Int>>(Either.Right(b * a)) |
| 173 | + } |
| 174 | + val expected = EitherT.pure<Id.F, Int, Int>(a * a) |
| 175 | + |
| 176 | + expected == value |
| 177 | + } |
| 178 | + |
| 179 | + forAll(Gen.oneOf(listOf(10000))) { limit: Int -> |
| 180 | + val value: EitherT<Id.F, Int, Int> = EitherTMonad<Id.F, Int>(Id).tailRecM(0) { current -> |
| 181 | + if (current == limit) |
| 182 | + EitherT.left<Id.F, Int, Either<Int, Int>>(current) |
| 183 | + else |
| 184 | + EitherT.pure<Id.F, Int, Either<Int, Int>>(Either.Left(current + 1)) |
| 185 | + } |
| 186 | + val expected = EitherT.left<Id.F, Int, Int>(limit) |
| 187 | + |
| 188 | + expected == value |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + "EitherTMonad#binding should for comprehend over option" { |
| 193 | + val M = EitherTMonad<NonEmptyList.F, Int>(NonEmptyList) |
| 194 | + val result = M.binding { |
| 195 | + val x = !M.pure(1) |
| 196 | + val y = M.pure(1).bind() |
| 197 | + val z = bind { M.pure(1) } |
| 198 | + yields(x + y + z) |
| 199 | + } |
| 200 | + result shouldBe M.pure(3) |
| 201 | + } |
| 202 | + |
| 203 | + "Cartesian builder should build products over option" { |
| 204 | + EitherTMonad<Id.F, Int>(Id).map(EitherT.pure(1), EitherT.pure("a"), EitherT.pure(true), { (a, b, c) -> |
| 205 | + "$a $b $c" |
| 206 | + }) shouldBe EitherT.pure<Id.F, Int, String>("1 a true") |
| 207 | + } |
| 208 | + |
| 209 | + "Cartesian builder works inside for comprehensions" { |
| 210 | + val M = EitherTMonad<NonEmptyList.F, Int>(NonEmptyList) |
| 211 | + val result = M.binding { |
| 212 | + val (x, y, z) = !M.tupled(M.pure(1), M.pure(1), M.pure(1)) |
| 213 | + val a = M.pure(1).bind() |
| 214 | + val b = bind { M.pure(1) } |
| 215 | + yields(x + y + z + a + b) |
| 216 | + } |
| 217 | + result shouldBe M.pure(5) |
| 218 | + } |
| 219 | + } |
| 220 | +} |
0 commit comments