|
| 1 | +package com.javiersc.kotlin.stdlib |
| 2 | + |
| 3 | +import kotlin.test.Test |
| 4 | +import kotlin.test.assertEquals |
| 5 | +import kotlin.test.assertFalse |
| 6 | +import kotlin.test.assertNull |
| 7 | +import kotlin.test.assertTrue |
| 8 | + |
| 9 | +class EitherTest { |
| 10 | + |
| 11 | + @Test |
| 12 | + fun given__a_left__when__isLeft__then__true() { |
| 13 | + assertTrue { LeftFixture.isLeft() } |
| 14 | + } |
| 15 | + |
| 16 | + @Test |
| 17 | + fun given__a_left__when__isRight__then__false() { |
| 18 | + assertFalse { LeftFixture.isRight() } |
| 19 | + } |
| 20 | + |
| 21 | + @Test |
| 22 | + fun given__a_right__when__isLeft__then__false() { |
| 23 | + assertFalse { RightFixture.isLeft() } |
| 24 | + } |
| 25 | + |
| 26 | + @Test |
| 27 | + fun given__a_right__when__isRight__then__true() { |
| 28 | + assertTrue { RightFixture.isRight() } |
| 29 | + } |
| 30 | + |
| 31 | + @Test |
| 32 | + fun given__eithers__when__isLeftLambda__then__compute() { |
| 33 | + assertTrue { LeftFixture.isLeft { it == "Left" } } |
| 34 | + assertFalse { LeftFixture.isLeft { it == "Right" } } |
| 35 | + assertFalse { RightFixture.isLeft { it == "Left" } } |
| 36 | + assertFalse { RightFixture.isLeft { it == "Right" } } |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + fun given__eithers__when__isRightLambda__then__compute() { |
| 41 | + assertTrue { RightFixture.isRight { it == 42 } } |
| 42 | + assertFalse { RightFixture.isRight { it == 0 } } |
| 43 | + assertFalse { LeftFixture.isRight { it == 42 } } |
| 44 | + assertFalse { LeftFixture.isRight { it == 0 } } |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + fun given__eithers__when__fold__then__compute() { |
| 49 | + assertTrue { LeftFixture.fold(ifLeft = { it == "Left" }, ifRight = { false }) } |
| 50 | + assertTrue { RightFixture.fold(ifLeft = { false }, ifRight = { it == 42 }) } |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + fun given__eithers__when__swaps__then__compute() { |
| 55 | + assertTrue { LeftFixture.swap().isRight() } |
| 56 | + assertTrue { RightFixture.swap().isLeft() } |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + fun given__a_right__when__map__then__newEither() { |
| 61 | + val result = RightFixture.map { it + 1 } |
| 62 | + assertEquals(Either.Right(43), result) |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + fun given__a_left__when__map__then__sameEither() { |
| 67 | + val result = LeftFixture.map { it + 1 } |
| 68 | + assertEquals(LeftFixture, result) |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + fun given__a_left__when__mapLeft__then__newEither() { |
| 73 | + val result = LeftFixture.mapLeft { it.length } |
| 74 | + assertEquals(Either.Left(4), result) |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + fun given__a_right__when__mapLeft__then__sameEither() { |
| 79 | + val result = RightFixture.mapLeft { it } |
| 80 | + assertEquals(RightFixture, result) |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + fun given__a_right__when__onRight__then__performAction() { |
| 85 | + var actionPerformed = false |
| 86 | + RightFixture.onRight { actionPerformed = true } |
| 87 | + assertTrue(actionPerformed) |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + fun given__a_left__when__onRight__then__noAction() { |
| 92 | + var actionPerformed = false |
| 93 | + LeftFixture.onRight { actionPerformed = true } |
| 94 | + assertFalse(actionPerformed) |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + fun given__a_left__when__onLeft__then__performAction() { |
| 99 | + var actionPerformed = false |
| 100 | + LeftFixture.onLeft { actionPerformed = true } |
| 101 | + assertTrue(actionPerformed) |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + fun given__a_right__when__onLeft__then__noAction() { |
| 106 | + var actionPerformed = false |
| 107 | + RightFixture.onLeft { actionPerformed = true } |
| 108 | + assertFalse(actionPerformed) |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + fun given__a_left__when__getOrNull__then__null() { |
| 113 | + assertNull(LeftFixture.getOrNull()) |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + fun given__a_right__when__getOrNull__then__value() { |
| 118 | + assertEquals(42, RightFixture.getOrNull()) |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + fun given__a_left__when__leftOrNull__then__value() { |
| 123 | + assertEquals("Left", LeftFixture.leftOrNull()) |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + fun given__a_right__when__leftOrNull__then__null() { |
| 128 | + assertNull(RightFixture.leftOrNull()) |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + fun given__a_right__when__flatMap__then__newEither() { |
| 133 | + val result = RightFixture.flatMap { Either.Right(it + 1) } |
| 134 | + assertEquals(Either.Right(43), result) |
| 135 | + } |
| 136 | + |
| 137 | + @Test |
| 138 | + fun given__a_left__when__flatMap__then__sameEither() { |
| 139 | + val result = LeftFixture.flatMap { Either.Right(it + 1) } |
| 140 | + assertEquals(LeftFixture, result) |
| 141 | + } |
| 142 | + |
| 143 | + @Test |
| 144 | + fun given__a_left__when__handleErrorWith__then__newEither() { |
| 145 | + val result = LeftFixture.handleErrorWith { Either.Left(it.length) } |
| 146 | + assertEquals(Either.Left(4), result) |
| 147 | + } |
| 148 | + |
| 149 | + @Test |
| 150 | + fun given__a_right__when__handleErrorWith__then__sameEither() { |
| 151 | + val result = RightFixture.handleErrorWith { Either.Left(it) } |
| 152 | + assertEquals(RightFixture, result) |
| 153 | + } |
| 154 | + |
| 155 | + @Test |
| 156 | + fun given__a_nestedEither__when__flatten__then__flattenedEither() { |
| 157 | + val nestedEither: Either<String, Either<String, Int>> = Either.Right(RightFixture) |
| 158 | + val result = nestedEither.flatten() |
| 159 | + assertEquals(RightFixture, result) |
| 160 | + } |
| 161 | + |
| 162 | + @Test |
| 163 | + fun given__a_left__when__getOrElse__then__defaultValue() { |
| 164 | + val result = LeftFixture getOrElse { it.length } |
| 165 | + assertEquals(4, result) |
| 166 | + } |
| 167 | + |
| 168 | + @Test |
| 169 | + fun given__a_right__when__getOrElse__then__value() { |
| 170 | + val result = RightFixture getOrElse { it.length } |
| 171 | + assertEquals(42, result) |
| 172 | + } |
| 173 | + |
| 174 | + @Test |
| 175 | + fun given__either__when__merge__then__mergedValue() { |
| 176 | + val either: Either<Int, Int> = Either.Right(42) |
| 177 | + assertEquals(42, either.merge()) |
| 178 | + } |
| 179 | + |
| 180 | + @Test |
| 181 | + fun given__value__when__left__then__leftEither() { |
| 182 | + val value = "Error" |
| 183 | + val result = value.left() |
| 184 | + assertEquals(Either.Left(value), result) |
| 185 | + } |
| 186 | + |
| 187 | + @Test |
| 188 | + fun given__value__when__right__then__rightEither() { |
| 189 | + val value = 42 |
| 190 | + val result = value.right() |
| 191 | + assertEquals(Either.Right(value), result) |
| 192 | + } |
| 193 | + |
| 194 | + @Test |
| 195 | + fun given__either__when__compareTo__then__comparisonResult() { |
| 196 | + val either1: Either<Int, Int> = Either.Right(42) |
| 197 | + val either2: Either<Int, Int> = Either.Right(43) |
| 198 | + assertTrue { either1 < either2 } |
| 199 | + } |
| 200 | + |
| 201 | + @Test |
| 202 | + fun given__either__when__combine__then__combinedEither() { |
| 203 | + val either1: Either<String, Int> = Either.Right(42) |
| 204 | + val either2: Either<String, Int> = Either.Right(43) |
| 205 | + val result = either1.combine(either2, { a, b -> a + b }, { a, b -> a + b }) |
| 206 | + assertEquals(Either.Right(85), result) |
| 207 | + } |
| 208 | + |
| 209 | + @Test |
| 210 | + fun given__either__when__toEitherNel__then__eitherNel() { |
| 211 | + val either: Either<String, Int> = Either.Left("Error") |
| 212 | + val result = either.toEitherNel() |
| 213 | + assertEquals(Either.Left(listOf("Error")), result) |
| 214 | + } |
| 215 | + |
| 216 | + @Test |
| 217 | + fun given__value__when__leftNel__then__eitherNel() { |
| 218 | + val value = "Error" |
| 219 | + val result = value.leftNel() |
| 220 | + assertEquals(Either.Left(listOf(value)), result) |
| 221 | + } |
| 222 | +} |
| 223 | + |
| 224 | +private val LeftFixture: Either<String, Int> = Either.Left("Left") |
| 225 | +private val RightFixture: Either<String, Int> = Either.Right(42) |
0 commit comments