|
32 | 32 | import static io.vavr.API.Right;
|
33 | 33 | import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
34 | 34 | import static org.junit.jupiter.api.Assertions.assertThrows;
|
| 35 | +import static org.junit.jupiter.api.Assertions.fail; |
35 | 36 |
|
36 | 37 | @SuppressWarnings("deprecation")
|
37 | 38 | public class EitherTest extends AbstractValueTest {
|
@@ -333,6 +334,89 @@ public void shouldNarrowLeftEither() {
|
333 | 334 | }
|
334 | 335 | }
|
335 | 336 |
|
| 337 | + @Nested |
| 338 | + public class CondTests { |
| 339 | + |
| 340 | + @Test |
| 341 | + public void shouldReturnRightIfTestTrue() { |
| 342 | + Either<String, Integer> either = Either.cond(true, () -> 21, () -> "vavr"); |
| 343 | + assertThat(either).isEqualTo(Either.right(21)); |
| 344 | + } |
| 345 | + |
| 346 | + @Test |
| 347 | + public void shouldReturnLeftIfTestFalse() { |
| 348 | + Either<String, Integer> either = Either.cond(false, () -> 21, () -> "vavr"); |
| 349 | + assertThat(either).isEqualTo(Either.left("vavr")); |
| 350 | + } |
| 351 | + |
| 352 | + @Test |
| 353 | + public void shouldNotEvaluateRightSupplierOnFalse() { |
| 354 | + Either<String, Integer> either = Either.cond(false, () -> { |
| 355 | + fail("Should not be called"); |
| 356 | + return 21; |
| 357 | + }, () -> "vavr"); |
| 358 | + assertThat(either).isEqualTo(Either.left("vavr")); |
| 359 | + } |
| 360 | + |
| 361 | + @Test |
| 362 | + public void shouldNotEvaluateLeftSupplierOnTrue() { |
| 363 | + Either<String, Integer> either = Either.cond(true, () -> 21, () -> { |
| 364 | + fail("Should not be called"); |
| 365 | + return "vavr"; |
| 366 | + }); |
| 367 | + assertThat(either).isEqualTo(Either.right(21)); |
| 368 | + } |
| 369 | + private class Animal { |
| 370 | + String name; |
| 371 | + Animal(String name) { this.name = name; } |
| 372 | + |
| 373 | + @Override |
| 374 | + public boolean equals(Object o) { |
| 375 | + if (this == o) return true; |
| 376 | + if (!(o instanceof Animal)) return false; |
| 377 | + Animal other = (Animal) o; |
| 378 | + return name.equals(other.name); |
| 379 | + } |
| 380 | + |
| 381 | + @Override |
| 382 | + public int hashCode() { |
| 383 | + return name.hashCode(); |
| 384 | + } |
| 385 | + } |
| 386 | + |
| 387 | + private class Dog extends Animal { |
| 388 | + Dog(String name) { super(name); } |
| 389 | + } |
| 390 | + |
| 391 | + private class Cat extends Animal { |
| 392 | + Cat(String name) { super(name); } |
| 393 | + } |
| 394 | + |
| 395 | + @Test |
| 396 | + public void shouldBeFineWithCovariantLeft() { |
| 397 | + Either<Animal, Integer> either = Either.cond(false, () -> 21, () -> new Cat("vavr")); |
| 398 | + assertThat(either).isEqualTo(Either.left(new Cat("vavr"))); |
| 399 | + } |
| 400 | + |
| 401 | + @Test |
| 402 | + public void shouldBeFineWithCovariantRight() { |
| 403 | + Either<String, Animal> either = Either.cond(true, () -> new Dog("vavr"), () -> "vavr"); |
| 404 | + assertThat(either).isEqualTo(Either.right(new Dog("vavr"))); |
| 405 | + } |
| 406 | + |
| 407 | + @Test |
| 408 | + public void shouldMakeTheSameDecisionNoMatterHowItsCalled() { |
| 409 | + Either<String, Integer> e1 = Either.cond(true, () -> 21, () -> "vavr"); |
| 410 | + Either<String, Integer> e2 = Either.cond(true, 21, "vavr"); |
| 411 | + |
| 412 | + Either<String, Integer> e3 = Either.cond(false, () -> 21, () -> "vavr"); |
| 413 | + Either<String, Integer> e4 = Either.cond(false, 21, "vavr"); |
| 414 | + |
| 415 | + assertThat(List.of(e1, e2)).allMatch(e -> e.equals(Either.right(21))); |
| 416 | + assertThat(List.of(e3, e4)).allMatch(e -> e.equals(Either.left("vavr"))); |
| 417 | + } |
| 418 | + } |
| 419 | + |
336 | 420 | @Nested
|
337 | 421 | public class OrElseTests {
|
338 | 422 |
|
|
0 commit comments