|
29 | 29 | import io.vavr.*;
|
30 | 30 | import io.vavr.collection.Seq;
|
31 | 31 | import org.junit.jupiter.api.Assertions;
|
| 32 | +import org.junit.jupiter.api.DisplayName; |
| 33 | +import org.junit.jupiter.api.Nested; |
32 | 34 | import org.junit.jupiter.api.Test;
|
33 | 35 |
|
34 | 36 | import java.util.*;
|
@@ -509,22 +511,45 @@ public void shouldMakeLeftOnNoneToValidationSupplier() {
|
509 | 511 | assertThat(API.None().toValidation(() -> "bad")).isEqualTo(API.Invalid("bad"));
|
510 | 512 | }
|
511 | 513 |
|
512 |
| - // -- peek |
| 514 | + @Nested |
| 515 | + class Peek { |
| 516 | + @Test |
| 517 | + public void shouldConsumePresentValueOnPeekWhenValueIsDefined() { |
| 518 | + final int[] actual = new int[] { -1 }; |
| 519 | + final Option<Integer> testee = Option.of(1).peek(i -> actual[0] = i); |
| 520 | + assertThat(actual[0]).isEqualTo(1); |
| 521 | + assertThat(testee).isEqualTo(Option.of(1)); |
| 522 | + } |
513 | 523 |
|
514 |
| - @Test |
515 |
| - public void shouldConsumePresentValueOnPeekWhenValueIsDefined() { |
516 |
| - final int[] actual = new int[] { -1 }; |
517 |
| - final Option<Integer> testee = Option.of(1).peek(i -> actual[0] = i); |
518 |
| - assertThat(actual[0]).isEqualTo(1); |
519 |
| - assertThat(testee).isEqualTo(Option.of(1)); |
| 524 | + @Test |
| 525 | + public void shouldNotConsumeAnythingOnPeekWhenValueIsNotDefined() { |
| 526 | + final int[] actual = new int[] { -1 }; |
| 527 | + final Option<Integer> testee = Option.<Integer> none().peek(i -> actual[0] = i); |
| 528 | + assertThat(actual[0]).isEqualTo(-1); |
| 529 | + assertThat(testee).isEqualTo(Option.none()); |
| 530 | + } |
520 | 531 | }
|
521 | 532 |
|
522 |
| - @Test |
523 |
| - public void shouldNotConsumeAnythingOnPeekWhenValueIsNotDefined() { |
524 |
| - final int[] actual = new int[] { -1 }; |
525 |
| - final Option<Integer> testee = Option.<Integer> none().peek(i -> actual[0] = i); |
526 |
| - assertThat(actual[0]).isEqualTo(-1); |
527 |
| - assertThat(testee).isEqualTo(Option.none()); |
| 533 | + @Nested |
| 534 | + @DisplayName("peek(Runnable, Consumer)") |
| 535 | + class PeekRunnableConsumer { |
| 536 | + @Test |
| 537 | + void shouldConsumePresentValueOnPeekWhenValueIsDefined() { |
| 538 | + final int[] actual = new int[] { -1 }; |
| 539 | + final Option<Integer> testee = Option.of(1) |
| 540 | + .peek(() -> actual[0] = -2, i -> actual[0] = i); |
| 541 | + assertThat(actual[0]).isEqualTo(1); |
| 542 | + assertThat(testee).isEqualTo(Option.of(1)); |
| 543 | + } |
| 544 | + |
| 545 | + @Test |
| 546 | + void shouldRunRunnableWhenValueIsNotDefined() { |
| 547 | + final int[] actual = new int[] { -1 }; |
| 548 | + final Option<Integer> testee = Option.<Integer> none() |
| 549 | + .peek(() -> actual[0] = -2, i -> actual[0] = i); |
| 550 | + assertThat(actual[0]).isEqualTo(-2); |
| 551 | + assertThat(testee).isEqualTo(Option.none()); |
| 552 | + } |
528 | 553 | }
|
529 | 554 |
|
530 | 555 | // -- transform
|
|
0 commit comments