From 926025450c8fd4f4c882e7ffc3b0e961b0ea321a Mon Sep 17 00:00:00 2001 From: Boris van Katwijk Date: Sat, 28 Dec 2024 16:46:05 +0100 Subject: [PATCH 01/61] Add toolchain --- settings.gradle | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/settings.gradle b/settings.gradle index 400fe5c399..dc6a6a508b 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1 +1,5 @@ +plugins { + id 'org.gradle.toolchains.foojay-resolver-convention' version '0.9.0' +} + rootProject.name = 'vavr' From 967a6762bccbbe942005cb37be057e631e26b285 Mon Sep 17 00:00:00 2001 From: Boris van Katwijk Date: Sat, 28 Dec 2024 16:55:40 +0100 Subject: [PATCH 02/61] Add mapTry to Option --- src/main/java/io/vavr/control/Option.java | 5 ++++ src/test/java/io/vavr/control/OptionTest.java | 27 +++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/src/main/java/io/vavr/control/Option.java b/src/main/java/io/vavr/control/Option.java index 42ab9914d4..a3ce7c34c6 100644 --- a/src/main/java/io/vavr/control/Option.java +++ b/src/main/java/io/vavr/control/Option.java @@ -26,6 +26,7 @@ */ package io.vavr.control; +import io.vavr.CheckedFunction1; import io.vavr.PartialFunction; import io.vavr.collection.Iterator; import io.vavr.collection.Seq; @@ -662,6 +663,10 @@ public final Option map(Function mapper) { return isEmpty() ? none() : some(mapper.apply(get())); } + public final Try mapTry(CheckedFunction1 mapper) { + return toTry().mapTry(mapper); + } + /** * Folds either the {@code None} or the {@code Some} side of the Option value. * diff --git a/src/test/java/io/vavr/control/OptionTest.java b/src/test/java/io/vavr/control/OptionTest.java index 6d48dfb1b1..46834a3ebc 100644 --- a/src/test/java/io/vavr/control/OptionTest.java +++ b/src/test/java/io/vavr/control/OptionTest.java @@ -28,9 +28,12 @@ import io.vavr.*; import io.vavr.collection.Seq; +import io.vavr.concurrent.FutureTest; import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; +import java.io.IOException; import java.util.*; import java.util.concurrent.CompletableFuture; import java.util.concurrent.atomic.AtomicBoolean; @@ -400,6 +403,30 @@ public void shouldMapSome() { public void shouldMapNone() { assertThat(Option. none().map(String::valueOf)).isEqualTo(Option.none()); } + + @Nested + class MapTry { + @Test + public void shouldMapTrySome() { + assertThat(Option.of(1).mapTry(String::valueOf)).isEqualTo(Try.success("1")); + } + + @Test + public void shouldMapTryNone() { + Try result = Option.none().mapTry(String::valueOf); + assertThat(result.isFailure()).isTrue(); + assertThat(result.getCause().getClass()).isEqualTo(NoSuchElementException.class); + assertThat(result.getCause().getMessage()).isEqualTo("No value present"); + } + + @Test + public void shouldMapTryCheckedException() { + Try result = Option.of("a").mapTry(Integer::new); + assertThat(result.isFailure()).isTrue(); + assertThat(result.getCause().getClass()).isEqualTo(NumberFormatException.class); + assertThat(result.getCause().getMessage()).isEqualTo("For input string: \"a\""); + } + } // -- flatMap From 088a2b33107a964fb1259432cc8ef7bad355472e Mon Sep 17 00:00:00 2001 From: Boris van Katwijk Date: Sat, 28 Dec 2024 16:56:43 +0100 Subject: [PATCH 03/61] Update OptionTest.java --- src/test/java/io/vavr/control/OptionTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/io/vavr/control/OptionTest.java b/src/test/java/io/vavr/control/OptionTest.java index 46834a3ebc..bd300a0000 100644 --- a/src/test/java/io/vavr/control/OptionTest.java +++ b/src/test/java/io/vavr/control/OptionTest.java @@ -403,7 +403,7 @@ public void shouldMapSome() { public void shouldMapNone() { assertThat(Option. none().map(String::valueOf)).isEqualTo(Option.none()); } - + @Nested class MapTry { @Test From 390b8db1cab13e6a9f13e2665880c4a34e2cdb87 Mon Sep 17 00:00:00 2001 From: Boris van Katwijk Date: Sat, 28 Dec 2024 17:00:47 +0100 Subject: [PATCH 04/61] Update Option.java --- src/main/java/io/vavr/control/Option.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/main/java/io/vavr/control/Option.java b/src/main/java/io/vavr/control/Option.java index a3ce7c34c6..d683bbfe3f 100644 --- a/src/main/java/io/vavr/control/Option.java +++ b/src/main/java/io/vavr/control/Option.java @@ -663,6 +663,16 @@ public final Option map(Function mapper) { return isEmpty() ? none() : some(mapper.apply(get())); } + + /** + * Converts this to a {@link Try}, then runs the given checked function if this is a {@link Try.Success}, + * passing the result of the current expression to it. + * + * @param The new component type + * @param mapper A checked function + * @return a {@code Try} + * @throws NullPointerException if {@code mapper} is null + */ public final Try mapTry(CheckedFunction1 mapper) { return toTry().mapTry(mapper); } From b416dd95f434653239b287ada5560ebb9af15a55 Mon Sep 17 00:00:00 2001 From: Boris van Katwijk Date: Sun, 29 Dec 2024 17:36:55 +0100 Subject: [PATCH 05/61] Revert "Add toolchain" This reverts commit 89c20aa6c18032b4499c04965944b5df5a9ab9bc. --- settings.gradle | 4 ---- 1 file changed, 4 deletions(-) diff --git a/settings.gradle b/settings.gradle index dc6a6a508b..400fe5c399 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,5 +1 @@ -plugins { - id 'org.gradle.toolchains.foojay-resolver-convention' version '0.9.0' -} - rootProject.name = 'vavr' From 8e235c73b038de83cb83fa63dc5c21f5c4349b21 Mon Sep 17 00:00:00 2001 From: Boris van Katwijk Date: Mon, 30 Dec 2024 18:47:44 +0100 Subject: [PATCH 06/61] Create nested.sh --- nested.sh | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100755 nested.sh diff --git a/nested.sh b/nested.sh new file mode 100755 index 0000000000..c995b0271d --- /dev/null +++ b/nested.sh @@ -0,0 +1,7 @@ +#!/bin/zsh +set -euo pipefail + +find . -name "*.java" -type f -exec sed -i \ + -e 's|// -- \(.*\)|}\n\n@Nested\nclass \1 {|' {} + + +exit 0; From 690e6b436f55ac77fcdd389a3739248374accf18 Mon Sep 17 00:00:00 2001 From: Boris van Katwijk Date: Mon, 30 Dec 2024 18:50:03 +0100 Subject: [PATCH 07/61] Update nested.sh --- nested.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nested.sh b/nested.sh index c995b0271d..4350d57dc6 100755 --- a/nested.sh +++ b/nested.sh @@ -1,7 +1,7 @@ #!/bin/zsh set -euo pipefail -find . -name "*.java" -type f -exec sed -i \ - -e 's|// -- \(.*\)|}\n\n@Nested\nclass \1 {|' {} + +find . -name "*.java" -type f -exec sed -i '' \ + 's|// -- \(.*\)|}\n\n @Nested\n class \1 {|' {} + exit 0; From 5162880d831b29f9dbbcdbe334dedbddc3e883b9 Mon Sep 17 00:00:00 2001 From: Boris van Katwijk Date: Mon, 30 Dec 2024 18:50:14 +0100 Subject: [PATCH 08/61] Update nested.sh --- nested.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nested.sh b/nested.sh index 4350d57dc6..6fcc762d8e 100755 --- a/nested.sh +++ b/nested.sh @@ -1,7 +1,7 @@ #!/bin/zsh set -euo pipefail -find . -name "*.java" -type f -exec sed -i '' \ +find . -name "*Test.java" -type f -exec sed -i '' \ 's|// -- \(.*\)|}\n\n @Nested\n class \1 {|' {} + exit 0; From 843ffb1b7868d8927f7114b63a54117d05516448 Mon Sep 17 00:00:00 2001 From: Boris van Katwijk Date: Mon, 30 Dec 2024 18:50:42 +0100 Subject: [PATCH 09/61] Update nested.sh --- nested.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nested.sh b/nested.sh index 6fcc762d8e..5dcf3b554c 100755 --- a/nested.sh +++ b/nested.sh @@ -2,6 +2,6 @@ set -euo pipefail find . -name "*Test.java" -type f -exec sed -i '' \ - 's|// -- \(.*\)|}\n\n @Nested\n class \1 {|' {} + + 's|// -- \(.*\)|}\n\n @Nested\n class \1 {|' {} + exit 0; From 9c86b23f1a1352a13f71e401c8d6cdbff90bd965 Mon Sep 17 00:00:00 2001 From: Boris van Katwijk Date: Mon, 30 Dec 2024 18:53:03 +0100 Subject: [PATCH 10/61] Update nested.sh --- nested.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nested.sh b/nested.sh index 5dcf3b554c..dce116728b 100755 --- a/nested.sh +++ b/nested.sh @@ -2,6 +2,6 @@ set -euo pipefail find . -name "*Test.java" -type f -exec sed -i '' \ - 's|// -- \(.*\)|}\n\n @Nested\n class \1 {|' {} + + 's|// -- \([a-z][a-zA-Z0-9_]*\)|}\n\n @Nested\n class \1 {|' {} + exit 0; From 233e8e33e825adb0383f857930ab8cc5398e4c2c Mon Sep 17 00:00:00 2001 From: Boris van Katwijk Date: Mon, 30 Dec 2024 20:13:04 +0100 Subject: [PATCH 11/61] Update nested.sh --- nested.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nested.sh b/nested.sh index dce116728b..805ba9b6c2 100755 --- a/nested.sh +++ b/nested.sh @@ -2,6 +2,6 @@ set -euo pipefail find . -name "*Test.java" -type f -exec sed -i '' \ - 's|// -- \([a-z][a-zA-Z0-9_]*\)|}\n\n @Nested\n class \1 {|' {} + + 's|// -- \([a-z][a-zA-Z0-9_]*\).*|}\n\n @Nested\n class \1 {|' {} + exit 0; From c77e0faa323bc1c69ca10ea3abe8a6fef4eb8416 Mon Sep 17 00:00:00 2001 From: Boris van Katwijk Date: Mon, 30 Dec 2024 20:14:45 +0100 Subject: [PATCH 12/61] Update nested.sh --- nested.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nested.sh b/nested.sh index 805ba9b6c2..340d4b7f77 100755 --- a/nested.sh +++ b/nested.sh @@ -2,6 +2,6 @@ set -euo pipefail find . -name "*Test.java" -type f -exec sed -i '' \ - 's|// -- \([a-z][a-zA-Z0-9_]*\).*|}\n\n @Nested\n class \1 {|' {} + + 's|// -- \([a-z][a-zA-Z0-9_]*\).*|}\n\n @Nested\n class \u\1 {|' {} + exit 0; From b4d382950326fa2c28921aba6ffa3f2d7ff746e6 Mon Sep 17 00:00:00 2001 From: Boris van Katwijk Date: Mon, 30 Dec 2024 20:16:23 +0100 Subject: [PATCH 13/61] Update nested.sh --- nested.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nested.sh b/nested.sh index 340d4b7f77..f1fcfbfda2 100755 --- a/nested.sh +++ b/nested.sh @@ -2,6 +2,6 @@ set -euo pipefail find . -name "*Test.java" -type f -exec sed -i '' \ - 's|// -- \([a-z][a-zA-Z0-9_]*\).*|}\n\n @Nested\n class \u\1 {|' {} + + 's|// -- \([a-z][a-zA-Z0-9_]*\).*|}\n\n @Nested\n class \U\1 {|' {} + exit 0; From 4c4b05fef9ca01ac0d354ec4430887a89ace1df4 Mon Sep 17 00:00:00 2001 From: Boris van Katwijk Date: Mon, 30 Dec 2024 20:16:52 +0100 Subject: [PATCH 14/61] Update nested.sh --- nested.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nested.sh b/nested.sh index f1fcfbfda2..30528f67c3 100755 --- a/nested.sh +++ b/nested.sh @@ -1,7 +1,7 @@ #!/bin/zsh set -euo pipefail -find . -name "*Test.java" -type f -exec sed -i '' \ +find . -name "*Test.java" -type f -exec sed -E -i '' \ 's|// -- \([a-z][a-zA-Z0-9_]*\).*|}\n\n @Nested\n class \U\1 {|' {} + exit 0; From d3c06e69ca06e4db5e639fcafa3a578396623305 Mon Sep 17 00:00:00 2001 From: Boris van Katwijk Date: Mon, 30 Dec 2024 20:18:16 +0100 Subject: [PATCH 15/61] Update nested.sh --- nested.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nested.sh b/nested.sh index 30528f67c3..d8bb7ca6d7 100755 --- a/nested.sh +++ b/nested.sh @@ -2,6 +2,6 @@ set -euo pipefail find . -name "*Test.java" -type f -exec sed -E -i '' \ - 's|// -- \([a-z][a-zA-Z0-9_]*\).*|}\n\n @Nested\n class \U\1 {|' {} + + 's|// -- ([a-z])([a-zA-Z0-9_]*)|}\n\n @Nested\n class \u\1 {|' {} + exit 0; From e04ff8636d52ee8522053303dbe925b8e3abae56 Mon Sep 17 00:00:00 2001 From: Boris van Katwijk Date: Mon, 30 Dec 2024 20:18:32 +0100 Subject: [PATCH 16/61] Update nested.sh --- nested.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nested.sh b/nested.sh index d8bb7ca6d7..9baf3e6016 100755 --- a/nested.sh +++ b/nested.sh @@ -2,6 +2,6 @@ set -euo pipefail find . -name "*Test.java" -type f -exec sed -E -i '' \ - 's|// -- ([a-z])([a-zA-Z0-9_]*)|}\n\n @Nested\n class \u\1 {|' {} + + 's|// -- ([a-z])([a-zA-Z0-9_]*)|}\n\n @Nested\n class \U\1\E\2 {|' {} + exit 0; From ca6be88fcbec1cb29ef8687bc2bed10fce4a0c7a Mon Sep 17 00:00:00 2001 From: Boris van Katwijk Date: Mon, 30 Dec 2024 20:29:32 +0100 Subject: [PATCH 17/61] Update nested.sh --- nested.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nested.sh b/nested.sh index 9baf3e6016..7d7bae0513 100755 --- a/nested.sh +++ b/nested.sh @@ -2,6 +2,6 @@ set -euo pipefail find . -name "*Test.java" -type f -exec sed -E -i '' \ - 's|// -- ([a-z])([a-zA-Z0-9_]*)|}\n\n @Nested\n class \U\1\E\2 {|' {} + + 's|// -- ([a-z])([a-zA-Z0-9_]*)(.*)|}\n\n @Nested\n @DisplayName(\1\2\3)\n class \CAPS\1\2 {|' {} + exit 0; From d1eb9432b8867d5372e9a47b6a3957f2dc2b6f90 Mon Sep 17 00:00:00 2001 From: Boris van Katwijk Date: Mon, 30 Dec 2024 20:29:51 +0100 Subject: [PATCH 18/61] Update nested.sh --- nested.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nested.sh b/nested.sh index 7d7bae0513..b2feb7c29d 100755 --- a/nested.sh +++ b/nested.sh @@ -2,6 +2,6 @@ set -euo pipefail find . -name "*Test.java" -type f -exec sed -E -i '' \ - 's|// -- ([a-z])([a-zA-Z0-9_]*)(.*)|}\n\n @Nested\n @DisplayName(\1\2\3)\n class \CAPS\1\2 {|' {} + + 's|// -- ([a-z])([a-zA-Z0-9_]*)(.*)|}\n\n @Nested\n @DisplayName("\1\2\3")\n class \CAPS\1\2 {|' {} + exit 0; From 28c5ec3d561b67acca8a69c6143ac505bfe4da3e Mon Sep 17 00:00:00 2001 From: Boris van Katwijk Date: Mon, 30 Dec 2024 20:32:37 +0100 Subject: [PATCH 19/61] Update nested.sh --- nested.sh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/nested.sh b/nested.sh index b2feb7c29d..e7e2b7ff83 100755 --- a/nested.sh +++ b/nested.sh @@ -4,4 +4,6 @@ set -euo pipefail find . -name "*Test.java" -type f -exec sed -E -i '' \ 's|// -- ([a-z])([a-zA-Z0-9_]*)(.*)|}\n\n @Nested\n @DisplayName("\1\2\3")\n class \CAPS\1\2 {|' {} + +find . -type f -name '*Test.java' -exec sed -i '' -E 's/\bCAPS([a-z])([a-zA-Z]*)/\u\1\2/g' {} + + exit 0; From a0c066a653b9ed4af5f46e7ab4aa49a52f1ec8f5 Mon Sep 17 00:00:00 2001 From: Boris van Katwijk Date: Mon, 30 Dec 2024 20:33:09 +0100 Subject: [PATCH 20/61] Update nested.sh --- nested.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nested.sh b/nested.sh index e7e2b7ff83..03d5c1bc84 100755 --- a/nested.sh +++ b/nested.sh @@ -4,6 +4,6 @@ set -euo pipefail find . -name "*Test.java" -type f -exec sed -E -i '' \ 's|// -- ([a-z])([a-zA-Z0-9_]*)(.*)|}\n\n @Nested\n @DisplayName("\1\2\3")\n class \CAPS\1\2 {|' {} + -find . -type f -name '*Test.java' -exec sed -i '' -E 's/\bCAPS([a-z])([a-zA-Z]*)/\u\1\2/g' {} + +find . -name "*Test.java" -exec sed -i '' -E 's/\bCAPS([a-z])([a-zA-Z]*)/\u\1\2/g' {} + exit 0; From ae95b4f4a47154b4627cf19af2d253696400c933 Mon Sep 17 00:00:00 2001 From: Boris van Katwijk Date: Mon, 30 Dec 2024 20:35:07 +0100 Subject: [PATCH 21/61] Update nested.sh --- nested.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/nested.sh b/nested.sh index 03d5c1bc84..5eb8f187ac 100755 --- a/nested.sh +++ b/nested.sh @@ -4,6 +4,7 @@ set -euo pipefail find . -name "*Test.java" -type f -exec sed -E -i '' \ 's|// -- ([a-z])([a-zA-Z0-9_]*)(.*)|}\n\n @Nested\n @DisplayName("\1\2\3")\n class \CAPS\1\2 {|' {} + -find . -name "*Test.java" -exec sed -i '' -E 's/\bCAPS([a-z])([a-zA-Z]*)/\u\1\2/g' {} + +find . -type f -name '*Test.java' -exec sed -E 's/\bCAPS([a-z])([a-zA-Z]*)/\1 \2/g' {} + | \ +awk '{ gsub(/CAPS([a-z])([a-zA-Z]*)/, "\u\1\2"); print }' exit 0; From 6a4a981c85f8ae08e66bb47a315a3d063fa91993 Mon Sep 17 00:00:00 2001 From: Boris van Katwijk Date: Mon, 30 Dec 2024 20:36:10 +0100 Subject: [PATCH 22/61] Update nested.sh --- nested.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nested.sh b/nested.sh index 5eb8f187ac..b0dd7fdb18 100755 --- a/nested.sh +++ b/nested.sh @@ -5,6 +5,6 @@ find . -name "*Test.java" -type f -exec sed -E -i '' \ 's|// -- ([a-z])([a-zA-Z0-9_]*)(.*)|}\n\n @Nested\n @DisplayName("\1\2\3")\n class \CAPS\1\2 {|' {} + find . -type f -name '*Test.java' -exec sed -E 's/\bCAPS([a-z])([a-zA-Z]*)/\1 \2/g' {} + | \ -awk '{ gsub(/CAPS([a-z])([a-zA-Z]*)/, "\u\1\2"); print }' +awk '{ gsub(/CAPS([a-z])([a-zA-Z]*)/, "\\u\1\2"); }' exit 0; From f056d6fbeb8d071365b6408ab361cc1475ec76b4 Mon Sep 17 00:00:00 2001 From: Boris van Katwijk Date: Mon, 30 Dec 2024 20:42:27 +0100 Subject: [PATCH 23/61] Create dock.sh --- dock.sh | 4 ++++ 1 file changed, 4 insertions(+) create mode 100755 dock.sh diff --git a/dock.sh b/dock.sh new file mode 100755 index 0000000000..2747725c08 --- /dev/null +++ b/dock.sh @@ -0,0 +1,4 @@ +#!/bin/zsh + +docker run --rm -v .:/mnt alpine sh -c \ + "sh /mnt/nested.sh" \ No newline at end of file From 54d3dfe766321a7e1663b432cdb4dca610eb1f5f Mon Sep 17 00:00:00 2001 From: Boris van Katwijk Date: Mon, 30 Dec 2024 20:47:57 +0100 Subject: [PATCH 24/61] Update nested.sh --- nested.sh | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/nested.sh b/nested.sh index b0dd7fdb18..98b8e1b834 100755 --- a/nested.sh +++ b/nested.sh @@ -4,7 +4,6 @@ set -euo pipefail find . -name "*Test.java" -type f -exec sed -E -i '' \ 's|// -- ([a-z])([a-zA-Z0-9_]*)(.*)|}\n\n @Nested\n @DisplayName("\1\2\3")\n class \CAPS\1\2 {|' {} + -find . -type f -name '*Test.java' -exec sed -E 's/\bCAPS([a-z])([a-zA-Z]*)/\1 \2/g' {} + | \ -awk '{ gsub(/CAPS([a-z])([a-zA-Z]*)/, "\\u\1\2"); }' +find . -type f -name '*Test.java' -exec perl -i -pe 's/\bCAPS([a-z])([a-zA-Z]*)/\U\1\L\2/g' {} + exit 0; From 2657ddcd3c3cc1c05e7709edebd4d86a7118c1d5 Mon Sep 17 00:00:00 2001 From: Boris van Katwijk Date: Mon, 30 Dec 2024 20:48:34 +0100 Subject: [PATCH 25/61] Update nested.sh --- nested.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nested.sh b/nested.sh index 98b8e1b834..0302181651 100755 --- a/nested.sh +++ b/nested.sh @@ -4,6 +4,6 @@ set -euo pipefail find . -name "*Test.java" -type f -exec sed -E -i '' \ 's|// -- ([a-z])([a-zA-Z0-9_]*)(.*)|}\n\n @Nested\n @DisplayName("\1\2\3")\n class \CAPS\1\2 {|' {} + -find . -type f -name '*Test.java' -exec perl -i -pe 's/\bCAPS([a-z])([a-zA-Z]*)/\U\1\L\2/g' {} + +find . -type f -name '*Test.java' -exec perl -i -pe 's/\bCAPS([a-z])([a-zA-Z]*)/\u\1/g' {} + exit 0; From d79f15ee15306e608d038d8e96b4578fd7a0edec Mon Sep 17 00:00:00 2001 From: Boris van Katwijk Date: Mon, 30 Dec 2024 20:48:54 +0100 Subject: [PATCH 26/61] Update nested.sh --- nested.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nested.sh b/nested.sh index 0302181651..88c7bc894e 100755 --- a/nested.sh +++ b/nested.sh @@ -4,6 +4,6 @@ set -euo pipefail find . -name "*Test.java" -type f -exec sed -E -i '' \ 's|// -- ([a-z])([a-zA-Z0-9_]*)(.*)|}\n\n @Nested\n @DisplayName("\1\2\3")\n class \CAPS\1\2 {|' {} + -find . -type f -name '*Test.java' -exec perl -i -pe 's/\bCAPS([a-z])([a-zA-Z]*)/\u\1/g' {} + +find . -type f -name '*Test.java' -exec perl -i -pe 's/\bCAPS([a-z])([a-zA-Z]*)/\u\1\2/g' {} + exit 0; From eef0745d375eb2df9a2705f702d55a64e6bd4872 Mon Sep 17 00:00:00 2001 From: Boris van Katwijk Date: Mon, 30 Dec 2024 20:56:52 +0100 Subject: [PATCH 27/61] Update nested.sh --- nested.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nested.sh b/nested.sh index 88c7bc894e..1c04f00626 100755 --- a/nested.sh +++ b/nested.sh @@ -6,4 +6,4 @@ find . -name "*Test.java" -type f -exec sed -E -i '' \ find . -type f -name '*Test.java' -exec perl -i -pe 's/\bCAPS([a-z])([a-zA-Z]*)/\u\1\2/g' {} + -exit 0; +./gradlew test \ No newline at end of file From c9ea9a0f03e3b939704364fb056f3a35cb9d6590 Mon Sep 17 00:00:00 2001 From: Boris van Katwijk Date: Tue, 31 Dec 2024 14:38:04 +0100 Subject: [PATCH 28/61] Update nested.sh --- nested.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nested.sh b/nested.sh index 1c04f00626..1af3a51b64 100755 --- a/nested.sh +++ b/nested.sh @@ -2,7 +2,7 @@ set -euo pipefail find . -name "*Test.java" -type f -exec sed -E -i '' \ - 's|// -- ([a-z])([a-zA-Z0-9_]*)(.*)|}\n\n @Nested\n @DisplayName("\1\2\3")\n class \CAPS\1\2 {|' {} + + 's|// -- ([a-zA-Z])([a-zA-Z0-9_]*)(.*)|}\n\n @Nested\n @DisplayName("\1\2\3")\n class \CAPS\1\2 {|' {} + find . -type f -name '*Test.java' -exec perl -i -pe 's/\bCAPS([a-z])([a-zA-Z]*)/\u\1\2/g' {} + From 4f60de350cdf8da88eff8c92a25ae784b63aa8cf Mon Sep 17 00:00:00 2001 From: Boris van Katwijk Date: Tue, 31 Dec 2024 14:41:56 +0100 Subject: [PATCH 29/61] Update nested.sh --- nested.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nested.sh b/nested.sh index 1af3a51b64..294015a226 100755 --- a/nested.sh +++ b/nested.sh @@ -4,6 +4,6 @@ set -euo pipefail find . -name "*Test.java" -type f -exec sed -E -i '' \ 's|// -- ([a-zA-Z])([a-zA-Z0-9_]*)(.*)|}\n\n @Nested\n @DisplayName("\1\2\3")\n class \CAPS\1\2 {|' {} + -find . -type f -name '*Test.java' -exec perl -i -pe 's/\bCAPS([a-z])([a-zA-Z]*)/\u\1\2/g' {} + +find . -type f -name '*Test.java' -exec perl -i -pe 's/\bCAPS([a-zA-Z])([a-zA-Z]*)/\u\1\2/g' {} + ./gradlew test \ No newline at end of file From 2834ab6c140db28f29211a3c083e1c213e39190e Mon Sep 17 00:00:00 2001 From: Boris van Katwijk Date: Tue, 31 Dec 2024 14:42:33 +0100 Subject: [PATCH 30/61] Update nested.sh --- nested.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nested.sh b/nested.sh index 294015a226..f7bf0bf863 100755 --- a/nested.sh +++ b/nested.sh @@ -2,7 +2,7 @@ set -euo pipefail find . -name "*Test.java" -type f -exec sed -E -i '' \ - 's|// -- ([a-zA-Z])([a-zA-Z0-9_]*)(.*)|}\n\n @Nested\n @DisplayName("\1\2\3")\n class \CAPS\1\2 {|' {} + + 's|// -- ([a-zA-Z])([a-zA-Z0-9_]*)(.*)|}\n\n @Nested\n @DisplayName("\1\2\3")\n class \CAPS\1\2Test {|' {} + find . -type f -name '*Test.java' -exec perl -i -pe 's/\bCAPS([a-zA-Z])([a-zA-Z]*)/\u\1\2/g' {} + From bb84724d8fd6f077924322e0f6120a350a63344f Mon Sep 17 00:00:00 2001 From: Boris van Katwijk Date: Tue, 31 Dec 2024 14:54:28 +0100 Subject: [PATCH 31/61] Update nested.sh --- nested.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nested.sh b/nested.sh index f7bf0bf863..3d5a13f04a 100755 --- a/nested.sh +++ b/nested.sh @@ -2,7 +2,7 @@ set -euo pipefail find . -name "*Test.java" -type f -exec sed -E -i '' \ - 's|// -- ([a-zA-Z])([a-zA-Z0-9_]*)(.*)|}\n\n @Nested\n @DisplayName("\1\2\3")\n class \CAPS\1\2Test {|' {} + + 's|// -- [^a-zA-Z0-9 ]|}\n\n @Nested\n @DisplayName("\1")\n class \CAPS\1Test {|' {} + find . -type f -name '*Test.java' -exec perl -i -pe 's/\bCAPS([a-zA-Z])([a-zA-Z]*)/\u\1\2/g' {} + From 8367deb927cfb81d95f55dc32171b10e2dacc77b Mon Sep 17 00:00:00 2001 From: Boris van Katwijk Date: Tue, 31 Dec 2024 14:56:26 +0100 Subject: [PATCH 32/61] Update nested.sh --- nested.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nested.sh b/nested.sh index 3d5a13f04a..a5edbda532 100755 --- a/nested.sh +++ b/nested.sh @@ -2,7 +2,7 @@ set -euo pipefail find . -name "*Test.java" -type f -exec sed -E -i '' \ - 's|// -- [^a-zA-Z0-9 ]|}\n\n @Nested\n @DisplayName("\1")\n class \CAPS\1Test {|' {} + + 's|// -- ([^a-zA-Z0-9]+)|}\n\n @Nested\n @DisplayName("\1")\n class \CAPS\1Test {|' {} + find . -type f -name '*Test.java' -exec perl -i -pe 's/\bCAPS([a-zA-Z])([a-zA-Z]*)/\u\1\2/g' {} + From f1a082c77f1d30f78afaba58249052da724aede7 Mon Sep 17 00:00:00 2001 From: Boris van Katwijk Date: Tue, 31 Dec 2024 14:56:55 +0100 Subject: [PATCH 33/61] Update nested.sh --- nested.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nested.sh b/nested.sh index a5edbda532..787f0a72bb 100755 --- a/nested.sh +++ b/nested.sh @@ -2,8 +2,8 @@ set -euo pipefail find . -name "*Test.java" -type f -exec sed -E -i '' \ - 's|// -- ([^a-zA-Z0-9]+)|}\n\n @Nested\n @DisplayName("\1")\n class \CAPS\1Test {|' {} + + 's|// -- ([a-zA-Z0-9]+)|}\n\n @Nested\n @DisplayName("\1")\n class \CAPS\1Test {|' {} + find . -type f -name '*Test.java' -exec perl -i -pe 's/\bCAPS([a-zA-Z])([a-zA-Z]*)/\u\1\2/g' {} + -./gradlew test \ No newline at end of file +#./gradlew test \ No newline at end of file From 4c64244aede1739a4807996af457dd715f40b167 Mon Sep 17 00:00:00 2001 From: Boris van Katwijk Date: Tue, 31 Dec 2024 14:57:57 +0100 Subject: [PATCH 34/61] Update nested.sh --- nested.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nested.sh b/nested.sh index 787f0a72bb..92f8e0e290 100755 --- a/nested.sh +++ b/nested.sh @@ -2,7 +2,7 @@ set -euo pipefail find . -name "*Test.java" -type f -exec sed -E -i '' \ - 's|// -- ([a-zA-Z0-9]+)|}\n\n @Nested\n @DisplayName("\1")\n class \CAPS\1Test {|' {} + + 's|// -- ([a-zA-Z0-9]+)/g|}\n\n @Nested\n @DisplayName("\1")\n class \CAPS\1Test {|' {} + find . -type f -name '*Test.java' -exec perl -i -pe 's/\bCAPS([a-zA-Z])([a-zA-Z]*)/\u\1\2/g' {} + From ff39540fcbc542f0643f15e27f28ca2ea5bda5e7 Mon Sep 17 00:00:00 2001 From: Boris van Katwijk Date: Tue, 31 Dec 2024 15:10:04 +0100 Subject: [PATCH 35/61] Update nested.sh --- nested.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nested.sh b/nested.sh index 92f8e0e290..47af7913d5 100755 --- a/nested.sh +++ b/nested.sh @@ -2,7 +2,7 @@ set -euo pipefail find . -name "*Test.java" -type f -exec sed -E -i '' \ - 's|// -- ([a-zA-Z0-9]+)/g|}\n\n @Nested\n @DisplayName("\1")\n class \CAPS\1Test {|' {} + + 's|// -- ([a-zA-Z0-9]+)$|}\n\n @Nested\n @DisplayName("\1")\n class \CAPS\1Test {|' {} + find . -type f -name '*Test.java' -exec perl -i -pe 's/\bCAPS([a-zA-Z])([a-zA-Z]*)/\u\1\2/g' {} + From af70e965f6965f6a6e6f085def6ae1e76d5fb86f Mon Sep 17 00:00:00 2001 From: Boris van Katwijk Date: Tue, 31 Dec 2024 15:34:17 +0100 Subject: [PATCH 36/61] Update nested.sh --- nested.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nested.sh b/nested.sh index 47af7913d5..690992f5ed 100755 --- a/nested.sh +++ b/nested.sh @@ -1,9 +1,9 @@ #!/bin/zsh set -euo pipefail -find . -name "*Test.java" -type f -exec sed -E -i '' \ +find ./src -name "*Test.java" -type f -exec sed -E -i '' \ 's|// -- ([a-zA-Z0-9]+)$|}\n\n @Nested\n @DisplayName("\1")\n class \CAPS\1Test {|' {} + -find . -type f -name '*Test.java' -exec perl -i -pe 's/\bCAPS([a-zA-Z])([a-zA-Z]*)/\u\1\2/g' {} + +find ./src -type f -name '*Test.java' -exec perl -i -pe 's/\bCAPS([a-zA-Z])([a-zA-Z]*)/\u\1\2/g' {} + #./gradlew test \ No newline at end of file From 4d102bf51ff3dae7a755a5e6599c9ac8e7e52923 Mon Sep 17 00:00:00 2001 From: Boris van Katwijk Date: Tue, 31 Dec 2024 15:37:12 +0100 Subject: [PATCH 37/61] Update CheckedConsumerTest.java --- .../java/io/vavr/CheckedConsumerTest.java | 170 ++++++++++-------- 1 file changed, 99 insertions(+), 71 deletions(-) diff --git a/src/test/java/io/vavr/CheckedConsumerTest.java b/src/test/java/io/vavr/CheckedConsumerTest.java index a35d91e06b..b91b6f3df2 100644 --- a/src/test/java/io/vavr/CheckedConsumerTest.java +++ b/src/test/java/io/vavr/CheckedConsumerTest.java @@ -27,6 +27,8 @@ package io.vavr; import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import java.util.concurrent.atomic.AtomicBoolean; @@ -38,98 +40,124 @@ public class CheckedConsumerTest { - // -- of + @Nested + @DisplayName("of") + class OfTest { - @Test - public void shouldCreateCheckedConsumerUsingLambda() { - final CheckedConsumer consumer = CheckedConsumer.of(obj -> {}); - assertThat(consumer).isNotNull(); - } + @Test + public void shouldCreateCheckedConsumerUsingLambda() { + final CheckedConsumer consumer = CheckedConsumer.of(obj -> { + }); + assertThat(consumer).isNotNull(); + } - @Test - public void shouldCreateCheckedConsumerUsingMethodReference() { - final CheckedConsumer consumer = CheckedConsumer.of(CheckedConsumerTest::accept); - assertThat(consumer).isNotNull(); + @Test + public void shouldCreateCheckedConsumerUsingMethodReference() { + final CheckedConsumer consumer = CheckedConsumer.of(CheckedConsumerTest::accept); + assertThat(consumer).isNotNull(); + } + } private static void accept(Object obj) { } - // -- accept - - @Test - public void shouldApplyNonThrowingCheckedConsumer() { - final CheckedConsumer f = t -> {}; - try { - f.accept(null); - } catch(Throwable x) { - fail("should not have thrown", x); + @Nested + @DisplayName("accept") + class AcceptTest { + + @Test + public void shouldApplyNonThrowingCheckedConsumer() { + final CheckedConsumer f = t -> { + }; + try { + f.accept(null); + } catch (Throwable x) { + fail("should not have thrown", x); + } } - } - @Test - public void shouldApplyThrowingCheckedConsumer() { - final CheckedConsumer f = t -> { throw new Error(); }; - try { - f.accept(null); - fail("should have thrown"); - } catch(Throwable x) { - // ok + @Test + public void shouldApplyThrowingCheckedConsumer() { + final CheckedConsumer f = t -> { + throw new Error(); + }; + try { + f.accept(null); + fail("should have thrown"); + } catch (Throwable x) { + // ok + } } + } - // -- andThen + @Nested + @DisplayName("andThen") + class AndThenTest { - @Test - public void shouldThrowWhenComposingCheckedConsumerUsingAndThenWithNullParameter() { - final CheckedConsumer f = t -> {}; - assertThatThrownBy(() -> f.andThen(null)).isInstanceOf(NullPointerException.class); - } + @Test + public void shouldThrowWhenComposingCheckedConsumerUsingAndThenWithNullParameter() { + final CheckedConsumer f = t -> { + }; + assertThatThrownBy(() -> f.andThen(null)).isInstanceOf(NullPointerException.class); + } - @Test - public void shouldComposeCheckedConsumerUsingAndThenWhenFirstOneSucceeds() { - final AtomicBoolean result = new AtomicBoolean(false); - final CheckedConsumer f = t -> {}; - try { - f.andThen(ignored -> result.set(true)).accept(null); - assertThat(result.get()).isTrue(); - } catch(Throwable x) { - fail("should not have thrown", x); + @Test + public void shouldComposeCheckedConsumerUsingAndThenWhenFirstOneSucceeds() { + final AtomicBoolean result = new AtomicBoolean(false); + final CheckedConsumer f = t -> { + }; + try { + f.andThen(ignored -> result.set(true)).accept(null); + assertThat(result.get()).isTrue(); + } catch (Throwable x) { + fail("should not have thrown", x); + } } - } - @Test - public void shouldComposeCheckedConsumerUsingAndThenWhenFirstOneFails() { - final AtomicBoolean result = new AtomicBoolean(false); - final CheckedConsumer f = t -> { throw new Error(); }; - try { - f.andThen(ignored -> result.set(true)).accept(null); - fail("should have thrown"); - } catch(Throwable x) { - assertThat(result.get()).isFalse(); + @Test + public void shouldComposeCheckedConsumerUsingAndThenWhenFirstOneFails() { + final AtomicBoolean result = new AtomicBoolean(false); + final CheckedConsumer f = t -> { + throw new Error(); + }; + try { + f.andThen(ignored -> result.set(true)).accept(null); + fail("should have thrown"); + } catch (Throwable x) { + assertThat(result.get()).isFalse(); + } } - } - // -- unchecked + } - @Test - public void shouldApplyAnUncheckedFunctionThatDoesNotThrow() { - final Consumer consumer = CheckedConsumer.of(obj -> {}).unchecked(); - try { - consumer.accept(null); - } catch(Throwable x) { - Assertions.fail("Did not expect an exception but received: " + x.getMessage()); + @Nested + @DisplayName("unchecked") + class UncheckedTest { + + @Test + public void shouldApplyAnUncheckedFunctionThatDoesNotThrow() { + final Consumer consumer = CheckedConsumer.of(obj -> { + }).unchecked(); + try { + consumer.accept(null); + } catch (Throwable x) { + Assertions.fail("Did not expect an exception but received: " + x.getMessage()); + } } - } - @Test - public void shouldApplyAnUncheckedFunctionThatThrows() { - final Consumer consumer = CheckedConsumer.of(obj -> { throw new Error(); }).unchecked(); - try { - consumer.accept(null); - Assertions.fail("Did expect an exception."); - } catch(Error x) { - // ok! + @Test + public void shouldApplyAnUncheckedFunctionThatThrows() { + final Consumer consumer = CheckedConsumer.of(obj -> { + throw new Error(); + }).unchecked(); + try { + consumer.accept(null); + Assertions.fail("Did expect an exception."); + } catch (Error x) { + // ok! + } } } } From fcdf049ddf5d2b74bbfb92072203d1b4f1cae7bb Mon Sep 17 00:00:00 2001 From: Boris van Katwijk Date: Tue, 31 Dec 2024 15:37:16 +0100 Subject: [PATCH 38/61] Update API.java --- src-gen/main/java/io/vavr/API.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src-gen/main/java/io/vavr/API.java b/src-gen/main/java/io/vavr/API.java index aca28cc009..e6c95aabc8 100644 --- a/src-gen/main/java/io/vavr/API.java +++ b/src-gen/main/java/io/vavr/API.java @@ -6081,7 +6081,7 @@ public static final class Case0 implements Case { private static final long serialVersionUID = 1L; private final Pattern0 pattern; - private final Function f; + private transient final Function f; private Case0(Pattern0 pattern, Function f) { this.pattern = pattern; @@ -6104,7 +6104,7 @@ public static final class Case1 implements Case { private static final long serialVersionUID = 1L; private final Pattern1 pattern; - private final Function f; + private transient final Function f; private Case1(Pattern1 pattern, Function f) { this.pattern = pattern; @@ -6127,7 +6127,7 @@ public static final class Case2 implements Case { private static final long serialVersionUID = 1L; private final Pattern2 pattern; - private final BiFunction f; + private transient final BiFunction f; private Case2(Pattern2 pattern, BiFunction f) { this.pattern = pattern; From 06ff407a6b3ba7f5c1caa7088cbbd52f9cac21eb Mon Sep 17 00:00:00 2001 From: Boris van Katwijk Date: Tue, 31 Dec 2024 15:52:29 +0100 Subject: [PATCH 39/61] Update CheckedPredicateTest.java --- .../java/io/vavr/CheckedPredicateTest.java | 65 +++++++++++-------- 1 file changed, 38 insertions(+), 27 deletions(-) diff --git a/src/test/java/io/vavr/CheckedPredicateTest.java b/src/test/java/io/vavr/CheckedPredicateTest.java index 86673d2f76..4996f28b45 100644 --- a/src/test/java/io/vavr/CheckedPredicateTest.java +++ b/src/test/java/io/vavr/CheckedPredicateTest.java @@ -27,6 +27,8 @@ package io.vavr; import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import java.util.function.Predicate; @@ -35,44 +37,53 @@ public class CheckedPredicateTest { - // -- of + @Nested + @DisplayName("of") + class OfTest { - @Test - public void shouldCreateCheckedPredicateUsingLambda() { - final CheckedPredicate predicate = CheckedPredicate.of(obj -> true); - assertThat(predicate).isNotNull(); - } + @Test + public void shouldCreateCheckedPredicateUsingLambda() { + final CheckedPredicate predicate = CheckedPredicate.of(obj -> true); + assertThat(predicate).isNotNull(); + } - @Test - public void shouldCreateCheckedPredicateUsingMethodReference() { - final CheckedPredicate predicate = CheckedPredicate.of(CheckedPredicateTest::test); - assertThat(predicate).isNotNull(); + @Test + public void shouldCreateCheckedPredicateUsingMethodReference() { + final CheckedPredicate predicate = CheckedPredicate.of(CheckedPredicateTest::test); + assertThat(predicate).isNotNull(); + } } private static boolean test(Object obj) { return true; } - // -- unchecked - @Test - public void shouldApplyAnUncheckedFunctionThatDoesNotThrow() { - final Predicate predicate = CheckedPredicate.of(obj -> true).unchecked(); - try { - predicate.test(null); - } catch(Throwable x) { - Assertions.fail("Did not expect an exception but received: " + x.getMessage()); + @Nested + @DisplayName("unchecked") + class UncheckedTest { + + @Test + public void shouldApplyAnUncheckedFunctionThatDoesNotThrow() { + final Predicate predicate = CheckedPredicate.of(obj -> true).unchecked(); + try { + predicate.test(null); + } catch (Throwable x) { + Assertions.fail("Did not expect an exception but received: " + x.getMessage()); + } } - } - @Test - public void shouldApplyAnUncheckedFunctionThatThrows() { - final Predicate predicate = CheckedPredicate.of(obj -> { throw new Error(); }).unchecked(); - try { - predicate.test(null); - Assertions.fail("Did expect an exception."); - } catch(Error x) { - // ok! + @Test + public void shouldApplyAnUncheckedFunctionThatThrows() { + final Predicate predicate = CheckedPredicate.of(obj -> { + throw new Error(); + }).unchecked(); + try { + predicate.test(null); + Assertions.fail("Did expect an exception."); + } catch (Error x) { + // ok! + } } } } From 94df49d724fc8a2178be3c40f185616cb8af355d Mon Sep 17 00:00:00 2001 From: Boris van Katwijk Date: Tue, 31 Dec 2024 15:58:01 +0100 Subject: [PATCH 40/61] Update CheckedRunnableTest.java --- .../java/io/vavr/CheckedRunnableTest.java | 56 +++++++++++-------- 1 file changed, 33 insertions(+), 23 deletions(-) diff --git a/src/test/java/io/vavr/CheckedRunnableTest.java b/src/test/java/io/vavr/CheckedRunnableTest.java index b7ca54e525..d10d8738e7 100644 --- a/src/test/java/io/vavr/CheckedRunnableTest.java +++ b/src/test/java/io/vavr/CheckedRunnableTest.java @@ -27,44 +27,54 @@ package io.vavr; import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; public class CheckedRunnableTest { - // -- of + @Nested + @DisplayName("of") + class OfTest { - @Test - public void shouldCreateCheckedRunnableUsingLambda() { - final CheckedRunnable runnable = CheckedRunnable.of(() -> {}); - assertThat(runnable).isNotNull(); - } + @Test + public void shouldCreateCheckedRunnableUsingLambda() { + final CheckedRunnable runnable = CheckedRunnable.of(() -> { + }); + assertThat(runnable).isNotNull(); + } - @Test - public void shouldCreateCheckedRunnableUsingMethodReference() { - final CheckedRunnable runnable = CheckedRunnable.of(CheckedRunnableTest::run); - assertThat(runnable).isNotNull(); + @Test + public void shouldCreateCheckedRunnableUsingMethodReference() { + final CheckedRunnable runnable = CheckedRunnable.of(CheckedRunnableTest::run); + assertThat(runnable).isNotNull(); + } } private static void run() { } - // -- unchecked + @Nested + @DisplayName("unchecked") + class UncheckedTest { - @Test - public void shouldApplyAnUncheckedFunctionThatDoesNotThrow() { - final Runnable runnable = CheckedRunnable.of(() -> {}).unchecked(); - try { - runnable.run(); - } catch(Throwable x) { - Assertions.fail("Did not expect an exception but received: " + x.getMessage()); + @Test + public void shouldApplyAnUncheckedFunctionThatDoesNotThrow() { + final Runnable runnable = CheckedRunnable.of(() -> { + }).unchecked(); + try { + runnable.run(); + } catch (Throwable x) { + Assertions.fail("Did not expect an exception but received: " + x.getMessage()); + } } - } - @Test - public void shouldApplyAnUncheckedFunctionThatThrows() { - final Runnable runnable = CheckedRunnable.of(() -> { throw new Error(); }).unchecked(); - Assertions.assertThrows(Error.class, () -> runnable.run()); + @Test + public void shouldApplyAnUncheckedFunctionThatThrows() { + final Runnable runnable = CheckedRunnable.of(() -> { throw new Error(); }).unchecked(); + Assertions.assertThrows(Error.class, () -> runnable.run()); + } } } From 3a42cd1a74da8f1ce5570141dd9accc05f268fbd Mon Sep 17 00:00:00 2001 From: Boris van Katwijk Date: Wed, 1 Jan 2025 14:50:21 +0100 Subject: [PATCH 41/61] Update Generator.sc --- generator/Generator.sc | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/generator/Generator.sc b/generator/Generator.sc index 6c9b678de1..a4894809a1 100644 --- a/generator/Generator.sc +++ b/generator/Generator.sc @@ -2915,8 +2915,13 @@ def generateTestClasses(): Unit = { } // -- shortcuts + @Nested + class Shortcuts { + + ${genShortcutsTests(im, packageName, className)} + + } - ${genShortcutsTests(im, packageName, className)} // // Alias should return not null. @@ -3100,6 +3105,7 @@ def generateTestClasses(): Unit = { val generics = (1 to i + 1).gen(j => "Object")(", ") val test = im.getType("org.junit.jupiter.api.Test") + val nested = im.getType("org.junit.jupiter.api.Nested") val assertThrows = im.getStatic("org.junit.jupiter.api.Assertions.assertThrows") val assertThat = im.getStatic("org.assertj.core.api.Assertions.assertThat") val recFuncF1 = if (i == 0) "11;" else s"i1 <= 0 ? i1 : $className.recurrent2.apply(${(1 to i).gen(j => s"i$j" + (j == 1).gen(s" - 1"))(", ")}) + 1;" From 28c95fd0f5cd831287daa37da463585d947bd1e7 Mon Sep 17 00:00:00 2001 From: Boris van Katwijk Date: Wed, 1 Jan 2025 14:57:16 +0100 Subject: [PATCH 42/61] Update Generator.sc --- generator/Generator.sc | 106 ++++++++++++++++++++++------------------- 1 file changed, 56 insertions(+), 50 deletions(-) diff --git a/generator/Generator.sc b/generator/Generator.sc index a4894809a1..618a91e3bf 100644 --- a/generator/Generator.sc +++ b/generator/Generator.sc @@ -2636,6 +2636,7 @@ def generateTestClasses(): Unit = { genVavrFile("io.vavr", s"APITest", baseDir = TARGET_TEST)((im: ImportManager, packageName, className) => { val assertThat = im.getStatic("org.assertj.core.api.Assertions.assertThat") + val nested = im.getType("org.junit.jupiter.api.Nested") val test = im.getType("org.junit.jupiter.api.Test") val API = im.getType("io.vavr.API") @@ -2914,7 +2915,6 @@ def generateTestClasses(): Unit = { $AssertionsExtensions.assertThat($API.class).isNotInstantiable(); } - // -- shortcuts @Nested class Shortcuts { @@ -2930,63 +2930,69 @@ def generateTestClasses(): Unit = { ${genAliasesTests(im, packageName, className)} - // -- run + @Nested + class Run { + + @$test + public void shouldRunUnitAndReturnVoid() { + int[] i = { 0 }; + Void nothing = run(() -> i[0]++); + $assertThat(nothing).isNull(); + $assertThat(i[0]).isEqualTo(1); + } - @$test - public void shouldRunUnitAndReturnVoid() { - int[] i = { 0 }; - Void nothing = run(() -> i[0]++); - $assertThat(nothing).isNull(); - $assertThat(i[0]).isEqualTo(1); } - // -- For + @Nested + class For { - @$test - public void shouldIterateFor1UsingSimpleYield() { - final $ListType list = List.of(1, 2, 3); - final $ListType actual = For(list).yield().toList(); - $assertThat(actual).isEqualTo(list); - } + @$test + public void shouldIterateFor1UsingSimpleYield() { + final $ListType list = List.of(1, 2, 3); + final $ListType actual = For(list).yield().toList(); + $assertThat(actual).isEqualTo(list); + } - ${(1 to N).gen(i => xs""" - @$test - public void shouldIterateFor$ListType$i() { - final $ListType result = For( - ${(1 to i).gen(j => s"$ListType.of(1, 2, 3)")(",\n")} - ).yield(${(i > 1).gen("(")}${(1 to i).gen(j => s"i$j")(", ")}${(i > 1).gen(")")} -> ${(1 to i).gen(j => s"i$j")(" + ")}).toList(); - $assertThat(result.length()).isEqualTo((int) Math.pow(3, $i)); - $assertThat(result.head()).isEqualTo($i); - $assertThat(result.last()).isEqualTo(3 * $i); - } - """)("\n\n")} + ${(1 to N).gen(i => xs""" + @$test + public void shouldIterateFor$ListType$i() { + final $ListType result = For( + ${(1 to i).gen(j => s"$ListType.of(1, 2, 3)")(",\n")} + ).yield(${(i > 1).gen("(")}${(1 to i).gen(j => s"i$j")(", ")}${(i > 1).gen(")")} -> ${(1 to i).gen(j => s"i$j")(" + ")}).toList(); + $assertThat(result.length()).isEqualTo((int) Math.pow(3, $i)); + $assertThat(result.head()).isEqualTo($i); + $assertThat(result.last()).isEqualTo(3 * $i); + } + """)("\n\n")} - ${monadicTypesFor.gen(mtype => (1 to N).gen(i => { xs""" - @$test - public void shouldIterateFor$mtype$i() { - final $mtype result = For( - ${(1 to i).gen(j => s"$mtype.of($j)")(",\n")} - ).yield(${(i > 1).gen("(")}${(1 to i).gen(j => s"i$j")(", ")}${(i > 1).gen(")")} -> ${(1 to i).gen(j => s"i$j")(" + ")}); - $assertThat(result.get()).isEqualTo(${(1 to i).sum}); - } - """})("\n\n"))("\n\n")} + ${monadicTypesFor.gen(mtype => (1 to N).gen(i => { xs""" + @$test + public void shouldIterateFor$mtype$i() { + final $mtype result = For( + ${(1 to i).gen(j => s"$mtype.of($j)")(",\n")} + ).yield(${(i > 1).gen("(")}${(1 to i).gen(j => s"i$j")(", ")}${(i > 1).gen(")")} -> ${(1 to i).gen(j => s"i$j")(" + ")}); + $assertThat(result.get()).isEqualTo(${(1 to i).sum}); + } + """})("\n\n"))("\n\n")} - ${monadicFunctionTypesFor.gen(mtype => (1 to N).gen(i => { xs""" - @$test - public void shouldIterateFor$mtype$i() { - final ${if(mtype == "Either") mtype + "" else mtype + ""}result = For( - ${(1 to i).gen(j => s"${if(mtype == "Either") mtype + s".right($j)" else mtype + s".of(() -> $j)"}")(",\n")} - ).yield(${(i > 1).gen("(")}${(1 to i).gen(j => s"i$j")(", ")}${(i > 1).gen(")")} -> ${(1 to i).gen(j => s"i$j")(" + ")}); - $assertThat(result.get()).isEqualTo(${(1 to i).sum}); - } - """})("\n\n"))("\n\n")} + ${monadicFunctionTypesFor.gen(mtype => (1 to N).gen(i => { xs""" + @$test + public void shouldIterateFor$mtype$i() { + final ${if(mtype == "Either") mtype + "" else mtype + ""}result = For( + ${(1 to i).gen(j => s"${if(mtype == "Either") mtype + s".right($j)" else mtype + s".of(() -> $j)"}")(",\n")} + ).yield(${(i > 1).gen("(")}${(1 to i).gen(j => s"i$j")(", ")}${(i > 1).gen(")")} -> ${(1 to i).gen(j => s"i$j")(" + ")}); + $assertThat(result.get()).isEqualTo(${(1 to i).sum}); + } + """})("\n\n"))("\n\n")} + + @$test + public void shouldIterateNestedFor() { + final $ListType result = + For(${im.getType("java.util.Arrays")}.asList(1, 2), i -> + For(${im.getType("io.vavr.collection.List")}.of('a', 'b')).yield(c -> i + ":" + c)).toList(); + assertThat(result).isEqualTo($ListType.of("1:a", "1:b", "2:a", "2:b")); + } - @$test - public void shouldIterateNestedFor() { - final $ListType result = - For(${im.getType("java.util.Arrays")}.asList(1, 2), i -> - For(${im.getType("io.vavr.collection.List")}.of('a', 'b')).yield(c -> i + ":" + c)).toList(); - assertThat(result).isEqualTo($ListType.of("1:a", "1:b", "2:a", "2:b")); } // -- Match From 8d6dfb5e577006325dd813d7f88ed02b67115723 Mon Sep 17 00:00:00 2001 From: Boris van Katwijk Date: Wed, 1 Jan 2025 15:33:30 +0100 Subject: [PATCH 43/61] Update Generator.sc --- generator/Generator.sc | 169 +++++++++++++++++++++-------------------- 1 file changed, 87 insertions(+), 82 deletions(-) diff --git a/generator/Generator.sc b/generator/Generator.sc index 618a91e3bf..0076bd3df2 100644 --- a/generator/Generator.sc +++ b/generator/Generator.sc @@ -2995,98 +2995,103 @@ def generateTestClasses(): Unit = { } - // -- Match - - @$test - public void shouldReturnSomeWhenApplyingCaseGivenPredicateAndSupplier() { - final Match.Case _case = Case($$(ignored -> true), ignored -> 1); - assertThat(_case.isDefinedAt(null)).isTrue(); - assertThat(_case.apply(null)).isEqualTo(1); - } - - @$test - public void shouldReturnNoneWhenApplyingCaseGivenPredicateAndSupplier() { - assertThat(Case($$(ignored -> false), ignored -> 1).isDefinedAt(null)).isFalse(); - } - - @$test - public void shouldReturnSomeWhenApplyingCaseGivenPredicateAndValue() { - final Match.Case _case = Case($$(ignored -> true), 1); - assertThat(_case.isDefinedAt(null)).isTrue(); - assertThat(_case.apply(null)).isEqualTo(1); - } - - @$test - public void shouldReturnNoneWhenApplyingCaseGivenPredicateAndValue() { - assertThat(Case($$(ignored -> false), 1).isDefinedAt(null)).isFalse(); - } + @Nested + class MatchTest { - @$test - public void shouldPassIssue2401() { - final $SeqType empty = $StreamType.empty(); - try { - Match(empty).of( - Case($$($ListType.empty()), ignored -> "list") - ); - fail("expected MatchError"); - } catch (MatchError err) { - // ok! + @$test + public void shouldReturnSomeWhenApplyingCaseGivenPredicateAndSupplier() { + final Match.Case _case = Case($$(ignored -> true), ignored -> 1); + assertThat(_case.isDefinedAt(null)).isTrue(); + assertThat(_case.apply(null)).isEqualTo(1); } - } - @$test - public void shouldCatchClassCastExceptionWhenPredicateHasDifferentType() { - try { - final Object o = ""; - Match(o).of( - Case($$((Integer i) -> true), "never") - ); - fail("expected MatchError"); - } catch (MatchError err) { - // ok! + @$test + public void shouldReturnNoneWhenApplyingCaseGivenPredicateAndSupplier() { + assertThat(Case($$(ignored -> false), ignored -> 1).isDefinedAt(null)).isFalse(); } - } - - // -- Match patterns - static class ClzMatch {} - static class ClzMatch1 extends ClzMatch {} - static class ClzMatch2 extends ClzMatch {} + @$test + public void shouldReturnSomeWhenApplyingCaseGivenPredicateAndValue() { + final Match.Case _case = Case($$(ignored -> true), 1); + assertThat(_case.isDefinedAt(null)).isTrue(); + assertThat(_case.apply(null)).isEqualTo(1); + } - ${(1 to N).gen(i => { + @$test + public void shouldReturnNoneWhenApplyingCaseGivenPredicateAndValue() { + assertThat(Case($$(ignored -> false), 1).isDefinedAt(null)).isFalse(); + } - im.getStatic("io.vavr.API.*") - im.getStatic("io.vavr.Patterns.*") + @$test + public void shouldPassIssue2401() { + final $SeqType empty = $StreamType.empty(); + try { + Match(empty).of( + Case($$($ListType.empty()), ignored -> "list") + ); + fail("expected MatchError"); + } catch (MatchError err) { + // ok! + } + } - xs""" @$test - public void shouldMatchPattern$i() { - final Tuple$i<${(1 to i).gen(j => s"Integer")(", ")}> tuple = Tuple.of(${(1 to i).gen(j => s"1")(", ")}); - final String func = Match(tuple).of( - Case($$Tuple$i($d(0)${(2 to i).gen(j => s", $d()")}), (${(1 to i).gen(j => s"m$j")(", ")}) -> "fail"), - Case($$Tuple$i(${(1 to i).gen(j => s"$d()")(", ")}), (${(1 to i).gen(j => s"m$j")(", ")}) -> "okFunc") - ); - assertThat(func).isEqualTo("okFunc"); - final String supp = Match(tuple).of( - Case($$Tuple$i($d(0)${(2 to i).gen(j => s", $d()")}), () -> "fail"), - Case($$Tuple$i(${(1 to i).gen(j => s"$d()")(", ")}), () -> "okSupp") - ); - assertThat(supp).isEqualTo("okSupp"); - final String val = Match(tuple).of( - Case($$Tuple$i($d(0)${(2 to i).gen(j => s", $d()")}), "fail"), - Case($$Tuple$i(${(1 to i).gen(j => s"$d()")(", ")}), "okVal") - ); - assertThat(val).isEqualTo("okVal"); - - final ClzMatch c = new ClzMatch2(); - final String match = Match(c).of( - Case(Match.Pattern$i.of(ClzMatch1.class, ${(1 to i).gen(j => s"$d()")(", ")}, t -> Tuple.of(${(1 to i).gen(j => s"null")(", ")})), "fail"), - Case(Match.Pattern$i.of(ClzMatch2.class, ${(1 to i).gen(j => s"$d()")(", ")}, t -> Tuple.of(${(1 to i).gen(j => s"null")(", ")})), "okMatch") - ); - assertThat(match).isEqualTo("okMatch"); + public void shouldCatchClassCastExceptionWhenPredicateHasDifferentType() { + try { + final Object o = ""; + Match(o).of( + Case($$((Integer i) -> true), "never") + ); + fail("expected MatchError"); + } catch (MatchError err) { + // ok! + } } - """ - })("\n\n")} + + } + + @Nested + class MatchPatterns { + + class ClzMatch {} + class ClzMatch1 extends ClzMatch {} + class ClzMatch2 extends ClzMatch {} + + ${(1 to N).gen(i => { + + im.getStatic("io.vavr.API.*") + im.getStatic("io.vavr.Patterns.*") + + xs""" + @$test + public void shouldMatchPattern$i() { + final Tuple$i<${(1 to i).gen(j => s"Integer")(", ")}> tuple = Tuple.of(${(1 to i).gen(j => s"1")(", ")}); + final String func = Match(tuple).of( + Case($$Tuple$i($d(0)${(2 to i).gen(j => s", $d()")}), (${(1 to i).gen(j => s"m$j")(", ")}) -> "fail"), + Case($$Tuple$i(${(1 to i).gen(j => s"$d()")(", ")}), (${(1 to i).gen(j => s"m$j")(", ")}) -> "okFunc") + ); + assertThat(func).isEqualTo("okFunc"); + final String supp = Match(tuple).of( + Case($$Tuple$i($d(0)${(2 to i).gen(j => s", $d()")}), () -> "fail"), + Case($$Tuple$i(${(1 to i).gen(j => s"$d()")(", ")}), () -> "okSupp") + ); + assertThat(supp).isEqualTo("okSupp"); + final String val = Match(tuple).of( + Case($$Tuple$i($d(0)${(2 to i).gen(j => s", $d()")}), "fail"), + Case($$Tuple$i(${(1 to i).gen(j => s"$d()")(", ")}), "okVal") + ); + assertThat(val).isEqualTo("okVal"); + + final ClzMatch c = new ClzMatch2(); + final String match = Match(c).of( + Case(Match.Pattern$i.of(ClzMatch1.class, ${(1 to i).gen(j => s"$d()")(", ")}, t -> Tuple.of(${(1 to i).gen(j => s"null")(", ")})), "fail"), + Case(Match.Pattern$i.of(ClzMatch2.class, ${(1 to i).gen(j => s"$d()")(", ")}, t -> Tuple.of(${(1 to i).gen(j => s"null")(", ")})), "okMatch") + ); + assertThat(match).isEqualTo("okMatch"); + } + """ + })("\n\n")} + } } """ }) From d6373306ca88c674231b139f07eac2863e23d0e0 Mon Sep 17 00:00:00 2001 From: Boris van Katwijk Date: Wed, 1 Jan 2025 16:09:13 +0100 Subject: [PATCH 44/61] Update Generator.sc --- generator/Generator.sc | 1 - 1 file changed, 1 deletion(-) diff --git a/generator/Generator.sc b/generator/Generator.sc index 0076bd3df2..244e16d893 100644 --- a/generator/Generator.sc +++ b/generator/Generator.sc @@ -3116,7 +3116,6 @@ def generateTestClasses(): Unit = { val generics = (1 to i + 1).gen(j => "Object")(", ") val test = im.getType("org.junit.jupiter.api.Test") - val nested = im.getType("org.junit.jupiter.api.Nested") val assertThrows = im.getStatic("org.junit.jupiter.api.Assertions.assertThrows") val assertThat = im.getStatic("org.assertj.core.api.Assertions.assertThat") val recFuncF1 = if (i == 0) "11;" else s"i1 <= 0 ? i1 : $className.recurrent2.apply(${(1 to i).gen(j => s"i$j" + (j == 1).gen(s" - 1"))(", ")}) + 1;" From 4d11a54f162acbd45caa1d351918bf304eafab9e Mon Sep 17 00:00:00 2001 From: Boris van Katwijk Date: Wed, 1 Jan 2025 16:09:15 +0100 Subject: [PATCH 45/61] Update APITest.java --- src-gen/test/java/io/vavr/APITest.java | 1529 ++++++++++++------------ 1 file changed, 772 insertions(+), 757 deletions(-) diff --git a/src-gen/test/java/io/vavr/APITest.java b/src-gen/test/java/io/vavr/APITest.java index 0d2c05b296..1a90782f26 100644 --- a/src-gen/test/java/io/vavr/APITest.java +++ b/src-gen/test/java/io/vavr/APITest.java @@ -47,6 +47,7 @@ import java.util.Arrays; import java.util.Comparator; import java.util.concurrent.Executors; +import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; @SuppressWarnings("deprecation") @@ -57,53 +58,56 @@ public void shouldNotBeInstantiable() { AssertionsExtensions.assertThat(API.class).isNotInstantiable(); } - // -- shortcuts + @Nested + class Shortcuts { - @Test - public void shouldCompileTODOAndThrowDefaultMessageAtRuntime() { - try { - final String s = TODO(); - fail("TODO() should throw. s: " + s); - } catch(NotImplementedError err) { - assertThat(err.getMessage()).isEqualTo("An implementation is missing."); + @Test + public void shouldCompileTODOAndThrowDefaultMessageAtRuntime() { + try { + final String s = TODO(); + fail("TODO() should throw. s: " + s); + } catch(NotImplementedError err) { + assertThat(err.getMessage()).isEqualTo("An implementation is missing."); + } } - } - @Test - public void shouldCompileTODOAndThrowGivenMessageAtRuntime() { - final String msg = "Don't try this in production!"; - try { - final String s = TODO(msg); - fail("TODO(String) should throw. s: " + s); - } catch(NotImplementedError err) { - assertThat(err.getMessage()).isEqualTo(msg); + @Test + public void shouldCompileTODOAndThrowGivenMessageAtRuntime() { + final String msg = "Don't try this in production!"; + try { + final String s = TODO(msg); + fail("TODO(String) should throw. s: " + s); + } catch(NotImplementedError err) { + assertThat(err.getMessage()).isEqualTo(msg); + } } - } - @Test - public void shouldCallprint_Object() { - assertThat(captureStdOut(()->print("ok"))).isEqualTo("ok"); - } + @Test + public void shouldCallprint_Object() { + assertThat(captureStdOut(()->print("ok"))).isEqualTo("ok"); + } - @Test - public void shouldCallprintf() { - assertThat(captureStdOut(()->printf("%s", "ok"))).isEqualTo("ok"); - } + @Test + public void shouldCallprintf() { + assertThat(captureStdOut(()->printf("%s", "ok"))).isEqualTo("ok"); + } - @Test - public void shouldCallprintln_Object() { - assertThat(captureStdOut(()->println("ok"))).isEqualTo("ok\n"); - } + @Test + public void shouldCallprintln_Object() { + assertThat(captureStdOut(()->println("ok"))).isEqualTo("ok\n"); + } - @Test - public void shouldCallprintln() { - assertThat(captureStdOut(()->println())).isEqualTo("\n"); - } + @Test + public void shouldCallprintln() { + assertThat(captureStdOut(()->println())).isEqualTo("\n"); + } - @Test - public void shouldCallprintlnWithArguments() { - assertThat(captureStdOut(() -> println("this", "and", "that"))).isEqualTo("this and that\n"); - } + @Test + public void shouldCallprintlnWithArguments() { + assertThat(captureStdOut(() -> println("this", "and", "that"))).isEqualTo("this and that\n"); + } + + } // // Alias should return not null. @@ -967,780 +971,791 @@ public void shouldSortedMapFromTuplesAndComparatorReturnNotNull() { assertThat(SortedMap((Comparator)Integer::compareTo, Tuple(1, '1'), Tuple(2, '2'), Tuple(3, '3'))).isNotNull(); } - // -- run + @Nested + class Run { + + @Test + public void shouldRunUnitAndReturnVoid() { + int[] i = { 0 }; + Void nothing = run(() -> i[0]++); + assertThat(nothing).isNull(); + assertThat(i[0]).isEqualTo(1); + } - @Test - public void shouldRunUnitAndReturnVoid() { - int[] i = { 0 }; - Void nothing = run(() -> i[0]++); - assertThat(nothing).isNull(); - assertThat(i[0]).isEqualTo(1); } - // -- For + @Nested + class For { - @Test - public void shouldIterateFor1UsingSimpleYield() { - final List list = List.of(1, 2, 3); - final List actual = For(list).yield().toList(); - assertThat(actual).isEqualTo(list); - } + @Test + public void shouldIterateFor1UsingSimpleYield() { + final List list = List.of(1, 2, 3); + final List actual = For(list).yield().toList(); + assertThat(actual).isEqualTo(list); + } - @Test - public void shouldIterateForList1() { - final List result = For( - List.of(1, 2, 3) - ).yield(i1 -> i1).toList(); - assertThat(result.length()).isEqualTo((int) Math.pow(3, 1)); - assertThat(result.head()).isEqualTo(1); - assertThat(result.last()).isEqualTo(3 * 1); - } + @Test + public void shouldIterateForList1() { + final List result = For( + List.of(1, 2, 3) + ).yield(i1 -> i1).toList(); + assertThat(result.length()).isEqualTo((int) Math.pow(3, 1)); + assertThat(result.head()).isEqualTo(1); + assertThat(result.last()).isEqualTo(3 * 1); + } - @Test - public void shouldIterateForList2() { - final List result = For( - List.of(1, 2, 3), - List.of(1, 2, 3) - ).yield((i1, i2) -> i1 + i2).toList(); - assertThat(result.length()).isEqualTo((int) Math.pow(3, 2)); - assertThat(result.head()).isEqualTo(2); - assertThat(result.last()).isEqualTo(3 * 2); - } + @Test + public void shouldIterateForList2() { + final List result = For( + List.of(1, 2, 3), + List.of(1, 2, 3) + ).yield((i1, i2) -> i1 + i2).toList(); + assertThat(result.length()).isEqualTo((int) Math.pow(3, 2)); + assertThat(result.head()).isEqualTo(2); + assertThat(result.last()).isEqualTo(3 * 2); + } - @Test - public void shouldIterateForList3() { - final List result = For( - List.of(1, 2, 3), - List.of(1, 2, 3), - List.of(1, 2, 3) - ).yield((i1, i2, i3) -> i1 + i2 + i3).toList(); - assertThat(result.length()).isEqualTo((int) Math.pow(3, 3)); - assertThat(result.head()).isEqualTo(3); - assertThat(result.last()).isEqualTo(3 * 3); - } + @Test + public void shouldIterateForList3() { + final List result = For( + List.of(1, 2, 3), + List.of(1, 2, 3), + List.of(1, 2, 3) + ).yield((i1, i2, i3) -> i1 + i2 + i3).toList(); + assertThat(result.length()).isEqualTo((int) Math.pow(3, 3)); + assertThat(result.head()).isEqualTo(3); + assertThat(result.last()).isEqualTo(3 * 3); + } - @Test - public void shouldIterateForList4() { - final List result = For( - List.of(1, 2, 3), - List.of(1, 2, 3), - List.of(1, 2, 3), - List.of(1, 2, 3) - ).yield((i1, i2, i3, i4) -> i1 + i2 + i3 + i4).toList(); - assertThat(result.length()).isEqualTo((int) Math.pow(3, 4)); - assertThat(result.head()).isEqualTo(4); - assertThat(result.last()).isEqualTo(3 * 4); - } + @Test + public void shouldIterateForList4() { + final List result = For( + List.of(1, 2, 3), + List.of(1, 2, 3), + List.of(1, 2, 3), + List.of(1, 2, 3) + ).yield((i1, i2, i3, i4) -> i1 + i2 + i3 + i4).toList(); + assertThat(result.length()).isEqualTo((int) Math.pow(3, 4)); + assertThat(result.head()).isEqualTo(4); + assertThat(result.last()).isEqualTo(3 * 4); + } - @Test - public void shouldIterateForList5() { - final List result = For( - List.of(1, 2, 3), - List.of(1, 2, 3), - List.of(1, 2, 3), - List.of(1, 2, 3), - List.of(1, 2, 3) - ).yield((i1, i2, i3, i4, i5) -> i1 + i2 + i3 + i4 + i5).toList(); - assertThat(result.length()).isEqualTo((int) Math.pow(3, 5)); - assertThat(result.head()).isEqualTo(5); - assertThat(result.last()).isEqualTo(3 * 5); - } + @Test + public void shouldIterateForList5() { + final List result = For( + List.of(1, 2, 3), + List.of(1, 2, 3), + List.of(1, 2, 3), + List.of(1, 2, 3), + List.of(1, 2, 3) + ).yield((i1, i2, i3, i4, i5) -> i1 + i2 + i3 + i4 + i5).toList(); + assertThat(result.length()).isEqualTo((int) Math.pow(3, 5)); + assertThat(result.head()).isEqualTo(5); + assertThat(result.last()).isEqualTo(3 * 5); + } - @Test - public void shouldIterateForList6() { - final List result = For( - List.of(1, 2, 3), - List.of(1, 2, 3), - List.of(1, 2, 3), - List.of(1, 2, 3), - List.of(1, 2, 3), - List.of(1, 2, 3) - ).yield((i1, i2, i3, i4, i5, i6) -> i1 + i2 + i3 + i4 + i5 + i6).toList(); - assertThat(result.length()).isEqualTo((int) Math.pow(3, 6)); - assertThat(result.head()).isEqualTo(6); - assertThat(result.last()).isEqualTo(3 * 6); - } + @Test + public void shouldIterateForList6() { + final List result = For( + List.of(1, 2, 3), + List.of(1, 2, 3), + List.of(1, 2, 3), + List.of(1, 2, 3), + List.of(1, 2, 3), + List.of(1, 2, 3) + ).yield((i1, i2, i3, i4, i5, i6) -> i1 + i2 + i3 + i4 + i5 + i6).toList(); + assertThat(result.length()).isEqualTo((int) Math.pow(3, 6)); + assertThat(result.head()).isEqualTo(6); + assertThat(result.last()).isEqualTo(3 * 6); + } - @Test - public void shouldIterateForList7() { - final List result = For( - List.of(1, 2, 3), - List.of(1, 2, 3), - List.of(1, 2, 3), - List.of(1, 2, 3), - List.of(1, 2, 3), - List.of(1, 2, 3), - List.of(1, 2, 3) - ).yield((i1, i2, i3, i4, i5, i6, i7) -> i1 + i2 + i3 + i4 + i5 + i6 + i7).toList(); - assertThat(result.length()).isEqualTo((int) Math.pow(3, 7)); - assertThat(result.head()).isEqualTo(7); - assertThat(result.last()).isEqualTo(3 * 7); - } + @Test + public void shouldIterateForList7() { + final List result = For( + List.of(1, 2, 3), + List.of(1, 2, 3), + List.of(1, 2, 3), + List.of(1, 2, 3), + List.of(1, 2, 3), + List.of(1, 2, 3), + List.of(1, 2, 3) + ).yield((i1, i2, i3, i4, i5, i6, i7) -> i1 + i2 + i3 + i4 + i5 + i6 + i7).toList(); + assertThat(result.length()).isEqualTo((int) Math.pow(3, 7)); + assertThat(result.head()).isEqualTo(7); + assertThat(result.last()).isEqualTo(3 * 7); + } - @Test - public void shouldIterateForList8() { - final List result = For( - List.of(1, 2, 3), - List.of(1, 2, 3), - List.of(1, 2, 3), - List.of(1, 2, 3), - List.of(1, 2, 3), - List.of(1, 2, 3), - List.of(1, 2, 3), - List.of(1, 2, 3) - ).yield((i1, i2, i3, i4, i5, i6, i7, i8) -> i1 + i2 + i3 + i4 + i5 + i6 + i7 + i8).toList(); - assertThat(result.length()).isEqualTo((int) Math.pow(3, 8)); - assertThat(result.head()).isEqualTo(8); - assertThat(result.last()).isEqualTo(3 * 8); - } + @Test + public void shouldIterateForList8() { + final List result = For( + List.of(1, 2, 3), + List.of(1, 2, 3), + List.of(1, 2, 3), + List.of(1, 2, 3), + List.of(1, 2, 3), + List.of(1, 2, 3), + List.of(1, 2, 3), + List.of(1, 2, 3) + ).yield((i1, i2, i3, i4, i5, i6, i7, i8) -> i1 + i2 + i3 + i4 + i5 + i6 + i7 + i8).toList(); + assertThat(result.length()).isEqualTo((int) Math.pow(3, 8)); + assertThat(result.head()).isEqualTo(8); + assertThat(result.last()).isEqualTo(3 * 8); + } - @Test - public void shouldIterateForOption1() { - final Option result = For( - Option.of(1) - ).yield(i1 -> i1); - assertThat(result.get()).isEqualTo(1); - } - - @Test - public void shouldIterateForOption2() { - final Option result = For( - Option.of(1), - Option.of(2) - ).yield((i1, i2) -> i1 + i2); - assertThat(result.get()).isEqualTo(3); - } - - @Test - public void shouldIterateForOption3() { - final Option result = For( - Option.of(1), - Option.of(2), - Option.of(3) - ).yield((i1, i2, i3) -> i1 + i2 + i3); - assertThat(result.get()).isEqualTo(6); - } - - @Test - public void shouldIterateForOption4() { - final Option result = For( - Option.of(1), - Option.of(2), - Option.of(3), - Option.of(4) - ).yield((i1, i2, i3, i4) -> i1 + i2 + i3 + i4); - assertThat(result.get()).isEqualTo(10); - } + @Test + public void shouldIterateForOption1() { + final Option result = For( + Option.of(1) + ).yield(i1 -> i1); + assertThat(result.get()).isEqualTo(1); + } - @Test - public void shouldIterateForOption5() { - final Option result = For( - Option.of(1), - Option.of(2), - Option.of(3), - Option.of(4), - Option.of(5) - ).yield((i1, i2, i3, i4, i5) -> i1 + i2 + i3 + i4 + i5); - assertThat(result.get()).isEqualTo(15); - } + @Test + public void shouldIterateForOption2() { + final Option result = For( + Option.of(1), + Option.of(2) + ).yield((i1, i2) -> i1 + i2); + assertThat(result.get()).isEqualTo(3); + } - @Test - public void shouldIterateForOption6() { - final Option result = For( - Option.of(1), - Option.of(2), - Option.of(3), - Option.of(4), - Option.of(5), - Option.of(6) - ).yield((i1, i2, i3, i4, i5, i6) -> i1 + i2 + i3 + i4 + i5 + i6); - assertThat(result.get()).isEqualTo(21); - } + @Test + public void shouldIterateForOption3() { + final Option result = For( + Option.of(1), + Option.of(2), + Option.of(3) + ).yield((i1, i2, i3) -> i1 + i2 + i3); + assertThat(result.get()).isEqualTo(6); + } - @Test - public void shouldIterateForOption7() { - final Option result = For( - Option.of(1), - Option.of(2), - Option.of(3), - Option.of(4), - Option.of(5), - Option.of(6), - Option.of(7) - ).yield((i1, i2, i3, i4, i5, i6, i7) -> i1 + i2 + i3 + i4 + i5 + i6 + i7); - assertThat(result.get()).isEqualTo(28); - } + @Test + public void shouldIterateForOption4() { + final Option result = For( + Option.of(1), + Option.of(2), + Option.of(3), + Option.of(4) + ).yield((i1, i2, i3, i4) -> i1 + i2 + i3 + i4); + assertThat(result.get()).isEqualTo(10); + } - @Test - public void shouldIterateForOption8() { - final Option result = For( - Option.of(1), - Option.of(2), - Option.of(3), - Option.of(4), - Option.of(5), - Option.of(6), - Option.of(7), - Option.of(8) - ).yield((i1, i2, i3, i4, i5, i6, i7, i8) -> i1 + i2 + i3 + i4 + i5 + i6 + i7 + i8); - assertThat(result.get()).isEqualTo(36); - } + @Test + public void shouldIterateForOption5() { + final Option result = For( + Option.of(1), + Option.of(2), + Option.of(3), + Option.of(4), + Option.of(5) + ).yield((i1, i2, i3, i4, i5) -> i1 + i2 + i3 + i4 + i5); + assertThat(result.get()).isEqualTo(15); + } - @Test - public void shouldIterateForFuture1() { - final Futureresult = For( - Future.of(() -> 1) - ).yield(i1 -> i1); - assertThat(result.get()).isEqualTo(1); - } + @Test + public void shouldIterateForOption6() { + final Option result = For( + Option.of(1), + Option.of(2), + Option.of(3), + Option.of(4), + Option.of(5), + Option.of(6) + ).yield((i1, i2, i3, i4, i5, i6) -> i1 + i2 + i3 + i4 + i5 + i6); + assertThat(result.get()).isEqualTo(21); + } - @Test - public void shouldIterateForFuture2() { - final Futureresult = For( - Future.of(() -> 1), - Future.of(() -> 2) - ).yield((i1, i2) -> i1 + i2); - assertThat(result.get()).isEqualTo(3); - } + @Test + public void shouldIterateForOption7() { + final Option result = For( + Option.of(1), + Option.of(2), + Option.of(3), + Option.of(4), + Option.of(5), + Option.of(6), + Option.of(7) + ).yield((i1, i2, i3, i4, i5, i6, i7) -> i1 + i2 + i3 + i4 + i5 + i6 + i7); + assertThat(result.get()).isEqualTo(28); + } - @Test - public void shouldIterateForFuture3() { - final Futureresult = For( - Future.of(() -> 1), - Future.of(() -> 2), - Future.of(() -> 3) - ).yield((i1, i2, i3) -> i1 + i2 + i3); - assertThat(result.get()).isEqualTo(6); - } + @Test + public void shouldIterateForOption8() { + final Option result = For( + Option.of(1), + Option.of(2), + Option.of(3), + Option.of(4), + Option.of(5), + Option.of(6), + Option.of(7), + Option.of(8) + ).yield((i1, i2, i3, i4, i5, i6, i7, i8) -> i1 + i2 + i3 + i4 + i5 + i6 + i7 + i8); + assertThat(result.get()).isEqualTo(36); + } - @Test - public void shouldIterateForFuture4() { - final Futureresult = For( - Future.of(() -> 1), - Future.of(() -> 2), - Future.of(() -> 3), - Future.of(() -> 4) - ).yield((i1, i2, i3, i4) -> i1 + i2 + i3 + i4); - assertThat(result.get()).isEqualTo(10); - } + @Test + public void shouldIterateForFuture1() { + final Futureresult = For( + Future.of(() -> 1) + ).yield(i1 -> i1); + assertThat(result.get()).isEqualTo(1); + } - @Test - public void shouldIterateForFuture5() { - final Futureresult = For( - Future.of(() -> 1), - Future.of(() -> 2), - Future.of(() -> 3), - Future.of(() -> 4), - Future.of(() -> 5) - ).yield((i1, i2, i3, i4, i5) -> i1 + i2 + i3 + i4 + i5); - assertThat(result.get()).isEqualTo(15); - } + @Test + public void shouldIterateForFuture2() { + final Futureresult = For( + Future.of(() -> 1), + Future.of(() -> 2) + ).yield((i1, i2) -> i1 + i2); + assertThat(result.get()).isEqualTo(3); + } - @Test - public void shouldIterateForFuture6() { - final Futureresult = For( - Future.of(() -> 1), - Future.of(() -> 2), - Future.of(() -> 3), - Future.of(() -> 4), - Future.of(() -> 5), - Future.of(() -> 6) - ).yield((i1, i2, i3, i4, i5, i6) -> i1 + i2 + i3 + i4 + i5 + i6); - assertThat(result.get()).isEqualTo(21); - } + @Test + public void shouldIterateForFuture3() { + final Futureresult = For( + Future.of(() -> 1), + Future.of(() -> 2), + Future.of(() -> 3) + ).yield((i1, i2, i3) -> i1 + i2 + i3); + assertThat(result.get()).isEqualTo(6); + } - @Test - public void shouldIterateForFuture7() { - final Futureresult = For( - Future.of(() -> 1), - Future.of(() -> 2), - Future.of(() -> 3), - Future.of(() -> 4), - Future.of(() -> 5), - Future.of(() -> 6), - Future.of(() -> 7) - ).yield((i1, i2, i3, i4, i5, i6, i7) -> i1 + i2 + i3 + i4 + i5 + i6 + i7); - assertThat(result.get()).isEqualTo(28); - } + @Test + public void shouldIterateForFuture4() { + final Futureresult = For( + Future.of(() -> 1), + Future.of(() -> 2), + Future.of(() -> 3), + Future.of(() -> 4) + ).yield((i1, i2, i3, i4) -> i1 + i2 + i3 + i4); + assertThat(result.get()).isEqualTo(10); + } - @Test - public void shouldIterateForFuture8() { - final Futureresult = For( - Future.of(() -> 1), - Future.of(() -> 2), - Future.of(() -> 3), - Future.of(() -> 4), - Future.of(() -> 5), - Future.of(() -> 6), - Future.of(() -> 7), - Future.of(() -> 8) - ).yield((i1, i2, i3, i4, i5, i6, i7, i8) -> i1 + i2 + i3 + i4 + i5 + i6 + i7 + i8); - assertThat(result.get()).isEqualTo(36); - } + @Test + public void shouldIterateForFuture5() { + final Futureresult = For( + Future.of(() -> 1), + Future.of(() -> 2), + Future.of(() -> 3), + Future.of(() -> 4), + Future.of(() -> 5) + ).yield((i1, i2, i3, i4, i5) -> i1 + i2 + i3 + i4 + i5); + assertThat(result.get()).isEqualTo(15); + } - @Test - public void shouldIterateForTry1() { - final Tryresult = For( - Try.of(() -> 1) - ).yield(i1 -> i1); - assertThat(result.get()).isEqualTo(1); - } + @Test + public void shouldIterateForFuture6() { + final Futureresult = For( + Future.of(() -> 1), + Future.of(() -> 2), + Future.of(() -> 3), + Future.of(() -> 4), + Future.of(() -> 5), + Future.of(() -> 6) + ).yield((i1, i2, i3, i4, i5, i6) -> i1 + i2 + i3 + i4 + i5 + i6); + assertThat(result.get()).isEqualTo(21); + } - @Test - public void shouldIterateForTry2() { - final Tryresult = For( - Try.of(() -> 1), - Try.of(() -> 2) - ).yield((i1, i2) -> i1 + i2); - assertThat(result.get()).isEqualTo(3); - } + @Test + public void shouldIterateForFuture7() { + final Futureresult = For( + Future.of(() -> 1), + Future.of(() -> 2), + Future.of(() -> 3), + Future.of(() -> 4), + Future.of(() -> 5), + Future.of(() -> 6), + Future.of(() -> 7) + ).yield((i1, i2, i3, i4, i5, i6, i7) -> i1 + i2 + i3 + i4 + i5 + i6 + i7); + assertThat(result.get()).isEqualTo(28); + } - @Test - public void shouldIterateForTry3() { - final Tryresult = For( - Try.of(() -> 1), - Try.of(() -> 2), - Try.of(() -> 3) - ).yield((i1, i2, i3) -> i1 + i2 + i3); - assertThat(result.get()).isEqualTo(6); - } + @Test + public void shouldIterateForFuture8() { + final Futureresult = For( + Future.of(() -> 1), + Future.of(() -> 2), + Future.of(() -> 3), + Future.of(() -> 4), + Future.of(() -> 5), + Future.of(() -> 6), + Future.of(() -> 7), + Future.of(() -> 8) + ).yield((i1, i2, i3, i4, i5, i6, i7, i8) -> i1 + i2 + i3 + i4 + i5 + i6 + i7 + i8); + assertThat(result.get()).isEqualTo(36); + } - @Test - public void shouldIterateForTry4() { - final Tryresult = For( - Try.of(() -> 1), - Try.of(() -> 2), - Try.of(() -> 3), - Try.of(() -> 4) - ).yield((i1, i2, i3, i4) -> i1 + i2 + i3 + i4); - assertThat(result.get()).isEqualTo(10); - } + @Test + public void shouldIterateForTry1() { + final Tryresult = For( + Try.of(() -> 1) + ).yield(i1 -> i1); + assertThat(result.get()).isEqualTo(1); + } - @Test - public void shouldIterateForTry5() { - final Tryresult = For( - Try.of(() -> 1), - Try.of(() -> 2), - Try.of(() -> 3), - Try.of(() -> 4), - Try.of(() -> 5) - ).yield((i1, i2, i3, i4, i5) -> i1 + i2 + i3 + i4 + i5); - assertThat(result.get()).isEqualTo(15); - } + @Test + public void shouldIterateForTry2() { + final Tryresult = For( + Try.of(() -> 1), + Try.of(() -> 2) + ).yield((i1, i2) -> i1 + i2); + assertThat(result.get()).isEqualTo(3); + } - @Test - public void shouldIterateForTry6() { - final Tryresult = For( - Try.of(() -> 1), - Try.of(() -> 2), - Try.of(() -> 3), - Try.of(() -> 4), - Try.of(() -> 5), - Try.of(() -> 6) - ).yield((i1, i2, i3, i4, i5, i6) -> i1 + i2 + i3 + i4 + i5 + i6); - assertThat(result.get()).isEqualTo(21); - } + @Test + public void shouldIterateForTry3() { + final Tryresult = For( + Try.of(() -> 1), + Try.of(() -> 2), + Try.of(() -> 3) + ).yield((i1, i2, i3) -> i1 + i2 + i3); + assertThat(result.get()).isEqualTo(6); + } - @Test - public void shouldIterateForTry7() { - final Tryresult = For( - Try.of(() -> 1), - Try.of(() -> 2), - Try.of(() -> 3), - Try.of(() -> 4), - Try.of(() -> 5), - Try.of(() -> 6), - Try.of(() -> 7) - ).yield((i1, i2, i3, i4, i5, i6, i7) -> i1 + i2 + i3 + i4 + i5 + i6 + i7); - assertThat(result.get()).isEqualTo(28); - } + @Test + public void shouldIterateForTry4() { + final Tryresult = For( + Try.of(() -> 1), + Try.of(() -> 2), + Try.of(() -> 3), + Try.of(() -> 4) + ).yield((i1, i2, i3, i4) -> i1 + i2 + i3 + i4); + assertThat(result.get()).isEqualTo(10); + } - @Test - public void shouldIterateForTry8() { - final Tryresult = For( - Try.of(() -> 1), - Try.of(() -> 2), - Try.of(() -> 3), - Try.of(() -> 4), - Try.of(() -> 5), - Try.of(() -> 6), - Try.of(() -> 7), - Try.of(() -> 8) - ).yield((i1, i2, i3, i4, i5, i6, i7, i8) -> i1 + i2 + i3 + i4 + i5 + i6 + i7 + i8); - assertThat(result.get()).isEqualTo(36); - } + @Test + public void shouldIterateForTry5() { + final Tryresult = For( + Try.of(() -> 1), + Try.of(() -> 2), + Try.of(() -> 3), + Try.of(() -> 4), + Try.of(() -> 5) + ).yield((i1, i2, i3, i4, i5) -> i1 + i2 + i3 + i4 + i5); + assertThat(result.get()).isEqualTo(15); + } - @Test - public void shouldIterateForEither1() { - final Eitherresult = For( - Either.right(1) - ).yield(i1 -> i1); - assertThat(result.get()).isEqualTo(1); - } + @Test + public void shouldIterateForTry6() { + final Tryresult = For( + Try.of(() -> 1), + Try.of(() -> 2), + Try.of(() -> 3), + Try.of(() -> 4), + Try.of(() -> 5), + Try.of(() -> 6) + ).yield((i1, i2, i3, i4, i5, i6) -> i1 + i2 + i3 + i4 + i5 + i6); + assertThat(result.get()).isEqualTo(21); + } - @Test - public void shouldIterateForEither2() { - final Eitherresult = For( - Either.right(1), - Either.right(2) - ).yield((i1, i2) -> i1 + i2); - assertThat(result.get()).isEqualTo(3); - } + @Test + public void shouldIterateForTry7() { + final Tryresult = For( + Try.of(() -> 1), + Try.of(() -> 2), + Try.of(() -> 3), + Try.of(() -> 4), + Try.of(() -> 5), + Try.of(() -> 6), + Try.of(() -> 7) + ).yield((i1, i2, i3, i4, i5, i6, i7) -> i1 + i2 + i3 + i4 + i5 + i6 + i7); + assertThat(result.get()).isEqualTo(28); + } - @Test - public void shouldIterateForEither3() { - final Eitherresult = For( - Either.right(1), - Either.right(2), - Either.right(3) - ).yield((i1, i2, i3) -> i1 + i2 + i3); - assertThat(result.get()).isEqualTo(6); - } + @Test + public void shouldIterateForTry8() { + final Tryresult = For( + Try.of(() -> 1), + Try.of(() -> 2), + Try.of(() -> 3), + Try.of(() -> 4), + Try.of(() -> 5), + Try.of(() -> 6), + Try.of(() -> 7), + Try.of(() -> 8) + ).yield((i1, i2, i3, i4, i5, i6, i7, i8) -> i1 + i2 + i3 + i4 + i5 + i6 + i7 + i8); + assertThat(result.get()).isEqualTo(36); + } - @Test - public void shouldIterateForEither4() { - final Eitherresult = For( - Either.right(1), - Either.right(2), - Either.right(3), - Either.right(4) - ).yield((i1, i2, i3, i4) -> i1 + i2 + i3 + i4); - assertThat(result.get()).isEqualTo(10); - } + @Test + public void shouldIterateForEither1() { + final Eitherresult = For( + Either.right(1) + ).yield(i1 -> i1); + assertThat(result.get()).isEqualTo(1); + } - @Test - public void shouldIterateForEither5() { - final Eitherresult = For( - Either.right(1), - Either.right(2), - Either.right(3), - Either.right(4), - Either.right(5) - ).yield((i1, i2, i3, i4, i5) -> i1 + i2 + i3 + i4 + i5); - assertThat(result.get()).isEqualTo(15); - } + @Test + public void shouldIterateForEither2() { + final Eitherresult = For( + Either.right(1), + Either.right(2) + ).yield((i1, i2) -> i1 + i2); + assertThat(result.get()).isEqualTo(3); + } - @Test - public void shouldIterateForEither6() { - final Eitherresult = For( - Either.right(1), - Either.right(2), - Either.right(3), - Either.right(4), - Either.right(5), - Either.right(6) - ).yield((i1, i2, i3, i4, i5, i6) -> i1 + i2 + i3 + i4 + i5 + i6); - assertThat(result.get()).isEqualTo(21); - } + @Test + public void shouldIterateForEither3() { + final Eitherresult = For( + Either.right(1), + Either.right(2), + Either.right(3) + ).yield((i1, i2, i3) -> i1 + i2 + i3); + assertThat(result.get()).isEqualTo(6); + } - @Test - public void shouldIterateForEither7() { - final Eitherresult = For( - Either.right(1), - Either.right(2), - Either.right(3), - Either.right(4), - Either.right(5), - Either.right(6), - Either.right(7) - ).yield((i1, i2, i3, i4, i5, i6, i7) -> i1 + i2 + i3 + i4 + i5 + i6 + i7); - assertThat(result.get()).isEqualTo(28); - } + @Test + public void shouldIterateForEither4() { + final Eitherresult = For( + Either.right(1), + Either.right(2), + Either.right(3), + Either.right(4) + ).yield((i1, i2, i3, i4) -> i1 + i2 + i3 + i4); + assertThat(result.get()).isEqualTo(10); + } - @Test - public void shouldIterateForEither8() { - final Eitherresult = For( - Either.right(1), - Either.right(2), - Either.right(3), - Either.right(4), - Either.right(5), - Either.right(6), - Either.right(7), - Either.right(8) - ).yield((i1, i2, i3, i4, i5, i6, i7, i8) -> i1 + i2 + i3 + i4 + i5 + i6 + i7 + i8); - assertThat(result.get()).isEqualTo(36); - } + @Test + public void shouldIterateForEither5() { + final Eitherresult = For( + Either.right(1), + Either.right(2), + Either.right(3), + Either.right(4), + Either.right(5) + ).yield((i1, i2, i3, i4, i5) -> i1 + i2 + i3 + i4 + i5); + assertThat(result.get()).isEqualTo(15); + } - @Test - public void shouldIterateNestedFor() { - final List result = - For(Arrays.asList(1, 2), i -> - For(List.of('a', 'b')).yield(c -> i + ":" + c)).toList(); - assertThat(result).isEqualTo(List.of("1:a", "1:b", "2:a", "2:b")); - } + @Test + public void shouldIterateForEither6() { + final Eitherresult = For( + Either.right(1), + Either.right(2), + Either.right(3), + Either.right(4), + Either.right(5), + Either.right(6) + ).yield((i1, i2, i3, i4, i5, i6) -> i1 + i2 + i3 + i4 + i5 + i6); + assertThat(result.get()).isEqualTo(21); + } - // -- Match + @Test + public void shouldIterateForEither7() { + final Eitherresult = For( + Either.right(1), + Either.right(2), + Either.right(3), + Either.right(4), + Either.right(5), + Either.right(6), + Either.right(7) + ).yield((i1, i2, i3, i4, i5, i6, i7) -> i1 + i2 + i3 + i4 + i5 + i6 + i7); + assertThat(result.get()).isEqualTo(28); + } - @Test - public void shouldReturnSomeWhenApplyingCaseGivenPredicateAndSupplier() { - final Match.Case _case = Case($(ignored -> true), ignored -> 1); - assertThat(_case.isDefinedAt(null)).isTrue(); - assertThat(_case.apply(null)).isEqualTo(1); - } + @Test + public void shouldIterateForEither8() { + final Eitherresult = For( + Either.right(1), + Either.right(2), + Either.right(3), + Either.right(4), + Either.right(5), + Either.right(6), + Either.right(7), + Either.right(8) + ).yield((i1, i2, i3, i4, i5, i6, i7, i8) -> i1 + i2 + i3 + i4 + i5 + i6 + i7 + i8); + assertThat(result.get()).isEqualTo(36); + } - @Test - public void shouldReturnNoneWhenApplyingCaseGivenPredicateAndSupplier() { - assertThat(Case($(ignored -> false), ignored -> 1).isDefinedAt(null)).isFalse(); - } + @Test + public void shouldIterateNestedFor() { + final List result = + For(Arrays.asList(1, 2), i -> + For(List.of('a', 'b')).yield(c -> i + ":" + c)).toList(); + assertThat(result).isEqualTo(List.of("1:a", "1:b", "2:a", "2:b")); + } - @Test - public void shouldReturnSomeWhenApplyingCaseGivenPredicateAndValue() { - final Match.Case _case = Case($(ignored -> true), 1); - assertThat(_case.isDefinedAt(null)).isTrue(); - assertThat(_case.apply(null)).isEqualTo(1); } - @Test - public void shouldReturnNoneWhenApplyingCaseGivenPredicateAndValue() { - assertThat(Case($(ignored -> false), 1).isDefinedAt(null)).isFalse(); + @Nested + class MatchTest { + + @Test + public void shouldReturnSomeWhenApplyingCaseGivenPredicateAndSupplier() { + final Match.Case _case = Case($(ignored -> true), ignored -> 1); + assertThat(_case.isDefinedAt(null)).isTrue(); + assertThat(_case.apply(null)).isEqualTo(1); + } + + @Test + public void shouldReturnNoneWhenApplyingCaseGivenPredicateAndSupplier() { + assertThat(Case($(ignored -> false), ignored -> 1).isDefinedAt(null)).isFalse(); + } + + @Test + public void shouldReturnSomeWhenApplyingCaseGivenPredicateAndValue() { + final Match.Case _case = Case($(ignored -> true), 1); + assertThat(_case.isDefinedAt(null)).isTrue(); + assertThat(_case.apply(null)).isEqualTo(1); + } + + @Test + public void shouldReturnNoneWhenApplyingCaseGivenPredicateAndValue() { + assertThat(Case($(ignored -> false), 1).isDefinedAt(null)).isFalse(); + } + + @Test + public void shouldPassIssue2401() { + final Seq empty = Stream.empty(); + try { + Match(empty).of( + Case($(List.empty()), ignored -> "list") + ); + fail("expected MatchError"); + } catch (MatchError err) { + // ok! + } + } + + @Test + public void shouldCatchClassCastExceptionWhenPredicateHasDifferentType() { + try { + final Object o = ""; + Match(o).of( + Case($((Integer i) -> true), "never") + ); + fail("expected MatchError"); + } catch (MatchError err) { + // ok! + } + } + } - @Test - public void shouldPassIssue2401() { - final Seq empty = Stream.empty(); - try { - Match(empty).of( - Case($(List.empty()), ignored -> "list") + @Nested + class MatchPatterns { + + class ClzMatch {} + class ClzMatch1 extends ClzMatch {} + class ClzMatch2 extends ClzMatch {} + + @Test + public void shouldMatchPattern1() { + final Tuple1 tuple = Tuple.of(1); + final String func = Match(tuple).of( + Case($Tuple1($(0)), (m1) -> "fail"), + Case($Tuple1($()), (m1) -> "okFunc") + ); + assertThat(func).isEqualTo("okFunc"); + final String supp = Match(tuple).of( + Case($Tuple1($(0)), () -> "fail"), + Case($Tuple1($()), () -> "okSupp") ); - fail("expected MatchError"); - } catch (MatchError err) { - // ok! + assertThat(supp).isEqualTo("okSupp"); + final String val = Match(tuple).of( + Case($Tuple1($(0)), "fail"), + Case($Tuple1($()), "okVal") + ); + assertThat(val).isEqualTo("okVal"); + + final ClzMatch c = new ClzMatch2(); + final String match = Match(c).of( + Case(Match.Pattern1.of(ClzMatch1.class, $(), t -> Tuple.of(null)), "fail"), + Case(Match.Pattern1.of(ClzMatch2.class, $(), t -> Tuple.of(null)), "okMatch") + ); + assertThat(match).isEqualTo("okMatch"); } - } - @Test - public void shouldCatchClassCastExceptionWhenPredicateHasDifferentType() { - try { - final Object o = ""; - Match(o).of( - Case($((Integer i) -> true), "never") + @Test + public void shouldMatchPattern2() { + final Tuple2 tuple = Tuple.of(1, 1); + final String func = Match(tuple).of( + Case($Tuple2($(0), $()), (m1, m2) -> "fail"), + Case($Tuple2($(), $()), (m1, m2) -> "okFunc") ); - fail("expected MatchError"); - } catch (MatchError err) { - // ok! + assertThat(func).isEqualTo("okFunc"); + final String supp = Match(tuple).of( + Case($Tuple2($(0), $()), () -> "fail"), + Case($Tuple2($(), $()), () -> "okSupp") + ); + assertThat(supp).isEqualTo("okSupp"); + final String val = Match(tuple).of( + Case($Tuple2($(0), $()), "fail"), + Case($Tuple2($(), $()), "okVal") + ); + assertThat(val).isEqualTo("okVal"); + + final ClzMatch c = new ClzMatch2(); + final String match = Match(c).of( + Case(Match.Pattern2.of(ClzMatch1.class, $(), $(), t -> Tuple.of(null, null)), "fail"), + Case(Match.Pattern2.of(ClzMatch2.class, $(), $(), t -> Tuple.of(null, null)), "okMatch") + ); + assertThat(match).isEqualTo("okMatch"); } - } - // -- Match patterns - - static class ClzMatch {} - static class ClzMatch1 extends ClzMatch {} - static class ClzMatch2 extends ClzMatch {} - - @Test - public void shouldMatchPattern1() { - final Tuple1 tuple = Tuple.of(1); - final String func = Match(tuple).of( - Case($Tuple1($(0)), (m1) -> "fail"), - Case($Tuple1($()), (m1) -> "okFunc") - ); - assertThat(func).isEqualTo("okFunc"); - final String supp = Match(tuple).of( - Case($Tuple1($(0)), () -> "fail"), - Case($Tuple1($()), () -> "okSupp") - ); - assertThat(supp).isEqualTo("okSupp"); - final String val = Match(tuple).of( - Case($Tuple1($(0)), "fail"), - Case($Tuple1($()), "okVal") - ); - assertThat(val).isEqualTo("okVal"); - - final ClzMatch c = new ClzMatch2(); - final String match = Match(c).of( - Case(Match.Pattern1.of(ClzMatch1.class, $(), t -> Tuple.of(null)), "fail"), - Case(Match.Pattern1.of(ClzMatch2.class, $(), t -> Tuple.of(null)), "okMatch") - ); - assertThat(match).isEqualTo("okMatch"); - } - - @Test - public void shouldMatchPattern2() { - final Tuple2 tuple = Tuple.of(1, 1); - final String func = Match(tuple).of( - Case($Tuple2($(0), $()), (m1, m2) -> "fail"), - Case($Tuple2($(), $()), (m1, m2) -> "okFunc") - ); - assertThat(func).isEqualTo("okFunc"); - final String supp = Match(tuple).of( - Case($Tuple2($(0), $()), () -> "fail"), - Case($Tuple2($(), $()), () -> "okSupp") - ); - assertThat(supp).isEqualTo("okSupp"); - final String val = Match(tuple).of( - Case($Tuple2($(0), $()), "fail"), - Case($Tuple2($(), $()), "okVal") - ); - assertThat(val).isEqualTo("okVal"); - - final ClzMatch c = new ClzMatch2(); - final String match = Match(c).of( - Case(Match.Pattern2.of(ClzMatch1.class, $(), $(), t -> Tuple.of(null, null)), "fail"), - Case(Match.Pattern2.of(ClzMatch2.class, $(), $(), t -> Tuple.of(null, null)), "okMatch") - ); - assertThat(match).isEqualTo("okMatch"); - } - - @Test - public void shouldMatchPattern3() { - final Tuple3 tuple = Tuple.of(1, 1, 1); - final String func = Match(tuple).of( - Case($Tuple3($(0), $(), $()), (m1, m2, m3) -> "fail"), - Case($Tuple3($(), $(), $()), (m1, m2, m3) -> "okFunc") - ); - assertThat(func).isEqualTo("okFunc"); - final String supp = Match(tuple).of( - Case($Tuple3($(0), $(), $()), () -> "fail"), - Case($Tuple3($(), $(), $()), () -> "okSupp") - ); - assertThat(supp).isEqualTo("okSupp"); - final String val = Match(tuple).of( - Case($Tuple3($(0), $(), $()), "fail"), - Case($Tuple3($(), $(), $()), "okVal") - ); - assertThat(val).isEqualTo("okVal"); - - final ClzMatch c = new ClzMatch2(); - final String match = Match(c).of( - Case(Match.Pattern3.of(ClzMatch1.class, $(), $(), $(), t -> Tuple.of(null, null, null)), "fail"), - Case(Match.Pattern3.of(ClzMatch2.class, $(), $(), $(), t -> Tuple.of(null, null, null)), "okMatch") - ); - assertThat(match).isEqualTo("okMatch"); - } - - @Test - public void shouldMatchPattern4() { - final Tuple4 tuple = Tuple.of(1, 1, 1, 1); - final String func = Match(tuple).of( - Case($Tuple4($(0), $(), $(), $()), (m1, m2, m3, m4) -> "fail"), - Case($Tuple4($(), $(), $(), $()), (m1, m2, m3, m4) -> "okFunc") - ); - assertThat(func).isEqualTo("okFunc"); - final String supp = Match(tuple).of( - Case($Tuple4($(0), $(), $(), $()), () -> "fail"), - Case($Tuple4($(), $(), $(), $()), () -> "okSupp") - ); - assertThat(supp).isEqualTo("okSupp"); - final String val = Match(tuple).of( - Case($Tuple4($(0), $(), $(), $()), "fail"), - Case($Tuple4($(), $(), $(), $()), "okVal") - ); - assertThat(val).isEqualTo("okVal"); - - final ClzMatch c = new ClzMatch2(); - final String match = Match(c).of( - Case(Match.Pattern4.of(ClzMatch1.class, $(), $(), $(), $(), t -> Tuple.of(null, null, null, null)), "fail"), - Case(Match.Pattern4.of(ClzMatch2.class, $(), $(), $(), $(), t -> Tuple.of(null, null, null, null)), "okMatch") - ); - assertThat(match).isEqualTo("okMatch"); - } - - @Test - public void shouldMatchPattern5() { - final Tuple5 tuple = Tuple.of(1, 1, 1, 1, 1); - final String func = Match(tuple).of( - Case($Tuple5($(0), $(), $(), $(), $()), (m1, m2, m3, m4, m5) -> "fail"), - Case($Tuple5($(), $(), $(), $(), $()), (m1, m2, m3, m4, m5) -> "okFunc") - ); - assertThat(func).isEqualTo("okFunc"); - final String supp = Match(tuple).of( - Case($Tuple5($(0), $(), $(), $(), $()), () -> "fail"), - Case($Tuple5($(), $(), $(), $(), $()), () -> "okSupp") - ); - assertThat(supp).isEqualTo("okSupp"); - final String val = Match(tuple).of( - Case($Tuple5($(0), $(), $(), $(), $()), "fail"), - Case($Tuple5($(), $(), $(), $(), $()), "okVal") - ); - assertThat(val).isEqualTo("okVal"); - - final ClzMatch c = new ClzMatch2(); - final String match = Match(c).of( - Case(Match.Pattern5.of(ClzMatch1.class, $(), $(), $(), $(), $(), t -> Tuple.of(null, null, null, null, null)), "fail"), - Case(Match.Pattern5.of(ClzMatch2.class, $(), $(), $(), $(), $(), t -> Tuple.of(null, null, null, null, null)), "okMatch") - ); - assertThat(match).isEqualTo("okMatch"); - } - - @Test - public void shouldMatchPattern6() { - final Tuple6 tuple = Tuple.of(1, 1, 1, 1, 1, 1); - final String func = Match(tuple).of( - Case($Tuple6($(0), $(), $(), $(), $(), $()), (m1, m2, m3, m4, m5, m6) -> "fail"), - Case($Tuple6($(), $(), $(), $(), $(), $()), (m1, m2, m3, m4, m5, m6) -> "okFunc") - ); - assertThat(func).isEqualTo("okFunc"); - final String supp = Match(tuple).of( - Case($Tuple6($(0), $(), $(), $(), $(), $()), () -> "fail"), - Case($Tuple6($(), $(), $(), $(), $(), $()), () -> "okSupp") - ); - assertThat(supp).isEqualTo("okSupp"); - final String val = Match(tuple).of( - Case($Tuple6($(0), $(), $(), $(), $(), $()), "fail"), - Case($Tuple6($(), $(), $(), $(), $(), $()), "okVal") - ); - assertThat(val).isEqualTo("okVal"); - - final ClzMatch c = new ClzMatch2(); - final String match = Match(c).of( - Case(Match.Pattern6.of(ClzMatch1.class, $(), $(), $(), $(), $(), $(), t -> Tuple.of(null, null, null, null, null, null)), "fail"), - Case(Match.Pattern6.of(ClzMatch2.class, $(), $(), $(), $(), $(), $(), t -> Tuple.of(null, null, null, null, null, null)), "okMatch") - ); - assertThat(match).isEqualTo("okMatch"); - } - - @Test - public void shouldMatchPattern7() { - final Tuple7 tuple = Tuple.of(1, 1, 1, 1, 1, 1, 1); - final String func = Match(tuple).of( - Case($Tuple7($(0), $(), $(), $(), $(), $(), $()), (m1, m2, m3, m4, m5, m6, m7) -> "fail"), - Case($Tuple7($(), $(), $(), $(), $(), $(), $()), (m1, m2, m3, m4, m5, m6, m7) -> "okFunc") - ); - assertThat(func).isEqualTo("okFunc"); - final String supp = Match(tuple).of( - Case($Tuple7($(0), $(), $(), $(), $(), $(), $()), () -> "fail"), - Case($Tuple7($(), $(), $(), $(), $(), $(), $()), () -> "okSupp") - ); - assertThat(supp).isEqualTo("okSupp"); - final String val = Match(tuple).of( - Case($Tuple7($(0), $(), $(), $(), $(), $(), $()), "fail"), - Case($Tuple7($(), $(), $(), $(), $(), $(), $()), "okVal") - ); - assertThat(val).isEqualTo("okVal"); - - final ClzMatch c = new ClzMatch2(); - final String match = Match(c).of( - Case(Match.Pattern7.of(ClzMatch1.class, $(), $(), $(), $(), $(), $(), $(), t -> Tuple.of(null, null, null, null, null, null, null)), "fail"), - Case(Match.Pattern7.of(ClzMatch2.class, $(), $(), $(), $(), $(), $(), $(), t -> Tuple.of(null, null, null, null, null, null, null)), "okMatch") - ); - assertThat(match).isEqualTo("okMatch"); - } - - @Test - public void shouldMatchPattern8() { - final Tuple8 tuple = Tuple.of(1, 1, 1, 1, 1, 1, 1, 1); - final String func = Match(tuple).of( - Case($Tuple8($(0), $(), $(), $(), $(), $(), $(), $()), (m1, m2, m3, m4, m5, m6, m7, m8) -> "fail"), - Case($Tuple8($(), $(), $(), $(), $(), $(), $(), $()), (m1, m2, m3, m4, m5, m6, m7, m8) -> "okFunc") - ); - assertThat(func).isEqualTo("okFunc"); - final String supp = Match(tuple).of( - Case($Tuple8($(0), $(), $(), $(), $(), $(), $(), $()), () -> "fail"), - Case($Tuple8($(), $(), $(), $(), $(), $(), $(), $()), () -> "okSupp") - ); - assertThat(supp).isEqualTo("okSupp"); - final String val = Match(tuple).of( - Case($Tuple8($(0), $(), $(), $(), $(), $(), $(), $()), "fail"), - Case($Tuple8($(), $(), $(), $(), $(), $(), $(), $()), "okVal") - ); - assertThat(val).isEqualTo("okVal"); - - final ClzMatch c = new ClzMatch2(); - final String match = Match(c).of( - Case(Match.Pattern8.of(ClzMatch1.class, $(), $(), $(), $(), $(), $(), $(), $(), t -> Tuple.of(null, null, null, null, null, null, null, null)), "fail"), - Case(Match.Pattern8.of(ClzMatch2.class, $(), $(), $(), $(), $(), $(), $(), $(), t -> Tuple.of(null, null, null, null, null, null, null, null)), "okMatch") - ); - assertThat(match).isEqualTo("okMatch"); + @Test + public void shouldMatchPattern3() { + final Tuple3 tuple = Tuple.of(1, 1, 1); + final String func = Match(tuple).of( + Case($Tuple3($(0), $(), $()), (m1, m2, m3) -> "fail"), + Case($Tuple3($(), $(), $()), (m1, m2, m3) -> "okFunc") + ); + assertThat(func).isEqualTo("okFunc"); + final String supp = Match(tuple).of( + Case($Tuple3($(0), $(), $()), () -> "fail"), + Case($Tuple3($(), $(), $()), () -> "okSupp") + ); + assertThat(supp).isEqualTo("okSupp"); + final String val = Match(tuple).of( + Case($Tuple3($(0), $(), $()), "fail"), + Case($Tuple3($(), $(), $()), "okVal") + ); + assertThat(val).isEqualTo("okVal"); + + final ClzMatch c = new ClzMatch2(); + final String match = Match(c).of( + Case(Match.Pattern3.of(ClzMatch1.class, $(), $(), $(), t -> Tuple.of(null, null, null)), "fail"), + Case(Match.Pattern3.of(ClzMatch2.class, $(), $(), $(), t -> Tuple.of(null, null, null)), "okMatch") + ); + assertThat(match).isEqualTo("okMatch"); + } + + @Test + public void shouldMatchPattern4() { + final Tuple4 tuple = Tuple.of(1, 1, 1, 1); + final String func = Match(tuple).of( + Case($Tuple4($(0), $(), $(), $()), (m1, m2, m3, m4) -> "fail"), + Case($Tuple4($(), $(), $(), $()), (m1, m2, m3, m4) -> "okFunc") + ); + assertThat(func).isEqualTo("okFunc"); + final String supp = Match(tuple).of( + Case($Tuple4($(0), $(), $(), $()), () -> "fail"), + Case($Tuple4($(), $(), $(), $()), () -> "okSupp") + ); + assertThat(supp).isEqualTo("okSupp"); + final String val = Match(tuple).of( + Case($Tuple4($(0), $(), $(), $()), "fail"), + Case($Tuple4($(), $(), $(), $()), "okVal") + ); + assertThat(val).isEqualTo("okVal"); + + final ClzMatch c = new ClzMatch2(); + final String match = Match(c).of( + Case(Match.Pattern4.of(ClzMatch1.class, $(), $(), $(), $(), t -> Tuple.of(null, null, null, null)), "fail"), + Case(Match.Pattern4.of(ClzMatch2.class, $(), $(), $(), $(), t -> Tuple.of(null, null, null, null)), "okMatch") + ); + assertThat(match).isEqualTo("okMatch"); + } + + @Test + public void shouldMatchPattern5() { + final Tuple5 tuple = Tuple.of(1, 1, 1, 1, 1); + final String func = Match(tuple).of( + Case($Tuple5($(0), $(), $(), $(), $()), (m1, m2, m3, m4, m5) -> "fail"), + Case($Tuple5($(), $(), $(), $(), $()), (m1, m2, m3, m4, m5) -> "okFunc") + ); + assertThat(func).isEqualTo("okFunc"); + final String supp = Match(tuple).of( + Case($Tuple5($(0), $(), $(), $(), $()), () -> "fail"), + Case($Tuple5($(), $(), $(), $(), $()), () -> "okSupp") + ); + assertThat(supp).isEqualTo("okSupp"); + final String val = Match(tuple).of( + Case($Tuple5($(0), $(), $(), $(), $()), "fail"), + Case($Tuple5($(), $(), $(), $(), $()), "okVal") + ); + assertThat(val).isEqualTo("okVal"); + + final ClzMatch c = new ClzMatch2(); + final String match = Match(c).of( + Case(Match.Pattern5.of(ClzMatch1.class, $(), $(), $(), $(), $(), t -> Tuple.of(null, null, null, null, null)), "fail"), + Case(Match.Pattern5.of(ClzMatch2.class, $(), $(), $(), $(), $(), t -> Tuple.of(null, null, null, null, null)), "okMatch") + ); + assertThat(match).isEqualTo("okMatch"); + } + + @Test + public void shouldMatchPattern6() { + final Tuple6 tuple = Tuple.of(1, 1, 1, 1, 1, 1); + final String func = Match(tuple).of( + Case($Tuple6($(0), $(), $(), $(), $(), $()), (m1, m2, m3, m4, m5, m6) -> "fail"), + Case($Tuple6($(), $(), $(), $(), $(), $()), (m1, m2, m3, m4, m5, m6) -> "okFunc") + ); + assertThat(func).isEqualTo("okFunc"); + final String supp = Match(tuple).of( + Case($Tuple6($(0), $(), $(), $(), $(), $()), () -> "fail"), + Case($Tuple6($(), $(), $(), $(), $(), $()), () -> "okSupp") + ); + assertThat(supp).isEqualTo("okSupp"); + final String val = Match(tuple).of( + Case($Tuple6($(0), $(), $(), $(), $(), $()), "fail"), + Case($Tuple6($(), $(), $(), $(), $(), $()), "okVal") + ); + assertThat(val).isEqualTo("okVal"); + + final ClzMatch c = new ClzMatch2(); + final String match = Match(c).of( + Case(Match.Pattern6.of(ClzMatch1.class, $(), $(), $(), $(), $(), $(), t -> Tuple.of(null, null, null, null, null, null)), "fail"), + Case(Match.Pattern6.of(ClzMatch2.class, $(), $(), $(), $(), $(), $(), t -> Tuple.of(null, null, null, null, null, null)), "okMatch") + ); + assertThat(match).isEqualTo("okMatch"); + } + + @Test + public void shouldMatchPattern7() { + final Tuple7 tuple = Tuple.of(1, 1, 1, 1, 1, 1, 1); + final String func = Match(tuple).of( + Case($Tuple7($(0), $(), $(), $(), $(), $(), $()), (m1, m2, m3, m4, m5, m6, m7) -> "fail"), + Case($Tuple7($(), $(), $(), $(), $(), $(), $()), (m1, m2, m3, m4, m5, m6, m7) -> "okFunc") + ); + assertThat(func).isEqualTo("okFunc"); + final String supp = Match(tuple).of( + Case($Tuple7($(0), $(), $(), $(), $(), $(), $()), () -> "fail"), + Case($Tuple7($(), $(), $(), $(), $(), $(), $()), () -> "okSupp") + ); + assertThat(supp).isEqualTo("okSupp"); + final String val = Match(tuple).of( + Case($Tuple7($(0), $(), $(), $(), $(), $(), $()), "fail"), + Case($Tuple7($(), $(), $(), $(), $(), $(), $()), "okVal") + ); + assertThat(val).isEqualTo("okVal"); + + final ClzMatch c = new ClzMatch2(); + final String match = Match(c).of( + Case(Match.Pattern7.of(ClzMatch1.class, $(), $(), $(), $(), $(), $(), $(), t -> Tuple.of(null, null, null, null, null, null, null)), "fail"), + Case(Match.Pattern7.of(ClzMatch2.class, $(), $(), $(), $(), $(), $(), $(), t -> Tuple.of(null, null, null, null, null, null, null)), "okMatch") + ); + assertThat(match).isEqualTo("okMatch"); + } + + @Test + public void shouldMatchPattern8() { + final Tuple8 tuple = Tuple.of(1, 1, 1, 1, 1, 1, 1, 1); + final String func = Match(tuple).of( + Case($Tuple8($(0), $(), $(), $(), $(), $(), $(), $()), (m1, m2, m3, m4, m5, m6, m7, m8) -> "fail"), + Case($Tuple8($(), $(), $(), $(), $(), $(), $(), $()), (m1, m2, m3, m4, m5, m6, m7, m8) -> "okFunc") + ); + assertThat(func).isEqualTo("okFunc"); + final String supp = Match(tuple).of( + Case($Tuple8($(0), $(), $(), $(), $(), $(), $(), $()), () -> "fail"), + Case($Tuple8($(), $(), $(), $(), $(), $(), $(), $()), () -> "okSupp") + ); + assertThat(supp).isEqualTo("okSupp"); + final String val = Match(tuple).of( + Case($Tuple8($(0), $(), $(), $(), $(), $(), $(), $()), "fail"), + Case($Tuple8($(), $(), $(), $(), $(), $(), $(), $()), "okVal") + ); + assertThat(val).isEqualTo("okVal"); + + final ClzMatch c = new ClzMatch2(); + final String match = Match(c).of( + Case(Match.Pattern8.of(ClzMatch1.class, $(), $(), $(), $(), $(), $(), $(), $(), t -> Tuple.of(null, null, null, null, null, null, null, null)), "fail"), + Case(Match.Pattern8.of(ClzMatch2.class, $(), $(), $(), $(), $(), $(), $(), $(), t -> Tuple.of(null, null, null, null, null, null, null, null)), "okMatch") + ); + assertThat(match).isEqualTo("okMatch"); + } } } \ No newline at end of file From a074c5de61dc408699bd2269bce336a7b0e073cb Mon Sep 17 00:00:00 2001 From: Boris van Katwijk Date: Wed, 1 Jan 2025 16:19:32 +0100 Subject: [PATCH 46/61] Update ValidationTest.java --- .../java/io/vavr/control/ValidationTest.java | 1007 +++++++++-------- 1 file changed, 550 insertions(+), 457 deletions(-) diff --git a/src/test/java/io/vavr/control/ValidationTest.java b/src/test/java/io/vavr/control/ValidationTest.java index 131db4d399..3b7107cb06 100644 --- a/src/test/java/io/vavr/control/ValidationTest.java +++ b/src/test/java/io/vavr/control/ValidationTest.java @@ -31,6 +31,8 @@ import io.vavr.collection.List; import io.vavr.collection.Seq; import io.vavr.collection.Traversable; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import java.util.ArrayList; @@ -38,6 +40,7 @@ import java.util.Spliterator; import java.util.function.Function; +import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertThrows; public class ValidationTest extends AbstractValueTest { @@ -45,8 +48,6 @@ public class ValidationTest extends AbstractValueTest { private static final String OK = "ok"; private static final List ERRORS = List.of("error1", "error2", "error3"); - // -- AbstractValueTest - @Override protected Validation empty() { return Validation.invalid("empty"); @@ -73,545 +74,631 @@ protected int getPeekNonNilPerformingAnAction() { return 1; } - // -- Validation.valid - @Test - public void shouldCreateSuccessWhenCallingValidationSuccess() { - assertThat(Validation.valid(1) instanceof Validation.Valid).isTrue(); - } + @Nested + @DisplayName("valid") + class ValidTest { - // -- Validation.invalid + @Test + public void shouldCreateSuccessWhenCallingValidationSuccess() { + assertThat(Validation.valid(1) instanceof Validation.Valid).isTrue(); + } - @Test - public void shouldCreateFailureWhenCallingValidationFailure() { - assertThat(Validation.invalid("error") instanceof Validation.Invalid).isTrue(); } - // -- Validation.fromEither + @Nested + @DisplayName("invalid") + class InvalidTest { - @Test - public void shouldCreateFromRightEither() { - Validation validation = Validation.fromEither(Either.right(42)); - assertThat(validation.isValid()).isTrue(); - assertThat(validation.get()).isEqualTo(42); - } + @Test + public void shouldCreateFailureWhenCallingValidationFailure() { + assertThat(Validation.invalid("error") instanceof Validation.Invalid).isTrue(); + } - @Test - public void shouldCreateFromLeftEither() { - Validation validation = Validation.fromEither(Either.left("vavr")); - assertThat(validation.isValid()).isFalse(); - assertThat(validation.getError()).isEqualTo("vavr"); } - // -- Validation.fromTry + @Nested + @DisplayName("fromEither") + class FromEitherTest { - @Test - public void shouldCreateFromSuccessTry() { - Validation validation = Validation.fromTry(Try.success(42)); - assertThat(validation.isValid()).isTrue(); - assertThat(validation.get()).isEqualTo(42); - } + @Test + public void shouldCreateFromRightEither() { + Validation validation = Validation.fromEither(Either.right(42)); + assertThat(validation.isValid()).isTrue(); + assertThat(validation.get()).isEqualTo(42); + } + + @Test + public void shouldCreateFromLeftEither() { + Validation validation = Validation.fromEither(Either.left("vavr")); + assertThat(validation.isValid()).isFalse(); + assertThat(validation.getError()).isEqualTo("vavr"); + } - @Test - public void shouldCreateFromFailureTry() { - Throwable throwable = new Throwable("vavr"); - Validation validation = Validation.fromTry(Try.failure(throwable)); - assertThat(validation.isValid()).isFalse(); - assertThat(validation.getError()).isEqualTo(throwable); } + @Nested + @DisplayName("fromTry") + class FromTryTest { - // -- Validation.narrow + @Test + public void shouldCreateFromSuccessTry() { + Validation validation = Validation.fromTry(Try.success(42)); + assertThat(validation.isValid()).isTrue(); + assertThat(validation.get()).isEqualTo(42); + } + + @Test + public void shouldCreateFromFailureTry() { + Throwable throwable = new Throwable("vavr"); + Validation validation = Validation.fromTry(Try.failure(throwable)); + assertThat(validation.isValid()).isFalse(); + assertThat(validation.getError()).isEqualTo(throwable); + } - @Test - public void shouldNarrowValid() { - Validation validation = Validation.valid(42); - Validation narrow = Validation.narrow(validation); - assertThat(narrow.get()).isEqualTo(42); - } - @Test - public void shouldNarrowInvalid() { - Validation validation = Validation.invalid("vavr"); - Validation narrow = Validation.narrow(validation); - assertThat(narrow.getError()).isEqualTo("vavr"); } - // -- Validation.sequence + @Nested + @DisplayName("narrow") + class NarrowTest { - @Test - public void shouldThrowWhenSequencingNull() { - assertThrows(NullPointerException.class, () -> Validation.sequence(null)); - } + @Test + public void shouldNarrowValid() { + Validation validation = Validation.valid(42); + Validation narrow = Validation.narrow(validation); + assertThat(narrow.get()).isEqualTo(42); + } - @Test - public void shouldCreateValidWhenSequencingValids() { - final Validation, Seq> actual = Validation.sequence(List.of( - Validation.valid(1), - Validation.valid(2) - )); - assertThat(actual).isEqualTo(Validation.valid(List.of(1, 2))); - } + @Test + public void shouldNarrowInvalid() { + Validation validation = Validation.invalid("vavr"); + Validation narrow = Validation.narrow(validation); + assertThat(narrow.getError()).isEqualTo("vavr"); + } - @Test - public void shouldCreateInvalidWhenSequencingAnInvalid() { - final Validation, Seq> actual = Validation.sequence(List.of( - Validation.valid(1), - Validation.invalid(List.of("error1", "error2")), - Validation.valid(2), - Validation.invalid(List.of("error3", "error4")) - )); - assertThat(actual).isEqualTo(Validation.invalid(List.of("error1", "error2", "error3", "error4"))); } - // -- Validation.all + @Nested + @DisplayName("sequence") + class SequenceTest { - @Test - public void shouldThrowWhenAllCombiningNull() { - assertThrows(NullPointerException.class, () -> Validation.all((Traversable>) null)); - } + @Test + public void shouldThrowWhenSequencingNull() { + assertThrows(NullPointerException.class, () -> Validation.sequence(null)); + } - @Test - public void shouldCreateValidWhenAllCombiningValids() { - final Validation, Integer> actual = Validation.all(List.of( - Validation.valid(1), - Validation.valid(2) - )); - assertThat(actual).isEqualTo(Validation.valid(List.of(1, 2).last())); - } + @Test + public void shouldCreateValidWhenSequencingValids() { + final Validation, Seq> actual = Validation.sequence(List.of( + Validation.valid(1), + Validation.valid(2) + )); + assertThat(actual).isEqualTo(Validation.valid(List.of(1, 2))); + } - @Test - public void shouldCreateInvalidWhenAllCombiningInvalid() { - final Validation, Integer> actual = Validation.all(List.of( - Validation.valid(1), - Validation.invalid("error1"), - Validation.valid(2), - Validation.invalid("error2") - )); - assertThat(actual).isEqualTo(Validation.invalid(List.of("error1", "error2"))); - } + @Test + public void shouldCreateInvalidWhenSequencingAnInvalid() { + final Validation, Seq> actual = Validation.sequence(List.of( + Validation.valid(1), + Validation.invalid(List.of("error1", "error2")), + Validation.valid(2), + Validation.invalid(List.of("error3", "error4")) + )); + assertThat(actual).isEqualTo(Validation.invalid(List.of("error1", "error2", "error3", "error4"))); + } - @Test - public void shouldThrowWhenAllCombiningVargNull() { - assertThrows(NullPointerException.class, () -> Validation.all((Validation) null)); } - @Test - public void shouldCreateValidWhenAllCombiningVargValids() { - final Validation, Integer> actual = Validation.all( - Validation.valid(1), - Validation.valid(2) - ); - assertThat(actual).isEqualTo(Validation.valid(List.of(1, 2).last())); - } + @Nested + @DisplayName("all") + class AllTest { - @Test - public void shouldCreateInvalidWhenAllCombiningVargInvalid() { - Validation, Integer> actual = Validation.all( - Validation.valid(1), - Validation.invalid("error2"), - Validation.valid(3), - Validation.invalid("error4") - ); - assertThat(actual).isEqualTo(Validation.invalid(List.of("error2", "error4"))); - } + @Test + public void shouldThrowWhenAllCombiningNull() { + assertThrows(NullPointerException.class, () -> Validation.all((Traversable>) null)); + } - // -- Validation.traverse + @Test + public void shouldCreateValidWhenAllCombiningValids() { + final Validation, Integer> actual = Validation.all(List.of( + Validation.valid(1), + Validation.valid(2) + )); + assertThat(actual).isEqualTo(Validation.valid(List.of(1, 2).last())); + } - @Test - public void shouldThrowWhenTraversingNull() { - assertThrows(NullPointerException.class, () -> Validation.traverse(null, null)); - } + @Test + public void shouldCreateInvalidWhenAllCombiningInvalid() { + final Validation, Integer> actual = Validation.all(List.of( + Validation.valid(1), + Validation.invalid("error1"), + Validation.valid(2), + Validation.invalid("error2") + )); + assertThat(actual).isEqualTo(Validation.invalid(List.of("error1", "error2"))); + } - @Test - public void shouldCreateValidWhenTraversingValids() { - final Validation, Seq> actual = - Validation.traverse(List.of(1, 2), t -> Validation.valid(t)); // NOTE: Compilation error with Java 8 if we use a method reference - assertThat(actual).isEqualTo(Validation.valid(List.of(1, 2))); - } + @Test + public void shouldThrowWhenAllCombiningVargNull() { + assertThrows(NullPointerException.class, () -> Validation.all((Validation) null)); + } - @Test - public void shouldCreateInvalidWhenTraversingAnInvalid() { - final Validation, Seq> actual = - Validation.traverse( - List.of(1, -1, 2, -2), - x -> x >= 0 - ? Validation.valid(x) - : Validation.invalid(List.of("error" + x, "error" + (x+1)))); - assertThat(actual).isEqualTo(Validation.invalid(List.of("error-1", "error0", "error-2", "error-1"))); - } + @Test + public void shouldCreateValidWhenAllCombiningVargValids() { + final Validation, Integer> actual = Validation.all( + Validation.valid(1), + Validation.valid(2) + ); + assertThat(actual).isEqualTo(Validation.valid(List.of(1, 2).last())); + } - // -- toEither + @Test + public void shouldCreateInvalidWhenAllCombiningVargInvalid() { + Validation, Integer> actual = Validation.all( + Validation.valid(1), + Validation.invalid("error2"), + Validation.valid(3), + Validation.invalid("error4") + ); + assertThat(actual).isEqualTo(Validation.invalid(List.of("error2", "error4"))); + } - @Test - public void shouldConvertToRightEither() { - Either either = Validation.valid(42).toEither(); - assertThat(either.isRight()).isTrue(); - assertThat(either.get()).isEqualTo(42); } - @Test - public void shouldConvertToLeftEither() { - Either either = Validation.invalid("vavr").toEither(); - assertThat(either.isLeft()).isTrue(); - assertThat(either.getLeft()).isEqualTo("vavr"); - } + @Nested + @DisplayName("traverse") + class TraverseTest { - // -- filter + @Test + public void shouldThrowWhenTraversingNull() { + assertThrows(NullPointerException.class, () -> Validation.traverse(null, null)); + } - @Test - public void shouldFilterValid() { - Validation valid = Validation.valid(42); - assertThat(valid.filter(i -> true).get()).isSameAs(valid); - assertThat(valid.filter(i -> false)).isSameAs(Option.none()); - } + @Test + public void shouldCreateValidWhenTraversingValids() { + final Validation, Seq> actual = + Validation.traverse(List.of(1, 2), t -> Validation.valid(t)); // NOTE: Compilation error with Java 8 if we use a method reference + assertThat(actual).isEqualTo(Validation.valid(List.of(1, 2))); + } + + @Test + public void shouldCreateInvalidWhenTraversingAnInvalid() { + final Validation, Seq> actual = + Validation.traverse( + List.of(1, -1, 2, -2), + x -> x >= 0 + ? Validation.valid(x) + : Validation.invalid(List.of("error" + x, "error" + (x + 1)))); + assertThat(actual).isEqualTo(Validation.invalid(List.of("error-1", "error0", "error-2", "error-1"))); + } - @Test - public void shouldFilterInvalid() { - Validation invalid = Validation.invalid("vavr"); - assertThat(invalid.filter(i -> true).get()).isSameAs(invalid); - assertThat(invalid.filter(i -> false).get()).isSameAs(invalid); } - // -- flatMap + @Nested + @DisplayName("toEither") + class ToEitherTest { - @Test - public void shouldFlatMapValid() { - Validation valid = Validation.valid(42); - assertThat(valid.flatMap(v -> Validation.valid("ok")).get()).isEqualTo("ok"); - } + @Test + public void shouldConvertToRightEither() { + Either either = Validation.valid(42).toEither(); + assertThat(either.isRight()).isTrue(); + assertThat(either.get()).isEqualTo(42); + } + + @Test + public void shouldConvertToLeftEither() { + Either either = Validation.invalid("vavr").toEither(); + assertThat(either.isLeft()).isTrue(); + assertThat(either.getLeft()).isEqualTo("vavr"); + } - @Test - public void shouldFlatMapInvalid() { - Validation invalid = Validation.invalid("vavr"); - assertThat(invalid.flatMap(v -> Validation.valid("ok"))).isSameAs(invalid); } - // -- peekError + @Nested + @DisplayName("filter") + class FilterTest { - @Test - public void shouldPeekErrorNil() { - assertThat(empty().peekError(t -> {})).isEqualTo(empty()); - } + @Test + public void shouldFilterValid() { + Validation valid = Validation.valid(42); + assertThat(valid.filter(i -> true).get()).isSameAs(valid); + assertThat(valid.filter(i -> false)).isSameAs(Option.none()); + } - @Test - public void shouldPeekErrorForInvalid() { - final int[] effect = { 0 }; - final Validation actual = Validation.invalid(1).peekError(i -> effect[0] = i); - assertThat(actual).isEqualTo(Validation.invalid(1)); - assertThat(effect[0]).isEqualTo(1); - } + @Test + public void shouldFilterInvalid() { + Validation invalid = Validation.invalid("vavr"); + assertThat(invalid.filter(i -> true).get()).isSameAs(invalid); + assertThat(invalid.filter(i -> false).get()).isSameAs(invalid); + } - @Test - public void shouldNotPeekErrorForValid() { - Validation.valid(1).peekError(i -> { throw new IllegalStateException(); }); } - // -- orElse + @Nested + @DisplayName("flatMap") + class FlatMapTest { - @Test - public void shouldReturnSelfOnOrElseIfValid() { - Validation, String> validValidation = valid(); - assertThat(validValidation.orElse(invalid())).isSameAs(validValidation); - } + @Test + public void shouldFlatMapValid() { + Validation valid = Validation.valid(42); + assertThat(valid.flatMap(v -> Validation.valid("ok")).get()).isEqualTo("ok"); + } - @Test - public void shouldReturnSelfOnOrElseSupplierIfValid() { - Validation, String> validValidation = valid(); - assertThat(validValidation.orElse(this::invalid)).isSameAs(validValidation); - } + @Test + public void shouldFlatMapInvalid() { + Validation invalid = Validation.invalid("vavr"); + assertThat(invalid.flatMap(v -> Validation.valid("ok"))).isSameAs(invalid); + } - @Test - public void shouldReturnAlternativeOnOrElseIfValid() { - Validation, String> validValidation = valid(); - assertThat(invalid().orElse(validValidation)).isSameAs(validValidation); } - @Test - public void shouldReturnAlternativeOnOrElseSupplierIfValid() { - Validation, String> validValidation = valid(); - assertThat(invalid().orElse(() -> validValidation)).isSameAs(validValidation); - } + @Nested + @DisplayName("peekError") + class PeekErrorTest { - // -- getOrElseGet + @Test + public void shouldPeekErrorNil() { + assertThat(empty().peekError(t -> { + })).isEqualTo(empty()); + } - @Test - public void shouldReturnValueOnGetOrElseGetIfValid() { - Validation validValidation = valid(); - assertThat(validValidation.getOrElseGet(e -> "error" + e)).isEqualTo(OK); - } + @Test + public void shouldPeekErrorForInvalid() { + final int[] effect = {0}; + final Validation actual = Validation.invalid(1).peekError(i -> effect[0] = i); + assertThat(actual).isEqualTo(Validation.invalid(1)); + assertThat(effect[0]).isEqualTo(1); + } + + @Test + public void shouldNotPeekErrorForValid() { + Validation.valid(1).peekError(i -> { + throw new IllegalStateException(); + }); + } - @Test - public void shouldReturnCalculationOnGetOrElseGetIfInvalid() { - Validation invalidValidation = Validation.invalid(42); - assertThat(invalidValidation.getOrElseGet(e -> "error" + e)).isEqualTo("error42"); } - // -- fold + @Nested + @DisplayName("orElse") + class OrElseTest { - @Test - public void shouldConvertSuccessToU() { - Validation, String> validValidation = valid(); - Integer result = validValidation.fold(Seq::length, String::length); - assertThat(result).isEqualTo(2); - } + @Test + public void shouldReturnSelfOnOrElseIfValid() { + Validation, String> validValidation = valid(); + assertThat(validValidation.orElse(invalid())).isSameAs(validValidation); + } - @Test - public void shouldConvertFailureToU() { - Validation, String> invalidValidation = invalid(); - Integer result = invalidValidation.fold(Seq::length, String::length); - assertThat(result).isEqualTo(3); - } + @Test + public void shouldReturnSelfOnOrElseSupplierIfValid() { + Validation, String> validValidation = valid(); + assertThat(validValidation.orElse(ValidationTest.this::invalid)).isSameAs(validValidation); + } + @Test + public void shouldReturnAlternativeOnOrElseIfValid() { + Validation, String> validValidation = valid(); + assertThat(invalid().orElse(validValidation)).isSameAs(validValidation); + } - // getOrElseThrow(Function) + @Test + public void shouldReturnAlternativeOnOrElseSupplierIfValid() { + Validation, String> validValidation = valid(); + assertThat(invalid().orElse(() -> validValidation)).isSameAs(validValidation); + } - @Test - public void shouldReturnValidWhenGetOrElseThrowWithFunctionOnValid() { - final Integer actual = Validation. valid(1).getOrElseThrow(s -> new RuntimeException(s)); - assertThat(actual).isEqualTo(1); } - @Test - public void shouldThrowWhenGetOrElseThrowWithFunctionOnInvalid() { - assertThrows(RuntimeException.class, () -> Validation.invalid("some error").getOrElseThrow(s -> new RuntimeException(s))); - } + @Nested + @DisplayName("getOrElseGet") + class GetOrElseGetTest { - @Test - public void shouldThrowNullPointerExceptionWhenGetOrElseThrowNullWithFunctionOnValid() { - assertThrows(NullPointerException.class, () -> Validation.valid(1).getOrElseThrow((Function) null)); - } + @Test + public void shouldReturnValueOnGetOrElseGetIfValid() { + Validation validValidation = valid(); + assertThat(validValidation.getOrElseGet(e -> "error" + e)).isEqualTo(OK); + } + + @Test + public void shouldReturnCalculationOnGetOrElseGetIfInvalid() { + Validation invalidValidation = Validation.invalid(42); + assertThat(invalidValidation.getOrElseGet(e -> "error" + e)).isEqualTo("error42"); + } - @Test - public void shouldThrowNullPointerExceptionWhenGetOrElseThrowNullWithFunctionOnInvalid() { - assertThrows(NullPointerException.class, () -> Validation.invalid("some error").getOrElseThrow((Function) null)); } - // -- swap + @Nested + @DisplayName("fold") + class FoldTest { - @Test - public void shouldSwapSuccessToFailure() { - assertThat(valid().swap() instanceof Validation.Invalid).isTrue(); - assertThat(valid().swap().getError()).isEqualTo(OK); - } + @Test + public void shouldConvertSuccessToU() { + Validation, String> validValidation = valid(); + Integer result = validValidation.fold(Seq::length, String::length); + assertThat(result).isEqualTo(2); + } - @Test - public void shouldSwapFailureToSuccess() { - assertThat(invalid().swap() instanceof Validation.Valid).isTrue(); - assertThat(invalid().swap().get()).isEqualTo(ERRORS); - } + @Test + public void shouldConvertFailureToU() { + Validation, String> invalidValidation = invalid(); + Integer result = invalidValidation.fold(Seq::length, String::length); + assertThat(result).isEqualTo(3); + } - // -- map - @Test - public void shouldMapSuccessValue() { - assertThat(valid().map(s -> s + "!").get()).isEqualTo(OK + "!"); - } + // getOrElseThrow(Function) - @Test - public void shouldMapFailureError() { - assertThat(invalid().map(s -> 2).getError()).isEqualTo(ERRORS); - } + @Test + public void shouldReturnValidWhenGetOrElseThrowWithFunctionOnValid() { + final Integer actual = Validation.valid(1).getOrElseThrow(s -> new RuntimeException(s)); + assertThat(actual).isEqualTo(1); + } - @Test - public void shouldMapFailureErrorOnGet() { - assertThrows(RuntimeException.class, () -> assertThat(invalid().map(s -> 2).get()).isEqualTo(ERRORS)); - } + @Test + public void shouldThrowWhenGetOrElseThrowWithFunctionOnInvalid() { + assertThrows(RuntimeException.class, () -> Validation.invalid("some error").getOrElseThrow(s -> new RuntimeException(s))); + } - // -- bimap + @Test + public void shouldThrowNullPointerExceptionWhenGetOrElseThrowNullWithFunctionOnValid() { + assertThrows(NullPointerException.class, () -> Validation.valid(1).getOrElseThrow((Function) null)); + } - @Test - public void shouldMapOnlySuccessValue() { - Validation, String> validValidation = valid(); - Validation validMapping = validValidation.bimap(Seq::length, String::length); - assertThat(validMapping instanceof Validation.Valid).isTrue(); - assertThat(validMapping.get()).isEqualTo(2); - } + @Test + public void shouldThrowNullPointerExceptionWhenGetOrElseThrowNullWithFunctionOnInvalid() { + assertThrows(NullPointerException.class, () -> Validation.invalid("some error").getOrElseThrow((Function) null)); + } - @Test - public void shouldMapOnlyFailureValue() { - Validation, String> invalidValidation = invalid(); - Validation invalidMapping = invalidValidation.bimap(Seq::length, String::length); - assertThat(invalidMapping instanceof Validation.Invalid).isTrue(); - assertThat(invalidMapping.getError()).isEqualTo(3); } - // -- mapError + @Nested + @DisplayName("swap") + class SwapTest { - @Test - public void shouldNotMapSuccess() { - assertThat(valid().mapError(x -> 2).get()).isEqualTo(OK); - } + @Test + public void shouldSwapSuccessToFailure() { + assertThat(valid().swap() instanceof Validation.Invalid).isTrue(); + assertThat(valid().swap().getError()).isEqualTo(OK); + } + + @Test + public void shouldSwapFailureToSuccess() { + assertThat(invalid().swap() instanceof Validation.Valid).isTrue(); + assertThat(invalid().swap().get()).isEqualTo(ERRORS); + } - @Test - public void shouldMapFailure() { - assertThat(invalid().mapError(x -> 5).getError()).isEqualTo(5); } - // -- forEach + @Nested + @DisplayName("map") + class MapTest { - @Test - public void shouldProcessFunctionInForEach() { + @Test + public void shouldMapSuccessValue() { + assertThat(valid().map(s -> s + "!").get()).isEqualTo(OK + "!"); + } - { // Valid.forEach - java.util.List accumulator = new ArrayList<>(); - Validation v1 = Validation.valid("valid"); - v1.forEach(accumulator::add); - assertThat(accumulator.size()).isEqualTo(1); - assertThat(accumulator.get(0)).isEqualTo("valid"); + @Test + public void shouldMapFailureError() { + assertThat(invalid().map(s -> 2).getError()).isEqualTo(ERRORS); } - { // Invalid.forEach - java.util.List accumulator = new ArrayList<>(); - Validation v2 = Validation.invalid("error"); - v2.forEach(accumulator::add); - assertThat(accumulator.size()).isEqualTo(0); + @Test + public void shouldMapFailureErrorOnGet() { + assertThrows(RuntimeException.class, () -> assertThat(invalid().map(s -> 2).get()).isEqualTo(ERRORS)); } + } - // -- combine and apply + @Nested + @DisplayName("bimap") + class BimapTest { - @Test - public void shouldBuildUpForSuccessCombine() { - Validation v1 = Validation.valid("John Doe"); - Validation v2 = Validation.valid(39); - Validation> v3 = Validation.valid(Option.of("address")); - Validation> v4 = Validation.valid(Option.none()); - Validation v5 = Validation.valid("111-111-1111"); - Validation v6 = Validation.valid("alt1"); - Validation v7 = Validation.valid("alt2"); - Validation v8 = Validation.valid("alt3"); - Validation v9 = Validation.valid("alt4"); - - Validation, TestValidation> result = v1.combine(v2).ap(TestValidation::new); - - Validation, TestValidation> result2 = v1.combine(v2).combine(v3).ap(TestValidation::new); - Validation, TestValidation> result3 = v1.combine(v2).combine(v4).ap(TestValidation::new); - - Validation, TestValidation> result4 = v1.combine(v2).combine(v3).combine(v5).ap(TestValidation::new); - Validation, TestValidation> result5 = v1.combine(v2).combine(v3).combine(v5).combine(v6).ap(TestValidation::new); - Validation, TestValidation> result6 = v1.combine(v2).combine(v3).combine(v5).combine(v6).combine(v7).ap(TestValidation::new); - Validation, TestValidation> result7 = v1.combine(v2).combine(v3).combine(v5).combine(v6).combine(v7).combine(v8).ap(TestValidation::new); - Validation, TestValidation> result8 = v1.combine(v2).combine(v3).combine(v5).combine(v6).combine(v7).combine(v8).combine(v9).ap(TestValidation::new); - - Validation, String> result9 = v1.combine(v2).combine(v3).ap((p1, p2, p3) -> p1 + ":" + p2 + ":" + p3.getOrElse("none")); - - assertThat(result.isValid()).isTrue(); - assertThat(result2.isValid()).isTrue(); - assertThat(result3.isValid()).isTrue(); - assertThat(result4.isValid()).isTrue(); - assertThat(result5.isValid()).isTrue(); - assertThat(result6.isValid()).isTrue(); - assertThat(result7.isValid()).isTrue(); - assertThat(result8.isValid()).isTrue(); - assertThat(result9.isValid()).isTrue(); - - assertThat(result.get() instanceof TestValidation).isTrue(); - assertThat(result9.get() instanceof String).isTrue(); - } + @Test + public void shouldMapOnlySuccessValue() { + Validation, String> validValidation = valid(); + Validation validMapping = validValidation.bimap(Seq::length, String::length); + assertThat(validMapping instanceof Validation.Valid).isTrue(); + assertThat(validMapping.get()).isEqualTo(2); + } + + @Test + public void shouldMapOnlyFailureValue() { + Validation, String> invalidValidation = invalid(); + Validation invalidMapping = invalidValidation.bimap(Seq::length, String::length); + assertThat(invalidMapping instanceof Validation.Invalid).isTrue(); + assertThat(invalidMapping.getError()).isEqualTo(3); + } - @Test - public void shouldBuildUpForSuccessMapN() { - Validation v1 = Validation.valid("John Doe"); - Validation v2 = Validation.valid(39); - Validation> v3 = Validation.valid(Option.of("address")); - Validation> v4 = Validation.valid(Option.none()); - Validation v5 = Validation.valid("111-111-1111"); - Validation v6 = Validation.valid("alt1"); - Validation v7 = Validation.valid("alt2"); - Validation v8 = Validation.valid("alt3"); - Validation v9 = Validation.valid("alt4"); - - // Alternative map(n) functions to the 'combine' function - Validation, TestValidation> result = Validation.combine(v1, v2).ap(TestValidation::new); - Validation, TestValidation> result2 = Validation.combine(v1, v2, v3).ap(TestValidation::new); - Validation, TestValidation> result3 = Validation.combine(v1, v2, v4).ap(TestValidation::new); - Validation, TestValidation> result4 = Validation.combine(v1, v2, v3, v5).ap(TestValidation::new); - Validation, TestValidation> result5 = Validation.combine(v1, v2, v3, v5, v6).ap(TestValidation::new); - Validation, TestValidation> result6 = Validation.combine(v1, v2, v3, v5, v6, v7).ap(TestValidation::new); - Validation, TestValidation> result7 = Validation.combine(v1, v2, v3, v5, v6, v7, v8).ap(TestValidation::new); - Validation, TestValidation> result8 = Validation.combine(v1, v2, v3, v5, v6, v7, v8, v9).ap(TestValidation::new); - - Validation, String> result9 = Validation.combine(v1, v2, v3).ap((p1, p2, p3) -> p1 + ":" + p2 + ":" + p3.getOrElse("none")); - - assertThat(result.isValid()).isTrue(); - assertThat(result2.isValid()).isTrue(); - assertThat(result3.isValid()).isTrue(); - assertThat(result4.isValid()).isTrue(); - assertThat(result5.isValid()).isTrue(); - assertThat(result6.isValid()).isTrue(); - assertThat(result7.isValid()).isTrue(); - assertThat(result8.isValid()).isTrue(); - assertThat(result9.isValid()).isTrue(); - - assertThat(result.get() instanceof TestValidation).isTrue(); - assertThat(result9.get() instanceof String).isTrue(); } - @Test - public void shouldBuildUpForFailure() { - Validation v1 = Validation.valid("John Doe"); - Validation v2 = Validation.valid(39); - Validation> v3 = Validation.valid(Option.of("address")); + @Nested + @DisplayName("mapError") + class MapErrorTest { - Validation e1 = Validation.invalid("error2"); - Validation e2 = Validation.invalid("error1"); - Validation> e3 = Validation.invalid("error3"); + @Test + public void shouldNotMapSuccess() { + assertThat(valid().mapError(x -> 2).get()).isEqualTo(OK); + } - Validation, TestValidation> result = v1.combine(e2).combine(v3).ap(TestValidation::new); - Validation, TestValidation> result2 = e1.combine(v2).combine(e3).ap(TestValidation::new); + @Test + public void shouldMapFailure() { + assertThat(invalid().mapError(x -> 5).getError()).isEqualTo(5); + } - assertThat(result.isInvalid()).isTrue(); - assertThat(result2.isInvalid()).isTrue(); } - // -- miscellaneous + @Nested + @DisplayName("forEach") + class ForEachTest { - @Test - public void shouldThrowErrorOnGetErrorValid() { - assertThrows(RuntimeException.class, () -> { - Validation v1 = valid(); - v1.getError(); - }); - } + @Test + public void shouldProcessFunctionInForEach() { - @Test - public void shouldMatchLikeObjects() { - Validation v1 = Validation.valid("test"); - Validation v2 = Validation.valid("test"); - Validation v3 = Validation.valid("test diff"); + { // Valid.forEach + java.util.List accumulator = new ArrayList<>(); + Validation v1 = Validation.valid("valid"); + v1.forEach(accumulator::add); + assertThat(accumulator.size()).isEqualTo(1); + assertThat(accumulator.get(0)).isEqualTo("valid"); + } - Validation e1 = Validation.invalid("error1"); - Validation e2 = Validation.invalid("error1"); - Validation e3 = Validation.invalid("error diff"); + { // Invalid.forEach + java.util.List accumulator = new ArrayList<>(); + Validation v2 = Validation.invalid("error"); + v2.forEach(accumulator::add); + assertThat(accumulator.size()).isEqualTo(0); + } + } - assertThat(v1.equals(v1)).isTrue(); - assertThat(v1.equals(v2)).isTrue(); - assertThat(v1.equals(v3)).isFalse(); + // -- combine and apply + + @Test + public void shouldBuildUpForSuccessCombine() { + Validation v1 = Validation.valid("John Doe"); + Validation v2 = Validation.valid(39); + Validation> v3 = Validation.valid(Option.of("address")); + Validation> v4 = Validation.valid(Option.none()); + Validation v5 = Validation.valid("111-111-1111"); + Validation v6 = Validation.valid("alt1"); + Validation v7 = Validation.valid("alt2"); + Validation v8 = Validation.valid("alt3"); + Validation v9 = Validation.valid("alt4"); + + Validation, TestValidation> result = v1.combine(v2).ap(TestValidation::new); + + Validation, TestValidation> result2 = v1.combine(v2).combine(v3).ap(TestValidation::new); + Validation, TestValidation> result3 = v1.combine(v2).combine(v4).ap(TestValidation::new); + + Validation, TestValidation> result4 = v1.combine(v2).combine(v3).combine(v5).ap(TestValidation::new); + Validation, TestValidation> result5 = v1.combine(v2).combine(v3).combine(v5).combine(v6).ap(TestValidation::new); + Validation, TestValidation> result6 = v1.combine(v2).combine(v3).combine(v5).combine(v6).combine(v7).ap(TestValidation::new); + Validation, TestValidation> result7 = v1.combine(v2).combine(v3).combine(v5).combine(v6).combine(v7).combine(v8).ap(TestValidation::new); + Validation, TestValidation> result8 = v1.combine(v2).combine(v3).combine(v5).combine(v6).combine(v7).combine(v8).combine(v9).ap(TestValidation::new); + + Validation, String> result9 = v1.combine(v2).combine(v3).ap((p1, p2, p3) -> p1 + ":" + p2 + ":" + p3.getOrElse("none")); + + assertThat(result.isValid()).isTrue(); + assertThat(result2.isValid()).isTrue(); + assertThat(result3.isValid()).isTrue(); + assertThat(result4.isValid()).isTrue(); + assertThat(result5.isValid()).isTrue(); + assertThat(result6.isValid()).isTrue(); + assertThat(result7.isValid()).isTrue(); + assertThat(result8.isValid()).isTrue(); + assertThat(result9.isValid()).isTrue(); + + assertThat(result.get() instanceof TestValidation).isTrue(); + assertThat(result9.get() instanceof String).isTrue(); + } - assertThat(e1.equals(e1)).isTrue(); - assertThat(e1.equals(e2)).isTrue(); - assertThat(e1.equals(e3)).isFalse(); - } + @Test + public void shouldBuildUpForSuccessMapN() { + Validation v1 = Validation.valid("John Doe"); + Validation v2 = Validation.valid(39); + Validation> v3 = Validation.valid(Option.of("address")); + Validation> v4 = Validation.valid(Option.none()); + Validation v5 = Validation.valid("111-111-1111"); + Validation v6 = Validation.valid("alt1"); + Validation v7 = Validation.valid("alt2"); + Validation v8 = Validation.valid("alt3"); + Validation v9 = Validation.valid("alt4"); + + // Alternative map(n) functions to the 'combine' function + Validation, TestValidation> result = Validation.combine(v1, v2).ap(TestValidation::new); + Validation, TestValidation> result2 = Validation.combine(v1, v2, v3).ap(TestValidation::new); + Validation, TestValidation> result3 = Validation.combine(v1, v2, v4).ap(TestValidation::new); + Validation, TestValidation> result4 = Validation.combine(v1, v2, v3, v5).ap(TestValidation::new); + Validation, TestValidation> result5 = Validation.combine(v1, v2, v3, v5, v6).ap(TestValidation::new); + Validation, TestValidation> result6 = Validation.combine(v1, v2, v3, v5, v6, v7).ap(TestValidation::new); + Validation, TestValidation> result7 = Validation.combine(v1, v2, v3, v5, v6, v7, v8).ap(TestValidation::new); + Validation, TestValidation> result8 = Validation.combine(v1, v2, v3, v5, v6, v7, v8, v9).ap(TestValidation::new); + + Validation, String> result9 = Validation.combine(v1, v2, v3).ap((p1, p2, p3) -> p1 + ":" + p2 + ":" + p3.getOrElse("none")); + + assertThat(result.isValid()).isTrue(); + assertThat(result2.isValid()).isTrue(); + assertThat(result3.isValid()).isTrue(); + assertThat(result4.isValid()).isTrue(); + assertThat(result5.isValid()).isTrue(); + assertThat(result6.isValid()).isTrue(); + assertThat(result7.isValid()).isTrue(); + assertThat(result8.isValid()).isTrue(); + assertThat(result9.isValid()).isTrue(); + + assertThat(result.get() instanceof TestValidation).isTrue(); + assertThat(result9.get() instanceof String).isTrue(); + } - @Test - public void shouldReturnCorrectStringForToString() { - Validation v1 = Validation.valid("test"); - Validation v2 = Validation.invalid("error"); + @Test + public void shouldBuildUpForFailure() { + Validation v1 = Validation.valid("John Doe"); + Validation v2 = Validation.valid(39); + Validation> v3 = Validation.valid(Option.of("address")); - assertThat(v1.toString()).isEqualTo("Valid(test)"); - assertThat(v2.toString()).isEqualTo("Invalid(error)"); - } + Validation e1 = Validation.invalid("error2"); + Validation e2 = Validation.invalid("error1"); + Validation> e3 = Validation.invalid("error3"); - @Test - public void shouldReturnHashCode() { - Validation v1 = Validation.valid("test"); - Validation e1 = Validation.invalid("error"); + Validation, TestValidation> result = v1.combine(e2).combine(v3).ap(TestValidation::new); + Validation, TestValidation> result2 = e1.combine(v2).combine(e3).ap(TestValidation::new); + + assertThat(result.isInvalid()).isTrue(); + assertThat(result2.isInvalid()).isTrue(); + } - assertThat(v1.hashCode()).isEqualTo(Objects.hashCode(v1)); - assertThat(e1.hashCode()).isEqualTo(Objects.hashCode(e1)); } + @Nested + @DisplayName("miscellaneous") + class MiscellaneousTest { + + @Test + public void shouldThrowErrorOnGetErrorValid() { + assertThrows(RuntimeException.class, () -> { + Validation v1 = valid(); + v1.getError(); + }); + } + + @Test + public void shouldMatchLikeObjects() { + Validation v1 = Validation.valid("test"); + Validation v2 = Validation.valid("test"); + Validation v3 = Validation.valid("test diff"); + + Validation e1 = Validation.invalid("error1"); + Validation e2 = Validation.invalid("error1"); + Validation e3 = Validation.invalid("error diff"); + + assertThat(v1.equals(v1)).isTrue(); + assertThat(v1.equals(v2)).isTrue(); + assertThat(v1.equals(v3)).isFalse(); + + assertThat(e1.equals(e1)).isTrue(); + assertThat(e1.equals(e2)).isTrue(); + assertThat(e1.equals(e3)).isFalse(); + } + + @Test + public void shouldReturnCorrectStringForToString() { + Validation v1 = Validation.valid("test"); + Validation v2 = Validation.invalid("error"); + + assertThat(v1.toString()).isEqualTo("Valid(test)"); + assertThat(v2.toString()).isEqualTo("Invalid(error)"); + } + + @Test + public void shouldReturnHashCode() { + Validation v1 = Validation.valid("test"); + Validation e1 = Validation.invalid("error"); + + assertThat(v1.hashCode()).isEqualTo(Objects.hashCode(v1)); + assertThat(e1.hashCode()).isEqualTo(Objects.hashCode(e1)); + } + } // ------------------------------------------------------------------------------------------ // private Validation valid() { @@ -729,13 +816,13 @@ Validation, Person> validatePerson(String name, int age) { private Validation validateName(String name) { return CharSeq.of(name).replaceAll(validNameChars, "").transform(seq -> - seq.isEmpty() ? Validation. valid(name) - : Validation. invalid("Name contains invalid characters: '" + seq.distinct().sorted() + "'")); + seq.isEmpty() ? Validation.valid(name) + : Validation.invalid("Name contains invalid characters: '" + seq.distinct().sorted() + "'")); } private Validation validateAge(int age) { return (age < minAge) ? Validation.invalid("Age must be greater than 0") - : Validation.valid(age); + : Validation.valid(age); } } @@ -772,39 +859,45 @@ public String toString() { } } - // -- spliterator + @Nested + @DisplayName("spliterator") + class SpliteratorTest { - @Test - public void shouldHaveSizedSpliterator() { - assertThat(of(1).spliterator().hasCharacteristics(Spliterator.SIZED | Spliterator.SUBSIZED)).isTrue(); - } + @Test + public void shouldHaveSizedSpliterator() { + assertThat(of(1).spliterator().hasCharacteristics(Spliterator.SIZED | Spliterator.SUBSIZED)).isTrue(); + } - @Test - public void shouldHaveOrderedSpliterator() { - assertThat(of(1).spliterator().hasCharacteristics(Spliterator.ORDERED)).isTrue(); - } + @Test + public void shouldHaveOrderedSpliterator() { + assertThat(of(1).spliterator().hasCharacteristics(Spliterator.ORDERED)).isTrue(); + } - @Test - public void shouldReturnSizeWhenSpliterator() { - assertThat(of(1).spliterator().getExactSizeIfKnown()).isEqualTo(1); - } + @Test + public void shouldReturnSizeWhenSpliterator() { + assertThat(of(1).spliterator().getExactSizeIfKnown()).isEqualTo(1); + } - // -- transform + @Nested + @DisplayName("transform") + class TransformTest { - @Test - public void shouldThrowExceptionOnNullTransformFunction() { - assertThrows(NullPointerException.class, () -> Validation.valid(1).transform(null)); - } + @Test + public void shouldThrowExceptionOnNullTransformFunction() { + assertThrows(NullPointerException.class, () -> Validation.valid(1).transform(null)); + } - @Test - public void shouldApplyTransformFunctionToRight() { - final Validation validation = Validation.valid(1); - final Function, String> f = v -> v.get().toString().concat("-transformed"); - assertThat(validation.transform(f)).isEqualTo("1-transformed"); - } + @Test + public void shouldApplyTransformFunctionToRight() { + final Validation validation = Validation.valid(1); + final Function, String> f = v -> v.get().toString().concat("-transformed"); + assertThat(validation.transform(f)).isEqualTo("1-transformed"); + } - @Test - public void shouldHandleTransformOnLeft() { - assertThat(Validation.invalid(0). transform(self -> self.isEmpty() ? "ok" : "failed")).isEqualTo("ok"); + @Test + public void shouldHandleTransformOnLeft() { + assertThat(Validation.invalid(0).transform(self -> self.isEmpty() ? "ok" : "failed")).isEqualTo("ok"); + } + } } -} +} \ No newline at end of file From c7a780d3c2bbc555f3de3329f133b6baa64ddc6f Mon Sep 17 00:00:00 2001 From: Boris van Katwijk Date: Wed, 1 Jan 2025 16:59:23 +0100 Subject: [PATCH 47/61] Update ArrayTest.java --- src/test/java/io/vavr/TupleTest.java | 1078 +++++++++-------- .../java/io/vavr/collection/ArrayTest.java | 191 +-- 2 files changed, 661 insertions(+), 608 deletions(-) diff --git a/src/test/java/io/vavr/TupleTest.java b/src/test/java/io/vavr/TupleTest.java index 0627b8acce..96663298ee 100644 --- a/src/test/java/io/vavr/TupleTest.java +++ b/src/test/java/io/vavr/TupleTest.java @@ -28,6 +28,8 @@ import io.vavr.collection.List; import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import java.math.BigDecimal; @@ -38,530 +40,564 @@ public class TupleTest { - // -- Tuple0 + @Nested + @DisplayName("Tuple0") + class Tuple0Test { + + @Test + public void shouldCreateEmptyTuple() { + assertThat(Tuple.empty().toString()).isEqualTo("()"); + } + + @Test + public void shouldHashTuple0() { + assertThat(tuple0().hashCode()).isEqualTo(Objects.hash()); + } + + @Test + public void shouldReturnCorrectArityOfTuple0() { + assertThat(tuple0().arity()).isEqualTo(0); + } + + @Test + public void shouldReturnCorrectSeqOfTuple0() { + Assertions.assertThat(tuple0().toSeq()).isEqualTo(List.empty()); + } + + @SuppressWarnings("EqualsWithItself") + @Test + public void shouldEqualSameTuple0Instances() { + final Tuple0 t = tuple0(); + assertThat(t.equals(t)).isTrue(); + } + + @SuppressWarnings("ObjectEqualsNull") + @Test + public void shouldNotTuple0EqualsNull() { + assertThat(tuple0().equals(null)).isFalse(); + } + + @Test + public void shouldNotTuple0EqualsObject() { + assertThat(tuple0().equals(new Object())).isFalse(); + } + + @SuppressWarnings("EqualsWithItself") + @Test + public void shouldTuple0EqualTuple0() { + assertThat(tuple0().equals(tuple0())).isTrue(); + } + + @Test + public void shouldDeserializeSingletonOfTuple0() { + final Object tuple0 = Serializables.deserialize(Serializables.serialize(Tuple0.instance())); + assertThat(tuple0 == Tuple0.instance()).isTrue(); + } + + @Test + public void shouldReturnComparator() { + assertThat(Tuple0.comparator().compare(Tuple0.instance(), Tuple0.instance())).isEqualTo(0); + } + + @Test + public void shouldApplyTuple0() { + assertThat(Tuple0.instance().apply(() -> 1) == 1).isTrue(); + } + + } + + @Nested + @DisplayName("Tuple1") + class Tuple1Test { + + @Test + public void shouldCreateSingle() { + assertThat(tuple1().toString()).isEqualTo("(1)"); + } + + @Test + public void shouldHashTuple1() { + final Tuple1 t = tuple1(); + assertThat(t.hashCode()).isEqualTo(Tuple.hash(t._1)); + } + + @Test + public void shouldReturnCorrectArityOfTuple1() { + assertThat(tuple1().arity()).isEqualTo(1); + } + + @SuppressWarnings("EqualsWithItself") + @Test + public void shouldEqualSameTuple1Instances() { + final Tuple1 t = tuple1(); + assertThat(t.equals(t)).isTrue(); + } + + @SuppressWarnings("ObjectEqualsNull") + @Test + public void shouldNotTuple1EqualsNull() { + assertThat(tuple1().equals(null)).isFalse(); + } + + @Test + public void shouldNotTuple1EqualsObject() { + assertThat(tuple1().equals(new Object())).isFalse(); + } + + @Test + public void shouldTuple1EqualTuple1() { + assertThat(tuple1().equals(tuple1())).isTrue(); + } + + @Test + public void shouldNarrowTuple1() { + final Tuple1 wideTuple = Tuple.of(1.0d); + final Tuple1 narrowTuple = Tuple.narrow(wideTuple); + assertThat(narrowTuple._1()).isEqualTo(1.0d); + } + + } + + @Nested + @DisplayName("Tuple2") + class Tuple2Test { + + @Test + public void shouldCreatePair() { + assertThat(tuple2().toString()).isEqualTo("(1, 2)"); + } + + @Test + public void shouldCreateTuple2FromEntry() { + final Tuple2 tuple2FromEntry = Tuple.fromEntry(new AbstractMap.SimpleEntry<>(1, 2)); + assertThat(tuple2FromEntry.toString()).isEqualTo("(1, 2)"); + } + + @Test + public void shouldHashTuple2() { + final Tuple2 t = tuple2(); + assertThat(t.hashCode()).isEqualTo(new AbstractMap.SimpleEntry<>(t._1, t._2).hashCode()); + } + + @Test + public void shouldReturnCorrectArityOfTuple2() { + assertThat(tuple2().arity()).isEqualTo(2); + } + + @SuppressWarnings("EqualsWithItself") + @Test + public void shouldEqualSameTuple2Instances() { + final Tuple2 t = tuple2(); + assertThat(t.equals(t)).isTrue(); + } + + @SuppressWarnings("ObjectEqualsNull") + @Test + public void shouldNotTuple2EqualsNull() { + assertThat(tuple2().equals(null)).isFalse(); + } + + @Test + public void shouldNotTuple2EqualsObject() { + assertThat(tuple2().equals(new Object())).isFalse(); + } + + @Test + public void shouldTuple2EqualTuple2() { + assertThat(tuple2().equals(tuple2())).isTrue(); + } + + @Test + public void shouldNarrowTuple2() { + final Tuple2 wideTuple = Tuple.of("test", 1.0d); + final Tuple2 narrowTuple = Tuple.narrow(wideTuple); + assertThat(narrowTuple._1()).isEqualTo("test"); + assertThat(narrowTuple._2()).isEqualTo(1.0d); + } + + } + + @Nested + @DisplayName("Tuple3") + class Tuple3Test { + + @Test + public void shouldCreateTriple() { + assertThat(tuple3().toString()).isEqualTo("(1, 2, 3)"); + } + + @Test + public void shouldHashTuple3() { + final Tuple3 t = tuple3(); + assertThat(t.hashCode()).isEqualTo(Objects.hash(t._1, t._2, t._3)); + } + + @Test + public void shouldReturnCorrectArityOfTuple3() { + assertThat(tuple3().arity()).isEqualTo(3); + } + + @SuppressWarnings("EqualsWithItself") + @Test + public void shouldEqualSameTuple3Instances() { + final Tuple3 t = tuple3(); + assertThat(t.equals(t)).isTrue(); + } + + @SuppressWarnings("ObjectEqualsNull") + @Test + public void shouldNotTuple3EqualsNull() { + assertThat(tuple3().equals(null)).isFalse(); + } + + @Test + public void shouldNotTuple3EqualsObject() { + assertThat(tuple3().equals(new Object())).isFalse(); + } + + @Test + public void shouldTuple3EqualTuple3() { + assertThat(tuple3().equals(tuple3())).isTrue(); + } + + @Test + public void shouldNarrowTuple3() { + final Tuple3 wideTuple = Tuple.of("zero", 1.0D, 2.0F); + final Tuple3 narrowTuple = Tuple.narrow(wideTuple); + assertThat(narrowTuple._1()).isEqualTo("zero"); + assertThat(narrowTuple._2()).isEqualTo(1.0D); + assertThat(narrowTuple._3()).isEqualTo(2.0F); + } + + } + + @Nested + @DisplayName("Tuple4") + class Tuple4Test { + + @Test + public void shouldCreateQuadruple() { + assertThat(tuple4().toString()).isEqualTo("(1, 2, 3, 4)"); + } + + @Test + public void shouldHashTuple4() { + final Tuple4 t = tuple4(); + assertThat(t.hashCode()).isEqualTo(Objects.hash(t._1, t._2, t._3, t._4)); + } + + @Test + public void shouldReturnCorrectArityOfTuple4() { + assertThat(tuple4().arity()).isEqualTo(4); + } + + @SuppressWarnings("EqualsWithItself") + @Test + public void shouldEqualSameTuple4Instances() { + final Tuple4 t = tuple4(); + assertThat(t.equals(t)).isTrue(); + } + + @SuppressWarnings("ObjectEqualsNull") + @Test + public void shouldNotTuple4EqualsNull() { + assertThat(tuple4().equals(null)).isFalse(); + } + + @Test + public void shouldNotTuple4EqualsObject() { + assertThat(tuple4().equals(new Object())).isFalse(); + } + + @Test + public void shouldTuple4EqualTuple4() { + assertThat(tuple4().equals(tuple4())).isTrue(); + } + + @Test + public void shouldNarrowTuple4() { + final Tuple4 wideTuple = Tuple.of("zero", 1.0D, 2.0F, 3); + final Tuple4 narrowTuple = Tuple.narrow(wideTuple); + assertThat(narrowTuple._1()).isEqualTo("zero"); + assertThat(narrowTuple._2()).isEqualTo(1.0D); + assertThat(narrowTuple._3()).isEqualTo(2.0F); + assertThat(narrowTuple._4()).isEqualTo(3); + } + + } + + @Nested + @DisplayName("Tuple5") + class Tuple5Test { + + @Test + public void shouldCreateQuintuple() { + assertThat(tuple5().toString()).isEqualTo("(1, 2, 3, 4, 5)"); + } + + @Test + public void shouldHashTuple5() { + final Tuple5 t = tuple5(); + assertThat(t.hashCode()).isEqualTo(Objects.hash(t._1, t._2, t._3, t._4, t._5)); + } + + @Test + public void shouldReturnCorrectArityOfTuple5() { + assertThat(tuple5().arity()).isEqualTo(5); + } + + @SuppressWarnings("EqualsWithItself") + @Test + public void shouldEqualSameTuple5Instances() { + final Tuple5 t = tuple5(); + assertThat(t.equals(t)).isTrue(); + } + + @SuppressWarnings("ObjectEqualsNull") + @Test + public void shouldNotTuple5EqualsNull() { + assertThat(tuple5().equals(null)).isFalse(); + } + + @Test + public void shouldNotTuple5EqualsObject() { + assertThat(tuple5().equals(new Object())).isFalse(); + } + + @Test + public void shouldTuple5EqualTuple5() { + assertThat(tuple5().equals(tuple5())).isTrue(); + } + + @Test + public void shouldNarrowTuple5() { + final Tuple5 wideTuple = Tuple.of("zero", 1.0D, 2.0F, 3, 4L); + final Tuple5 narrowTuple = Tuple.narrow(wideTuple); + assertThat(narrowTuple._1()).isEqualTo("zero"); + assertThat(narrowTuple._2()).isEqualTo(1.0D); + assertThat(narrowTuple._3()).isEqualTo(2.0F); + assertThat(narrowTuple._4()).isEqualTo(3); + assertThat(narrowTuple._5()).isEqualTo(4L); + } + + } + + @Nested + @DisplayName("Tuple6") + class Tuple6Test { + + @Test + public void shouldCreateSextuple() { + assertThat(tuple6().toString()).isEqualTo("(1, 2, 3, 4, 5, 6)"); + } + + @Test + public void shouldHashTuple6() { + final Tuple6 t = tuple6(); + assertThat(t.hashCode()).isEqualTo(Objects.hash(t._1, t._2, t._3, t._4, t._5, t._6)); + } + + @Test + public void shouldReturnCorrectArityOfTuple6() { + assertThat(tuple6().arity()).isEqualTo(6); + } + + @SuppressWarnings("EqualsWithItself") + @Test + public void shouldEqualSameTuple6Instances() { + final Tuple6 t = tuple6(); + assertThat(t.equals(t)).isTrue(); + } + + @SuppressWarnings("ObjectEqualsNull") + @Test + public void shouldNotTuple6EqualsNull() { + assertThat(tuple6().equals(null)).isFalse(); + } + + @Test + public void shouldNotTuple6EqualsObject() { + assertThat(tuple6().equals(new Object())).isFalse(); + } + + @Test + public void shouldTuple6EqualTuple6() { + assertThat(tuple6().equals(tuple6())).isTrue(); + } + + @Test + public void shouldNarrowTuple6() { + final Tuple6 wideTuple = Tuple.of("zero", 1.0D, 2.0F, 3, 4L, (byte) 5); + final Tuple6 narrowTuple = Tuple.narrow(wideTuple); + assertThat(narrowTuple._1()).isEqualTo("zero"); + assertThat(narrowTuple._2()).isEqualTo(1.0D); + assertThat(narrowTuple._3()).isEqualTo(2.0F); + assertThat(narrowTuple._4()).isEqualTo(3); + assertThat(narrowTuple._5()).isEqualTo(4L); + assertThat(narrowTuple._6()).isEqualTo((byte) 5); + } + + } + + @Nested + @DisplayName("Tuple7") + class Tuple7Test { + + @Test + public void shouldCreateTuple7() { + assertThat(tuple7().toString()).isEqualTo("(1, 2, 3, 4, 5, 6, 7)"); + } + + @Test + public void shouldHashTuple7() { + final Tuple7 t = tuple7(); + assertThat(t.hashCode()).isEqualTo(Objects.hash(t._1, t._2, t._3, t._4, t._5, t._6, t._7)); + } + + @Test + public void shouldReturnCorrectArityOfTuple7() { + assertThat(tuple7().arity()).isEqualTo(7); + } + + @SuppressWarnings("EqualsWithItself") + @Test + public void shouldEqualSameTuple7Instances() { + final Tuple7 t = tuple7(); + assertThat(t.equals(t)).isTrue(); + } + + @SuppressWarnings("ObjectEqualsNull") + @Test + public void shouldNotTuple7EqualsNull() { + assertThat(tuple7().equals(null)).isFalse(); + } + + @Test + public void shouldNotTuple7EqualsObject() { + assertThat(tuple7().equals(new Object())).isFalse(); + } + + @Test + public void shouldTuple7EqualTuple7() { + assertThat(tuple7().equals(tuple7())).isTrue(); + } + + @Test + public void shouldNarrowTuple7() { + final Tuple7 wideTuple = Tuple.of("zero", 1.0D, 2.0F, 3, 4L, (byte) 5, (short) 6); + final Tuple7 narrowTuple = Tuple.narrow(wideTuple); + assertThat(narrowTuple._1()).isEqualTo("zero"); + assertThat(narrowTuple._2()).isEqualTo(1.0D); + assertThat(narrowTuple._3()).isEqualTo(2.0F); + assertThat(narrowTuple._4()).isEqualTo(3); + assertThat(narrowTuple._5()).isEqualTo(4L); + assertThat(narrowTuple._6()).isEqualTo((byte) 5); + assertThat(narrowTuple._7()).isEqualTo((short) 6); + } + + } + + @Nested + @DisplayName("Tuple8") + class Tuple8Test { + + @Test + public void shouldCreateTuple8() { + assertThat(tuple8().toString()).isEqualTo("(1, 2, 3, 4, 5, 6, 7, 8)"); + } + + @Test + public void shouldHashTuple8() { + final Tuple8 t = tuple8(); + assertThat(t.hashCode()).isEqualTo(Objects.hash(t._1, t._2, t._3, t._4, t._5, t._6, t._7, t._8)); + } + + @Test + public void shouldReturnCorrectArityOfTuple8() { + assertThat(tuple8().arity()).isEqualTo(8); + } + + @SuppressWarnings("EqualsWithItself") + @Test + public void shouldEqualSameTuple8Instances() { + final Tuple8 t = tuple8(); + assertThat(t.equals(t)).isTrue(); + } + + @SuppressWarnings("ObjectEqualsNull") + @Test + public void shouldNotTuple8EqualsNull() { + assertThat(tuple8().equals(null)).isFalse(); + } + + @Test + public void shouldNotTuple8EqualsObject() { + assertThat(tuple8().equals(new Object())).isFalse(); + } + + @Test + public void shouldTuple8EqualTuple8() { + assertThat(tuple8().equals(tuple8())).isTrue(); + } + + @Test + public void shouldNarrowTuple8() { + final Tuple8 wideTuple = Tuple.of("zero", 1.0D, 2.0F, 3, 4L, (byte) 5, (short) 6, new BigDecimal(7)); + final Tuple8 narrowTuple = Tuple.narrow(wideTuple); + assertThat(narrowTuple._1()).isEqualTo("zero"); + assertThat(narrowTuple._2()).isEqualTo(1.0D); + assertThat(narrowTuple._3()).isEqualTo(2.0F); + assertThat(narrowTuple._4()).isEqualTo(3); + assertThat(narrowTuple._5()).isEqualTo(4L); + assertThat(narrowTuple._6()).isEqualTo((byte) 5); + assertThat(narrowTuple._7()).isEqualTo((short) 6); + assertThat(narrowTuple._8()).isEqualTo(new BigDecimal(7)); + } + + // -- nested tuples + + @Test + public void shouldDetectEqualityOnTupleOfTuples() { + + final Tuple tupleA = Tuple.of(Tuple.of(1), Tuple.of(1)); + final Tuple tupleB = Tuple.of(Tuple.of(1), Tuple.of(1)); + + assertThat(tupleA.equals(tupleB)).isTrue(); + } + + @Test + public void shouldDetectUnequalityOnTupleOfTuples() { + + final Tuple tupleA = Tuple.of(Tuple.of(1), Tuple.of(1)); + final Tuple tupleB = Tuple.of(Tuple.of(1), Tuple.of(2)); + + assertThat(tupleA.equals(tupleB)).isFalse(); + } + + // -- Serializable interface + + @Test + public void shouldSerializeDeserializeTuple0() { + final Object actual = Serializables.deserialize(Serializables.serialize(Tuple0.instance())); + final Object expected = Tuple0.instance(); + assertThat(actual).isEqualTo(expected); + } + + @Test + public void shouldPreserveSingletonInstanceOnDeserialization() { + final boolean actual = Serializables.deserialize(Serializables.serialize(Tuple0.instance())) == Tuple0.instance(); + assertThat(actual).isTrue(); + } + + @Test + public void shouldSerializeDeserializeNonEmptyTuple() { + final Object actual = Serializables.deserialize(Serializables.serialize(Tuple.of(1, 2, 3))); + final Object expected = Tuple.of(1, 2, 3); + assertThat(actual).isEqualTo(expected); + } - @Test - public void shouldCreateEmptyTuple() { - assertThat(Tuple.empty().toString()).isEqualTo("()"); } - @Test - public void shouldHashTuple0() { - assertThat(tuple0().hashCode()).isEqualTo(Objects.hash()); - } - - @Test - public void shouldReturnCorrectArityOfTuple0() { - assertThat(tuple0().arity()).isEqualTo(0); - } - - @Test - public void shouldReturnCorrectSeqOfTuple0() { - Assertions.assertThat(tuple0().toSeq()).isEqualTo(List.empty()); - } - - @SuppressWarnings("EqualsWithItself") - @Test - public void shouldEqualSameTuple0Instances() { - final Tuple0 t = tuple0(); - assertThat(t.equals(t)).isTrue(); - } - - @SuppressWarnings("ObjectEqualsNull") - @Test - public void shouldNotTuple0EqualsNull() { - assertThat(tuple0().equals(null)).isFalse(); - } - - @Test - public void shouldNotTuple0EqualsObject() { - assertThat(tuple0().equals(new Object())).isFalse(); - } - - @SuppressWarnings("EqualsWithItself") - @Test - public void shouldTuple0EqualTuple0() { - assertThat(tuple0().equals(tuple0())).isTrue(); - } - - @Test - public void shouldDeserializeSingletonOfTuple0() { - final Object tuple0 = Serializables.deserialize(Serializables.serialize(Tuple0.instance())); - assertThat(tuple0 == Tuple0.instance()).isTrue(); - } - - @Test - public void shouldReturnComparator() { - assertThat(Tuple0.comparator().compare(Tuple0.instance(), Tuple0.instance())).isEqualTo(0); - } - - @Test - public void shouldApplyTuple0() { - assertThat(Tuple0.instance().apply(() -> 1) == 1).isTrue(); - } - - // -- Tuple1 - - @Test - public void shouldCreateSingle() { - assertThat(tuple1().toString()).isEqualTo("(1)"); - } - - @Test - public void shouldHashTuple1() { - final Tuple1 t = tuple1(); - assertThat(t.hashCode()).isEqualTo(Tuple.hash(t._1)); - } - - @Test - public void shouldReturnCorrectArityOfTuple1() { - assertThat(tuple1().arity()).isEqualTo(1); - } - - @SuppressWarnings("EqualsWithItself") - @Test - public void shouldEqualSameTuple1Instances() { - final Tuple1 t = tuple1(); - assertThat(t.equals(t)).isTrue(); - } - - @SuppressWarnings("ObjectEqualsNull") - @Test - public void shouldNotTuple1EqualsNull() { - assertThat(tuple1().equals(null)).isFalse(); - } - - @Test - public void shouldNotTuple1EqualsObject() { - assertThat(tuple1().equals(new Object())).isFalse(); - } - - @Test - public void shouldTuple1EqualTuple1() { - assertThat(tuple1().equals(tuple1())).isTrue(); - } - - @Test - public void shouldNarrowTuple1() { - final Tuple1 wideTuple = Tuple.of(1.0d); - final Tuple1 narrowTuple = Tuple.narrow(wideTuple); - assertThat(narrowTuple._1()).isEqualTo(1.0d); - } - - // -- Tuple2 - - @Test - public void shouldCreatePair() { - assertThat(tuple2().toString()).isEqualTo("(1, 2)"); - } - - @Test - public void shouldCreateTuple2FromEntry() { - final Tuple2 tuple2FromEntry = Tuple.fromEntry(new AbstractMap.SimpleEntry<>(1, 2)); - assertThat(tuple2FromEntry.toString()).isEqualTo("(1, 2)"); - } - - @Test - public void shouldHashTuple2() { - final Tuple2 t = tuple2(); - assertThat(t.hashCode()).isEqualTo(new AbstractMap.SimpleEntry<>(t._1, t._2).hashCode()); - } - - @Test - public void shouldReturnCorrectArityOfTuple2() { - assertThat(tuple2().arity()).isEqualTo(2); - } - - @SuppressWarnings("EqualsWithItself") - @Test - public void shouldEqualSameTuple2Instances() { - final Tuple2 t = tuple2(); - assertThat(t.equals(t)).isTrue(); - } - - @SuppressWarnings("ObjectEqualsNull") - @Test - public void shouldNotTuple2EqualsNull() { - assertThat(tuple2().equals(null)).isFalse(); - } - - @Test - public void shouldNotTuple2EqualsObject() { - assertThat(tuple2().equals(new Object())).isFalse(); - } - - @Test - public void shouldTuple2EqualTuple2() { - assertThat(tuple2().equals(tuple2())).isTrue(); - } - - @Test - public void shouldNarrowTuple2() { - final Tuple2 wideTuple = Tuple.of("test", 1.0d); - final Tuple2 narrowTuple = Tuple.narrow(wideTuple); - assertThat(narrowTuple._1()).isEqualTo("test"); - assertThat(narrowTuple._2()).isEqualTo(1.0d); - } - - // -- Tuple3 - - @Test - public void shouldCreateTriple() { - assertThat(tuple3().toString()).isEqualTo("(1, 2, 3)"); - } - - @Test - public void shouldHashTuple3() { - final Tuple3 t = tuple3(); - assertThat(t.hashCode()).isEqualTo(Objects.hash(t._1, t._2, t._3)); - } - - @Test - public void shouldReturnCorrectArityOfTuple3() { - assertThat(tuple3().arity()).isEqualTo(3); - } - - @SuppressWarnings("EqualsWithItself") - @Test - public void shouldEqualSameTuple3Instances() { - final Tuple3 t = tuple3(); - assertThat(t.equals(t)).isTrue(); - } - - @SuppressWarnings("ObjectEqualsNull") - @Test - public void shouldNotTuple3EqualsNull() { - assertThat(tuple3().equals(null)).isFalse(); - } - - @Test - public void shouldNotTuple3EqualsObject() { - assertThat(tuple3().equals(new Object())).isFalse(); - } - - @Test - public void shouldTuple3EqualTuple3() { - assertThat(tuple3().equals(tuple3())).isTrue(); - } - - @Test - public void shouldNarrowTuple3() { - final Tuple3 wideTuple = Tuple.of("zero", 1.0D, 2.0F); - final Tuple3 narrowTuple = Tuple.narrow(wideTuple); - assertThat(narrowTuple._1()).isEqualTo("zero"); - assertThat(narrowTuple._2()).isEqualTo(1.0D); - assertThat(narrowTuple._3()).isEqualTo(2.0F); - } - - // -- Tuple4 - - @Test - public void shouldCreateQuadruple() { - assertThat(tuple4().toString()).isEqualTo("(1, 2, 3, 4)"); - } - - @Test - public void shouldHashTuple4() { - final Tuple4 t = tuple4(); - assertThat(t.hashCode()).isEqualTo(Objects.hash(t._1, t._2, t._3, t._4)); - } - - @Test - public void shouldReturnCorrectArityOfTuple4() { - assertThat(tuple4().arity()).isEqualTo(4); - } - - @SuppressWarnings("EqualsWithItself") - @Test - public void shouldEqualSameTuple4Instances() { - final Tuple4 t = tuple4(); - assertThat(t.equals(t)).isTrue(); - } - - @SuppressWarnings("ObjectEqualsNull") - @Test - public void shouldNotTuple4EqualsNull() { - assertThat(tuple4().equals(null)).isFalse(); - } - - @Test - public void shouldNotTuple4EqualsObject() { - assertThat(tuple4().equals(new Object())).isFalse(); - } - - @Test - public void shouldTuple4EqualTuple4() { - assertThat(tuple4().equals(tuple4())).isTrue(); - } - - @Test - public void shouldNarrowTuple4() { - final Tuple4 wideTuple = Tuple.of("zero", 1.0D, 2.0F, 3); - final Tuple4 narrowTuple = Tuple.narrow(wideTuple); - assertThat(narrowTuple._1()).isEqualTo("zero"); - assertThat(narrowTuple._2()).isEqualTo(1.0D); - assertThat(narrowTuple._3()).isEqualTo(2.0F); - assertThat(narrowTuple._4()).isEqualTo(3); - } - - // -- Tuple5 - - @Test - public void shouldCreateQuintuple() { - assertThat(tuple5().toString()).isEqualTo("(1, 2, 3, 4, 5)"); - } - - @Test - public void shouldHashTuple5() { - final Tuple5 t = tuple5(); - assertThat(t.hashCode()).isEqualTo(Objects.hash(t._1, t._2, t._3, t._4, t._5)); - } - - @Test - public void shouldReturnCorrectArityOfTuple5() { - assertThat(tuple5().arity()).isEqualTo(5); - } - - @SuppressWarnings("EqualsWithItself") - @Test - public void shouldEqualSameTuple5Instances() { - final Tuple5 t = tuple5(); - assertThat(t.equals(t)).isTrue(); - } - - @SuppressWarnings("ObjectEqualsNull") - @Test - public void shouldNotTuple5EqualsNull() { - assertThat(tuple5().equals(null)).isFalse(); - } - - @Test - public void shouldNotTuple5EqualsObject() { - assertThat(tuple5().equals(new Object())).isFalse(); - } - - @Test - public void shouldTuple5EqualTuple5() { - assertThat(tuple5().equals(tuple5())).isTrue(); - } - - @Test - public void shouldNarrowTuple5() { - final Tuple5 wideTuple = Tuple.of("zero", 1.0D, 2.0F, 3, 4L); - final Tuple5 narrowTuple = Tuple.narrow(wideTuple); - assertThat(narrowTuple._1()).isEqualTo("zero"); - assertThat(narrowTuple._2()).isEqualTo(1.0D); - assertThat(narrowTuple._3()).isEqualTo(2.0F); - assertThat(narrowTuple._4()).isEqualTo(3); - assertThat(narrowTuple._5()).isEqualTo(4L); - } - - // -- Tuple6 - - @Test - public void shouldCreateSextuple() { - assertThat(tuple6().toString()).isEqualTo("(1, 2, 3, 4, 5, 6)"); - } - - @Test - public void shouldHashTuple6() { - final Tuple6 t = tuple6(); - assertThat(t.hashCode()).isEqualTo(Objects.hash(t._1, t._2, t._3, t._4, t._5, t._6)); - } - - @Test - public void shouldReturnCorrectArityOfTuple6() { - assertThat(tuple6().arity()).isEqualTo(6); - } - - @SuppressWarnings("EqualsWithItself") - @Test - public void shouldEqualSameTuple6Instances() { - final Tuple6 t = tuple6(); - assertThat(t.equals(t)).isTrue(); - } - - @SuppressWarnings("ObjectEqualsNull") - @Test - public void shouldNotTuple6EqualsNull() { - assertThat(tuple6().equals(null)).isFalse(); - } - - @Test - public void shouldNotTuple6EqualsObject() { - assertThat(tuple6().equals(new Object())).isFalse(); - } - - @Test - public void shouldTuple6EqualTuple6() { - assertThat(tuple6().equals(tuple6())).isTrue(); - } - - @Test - public void shouldNarrowTuple6() { - final Tuple6 wideTuple = Tuple.of("zero", 1.0D, 2.0F, 3, 4L, (byte) 5); - final Tuple6 narrowTuple = Tuple.narrow(wideTuple); - assertThat(narrowTuple._1()).isEqualTo("zero"); - assertThat(narrowTuple._2()).isEqualTo(1.0D); - assertThat(narrowTuple._3()).isEqualTo(2.0F); - assertThat(narrowTuple._4()).isEqualTo(3); - assertThat(narrowTuple._5()).isEqualTo(4L); - assertThat(narrowTuple._6()).isEqualTo((byte) 5); - } - - // -- Tuple7 - - @Test - public void shouldCreateTuple7() { - assertThat(tuple7().toString()).isEqualTo("(1, 2, 3, 4, 5, 6, 7)"); - } - - @Test - public void shouldHashTuple7() { - final Tuple7 t = tuple7(); - assertThat(t.hashCode()).isEqualTo(Objects.hash(t._1, t._2, t._3, t._4, t._5, t._6, t._7)); - } - - @Test - public void shouldReturnCorrectArityOfTuple7() { - assertThat(tuple7().arity()).isEqualTo(7); - } - - @SuppressWarnings("EqualsWithItself") - @Test - public void shouldEqualSameTuple7Instances() { - final Tuple7 t = tuple7(); - assertThat(t.equals(t)).isTrue(); - } - - @SuppressWarnings("ObjectEqualsNull") - @Test - public void shouldNotTuple7EqualsNull() { - assertThat(tuple7().equals(null)).isFalse(); - } - - @Test - public void shouldNotTuple7EqualsObject() { - assertThat(tuple7().equals(new Object())).isFalse(); - } - - @Test - public void shouldTuple7EqualTuple7() { - assertThat(tuple7().equals(tuple7())).isTrue(); - } - - @Test - public void shouldNarrowTuple7() { - final Tuple7 wideTuple = Tuple.of("zero", 1.0D, 2.0F, 3, 4L, (byte) 5, (short) 6); - final Tuple7 narrowTuple = Tuple.narrow(wideTuple); - assertThat(narrowTuple._1()).isEqualTo("zero"); - assertThat(narrowTuple._2()).isEqualTo(1.0D); - assertThat(narrowTuple._3()).isEqualTo(2.0F); - assertThat(narrowTuple._4()).isEqualTo(3); - assertThat(narrowTuple._5()).isEqualTo(4L); - assertThat(narrowTuple._6()).isEqualTo((byte) 5); - assertThat(narrowTuple._7()).isEqualTo((short) 6); - } - - // -- Tuple8 - - @Test - public void shouldCreateTuple8() { - assertThat(tuple8().toString()).isEqualTo("(1, 2, 3, 4, 5, 6, 7, 8)"); - } - - @Test - public void shouldHashTuple8() { - final Tuple8 t = tuple8(); - assertThat(t.hashCode()).isEqualTo(Objects.hash(t._1, t._2, t._3, t._4, t._5, t._6, t._7, t._8)); - } - - @Test - public void shouldReturnCorrectArityOfTuple8() { - assertThat(tuple8().arity()).isEqualTo(8); - } - - @SuppressWarnings("EqualsWithItself") - @Test - public void shouldEqualSameTuple8Instances() { - final Tuple8 t = tuple8(); - assertThat(t.equals(t)).isTrue(); - } - - @SuppressWarnings("ObjectEqualsNull") - @Test - public void shouldNotTuple8EqualsNull() { - assertThat(tuple8().equals(null)).isFalse(); - } - - @Test - public void shouldNotTuple8EqualsObject() { - assertThat(tuple8().equals(new Object())).isFalse(); - } - - @Test - public void shouldTuple8EqualTuple8() { - assertThat(tuple8().equals(tuple8())).isTrue(); - } - - @Test - public void shouldNarrowTuple8() { - final Tuple8 wideTuple = Tuple.of("zero", 1.0D, 2.0F, 3, 4L, (byte) 5, (short) 6, new BigDecimal(7)); - final Tuple8 narrowTuple = Tuple.narrow(wideTuple); - assertThat(narrowTuple._1()).isEqualTo("zero"); - assertThat(narrowTuple._2()).isEqualTo(1.0D); - assertThat(narrowTuple._3()).isEqualTo(2.0F); - assertThat(narrowTuple._4()).isEqualTo(3); - assertThat(narrowTuple._5()).isEqualTo(4L); - assertThat(narrowTuple._6()).isEqualTo((byte) 5); - assertThat(narrowTuple._7()).isEqualTo((short) 6); - assertThat(narrowTuple._8()).isEqualTo(new BigDecimal(7)); - } - - // -- nested tuples - - @Test - public void shouldDetectEqualityOnTupleOfTuples() { - - final Tuple tupleA = Tuple.of(Tuple.of(1), Tuple.of(1)); - final Tuple tupleB = Tuple.of(Tuple.of(1), Tuple.of(1)); - - assertThat(tupleA.equals(tupleB)).isTrue(); - } - - @Test - public void shouldDetectUnequalityOnTupleOfTuples() { - - final Tuple tupleA = Tuple.of(Tuple.of(1), Tuple.of(1)); - final Tuple tupleB = Tuple.of(Tuple.of(1), Tuple.of(2)); - - assertThat(tupleA.equals(tupleB)).isFalse(); - } - - // -- Serializable interface - - @Test - public void shouldSerializeDeserializeTuple0() { - final Object actual = Serializables.deserialize(Serializables.serialize(Tuple0.instance())); - final Object expected = Tuple0.instance(); - assertThat(actual).isEqualTo(expected); - } - - @Test - public void shouldPreserveSingletonInstanceOnDeserialization() { - final boolean actual = Serializables.deserialize(Serializables.serialize(Tuple0.instance())) == Tuple0.instance(); - assertThat(actual).isTrue(); - } - - @Test - public void shouldSerializeDeserializeNonEmptyTuple() { - final Object actual = Serializables.deserialize(Serializables.serialize(Tuple.of(1, 2, 3))); - final Object expected = Tuple.of(1, 2, 3); - assertThat(actual).isEqualTo(expected); - } - - // -- helpers - private Tuple0 tuple0() { return Tuple.empty(); } diff --git a/src/test/java/io/vavr/collection/ArrayTest.java b/src/test/java/io/vavr/collection/ArrayTest.java index 55f211f784..e35960cb71 100644 --- a/src/test/java/io/vavr/collection/ArrayTest.java +++ b/src/test/java/io/vavr/collection/ArrayTest.java @@ -31,6 +31,8 @@ import io.vavr.collection.JavaConverters.ListView; import io.vavr.control.Option; import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import java.math.BigDecimal; @@ -249,92 +251,107 @@ public void shouldReturnSelfWhenIterableIsInstanceOfListView() { public void shouldThrowExceptionWhenGetIndexEqualToLength() { final Array array = of(1); Assertions.assertThatThrownBy(() -> array.get(1)) - .isInstanceOf(IndexOutOfBoundsException.class).hasMessage("get(1)"); - } - - // -- partition - - @Test - public void shouldPartitionInOneIteration() { - final AtomicInteger count = new AtomicInteger(0); - final Tuple2, Array> results = of(1, 2, 3).partition(i -> { - count.incrementAndGet(); - return true; - }); - assertThat(results._1).isEqualTo(of(1, 2, 3)); - assertThat(results._2).isEqualTo(of()); - assertThat(count.get()).isEqualTo(3); - } - - // -- transform() - - @Test - public void shouldTransform() { - String transformed = of(42).transform(v -> String.valueOf(v.get())); - assertThat(transformed).isEqualTo("42"); - } - - // -- unfold - - @Test - public void shouldUnfoldRightToEmpty() { - assertThat(Array.unfoldRight(0, x -> Option.none())).isEqualTo(empty()); - } - - @Test - public void shouldUnfoldRightSimpleArray() { - assertThat( - Array.unfoldRight(10, x -> x == 0 - ? Option.none() - : Option.of(new Tuple2<>(x, x - 1)))) - .isEqualTo(of(10, 9, 8, 7, 6, 5, 4, 3, 2, 1)); - } - - @Test - public void shouldUnfoldLeftToEmpty() { - assertThat(Array.unfoldLeft(0, x -> Option.none())).isEqualTo(empty()); - } - - @Test - public void shouldUnfoldLeftSimpleArray() { - assertThat( - Array.unfoldLeft(10, x -> x == 0 - ? Option.none() - : Option.of(new Tuple2<>(x - 1, x)))) - .isEqualTo(of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)); - } - - @Test - public void shouldUnfoldToEmpty() { - assertThat(Array.unfold(0, x -> Option.none())).isEqualTo(empty()); - } - - @Test - public void shouldUnfoldSimpleArray() { - assertThat( - Array.unfold(10, x -> x == 0 - ? Option.none() - : Option.of(new Tuple2<>(x - 1, x)))) - .isEqualTo(of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)); - } - - // -- toString - - @Test - public void shouldStringifyNil() { - assertThat(empty().toString()).isEqualTo("Array()"); - } - - @Test - public void shouldStringifyNonNil() { - assertThat(of(1, 2, 3).toString()).isEqualTo("Array(1, 2, 3)"); - } - - // -- toArray - - @Test - public void shouldReturnSelfOnConvertToArray() { - Array value = of(1, 2, 3); - assertThat(value.toArray()).isSameAs(value); + .isInstanceOf(IndexOutOfBoundsException.class).hasMessage("get(1)"); + } + + @Nested + @DisplayName("partition") + class PartitionTest { + + @Test + public void shouldPartitionInOneIteration() { + final AtomicInteger count = new AtomicInteger(0); + final Tuple2, Array> results = of(1, 2, 3).partition(i -> { + count.incrementAndGet(); + return true; + }); + assertThat(results._1).isEqualTo(of(1, 2, 3)); + assertThat(results._2).isEqualTo(of()); + assertThat(count.get()).isEqualTo(3); + } + + // -- transform() + + @Test + public void shouldTransform() { + String transformed = of(42).transform(v -> String.valueOf(v.get())); + assertThat(transformed).isEqualTo("42"); + } + + } + + @Nested + @DisplayName("unfold") + class UnfoldTest { + + @Test + public void shouldUnfoldRightToEmpty() { + assertThat(Array.unfoldRight(0, x -> Option.none())).isEqualTo(empty()); + } + + @Test + public void shouldUnfoldRightSimpleArray() { + assertThat( + Array.unfoldRight(10, x -> x == 0 + ? Option.none() + : Option.of(new Tuple2<>(x, x - 1)))) + .isEqualTo(of(10, 9, 8, 7, 6, 5, 4, 3, 2, 1)); + } + + @Test + public void shouldUnfoldLeftToEmpty() { + assertThat(Array.unfoldLeft(0, x -> Option.none())).isEqualTo(empty()); + } + + @Test + public void shouldUnfoldLeftSimpleArray() { + assertThat( + Array.unfoldLeft(10, x -> x == 0 + ? Option.none() + : Option.of(new Tuple2<>(x - 1, x)))) + .isEqualTo(of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)); + } + + @Test + public void shouldUnfoldToEmpty() { + assertThat(Array.unfold(0, x -> Option.none())).isEqualTo(empty()); + } + + @Test + public void shouldUnfoldSimpleArray() { + assertThat( + Array.unfold(10, x -> x == 0 + ? Option.none() + : Option.of(new Tuple2<>(x - 1, x)))) + .isEqualTo(of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)); + } + + } + + @Nested + @DisplayName("toString") + class ToStringTest { + + @Test + public void shouldStringifyNil() { + assertThat(empty().toString()).isEqualTo("Array()"); + } + + @Test + public void shouldStringifyNonNil() { + assertThat(of(1, 2, 3).toString()).isEqualTo("Array(1, 2, 3)"); + } + + } + + @Nested + @DisplayName("toArray") + class ToArrayTest { + + @Test + public void shouldReturnSelfOnConvertToArray() { + Array value = of(1, 2, 3); + assertThat(value.toArray()).isSameAs(value); + } } } From 7a6073c8440469d6e73ef0c24e2e201f64a44c06 Mon Sep 17 00:00:00 2001 From: Boris van Katwijk Date: Wed, 1 Jan 2025 16:59:58 +0100 Subject: [PATCH 48/61] Update PredicatesTest.java --- src/test/java/io/vavr/PredicatesTest.java | 299 +++++++++++++--------- 1 file changed, 172 insertions(+), 127 deletions(-) diff --git a/src/test/java/io/vavr/PredicatesTest.java b/src/test/java/io/vavr/PredicatesTest.java index 56ae874e28..28ce1dbf7f 100644 --- a/src/test/java/io/vavr/PredicatesTest.java +++ b/src/test/java/io/vavr/PredicatesTest.java @@ -27,6 +27,8 @@ package io.vavr; import io.vavr.collection.List; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import java.math.BigDecimal; @@ -44,176 +46,219 @@ public class PredicatesTest { private static final Predicate IS_GT_ONE = i -> i > 1; private static final Predicate IS_GT_TWO = i -> i > 2; - // -- allOf + @Nested + @DisplayName("allOf") + class AllOfTest { - @Test - public void shouldTestAllOf_PositiveCase() { - assertThat(allOf().test(1)).isTrue(); - assertThat(allOf(IS_GT_ONE, IS_GT_TWO).test(3)).isTrue(); - } - - @Test - public void shouldTestAllOf_NegativeCase() { - assertThat(allOf(IS_GT_ONE, IS_GT_TWO).test(2)).isFalse(); - } + @Test + public void shouldTestAllOf_PositiveCase() { + assertThat(allOf().test(1)).isTrue(); + assertThat(allOf(IS_GT_ONE, IS_GT_TWO).test(3)).isTrue(); + } - // -- anyOf + @Test + public void shouldTestAllOf_NegativeCase() { + assertThat(allOf(IS_GT_ONE, IS_GT_TWO).test(2)).isFalse(); + } - @Test - public void shouldTestAnyOf_PositiveCase() { - assertThat(anyOf(IS_GT_ONE, IS_GT_TWO).test(3)).isTrue(); - assertThat(anyOf(IS_GT_ONE, IS_GT_TWO).test(2)).isTrue(); } - @Test - public void shouldTestAnyOf_NegativeCase() { - assertThat(anyOf().test(1)).isFalse(); - assertThat(anyOf(IS_GT_ONE, IS_GT_TWO).test(1)).isFalse(); - } + @Nested + @DisplayName("anyOf") + class AnyOfTest { - // -- exits + @Test + public void shouldTestAnyOf_PositiveCase() { + assertThat(anyOf(IS_GT_ONE, IS_GT_TWO).test(3)).isTrue(); + assertThat(anyOf(IS_GT_ONE, IS_GT_TWO).test(2)).isTrue(); + } - @Test - public void shouldTestExists_PositiveCase() { - assertThat(exists(IS_GT_ONE).test(List.of(1, 3))).isTrue(); - } + @Test + public void shouldTestAnyOf_NegativeCase() { + assertThat(anyOf().test(1)).isFalse(); + assertThat(anyOf(IS_GT_ONE, IS_GT_TWO).test(1)).isFalse(); + } - @Test - public void shouldTestExists_NegativeCase() { - assertThat(exists(IS_GT_ONE).test(List.of(1, 0))).isFalse(); } - @Test - public void shouldCheckExistsByLiftingPredicateInContravariantPositionToPredicateInCovariantPosition() { - final List list = List(1, 2, 3); - final Predicate p = n -> n.intValue() % 2 == 0; - final boolean actual = Match(list).of( - Case($(exists(p)), true), - Case($(), false) - ); - assertThat(actual).isTrue(); + @Nested + @DisplayName("exits") + class ExitsTest { + + @Test + public void shouldTestExists_PositiveCase() { + assertThat(exists(IS_GT_ONE).test(List.of(1, 3))).isTrue(); + } + + @Test + public void shouldTestExists_NegativeCase() { + assertThat(exists(IS_GT_ONE).test(List.of(1, 0))).isFalse(); + } + + @Test + public void shouldCheckExistsByLiftingPredicateInContravariantPositionToPredicateInCovariantPosition() { + final List list = List(1, 2, 3); + final Predicate p = n -> n.intValue() % 2 == 0; + final boolean actual = Match(list).of( + Case($(exists(p)), true), + Case($(), false) + ); + assertThat(actual).isTrue(); + } + } - // -- forAll + @Nested + @DisplayName("forAll") + class ForAllTest { + + @Test + public void shouldTestForAll_PositiveCase() { + assertThat(forAll(IS_GT_ONE).test(List.of(2, 3))).isTrue(); + } + + @Test + public void shouldTestForAll_NegativeCase() { + assertThat(forAll(IS_GT_ONE).test(List.of(3, 0))).isFalse(); + } + + @Test + public void shouldCheckForAllByLiftingPredicateInContravariantPositionToPredicateInCovariantPosition() { + final List list = List(1, 2, 3); + final Predicate p = n -> n.intValue() > 0; + final boolean actual = Match(list).of( + Case($(forAll(p)), true), + Case($(), false) + ); + assertThat(actual).isTrue(); + } - @Test - public void shouldTestForAll_PositiveCase() { - assertThat(forAll(IS_GT_ONE).test(List.of(2, 3))).isTrue(); } - @Test - public void shouldTestForAll_NegativeCase() { - assertThat(forAll(IS_GT_ONE).test(List.of(3, 0))).isFalse(); - } + @Nested + @DisplayName("instanceOf") + class InstanceOfTest { - @Test - public void shouldCheckForAllByLiftingPredicateInContravariantPositionToPredicateInCovariantPosition() { - final List list = List(1, 2, 3); - final Predicate p = n -> n.intValue() > 0; - final boolean actual = Match(list).of( - Case($(forAll(p)), true), - Case($(), false) - ); - assertThat(actual).isTrue(); - } + @Test + public void shouldTestInstanceOf_PositiveCase() { + assertThat(instanceOf(Number.class).test(1)).isTrue(); + assertThat(instanceOf(Number.class).test(new BigDecimal("1"))).isTrue(); + assertThat(IS_RUNTIME_EXCEPTION.test(new NullPointerException())).isTrue(); + } - // -- instanceOf + @Test + public void shouldTestInstanceOf_NegativeCase() { + assertThat(IS_RUNTIME_EXCEPTION.test(new Exception())).isFalse(); + assertThat(IS_RUNTIME_EXCEPTION.test(new Error("error"))).isFalse(); + assertThat(IS_RUNTIME_EXCEPTION.test(null)).isFalse(); + } - @Test - public void shouldTestInstanceOf_PositiveCase() { - assertThat(instanceOf(Number.class).test(1)).isTrue(); - assertThat(instanceOf(Number.class).test(new BigDecimal("1"))).isTrue(); - assertThat(IS_RUNTIME_EXCEPTION.test(new NullPointerException())).isTrue(); } - @Test - public void shouldTestInstanceOf_NegativeCase() { - assertThat(IS_RUNTIME_EXCEPTION.test(new Exception())).isFalse(); - assertThat(IS_RUNTIME_EXCEPTION.test(new Error("error"))).isFalse(); - assertThat(IS_RUNTIME_EXCEPTION.test(null)).isFalse(); - } + @Nested + @DisplayName("is") + class IsTest { - // -- is + @Test + public void shouldTestIs_PositiveCase() { + assertThat(is(1).test(1)).isTrue(); + assertThat(is((CharSequence) "1").test("1")).isTrue(); + } - @Test - public void shouldTestIs_PositiveCase() { - assertThat(is(1).test(1)).isTrue(); - assertThat(is((CharSequence) "1").test("1")).isTrue(); - } + @Test + public void shouldTestIs_NegativeCase() { + assertThat(is(1).test(2)).isFalse(); + assertThat(is((CharSequence) "1").test(new StringBuilder("1"))).isFalse(); + } - @Test - public void shouldTestIs_NegativeCase() { - assertThat(is(1).test(2)).isFalse(); - assertThat(is((CharSequence) "1").test(new StringBuilder("1"))).isFalse(); } - // -- isIn + @Nested + @DisplayName("isIn") + class IsInTest { - @Test - public void shouldTestIsIn_PositiveCase() { - assertThat(isIn(1, 2, 3).test(2)).isTrue(); - assertThat(isIn((CharSequence) "1", "2", "3").test("2")).isTrue(); - } + @Test + public void shouldTestIsIn_PositiveCase() { + assertThat(isIn(1, 2, 3).test(2)).isTrue(); + assertThat(isIn((CharSequence) "1", "2", "3").test("2")).isTrue(); + } + + @Test + public void shouldTestIsIn_NegativeCase() { + assertThat(isIn(1, 2, 3).test(4)).isFalse(); + assertThat(isIn((CharSequence) "1", "2", "3").test("4")).isFalse(); + } - @Test - public void shouldTestIsIn_NegativeCase() { - assertThat(isIn(1, 2, 3).test(4)).isFalse(); - assertThat(isIn((CharSequence) "1", "2", "3").test("4")).isFalse(); } - // -- isNull + @Nested + @DisplayName("isNull") + class IsNullTest { - @Test - public void shouldTestIsNull_PositiveCase() { - assertThat(isNull().test(null)).isTrue(); - } + @Test + public void shouldTestIsNull_PositiveCase() { + assertThat(isNull().test(null)).isTrue(); + } + + @Test + public void shouldTestIsNull_NegativeCase() { + assertThat(isNull().test("")).isFalse(); + } - @Test - public void shouldTestIsNull_NegativeCase() { - assertThat(isNull().test("")).isFalse(); } - // -- isNotNull + @Nested + @DisplayName("isNotNull") + class IsNotNullTest { - @Test - public void shouldTestIsNotNull_PositiveCase() { - assertThat(isNotNull().test("")).isTrue(); - } + @Test + public void shouldTestIsNotNull_PositiveCase() { + assertThat(isNotNull().test("")).isTrue(); + } + + @Test + public void shouldTestIsNotNull_NegativeCase() { + assertThat(isNotNull().test(null)).isFalse(); + } - @Test - public void shouldTestIsNotNull_NegativeCase() { - assertThat(isNotNull().test(null)).isFalse(); } - // -- noneOf + @Nested + @DisplayName("noneOf") + class NoneOfTest { - @Test - public void shouldTestNoneOf_PositiveCase() { - assertThat(noneOf().test(1)).isTrue(); - assertThat(noneOf(IS_GT_ONE, IS_GT_TWO).test(1)).isTrue(); - } + @Test + public void shouldTestNoneOf_PositiveCase() { + assertThat(noneOf().test(1)).isTrue(); + assertThat(noneOf(IS_GT_ONE, IS_GT_TWO).test(1)).isTrue(); + } + + @Test + public void shouldTestNoneOf_NegativeCase() { + assertThat(noneOf(IS_GT_ONE).test(2)).isFalse(); + assertThat(noneOf(IS_GT_ONE, IS_GT_TWO).test(2)).isFalse(); + } - @Test - public void shouldTestNoneOf_NegativeCase() { - assertThat(noneOf(IS_GT_ONE).test(2)).isFalse(); - assertThat(noneOf(IS_GT_ONE, IS_GT_TWO).test(2)).isFalse(); } - // -- not + @Nested + @DisplayName("not") + class NotTest { - @Test - public void shouldThrowWhenNegatingNull() { - assertThatThrownBy(() -> not(null)).isInstanceOf(NullPointerException.class); - } + @Test + public void shouldThrowWhenNegatingNull() { + assertThatThrownBy(() -> not(null)).isInstanceOf(NullPointerException.class); + } - @Test - public void shouldNegate_PositiveCase() { - assertThat(not(IS_GT_ONE).test(0)).isTrue(); - } + @Test + public void shouldNegate_PositiveCase() { + assertThat(not(IS_GT_ONE).test(0)).isTrue(); + } - @Test - public void shouldNegate_NegativeCase() { - assertThat(not(IS_GT_ONE).test(2)).isFalse(); - } + @Test + public void shouldNegate_NegativeCase() { + assertThat(not(IS_GT_ONE).test(2)).isFalse(); + } + } } From 3f4b02f81ddf6827b29491aa85042c3d7e285f84 Mon Sep 17 00:00:00 2001 From: Boris van Katwijk Date: Wed, 1 Jan 2025 17:01:09 +0100 Subject: [PATCH 49/61] Update MatchTest.java --- src/test/java/io/vavr/MatchTest.java | 685 ++++++++++++++------------- 1 file changed, 365 insertions(+), 320 deletions(-) diff --git a/src/test/java/io/vavr/MatchTest.java b/src/test/java/io/vavr/MatchTest.java index e6fd6d10a1..0c9d15d6fe 100644 --- a/src/test/java/io/vavr/MatchTest.java +++ b/src/test/java/io/vavr/MatchTest.java @@ -32,6 +32,8 @@ import io.vavr.control.Option; import io.vavr.control.Option.Some; import io.vavr.control.Validation; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import java.math.BigDecimal; @@ -48,292 +50,308 @@ public class MatchTest { - // -- MatchError + @Nested + @DisplayName("MatchError") + class MatchErrorTest { - @Test - public void shouldThrowIfNotMatching() { - assertThrows(MatchError.class, () -> Match(new Object()).of( - Case($(ignored -> false), o -> null) - )); - } + @Test + public void shouldThrowIfNotMatching() { + assertThrows(MatchError.class, () -> Match(new Object()).of( + Case($(ignored -> false), o -> null) + )); + } - // -- $() + // -- $() - @Test - public void shouldMatchNullWithAnyReturningValue() { - final Match.Case _case = Case($(), 1); - final Object obj = null; - assertThat(_case.isDefinedAt(obj)).isTrue(); - assertThat(_case.apply(obj)).isEqualTo(1); - } + @Test + public void shouldMatchNullWithAnyReturningValue() { + final Match.Case _case = Case($(), 1); + final Object obj = null; + assertThat(_case.isDefinedAt(obj)).isTrue(); + assertThat(_case.apply(obj)).isEqualTo(1); + } - @Test - public void shouldMatchAnyReturningValue() { - final Match.Case _case = Case($(), 1); - final Object obj = new Object(); - assertThat(_case.isDefinedAt(obj)).isTrue(); - assertThat(_case.apply(obj)).isEqualTo(1); - } + @Test + public void shouldMatchAnyReturningValue() { + final Match.Case _case = Case($(), 1); + final Object obj = new Object(); + assertThat(_case.isDefinedAt(obj)).isTrue(); + assertThat(_case.apply(obj)).isEqualTo(1); + } - @Test - public void shouldMatchNullWithAnyReturningAppliedFunction() { - final Match.Case _case = Case($(), o -> 1); - final Object obj = null; - assertThat(_case.isDefinedAt(obj)).isTrue(); - assertThat(_case.apply(obj)).isEqualTo(1); - } + @Test + public void shouldMatchNullWithAnyReturningAppliedFunction() { + final Match.Case _case = Case($(), o -> 1); + final Object obj = null; + assertThat(_case.isDefinedAt(obj)).isTrue(); + assertThat(_case.apply(obj)).isEqualTo(1); + } - @Test - public void shouldMatchAnyReturningAppliedFunction() { - final Match.Case _case = Case($(), o -> 1); - final Object obj = new Object(); - assertThat(_case.isDefinedAt(obj)).isTrue(); - assertThat(_case.apply(obj)).isEqualTo(1); - } + @Test + public void shouldMatchAnyReturningAppliedFunction() { + final Match.Case _case = Case($(), o -> 1); + final Object obj = new Object(); + assertThat(_case.isDefinedAt(obj)).isTrue(); + assertThat(_case.apply(obj)).isEqualTo(1); + } - @Test - public void shouldTakeFirstMatch() { - final String actual = Match(new Object()).of( - Case($(), "first"), - Case($(), "second") - ); - assertThat(actual).isEqualTo("first"); - } + @Test + public void shouldTakeFirstMatch() { + final String actual = Match(new Object()).of( + Case($(), "first"), + Case($(), "second") + ); + assertThat(actual).isEqualTo("first"); + } - // -- $(value) + // -- $(value) - @Test - public void shouldMatchValueReturningValue() { - final Object obj = new Object(); - final Match.Case _case = Case($(obj), 1); - assertThat(_case.isDefinedAt(obj)).isTrue(); - assertThat(_case.apply(obj)).isEqualTo(1); - } + @Test + public void shouldMatchValueReturningValue() { + final Object obj = new Object(); + final Match.Case _case = Case($(obj), 1); + assertThat(_case.isDefinedAt(obj)).isTrue(); + assertThat(_case.apply(obj)).isEqualTo(1); + } - @Test - public void shouldMatchValueReturningValue_NegativeCase() { - final Object obj = new Object(); - final Match.Case _case = Case($(obj), 1); - assertThat(_case.isDefinedAt(new Object())).isFalse(); - } + @Test + public void shouldMatchValueReturningValue_NegativeCase() { + final Object obj = new Object(); + final Match.Case _case = Case($(obj), 1); + assertThat(_case.isDefinedAt(new Object())).isFalse(); + } - @Test - public void shouldMatchValueReturningAppliedFunction() { - final Object obj = new Object(); - final Match.Case _case = Case($(obj), o -> 1); - assertThat(_case.isDefinedAt(obj)).isTrue(); - assertThat(_case.apply(obj)).isEqualTo(1); - } + @Test + public void shouldMatchValueReturningAppliedFunction() { + final Object obj = new Object(); + final Match.Case _case = Case($(obj), o -> 1); + assertThat(_case.isDefinedAt(obj)).isTrue(); + assertThat(_case.apply(obj)).isEqualTo(1); + } - @Test - public void shouldMatchValueReturningAppliedFunction_NegativeCase() { - final Object obj = new Object(); - final Match.Case _case = Case($(obj), o -> 1); - assertThat(_case.isDefinedAt(new Object())).isFalse(); - } + @Test + public void shouldMatchValueReturningAppliedFunction_NegativeCase() { + final Object obj = new Object(); + final Match.Case _case = Case($(obj), o -> 1); + assertThat(_case.isDefinedAt(new Object())).isFalse(); + } - // -- $(predicate) + // -- $(predicate) - @Test - public void shouldMatchPredicateReturningValue() { - final Object obj = new Object(); - final Match.Case _case = Case($(is(obj)), 1); - assertThat(_case.isDefinedAt(obj)).isTrue(); - assertThat(_case.apply(obj)).isEqualTo(1); - } + @Test + public void shouldMatchPredicateReturningValue() { + final Object obj = new Object(); + final Match.Case _case = Case($(is(obj)), 1); + assertThat(_case.isDefinedAt(obj)).isTrue(); + assertThat(_case.apply(obj)).isEqualTo(1); + } - @Test - public void shouldMatchPredicateReturningValue_NegativeCase() { - final Object obj = new Object(); - final Match.Case _case = Case($(is(obj)), 1); - assertThat(_case.isDefinedAt(new Object())).isFalse(); - } + @Test + public void shouldMatchPredicateReturningValue_NegativeCase() { + final Object obj = new Object(); + final Match.Case _case = Case($(is(obj)), 1); + assertThat(_case.isDefinedAt(new Object())).isFalse(); + } - @Test - public void shouldMatchPredicateReturningAppliedFunction() { - final Object obj = new Object(); - final Match.Case _case = Case($(is(obj)), o -> 1); - assertThat(_case.isDefinedAt(obj)).isTrue(); - assertThat(_case.apply(obj)).isEqualTo(1); - } + @Test + public void shouldMatchPredicateReturningAppliedFunction() { + final Object obj = new Object(); + final Match.Case _case = Case($(is(obj)), o -> 1); + assertThat(_case.isDefinedAt(obj)).isTrue(); + assertThat(_case.apply(obj)).isEqualTo(1); + } - @Test - public void shouldMatchPredicateReturningAppliedFunction_NegativeCase() { - final Object obj = new Object(); - final Match.Case _case = Case($(is(obj)), o -> 1); - assertThat(_case.isDefinedAt(new Object())).isFalse(); - } + @Test + public void shouldMatchPredicateReturningAppliedFunction_NegativeCase() { + final Object obj = new Object(); + final Match.Case _case = Case($(is(obj)), o -> 1); + assertThat(_case.isDefinedAt(new Object())).isFalse(); + } - @Test(/* #2419 */) - public void shouldPatternMatchForGivenPredicate() { - final List list = List(1, 2, 3); - final Predicate p = n -> n.intValue() > 0; - final boolean actual = Match(list).of( - Case($(l -> l.exists(anyOf(p))), true), - Case($(), false) - ); - assertThat(actual).isTrue(); - } + @Test(/* #2419 */) + public void shouldPatternMatchForGivenPredicate() { + final List list = List(1, 2, 3); + final Predicate p = n -> n.intValue() > 0; + final boolean actual = Match(list).of( + Case($(l -> l.exists(anyOf(p))), true), + Case($(), false) + ); + assertThat(actual).isTrue(); + } - // -- multiple cases + // -- multiple cases + + // i match { + // case 1 => "one" + // case 2 => "two" + // case _ => "many" + // } + + @Test + public void shouldMatchIntUsingPatterns() { + final String actual = Match(3).of( + Case($(1), "one"), + Case($(2), "two"), + Case($(), "many") + ); + assertThat(actual).isEqualTo("many"); + } - // i match { - // case 1 => "one" - // case 2 => "two" - // case _ => "many" - // } + @Test + public void shouldMatchIntUsingPredicates() { + final String actual = Match(3).of( + Case($(is(1)), "one"), + Case($(is(2)), "two"), + Case($(), "many") + ); + assertThat(actual).isEqualTo("many"); + } - @Test - public void shouldMatchIntUsingPatterns() { - final String actual = Match(3).of( - Case($(1), "one"), - Case($(2), "two"), - Case($(), "many") - ); - assertThat(actual).isEqualTo("many"); - } + @Test + public void shouldComputeUpperBoundOfReturnValue() { + final Number num = Match(3).of( + Case($(is(1)), 1), + Case($(is(2)), 2.0), + Case($(), i -> new BigDecimal("" + i)) + ); + assertThat(num).isEqualTo(new BigDecimal("3")); + } - @Test - public void shouldMatchIntUsingPredicates() { - final String actual = Match(3).of( - Case($(is(1)), "one"), - Case($(is(2)), "two"), - Case($(), "many") - ); - assertThat(actual).isEqualTo("many"); } - @Test - public void shouldComputeUpperBoundOfReturnValue() { - final Number num = Match(3). of( - Case($(is(1)), 1), - Case($(is(2)), 2.0), - Case($(), i -> new BigDecimal("" + i)) - ); - assertThat(num).isEqualTo(new BigDecimal("3")); - } + @Nested + @DisplayName("instanceOf") + class InstanceOfTest { - // -- instanceOf + @Test + public void shouldMatchUsingInstanceOf() { + final Object obj = 1; + final int actual = Match(obj).of( + Case($(instanceOf(Year.class)), y -> 0), + Case($(instanceOf(Integer.class)), i -> 1) + ); + assertThat(actual).isEqualTo(1); + } - @Test - public void shouldMatchUsingInstanceOf() { - final Object obj = 1; - final int actual = Match(obj).of( - Case($(instanceOf(Year.class)), y -> 0), - Case($(instanceOf(Integer.class)), i -> 1) - ); - assertThat(actual).isEqualTo(1); } - // -- Either + @Nested + @DisplayName("Either") + class EitherTest { - @Test - public void shouldMatchLeft() { - final Either either = Either.left(1); - final String actual = Match(either).of( - Case($Left($()), l -> "left: " + l), - Case($Right($()), r -> "right: " + r) - ); - assertThat(actual).isEqualTo("left: 1"); - } + @Test + public void shouldMatchLeft() { + final Either either = Either.left(1); + final String actual = Match(either).of( + Case($Left($()), l -> "left: " + l), + Case($Right($()), r -> "right: " + r) + ); + assertThat(actual).isEqualTo("left: 1"); + } + + @Test + public void shouldMatchRight() { + final Either either = Either.right("a"); + final String actual = Match(either).of( + Case($Left($()), l -> "left: " + l), + Case($Right($()), r -> "right: " + r) + ); + assertThat(actual).isEqualTo("right: a"); + } - @Test - public void shouldMatchRight() { - final Either either = Either.right("a"); - final String actual = Match(either).of( - Case($Left($()), l -> "left: " + l), - Case($Right($()), r -> "right: " + r) - ); - assertThat(actual).isEqualTo("right: a"); } - // -- Option + @Nested + @DisplayName("Option") + class OptionTest { - @Test - public void shouldMatchSome() { - final Option opt = Option.some(1); - final String actual = Match(opt).of( - Case($None(), "no value"), - Case($Some($()), String::valueOf) - ); - assertThat(actual).isEqualTo("1"); - } + @Test + public void shouldMatchSome() { + final Option opt = Option.some(1); + final String actual = Match(opt).of( + Case($None(), "no value"), + Case($Some($()), String::valueOf) + ); + assertThat(actual).isEqualTo("1"); + } - @Test - public void shouldMatchNone() { - final Option opt = Option.none(); - final String actual = Match(opt).of( - Case($Some($()), String::valueOf), - Case($None(), "no value") - ); - assertThat(actual).isEqualTo("no value"); - } + @Test + public void shouldMatchNone() { + final Option opt = Option.none(); + final String actual = Match(opt).of( + Case($Some($()), String::valueOf), + Case($None(), "no value") + ); + assertThat(actual).isEqualTo("no value"); + } - @Test - public void shouldDecomposeSomeTuple() { - final Option> tuple2Option = Option.of(Tuple.of("Test", 123)); - final Tuple2 actual = Match(tuple2Option).of( - Case($Some($()), value -> { - @SuppressWarnings("UnnecessaryLocalVariable") - final Tuple2 tuple2 = value; // types are inferred correctly! - return tuple2; - }) - ); - assertThat(actual).isEqualTo(Tuple.of("Test", 123)); - } + @Test + public void shouldDecomposeSomeTuple() { + final Option> tuple2Option = Option.of(Tuple.of("Test", 123)); + final Tuple2 actual = Match(tuple2Option).of( + Case($Some($()), value -> { + @SuppressWarnings("UnnecessaryLocalVariable") final Tuple2 tuple2 = value; // types are inferred correctly! + return tuple2; + }) + ); + assertThat(actual).isEqualTo(Tuple.of("Test", 123)); + } + + @Test + public void shouldDecomposeSomeSomeTuple() { + final Option>> tuple2OptionOption = Option.of(Option.of(Tuple.of("Test", 123))); + final Some> actual = Match(tuple2OptionOption).of( + Case($Some($Some($(Tuple.of("Test", 123)))), value -> { + @SuppressWarnings("UnnecessaryLocalVariable") final Some> some = value; // types are inferred correctly! + return some; + }) + ); + assertThat(actual).isEqualTo(Option.of(Tuple.of("Test", 123))); + } - @Test - public void shouldDecomposeSomeSomeTuple() { - final Option>> tuple2OptionOption = Option.of(Option.of(Tuple.of("Test", 123))); - final Some> actual = Match(tuple2OptionOption).of( - Case($Some($Some($(Tuple.of("Test", 123)))), value -> { - @SuppressWarnings("UnnecessaryLocalVariable") - final Some> some = value; // types are inferred correctly! - return some; - }) - ); - assertThat(actual).isEqualTo(Option.of(Tuple.of("Test", 123))); } - // -- List + @Nested + @DisplayName("List") + class ListTest { - @Test - public void shouldDecomposeEmptyList() { - final List list = List.empty(); - final boolean isEmpty = Match(list).of( - Case($Cons($(), $()), (x, xs) -> false), - Case($Nil(), true) - ); - assertThat(isEmpty).isTrue(); - } + @Test + public void shouldDecomposeEmptyList() { + final List list = List.empty(); + final boolean isEmpty = Match(list).of( + Case($Cons($(), $()), (x, xs) -> false), + Case($Nil(), true) + ); + assertThat(isEmpty).isTrue(); + } - @Test - public void shouldDecomposeNonEmptyList() { - final List list = List.of(1); - final boolean isNotEmpty = Match(list).of( - Case($Nil(), false), - Case($Cons($(), $()), (x, xs) -> true) - ); - assertThat(isNotEmpty).isTrue(); - } + @Test + public void shouldDecomposeNonEmptyList() { + final List list = List.of(1); + final boolean isNotEmpty = Match(list).of( + Case($Nil(), false), + Case($Cons($(), $()), (x, xs) -> true) + ); + assertThat(isNotEmpty).isTrue(); + } - @SuppressWarnings("UnnecessaryLocalVariable") - @Test - public void shouldDecomposeListOfTuple3() { - final List> tuple3List = List.of( - Tuple.of("begin", 10, 4.5), - Tuple.of("middle", 11, 0.0), - Tuple.of("end", 12, 1.2)); - final String actual = Match(tuple3List).of( - Case($Cons($(), $()), (x, xs) -> { - // types are inferred correctly! - final Tuple3 head = x; - final List> tail = xs; - return head + "::" + tail; - }) - ); - assertThat(actual).isEqualTo("(begin, 10, 4.5)::List((middle, 11, 0.0), (end, 12, 1.2))"); - } + @SuppressWarnings("UnnecessaryLocalVariable") + @Test + public void shouldDecomposeListOfTuple3() { + final List> tuple3List = List.of( + Tuple.of("begin", 10, 4.5), + Tuple.of("middle", 11, 0.0), + Tuple.of("end", 12, 1.2)); + final String actual = Match(tuple3List).of( + Case($Cons($(), $()), (x, xs) -> { + // types are inferred correctly! + final Tuple3 head = x; + final List> tail = xs; + return head + "::" + tail; + }) + ); + assertThat(actual).isEqualTo("(begin, 10, 4.5)::List((middle, 11, 0.0), (end, 12, 1.2))"); + } /* JDK 9 compiler errors: @@ -357,106 +375,127 @@ public void shouldDecomposeListWithNonEmptyTail() { } */ - // -- Set - - @Test - public void shouldDecomposeSet() { - final Set abc = Set("abc"); - final Set result = Match(abc).of( // Does not compile: the Java inference engine sees abc as a Function1 before a Set thus expects result to be of type Boolean - Case($(), () -> abc) - ); - assertThat(result).isEqualTo(abc); } - // -- Validation + @Nested + @DisplayName("Set") + class SetTest { + + @Test + public void shouldDecomposeSet() { + final Set abc = Set("abc"); + final Set result = Match(abc).of( // Does not compile: the Java inference engine sees abc as a Function1 before a Set thus expects result to be of type Boolean + Case($(), () -> abc) + ); + assertThat(result).isEqualTo(abc); + } - @Test - public void shouldDecomposeValid() { - final Validation valid = Validation.valid(1); - final String actual = Match(valid).of( - Case($Valid($(1)), i -> "ok"), - Case($Invalid($()), error -> error) - ); - assertThat(actual).isEqualTo("ok"); } - @Test - public void shouldDecomposeInvalid() { - final Validation valid = Validation.invalid("ok"); - final String actual = Match(valid).of( - Case($Valid($()), i -> "error"), - Case($Invalid($("ok")), error -> error) - ); - assertThat(actual).isEqualTo("ok"); + @Nested + @DisplayName("Validation") + class ValidationTest { + + @Test + public void shouldDecomposeValid() { + final Validation valid = Validation.valid(1); + final String actual = Match(valid).of( + Case($Valid($(1)), i -> "ok"), + Case($Invalid($()), error -> error) + ); + assertThat(actual).isEqualTo("ok"); + } + + @Test + public void shouldDecomposeInvalid() { + final Validation valid = Validation.invalid("ok"); + final String actual = Match(valid).of( + Case($Valid($()), i -> "error"), + Case($Invalid($("ok")), error -> error) + ); + assertThat(actual).isEqualTo("ok"); + } + } - // -- run + @Nested + @DisplayName("run") + class RunTest { - @Test - public void shouldRunUnitOfWork() { + @Test + public void shouldRunUnitOfWork() { - class OuterWorld { + class OuterWorld { - String effect = null; + String effect = null; - void displayHelp() { - effect = "help"; - } + void displayHelp() { + effect = "help"; + } - void displayVersion() { - effect = "version"; + void displayVersion() { + effect = "version"; + } } - } - final OuterWorld outerWorld = new OuterWorld(); + final OuterWorld outerWorld = new OuterWorld(); - Match("-v").of( - Case($(isIn("-h", "--help")), o -> run(outerWorld::displayHelp)), - Case($(isIn("-v", "--version")), o -> run(outerWorld::displayVersion)), - Case($(), o -> { throw new IllegalArgumentException(); }) - ); + Match("-v").of( + Case($(isIn("-h", "--help")), o -> run(outerWorld::displayHelp)), + Case($(isIn("-v", "--version")), o -> run(outerWorld::displayVersion)), + Case($(), o -> { + throw new IllegalArgumentException(); + }) + ); - assertThat(outerWorld.effect).isEqualTo("version"); - } + assertThat(outerWorld.effect).isEqualTo("version"); + } - @Test - public void shouldRunWithInferredArguments() { + @Test + public void shouldRunWithInferredArguments() { - class OuterWorld { + class OuterWorld { - Number effect = null; + Number effect = null; - void writeInt(int i) { - effect = i; - } + void writeInt(int i) { + effect = i; + } - void writeDouble(double d) { - effect = d; + void writeDouble(double d) { + effect = d; + } } - } - final OuterWorld outerWorld = new OuterWorld(); - final Object obj = .1d; + final OuterWorld outerWorld = new OuterWorld(); + final Object obj = .1d; - Match(obj).of( - Case($(instanceOf(Integer.class)), i -> run(() -> outerWorld.writeInt(i))), - Case($(instanceOf(Double.class)), d -> run(() -> outerWorld.writeDouble(d))), - Case($(), o -> { throw new NumberFormatException(); }) - ); + Match(obj).of( + Case($(instanceOf(Integer.class)), i -> run(() -> outerWorld.writeInt(i))), + Case($(instanceOf(Double.class)), d -> run(() -> outerWorld.writeDouble(d))), + Case($(), o -> { + throw new NumberFormatException(); + }) + ); + + assertThat(outerWorld.effect).isEqualTo(.1d); + } - assertThat(outerWorld.effect).isEqualTo(.1d); } - // -- Developer + @Nested + @DisplayName("Developer") + class DeveloperTest { - @Test - public void shouldMatchCustomTypeWithUnapplyMethod() { - final Person person = new Developer("Daniel", true, Option.some(13)); - final String actual = Match(person).of( - Case($Developer($("Daniel"), $(true), $()), Person.Util::devInfo), - Case($(), p -> "Unknown person: " + p.getName()) - ); - assertThat(actual).isEqualTo("Daniel is caffeinated."); + @Test + public void shouldMatchCustomTypeWithUnapplyMethod() { + final Person person = new Developer("Daniel", true, Option.some(13)); + final String actual = Match(person).of( + Case($Developer($("Daniel"), $(true), $()), Person.Util::devInfo), + Case($(), p -> "Unknown person: " + p.getName()) + ); + assertThat(actual).isEqualTo("Daniel is caffeinated."); + } } interface Person { @@ -480,11 +519,17 @@ static final class Developer implements Person { this.number = number; } - public String getName() { return name; } + public String getName() { + return name; + } - public boolean isCaffeinated() { return isCaffeinated; } + public boolean isCaffeinated() { + return isCaffeinated; + } - public Option number() { return number; } + public Option number() { + return number; + } static class $ { static Tuple3> Developer(Developer dev) { From 6adb5ca668a7260bf205293d36ee1df184f99713 Mon Sep 17 00:00:00 2001 From: Boris van Katwijk Date: Wed, 1 Jan 2025 17:01:49 +0100 Subject: [PATCH 50/61] Update IterableTest.java --- src/test/java/io/vavr/IterableTest.java | 54 +++++++++++++------------ 1 file changed, 29 insertions(+), 25 deletions(-) diff --git a/src/test/java/io/vavr/IterableTest.java b/src/test/java/io/vavr/IterableTest.java index ac38eccda7..192091edb0 100644 --- a/src/test/java/io/vavr/IterableTest.java +++ b/src/test/java/io/vavr/IterableTest.java @@ -30,6 +30,8 @@ import io.vavr.collection.Stream; import io.vavr.collection.List; import io.vavr.control.Option; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import java.util.Arrays; @@ -40,34 +42,36 @@ // Specific tests. For general tests, see AbstractIterableTest. public class IterableTest { - // -- eq + @Nested + @DisplayName("eq") + class EqTest { - @Test - public void shouldEqNoneAndEmptyList() { - assertThat(Option.none().eq(List.empty())).isTrue(); - assertThat(Option.none().eq(List.of(1))).isFalse(); - } + @Test + public void shouldEqNoneAndEmptyList() { + assertThat(Option.none().eq(List.empty())).isTrue(); + assertThat(Option.none().eq(List.of(1))).isFalse(); + } - @Test - public void shouldEqSomeAndNonEmptyList() { - assertThat(Option.some(1).eq(List.of(1))).isTrue(); - assertThat(Option.some(1).eq(List.of(2))).isFalse(); - assertThat(Option.some(1).eq(List.empty())).isFalse(); - } + @Test + public void shouldEqSomeAndNonEmptyList() { + assertThat(Option.some(1).eq(List.of(1))).isTrue(); + assertThat(Option.some(1).eq(List.of(2))).isFalse(); + assertThat(Option.some(1).eq(List.empty())).isFalse(); + } - @Test - public void shouldEqIterableAndJavaIterable() { - assertThat(List.of(1, 2, 3).eq(Arrays.asList(1, 2, 3))).isTrue(); - } + @Test + public void shouldEqIterableAndJavaIterable() { + assertThat(List.of(1, 2, 3).eq(Arrays.asList(1, 2, 3))).isTrue(); + } - @Test - public void shouldEqNestedIterables() { - // ((1, 2), ((3))) - final Value i1 = List.of(List.of(1, 2), Collections.singletonList(List.of(3))); - final Value i2 = Queue.of(Stream.of(1, 2), List.of(Lazy.of(() -> 3))); - final Value i3 = Queue.of(Stream.of(1, 2), List.of(List.of())); - assertThat(i1.eq(i2)).isTrue(); - assertThat(i1.eq(i3)).isFalse(); + @Test + public void shouldEqNestedIterables() { + // ((1, 2), ((3))) + final Value i1 = List.of(List.of(1, 2), Collections.singletonList(List.of(3))); + final Value i2 = Queue.of(Stream.of(1, 2), List.of(Lazy.of(() -> 3))); + final Value i3 = Queue.of(Stream.of(1, 2), List.of(List.of())); + assertThat(i1.eq(i2)).isTrue(); + assertThat(i1.eq(i3)).isFalse(); + } } - } From ad34127dce83afa140d1cac31ac8da85d163fc5d Mon Sep 17 00:00:00 2001 From: Boris van Katwijk Date: Fri, 3 Jan 2025 12:56:34 +0100 Subject: [PATCH 51/61] Delete dock.sh --- dock.sh | 4 ---- 1 file changed, 4 deletions(-) delete mode 100755 dock.sh diff --git a/dock.sh b/dock.sh deleted file mode 100755 index 2747725c08..0000000000 --- a/dock.sh +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/zsh - -docker run --rm -v .:/mnt alpine sh -c \ - "sh /mnt/nested.sh" \ No newline at end of file From f0bbb9efa15da84873a03fa529294ae07dea392a Mon Sep 17 00:00:00 2001 From: Boris van Katwijk Date: Fri, 3 Jan 2025 13:00:55 +0100 Subject: [PATCH 52/61] Revert "Update Option.java" This reverts commit 390b8db1cab13e6a9f13e2665880c4a34e2cdb87. --- src/main/java/io/vavr/control/Option.java | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/src/main/java/io/vavr/control/Option.java b/src/main/java/io/vavr/control/Option.java index d683bbfe3f..a3ce7c34c6 100644 --- a/src/main/java/io/vavr/control/Option.java +++ b/src/main/java/io/vavr/control/Option.java @@ -663,16 +663,6 @@ public final Option map(Function mapper) { return isEmpty() ? none() : some(mapper.apply(get())); } - - /** - * Converts this to a {@link Try}, then runs the given checked function if this is a {@link Try.Success}, - * passing the result of the current expression to it. - * - * @param The new component type - * @param mapper A checked function - * @return a {@code Try} - * @throws NullPointerException if {@code mapper} is null - */ public final Try mapTry(CheckedFunction1 mapper) { return toTry().mapTry(mapper); } From 7a7a534c911d592b4da113d45cb88dc5c7a37551 Mon Sep 17 00:00:00 2001 From: Boris van Katwijk Date: Fri, 3 Jan 2025 13:00:59 +0100 Subject: [PATCH 53/61] Revert "Update OptionTest.java" This reverts commit 088a2b33107a964fb1259432cc8ef7bad355472e. --- src/test/java/io/vavr/control/OptionTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/io/vavr/control/OptionTest.java b/src/test/java/io/vavr/control/OptionTest.java index bd300a0000..46834a3ebc 100644 --- a/src/test/java/io/vavr/control/OptionTest.java +++ b/src/test/java/io/vavr/control/OptionTest.java @@ -403,7 +403,7 @@ public void shouldMapSome() { public void shouldMapNone() { assertThat(Option. none().map(String::valueOf)).isEqualTo(Option.none()); } - + @Nested class MapTry { @Test From 8dec8408de2a2f23245d2f6e816ce78ff7ca5853 Mon Sep 17 00:00:00 2001 From: Boris van Katwijk Date: Fri, 3 Jan 2025 13:01:02 +0100 Subject: [PATCH 54/61] Revert "Add mapTry to Option" This reverts commit 967a6762bccbbe942005cb37be057e631e26b285. --- src/main/java/io/vavr/control/Option.java | 5 ---- src/test/java/io/vavr/control/OptionTest.java | 27 ------------------- 2 files changed, 32 deletions(-) diff --git a/src/main/java/io/vavr/control/Option.java b/src/main/java/io/vavr/control/Option.java index a3ce7c34c6..42ab9914d4 100644 --- a/src/main/java/io/vavr/control/Option.java +++ b/src/main/java/io/vavr/control/Option.java @@ -26,7 +26,6 @@ */ package io.vavr.control; -import io.vavr.CheckedFunction1; import io.vavr.PartialFunction; import io.vavr.collection.Iterator; import io.vavr.collection.Seq; @@ -663,10 +662,6 @@ public final Option map(Function mapper) { return isEmpty() ? none() : some(mapper.apply(get())); } - public final Try mapTry(CheckedFunction1 mapper) { - return toTry().mapTry(mapper); - } - /** * Folds either the {@code None} or the {@code Some} side of the Option value. * diff --git a/src/test/java/io/vavr/control/OptionTest.java b/src/test/java/io/vavr/control/OptionTest.java index 46834a3ebc..6d48dfb1b1 100644 --- a/src/test/java/io/vavr/control/OptionTest.java +++ b/src/test/java/io/vavr/control/OptionTest.java @@ -28,12 +28,9 @@ import io.vavr.*; import io.vavr.collection.Seq; -import io.vavr.concurrent.FutureTest; import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; -import java.io.IOException; import java.util.*; import java.util.concurrent.CompletableFuture; import java.util.concurrent.atomic.AtomicBoolean; @@ -403,30 +400,6 @@ public void shouldMapSome() { public void shouldMapNone() { assertThat(Option. none().map(String::valueOf)).isEqualTo(Option.none()); } - - @Nested - class MapTry { - @Test - public void shouldMapTrySome() { - assertThat(Option.of(1).mapTry(String::valueOf)).isEqualTo(Try.success("1")); - } - - @Test - public void shouldMapTryNone() { - Try result = Option.none().mapTry(String::valueOf); - assertThat(result.isFailure()).isTrue(); - assertThat(result.getCause().getClass()).isEqualTo(NoSuchElementException.class); - assertThat(result.getCause().getMessage()).isEqualTo("No value present"); - } - - @Test - public void shouldMapTryCheckedException() { - Try result = Option.of("a").mapTry(Integer::new); - assertThat(result.isFailure()).isTrue(); - assertThat(result.getCause().getClass()).isEqualTo(NumberFormatException.class); - assertThat(result.getCause().getMessage()).isEqualTo("For input string: \"a\""); - } - } // -- flatMap From 1feab7440f7d23abefe97e3932fc538c60ced289 Mon Sep 17 00:00:00 2001 From: Boris van Katwijk Date: Fri, 3 Jan 2025 13:04:47 +0100 Subject: [PATCH 55/61] Revert "Update API.java" This reverts commit fcdf049ddf5d2b74bbfb92072203d1b4f1cae7bb. --- src-gen/main/java/io/vavr/API.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src-gen/main/java/io/vavr/API.java b/src-gen/main/java/io/vavr/API.java index e6c95aabc8..aca28cc009 100644 --- a/src-gen/main/java/io/vavr/API.java +++ b/src-gen/main/java/io/vavr/API.java @@ -6081,7 +6081,7 @@ public static final class Case0 implements Case { private static final long serialVersionUID = 1L; private final Pattern0 pattern; - private transient final Function f; + private final Function f; private Case0(Pattern0 pattern, Function f) { this.pattern = pattern; @@ -6104,7 +6104,7 @@ public static final class Case1 implements Case { private static final long serialVersionUID = 1L; private final Pattern1 pattern; - private transient final Function f; + private final Function f; private Case1(Pattern1 pattern, Function f) { this.pattern = pattern; @@ -6127,7 +6127,7 @@ public static final class Case2 implements Case { private static final long serialVersionUID = 1L; private final Pattern2 pattern; - private transient final BiFunction f; + private final BiFunction f; private Case2(Pattern2 pattern, BiFunction f) { this.pattern = pattern; From e9a7b8aa5c7cd099fe2acd94d6e6b7c911d4c395 Mon Sep 17 00:00:00 2001 From: Boris van Katwijk Date: Sun, 19 Jan 2025 10:53:14 +0100 Subject: [PATCH 56/61] Update EitherLeftProjectionTest.java --- .../control/EitherLeftProjectionTest.java | 696 +++++++++--------- 1 file changed, 365 insertions(+), 331 deletions(-) diff --git a/src/test/java/io/vavr/control/EitherLeftProjectionTest.java b/src/test/java/io/vavr/control/EitherLeftProjectionTest.java index 120ae90365..52174b1e92 100644 --- a/src/test/java/io/vavr/control/EitherLeftProjectionTest.java +++ b/src/test/java/io/vavr/control/EitherLeftProjectionTest.java @@ -30,6 +30,8 @@ import io.vavr.AbstractValueTest; import io.vavr.Tuple; import io.vavr.Tuple2; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import java.util.*; @@ -39,16 +41,14 @@ @SuppressWarnings("deprecation") public class EitherLeftProjectionTest extends AbstractValueTest { - // -- AbstractValueTest - @Override protected Either.LeftProjection empty() { - return Either. right(null).left(); + return Either.right(null).left(); } @Override protected Either.LeftProjection of(T element) { - return Either. left(element).left(); + return Either.left(element).left(); } @SafeVarargs @@ -67,365 +67,399 @@ protected int getPeekNonNilPerformingAnAction() { return 1; } - // -- LeftProjection - - // get - - @Test - public void shouldThrowOnGetOnLeftProjectionOfRight() { - assertThrows(NoSuchElementException.class, () -> Either.right(1).left().get()); - } - - @Test - public void shouldGetOnLeftProjectionOfLeft() { - assertThat(Either.left(1).left().get()).isEqualTo(1); - } - - // orElse - - @Test - public void shouldLeftProjectionOrElseLeftProjection() { - final Either.LeftProjection elseProjection = API.Left(2).left(); - assertThat(API.Left(1).left().orElse(elseProjection).get()).isEqualTo(1); - assertThat(API.Right(1).left().orElse(elseProjection).get()).isEqualTo(2); - } - - @Test - public void shouldLeftProjectionOrElseLeftProjectionFromSupplier() { - final Either.LeftProjection elseProjection = API.Left(2).left(); - assertThat(API.Left(1).left().orElse(() -> elseProjection).get()).isEqualTo(1); - assertThat(API.Right(1).left().orElse(() -> elseProjection).get()).isEqualTo(2); - } - - // getOrElse - - @Test - public void shouldReturnLeftWhenOrElseOnLeftProjectionOfLeft() { - final Integer actual = Either.left(1).left().getOrElse(2); - assertThat(actual).isEqualTo(1); - } - - @Test - public void shouldReturnOtherWhenOrElseOnLeftProjectionOfRight() { - final Integer actual = Either. right("1").left().getOrElse(2); - assertThat(actual).isEqualTo(2); - } - - // getOrElse(Function) - - @Test - public void shouldReturnLeftWhenOrElseGetGivenFunctionOnLeftProjectionOfLeft() { - final Integer actual = Either.left(1).left().getOrElseGet(r -> 2); - assertThat(actual).isEqualTo(1); - } - - @Test - public void shouldReturnOtherWhenOrElseGetGivenFunctionOnLeftProjectionOfRight() { - final Integer actual = Either. right("1").left().getOrElseGet(r -> 2); - assertThat(actual).isEqualTo(2); - } - - // orElseRun - - @Test - public void shouldReturnLeftWhenOrElseRunOnLeftProjectionOfLeft() { - final boolean[] actual = new boolean[] { true }; - Either.left(1).left().orElseRun(s -> actual[0] = false); - assertThat(actual[0]).isTrue(); - } - - @Test - public void shouldReturnOtherWhenOrElseRunOnLeftProjectionOfRight() { - final boolean[] actual = new boolean[] { false }; - Either.right("1").left().orElseRun(s -> { - actual[0] = true; - }); - assertThat(actual[0]).isTrue(); - } - - // getOrElseThrow(Function) - - @Test - public void shouldReturnLeftWhenOrElseThrowWithFunctionOnLeftProjectionOfLeft() { - final Integer actual = Either. left(1).left().getOrElseThrow(s -> new RuntimeException(s)); - assertThat(actual).isEqualTo(1); - } - - @Test - public void shouldThrowWhenOrElseThrowWithFunctionOnLeftProjectionOfRight() { - assertThrows(RuntimeException.class, () -> Either.right("1").left().getOrElseThrow(s -> new RuntimeException(s))); - } - - // toOption - - @Test - public void shouldConvertLeftProjectionOfLeftToSome() { - assertThat(Either.left(1).left().toOption()).isEqualTo(Option.of(1)); - } - - @Test - public void shouldConvertLeftProjectionOfRightToNone() { - assertThat(Either.right("x").left().toOption()).isEqualTo(Option.none()); - } - - // toEither - - @Test - public void shouldConvertLeftProjectionOfLeftToEither() { - final Either self = Either.left(1); - assertThat(self.left().toEither()).isEqualTo(self); - } - - @Test - public void shouldConvertLeftProjectionOfRightToEither() { - final Either self = Either.right("1"); - assertThat(self.left().toEither()).isEqualTo(self); - } - - // toJavaOptional - - @Test - public void shouldConvertLeftProjectionOfLeftToJavaOptional() { - assertThat(Either.left(1).left().toJavaOptional()).isEqualTo(Optional.of(1)); - } - - @Test - public void shouldConvertLeftProjectionOfRightToJavaOptional() { - assertThat(Either. right("x").left().toJavaOptional()).isEqualTo(Optional.empty()); - } - - // filter - - @Test - public void shouldFilterSomeOnLeftProjectionOfLeftIfPredicateMatches() { - final boolean actual = Either.left(1).left().filter(i -> true).toOption().isDefined(); - assertThat(actual).isTrue(); - } - - @Test - public void shouldFilterNoneOnLeftProjectionOfLeftIfPredicateNotMatches() { - assertThat(Either.left(1).left().filter(i -> false)).isEqualTo(Option.none()); - } - - @Test - public void shouldFilterSomeOnLeftProjectionOfRightIfPredicateMatches() { - final boolean actual = Either.right("1").left().filter(i -> true).isDefined(); - assertThat(actual).isTrue(); - } - - @Test - public void shouldFilterNoneOnLeftProjectionOfRightIfPredicateNotMatches() { - final boolean actual = Either.right("1").left().filter(i -> false).isDefined(); - assertThat(actual).isTrue(); - } - - // flatMap - - @Test - public void shouldFlatMapOnLeftProjectionOfLeft() { - final Either actual = Either. left(1).left().flatMap(i -> Either. left(i + 1).left()).toEither(); - assertThat(actual).isEqualTo(Either.left(2)); - } - - @Test - public void shouldFlatMapOnLeftProjectionOfRight() { - final Either actual = Either. right("1").left().flatMap(i -> Either. left(i + 1).left()).toEither(); - assertThat(actual).isEqualTo(Either.right("1")); - } - - @Test - public void shouldFlatMapLeftProjectionOfRightOnLeftProjectionOfLeft() { - final Either good = Either.left("good"); - final Either bad = Either.right("bad"); - final Either.LeftProjection, String> actual = good.left().flatMap(g -> bad.left().map(b -> Tuple.of(g, b))); - assertThat(actual.toEither()).isEqualTo(Either.right("bad")); - - } - - // -- exists - - @Test - public void shouldBeAwareOfPropertyThatHoldsExistsOfLeftProjectionOfLeft() { - assertThat(Either.left(1).left().exists(i -> i == 1)).isTrue(); - } - - @Test - public void shouldBeAwareOfPropertyThatNotHoldsExistsOfLeftProjectionOfLeft() { - assertThat(Either.left(1).left().exists(i -> i == 2)).isFalse(); - } - - @Test - public void shouldNotHoldPropertyExistsOfLeftProjectionOfRight() { - assertThat(Either.left(1).right().exists(e -> true)).isFalse(); - } - - // -- forall - - @Test - public void shouldBeAwareOfPropertyThatHoldsForAllOfLeftProjectionOfLeft() { - assertThat(Either.left(1).left().forAll(i -> i == 1)).isTrue(); - } - - @Test - public void shouldBeAwareOfPropertyThatNotHoldsForAllOfLeftProjectionOfLeft() { - assertThat(Either.left(1).left().forAll(i -> i == 2)).isFalse(); - } + @Nested + @DisplayName("LeftProjection") + class LeftProjectionTest { + + // get + + @Test + public void shouldThrowOnGetOnLeftProjectionOfRight() { + assertThrows(NoSuchElementException.class, () -> Either.right(1).left().get()); + } + + @Test + public void shouldGetOnLeftProjectionOfLeft() { + assertThat(Either.left(1).left().get()).isEqualTo(1); + } + + // orElse + + @Test + public void shouldLeftProjectionOrElseLeftProjection() { + final Either.LeftProjection elseProjection = API.Left(2).left(); + assertThat(API.Left(1).left().orElse(elseProjection).get()).isEqualTo(1); + assertThat(API.Right(1).left().orElse(elseProjection).get()).isEqualTo(2); + } + + @Test + public void shouldLeftProjectionOrElseLeftProjectionFromSupplier() { + final Either.LeftProjection elseProjection = API.Left(2).left(); + assertThat(API.Left(1).left().orElse(() -> elseProjection).get()).isEqualTo(1); + assertThat(API.Right(1).left().orElse(() -> elseProjection).get()).isEqualTo(2); + } + + // getOrElse + + @Test + public void shouldReturnLeftWhenOrElseOnLeftProjectionOfLeft() { + final Integer actual = Either.left(1).left().getOrElse(2); + assertThat(actual).isEqualTo(1); + } + + @Test + public void shouldReturnOtherWhenOrElseOnLeftProjectionOfRight() { + final Integer actual = Either.right("1").left().getOrElse(2); + assertThat(actual).isEqualTo(2); + } + + // getOrElse(Function) + + @Test + public void shouldReturnLeftWhenOrElseGetGivenFunctionOnLeftProjectionOfLeft() { + final Integer actual = Either.left(1).left().getOrElseGet(r -> 2); + assertThat(actual).isEqualTo(1); + } + + @Test + public void shouldReturnOtherWhenOrElseGetGivenFunctionOnLeftProjectionOfRight() { + final Integer actual = Either.right("1").left().getOrElseGet(r -> 2); + assertThat(actual).isEqualTo(2); + } + + // orElseRun + + @Test + public void shouldReturnLeftWhenOrElseRunOnLeftProjectionOfLeft() { + final boolean[] actual = new boolean[]{true}; + Either.left(1).left().orElseRun(s -> actual[0] = false); + assertThat(actual[0]).isTrue(); + } + + @Test + public void shouldReturnOtherWhenOrElseRunOnLeftProjectionOfRight() { + final boolean[] actual = new boolean[]{false}; + Either.right("1").left().orElseRun(s -> { + actual[0] = true; + }); + assertThat(actual[0]).isTrue(); + } + + // getOrElseThrow(Function) + + @Test + public void shouldReturnLeftWhenOrElseThrowWithFunctionOnLeftProjectionOfLeft() { + final Integer actual = Either.left(1).left().getOrElseThrow(s -> new RuntimeException(s)); + assertThat(actual).isEqualTo(1); + } + + @Test + public void shouldThrowWhenOrElseThrowWithFunctionOnLeftProjectionOfRight() { + assertThrows(RuntimeException.class, () -> Either.right("1").left().getOrElseThrow(s -> new RuntimeException(s))); + } + + // toOption + + @Test + public void shouldConvertLeftProjectionOfLeftToSome() { + assertThat(Either.left(1).left().toOption()).isEqualTo(Option.of(1)); + } + + @Test + public void shouldConvertLeftProjectionOfRightToNone() { + assertThat(Either.right("x").left().toOption()).isEqualTo(Option.none()); + } + + // toEither + + @Test + public void shouldConvertLeftProjectionOfLeftToEither() { + final Either self = Either.left(1); + assertThat(self.left().toEither()).isEqualTo(self); + } + + @Test + public void shouldConvertLeftProjectionOfRightToEither() { + final Either self = Either.right("1"); + assertThat(self.left().toEither()).isEqualTo(self); + } + + // toJavaOptional + + @Test + public void shouldConvertLeftProjectionOfLeftToJavaOptional() { + assertThat(Either.left(1).left().toJavaOptional()).isEqualTo(Optional.of(1)); + } + + @Test + public void shouldConvertLeftProjectionOfRightToJavaOptional() { + assertThat(Either.right("x").left().toJavaOptional()).isEqualTo(Optional.empty()); + } + + // filter + + @Test + public void shouldFilterSomeOnLeftProjectionOfLeftIfPredicateMatches() { + final boolean actual = Either.left(1).left().filter(i -> true).toOption().isDefined(); + assertThat(actual).isTrue(); + } + + @Test + public void shouldFilterNoneOnLeftProjectionOfLeftIfPredicateNotMatches() { + assertThat(Either.left(1).left().filter(i -> false)).isEqualTo(Option.none()); + } + + @Test + public void shouldFilterSomeOnLeftProjectionOfRightIfPredicateMatches() { + final boolean actual = Either.right("1").left().filter(i -> true).isDefined(); + assertThat(actual).isTrue(); + } + + @Test + public void shouldFilterNoneOnLeftProjectionOfRightIfPredicateNotMatches() { + final boolean actual = Either.right("1").left().filter(i -> false).isDefined(); + assertThat(actual).isTrue(); + } + + // flatMap + + @Test + public void shouldFlatMapOnLeftProjectionOfLeft() { + final Either actual = Either.left(1).left().flatMap(i -> Either.left(i + 1).left()).toEither(); + assertThat(actual).isEqualTo(Either.left(2)); + } + + @Test + public void shouldFlatMapOnLeftProjectionOfRight() { + final Either actual = Either.right("1").left().flatMap(i -> Either.left(i + 1).left()).toEither(); + assertThat(actual).isEqualTo(Either.right("1")); + } + + @Test + public void shouldFlatMapLeftProjectionOfRightOnLeftProjectionOfLeft() { + final Either good = Either.left("good"); + final Either bad = Either.right("bad"); + final Either.LeftProjection, String> actual = good.left().flatMap(g -> bad.left().map(b -> Tuple.of(g, b))); + assertThat(actual.toEither()).isEqualTo(Either.right("bad")); + + } + + } + + @Nested + @DisplayName("exists") + class ExistsTest { + + @Test + public void shouldBeAwareOfPropertyThatHoldsExistsOfLeftProjectionOfLeft() { + assertThat(Either.left(1).left().exists(i -> i == 1)).isTrue(); + } + + @Test + public void shouldBeAwareOfPropertyThatNotHoldsExistsOfLeftProjectionOfLeft() { + assertThat(Either.left(1).left().exists(i -> i == 2)).isFalse(); + } + + @Test + public void shouldNotHoldPropertyExistsOfLeftProjectionOfRight() { + assertThat(Either.left(1).right().exists(e -> true)).isFalse(); + } + + } + + @Nested + @DisplayName("forall") + class ForallTest { + + @Test + public void shouldBeAwareOfPropertyThatHoldsForAllOfLeftProjectionOfLeft() { + assertThat(Either.left(1).left().forAll(i -> i == 1)).isTrue(); + } + + @Test + public void shouldBeAwareOfPropertyThatNotHoldsForAllOfLeftProjectionOfLeft() { + assertThat(Either.left(1).left().forAll(i -> i == 2)).isFalse(); + } + + @Test// a property holds for all elements of no elements + public void shouldNotHoldPropertyForAllOfLeftProjectionOfRight() { + assertThat(Either.left(1).right().forAll(e -> true)).isTrue(); + } + + // forEach + + @Test + public void shouldForEachOnLeftProjectionOfLeft() { + final List actual = new ArrayList<>(); + Either.left(1).left().forEach(actual::add); + assertThat(actual).isEqualTo(Collections.singletonList(1)); + } + + @Test + public void shouldForEachOnLeftProjectionOfRight() { + final List actual = new ArrayList<>(); + Either.right("1").left().forEach(actual::add); + assertThat(actual.isEmpty()).isTrue(); + } + + // peek + + @Test + public void shouldPeekOnLeftProjectionOfLeft() { + final List actual = new ArrayList<>(); + final Either testee = Either.left(1).left().peek(actual::add).toEither(); + assertThat(actual).isEqualTo(Collections.singletonList(1)); + assertThat(testee).isEqualTo(Either.left(1)); + } + + @Test + public void shouldPeekOnLeftProjectionOfRight() { + final List actual = new ArrayList<>(); + final Either testee = Either.right("1").left().peek(actual::add).toEither(); + assertThat(actual.isEmpty()).isTrue(); + assertThat(testee).isEqualTo(Either.right("1")); + } - @Test// a property holds for all elements of no elements - public void shouldNotHoldPropertyForAllOfLeftProjectionOfRight() { - assertThat(Either.left(1).right().forAll(e -> true)).isTrue(); + // map + + @Test + public void shouldMapOnLeftProjectionOfLeft() { + final Either actual = Either.left(1).left().map(i -> i + 1).toEither(); + assertThat(actual).isEqualTo(Either.left(2)); + } + + @Test + public void shouldMapOnLeftProjectionOfRight() { + final Either actual = Either.right("1").left().map(i -> i + 1).toEither(); + assertThat(actual).isEqualTo(Either.right("1")); + } } - // forEach + @Nested + @DisplayName("iterator") + class TteratorTest { - @Test - public void shouldForEachOnLeftProjectionOfLeft() { - final List actual = new ArrayList<>(); - Either.left(1).left().forEach(actual::add); - assertThat(actual).isEqualTo(Collections.singletonList(1)); - } - - @Test - public void shouldForEachOnLeftProjectionOfRight() { - final List actual = new ArrayList<>(); - Either. right("1").left().forEach(actual::add); - assertThat(actual.isEmpty()).isTrue(); - } + @Test + public void shouldReturnIteratorOfLeftOfLeftProjection() { + assertThat((Iterator) Either.left(1).left().iterator()).isNotNull(); + } - // peek + @Test + public void shouldReturnIteratorOfRightOfLeftProjection() { + assertThat((Iterator) Either.right(1).left().iterator()).isNotNull(); + } - @Test - public void shouldPeekOnLeftProjectionOfLeft() { - final List actual = new ArrayList<>(); - final Either testee = Either. left(1).left().peek(actual::add).toEither(); - assertThat(actual).isEqualTo(Collections.singletonList(1)); - assertThat(testee).isEqualTo(Either.left(1)); } - @Test - public void shouldPeekOnLeftProjectionOfRight() { - final List actual = new ArrayList<>(); - final Either testee = Either. right("1").left().peek(actual::add).toEither(); - assertThat(actual.isEmpty()).isTrue(); - assertThat(testee).isEqualTo(Either.right("1")); - } + @Nested + @DisplayName("equals") + class EqualsTest { - // map + @Test + public void shouldEqualLeftProjectionOfLeftIfObjectIsSame() { + final Either.LeftProjection l = Either.left(1).left(); + assertThat(l.equals(l)).isTrue(); + } - @Test - public void shouldMapOnLeftProjectionOfLeft() { - final Either actual = Either. left(1).left().map(i -> i + 1).toEither(); - assertThat(actual).isEqualTo(Either.left(2)); - } + @Test + public void shouldEqualLeftProjectionOfRightIfObjectIsSame() { + final Either.LeftProjection l = Either.right(1).left(); + assertThat(l.equals(l)).isTrue(); + } - @Test - public void shouldMapOnLeftProjectionOfRight() { - final Either actual = Either. right("1").left().map(i -> i + 1).toEither(); - assertThat(actual).isEqualTo(Either.right("1")); - } + @Test + public void shouldNotEqualLeftProjectionOfLeftIfObjectIsNull() { + assertThat(Either.left(1).left().equals(null)).isFalse(); + } + + @Test + public void shouldNotEqualLeftProjectionOfRightIfObjectIsNull() { + assertThat(Either.right(1).left().equals(null)).isFalse(); + } + + @Test + public void shouldNotEqualLeftProjectionOfLeftIfObjectIsOfDifferentType() { + assertThat(Either.left(1).left().equals(new Object())).isFalse(); + } + + @Test + public void shouldNotEqualLeftProjectionOfRightIfObjectIsOfDifferentType() { + assertThat(Either.right(1).left().equals(new Object())).isFalse(); + } - // iterator + @Test + public void shouldEqualLeftProjectionOfLeft() { + assertThat(Either.left(1).left()).isEqualTo(Either.left(1).left()); + } - @Test - public void shouldReturnIteratorOfLeftOfLeftProjection() { - assertThat((Iterator) Either.left(1).left().iterator()).isNotNull(); - } + @Test + public void shouldEqualLeftProjectionOfRight() { + assertThat(Either.right(1).left()).isEqualTo(Either.right(1).left()); + } - @Test - public void shouldReturnIteratorOfRightOfLeftProjection() { - assertThat((Iterator) Either.right(1).left().iterator()).isNotNull(); } - // equals + @Nested + @DisplayName("hashCode") + class HashCodeTest { - @Test - public void shouldEqualLeftProjectionOfLeftIfObjectIsSame() { - final Either.LeftProjection l = Either.left(1).left(); - assertThat(l.equals(l)).isTrue(); - } + @Test + public void shouldHashLeftProjectionOfLeft() { + assertThat(Either.left(1).left().hashCode()).isEqualTo(Objects.hashCode(Either.right(1))); + } - @Test - public void shouldEqualLeftProjectionOfRightIfObjectIsSame() { - final Either.LeftProjection l = Either.right(1).left(); - assertThat(l.equals(l)).isTrue(); - } + @Test + public void shouldHashLeftProjectionOfRight() { + assertThat(Either.right(1).left().hashCode()).isEqualTo(Objects.hashCode(Either.left(1))); + } - @Test - public void shouldNotEqualLeftProjectionOfLeftIfObjectIsNull() { - assertThat(Either.left(1).left().equals(null)).isFalse(); } - @Test - public void shouldNotEqualLeftProjectionOfRightIfObjectIsNull() { - assertThat(Either.right(1).left().equals(null)).isFalse(); - } + @Nested + @DisplayName("toString") + class ToStringTest { - @Test - public void shouldNotEqualLeftProjectionOfLeftIfObjectIsOfDifferentType() { - assertThat(Either.left(1).left().equals(new Object())).isFalse(); - } + @Test + public void shouldConvertLeftProjectionOfLeftToString() { + assertThat(Either.left(1).left().toString()).isEqualTo("LeftProjection(Left(1))"); + } - @Test - public void shouldNotEqualLeftProjectionOfRightIfObjectIsOfDifferentType() { - assertThat(Either.right(1).left().equals(new Object())).isFalse(); - } + @Test + public void shouldConvertLeftProjectionOfRightToString() { + assertThat(Either.right(1).left().toString()).isEqualTo("LeftProjection(Right(1))"); + } - @Test - public void shouldEqualLeftProjectionOfLeft() { - assertThat(Either.left(1).left()).isEqualTo(Either.left(1).left()); } - @Test - public void shouldEqualLeftProjectionOfRight() { - assertThat(Either.right(1).left()).isEqualTo(Either.right(1).left()); - } + @Nested + @DisplayName("transform") + class TransformTest { - // hashCode + @Test + public void shouldTransform() { + final String transformed = of(1).transform(v -> String.valueOf(v.get())); + assertThat(transformed).isEqualTo("1"); + } - @Test - public void shouldHashLeftProjectionOfLeft() { - assertThat(Either.left(1).left().hashCode()).isEqualTo(Objects.hashCode(Either.right(1))); } - @Test - public void shouldHashLeftProjectionOfRight() { - assertThat(Either.right(1).left().hashCode()).isEqualTo(Objects.hashCode(Either.left(1))); - } - - // toString - - @Test - public void shouldConvertLeftProjectionOfLeftToString() { - assertThat(Either.left(1).left().toString()).isEqualTo("LeftProjection(Left(1))"); - } - - @Test - public void shouldConvertLeftProjectionOfRightToString() { - assertThat(Either.right(1).left().toString()).isEqualTo("LeftProjection(Right(1))"); - } + @Nested + @DisplayName("spliterator") + class SpliteratorTest { - // -- transform() + @Test + public void shouldHaveSizedSpliterator() { + assertThat(of(1).spliterator().hasCharacteristics(Spliterator.SIZED | Spliterator.SUBSIZED)).isTrue(); + } - @Test - public void shouldTransform() { - final String transformed = of(1).transform(v -> String.valueOf(v.get())); - assertThat(transformed).isEqualTo("1"); - } - - // -- spliterator - - @Test - public void shouldHaveSizedSpliterator() { - assertThat(of(1).spliterator().hasCharacteristics(Spliterator.SIZED | Spliterator.SUBSIZED)).isTrue(); - } - - @Test - public void shouldHaveOrderedSpliterator() { - assertThat(of(1).spliterator().hasCharacteristics(Spliterator.ORDERED)).isTrue(); - } + @Test + public void shouldHaveOrderedSpliterator() { + assertThat(of(1).spliterator().hasCharacteristics(Spliterator.ORDERED)).isTrue(); + } - @Test - public void shouldReturnSizeWhenSpliterator() { - assertThat(of(1).spliterator().getExactSizeIfKnown()).isEqualTo(1); + @Test + public void shouldReturnSizeWhenSpliterator() { + assertThat(of(1).spliterator().getExactSizeIfKnown()).isEqualTo(1); + } } } From f73c536d344f2b72ac74e319234c9ab3d5412914 Mon Sep 17 00:00:00 2001 From: Boris van Katwijk Date: Sun, 19 Jan 2025 10:54:09 +0100 Subject: [PATCH 57/61] Update EitherLeftProjectionTest.java --- .../vavr/control/EitherLeftProjectionTest.java | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/test/java/io/vavr/control/EitherLeftProjectionTest.java b/src/test/java/io/vavr/control/EitherLeftProjectionTest.java index 52174b1e92..5c9cdb9a42 100644 --- a/src/test/java/io/vavr/control/EitherLeftProjectionTest.java +++ b/src/test/java/io/vavr/control/EitherLeftProjectionTest.java @@ -286,7 +286,11 @@ public void shouldNotHoldPropertyForAllOfLeftProjectionOfRight() { assertThat(Either.left(1).right().forAll(e -> true)).isTrue(); } - // forEach + } + + @Nested + @DisplayName("forEach") + class ForEachTest { @Test public void shouldForEachOnLeftProjectionOfLeft() { @@ -302,7 +306,11 @@ public void shouldForEachOnLeftProjectionOfRight() { assertThat(actual.isEmpty()).isTrue(); } - // peek + } + + @Nested + @DisplayName("peek") + class PeekTest { @Test public void shouldPeekOnLeftProjectionOfLeft() { @@ -320,7 +328,11 @@ public void shouldPeekOnLeftProjectionOfRight() { assertThat(testee).isEqualTo(Either.right("1")); } - // map + } + + @Nested + @DisplayName("map") + class MapTest { @Test public void shouldMapOnLeftProjectionOfLeft() { From 52c34f05ad4268463e542f7597557e3bfda5fc81 Mon Sep 17 00:00:00 2001 From: Boris van Katwijk Date: Sun, 19 Jan 2025 11:22:32 +0100 Subject: [PATCH 58/61] Update EitherLeftProjectionTest.java --- .../control/EitherLeftProjectionTest.java | 66 +++++++++++++++---- 1 file changed, 52 insertions(+), 14 deletions(-) diff --git a/src/test/java/io/vavr/control/EitherLeftProjectionTest.java b/src/test/java/io/vavr/control/EitherLeftProjectionTest.java index 5c9cdb9a42..20ebb62f80 100644 --- a/src/test/java/io/vavr/control/EitherLeftProjectionTest.java +++ b/src/test/java/io/vavr/control/EitherLeftProjectionTest.java @@ -68,10 +68,8 @@ protected int getPeekNonNilPerformingAnAction() { } @Nested - @DisplayName("LeftProjection") - class LeftProjectionTest { - - // get + @DisplayName("Get") + class GetTest { @Test public void shouldThrowOnGetOnLeftProjectionOfRight() { @@ -83,7 +81,11 @@ public void shouldGetOnLeftProjectionOfLeft() { assertThat(Either.left(1).left().get()).isEqualTo(1); } - // orElse + } + + @Nested + @DisplayName("orElse") + class OrElseTest { @Test public void shouldLeftProjectionOrElseLeftProjection() { @@ -99,7 +101,11 @@ public void shouldLeftProjectionOrElseLeftProjectionFromSupplier() { assertThat(API.Right(1).left().orElse(() -> elseProjection).get()).isEqualTo(2); } - // getOrElse + } + + @Nested + @DisplayName("getOrElse") + class GetOrElseTest { @Test public void shouldReturnLeftWhenOrElseOnLeftProjectionOfLeft() { @@ -113,7 +119,11 @@ public void shouldReturnOtherWhenOrElseOnLeftProjectionOfRight() { assertThat(actual).isEqualTo(2); } - // getOrElse(Function) + } + + @Nested + @DisplayName("getOrElse(Function)") + class GetOrElseFunctionTest { @Test public void shouldReturnLeftWhenOrElseGetGivenFunctionOnLeftProjectionOfLeft() { @@ -127,7 +137,11 @@ public void shouldReturnOtherWhenOrElseGetGivenFunctionOnLeftProjectionOfRight() assertThat(actual).isEqualTo(2); } - // orElseRun + } + + @Nested + @DisplayName("orElseRun(Function)") + class OrElseRunTest { @Test public void shouldReturnLeftWhenOrElseRunOnLeftProjectionOfLeft() { @@ -145,7 +159,11 @@ public void shouldReturnOtherWhenOrElseRunOnLeftProjectionOfRight() { assertThat(actual[0]).isTrue(); } - // getOrElseThrow(Function) + } + + @Nested + @DisplayName("getOrElseThrow(Function)") + class GetOrElseThrowFunctionTest { @Test public void shouldReturnLeftWhenOrElseThrowWithFunctionOnLeftProjectionOfLeft() { @@ -158,7 +176,11 @@ public void shouldThrowWhenOrElseThrowWithFunctionOnLeftProjectionOfRight() { assertThrows(RuntimeException.class, () -> Either.right("1").left().getOrElseThrow(s -> new RuntimeException(s))); } - // toOption + } + + @Nested + @DisplayName("toOption") + class ToOptionTest { @Test public void shouldConvertLeftProjectionOfLeftToSome() { @@ -170,7 +192,11 @@ public void shouldConvertLeftProjectionOfRightToNone() { assertThat(Either.right("x").left().toOption()).isEqualTo(Option.none()); } - // toEither + } + + @Nested + @DisplayName("toEither") + class ToEitherTest { @Test public void shouldConvertLeftProjectionOfLeftToEither() { @@ -184,7 +210,11 @@ public void shouldConvertLeftProjectionOfRightToEither() { assertThat(self.left().toEither()).isEqualTo(self); } - // toJavaOptional + } + + @Nested + @DisplayName("toJavaOptional") + class ToJavaOptionalTest { @Test public void shouldConvertLeftProjectionOfLeftToJavaOptional() { @@ -196,7 +226,11 @@ public void shouldConvertLeftProjectionOfRightToJavaOptional() { assertThat(Either.right("x").left().toJavaOptional()).isEqualTo(Optional.empty()); } - // filter + } + + @Nested + @DisplayName("filter") + class FilterTest { @Test public void shouldFilterSomeOnLeftProjectionOfLeftIfPredicateMatches() { @@ -221,7 +255,11 @@ public void shouldFilterNoneOnLeftProjectionOfRightIfPredicateNotMatches() { assertThat(actual).isTrue(); } - // flatMap + } + + @Nested + @DisplayName("flatMap") + class FlatMapTest { @Test public void shouldFlatMapOnLeftProjectionOfLeft() { From e682beadb589017bc5dba3ac1230b7cf8943a748 Mon Sep 17 00:00:00 2001 From: Boris van Katwijk Date: Sun, 19 Jan 2025 11:22:33 +0100 Subject: [PATCH 59/61] Update EitherRightProjectionTest.java --- .../control/EitherRightProjectionTest.java | 618 ++++++++++-------- 1 file changed, 353 insertions(+), 265 deletions(-) diff --git a/src/test/java/io/vavr/control/EitherRightProjectionTest.java b/src/test/java/io/vavr/control/EitherRightProjectionTest.java index f9fd0c1fde..8731f5889d 100644 --- a/src/test/java/io/vavr/control/EitherRightProjectionTest.java +++ b/src/test/java/io/vavr/control/EitherRightProjectionTest.java @@ -30,6 +30,8 @@ import io.vavr.AbstractValueTest; import io.vavr.Tuple; import io.vavr.Tuple2; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import java.util.*; @@ -45,12 +47,12 @@ public class EitherRightProjectionTest extends AbstractValueTest { @Override protected Either.RightProjection empty() { - return Either. left(null).right(); + return Either.left(null).right(); } @Override protected Either.RightProjection of(T element) { - return Either. right(element).right(); + return Either.right(element).right(); } @SafeVarargs @@ -71,365 +73,451 @@ protected int getPeekNonNilPerformingAnAction() { // -- RightProjection - // get + @Nested + @DisplayName("Get") + class GetTest { - @Test - public void shouldThrowOnGetOnRightProjectionOfLeft() { - assertThrows(NoSuchElementException.class, () -> Either.left(1).right().get()); - } + @Test + public void shouldThrowOnGetOnRightProjectionOfLeft() { + assertThrows(NoSuchElementException.class, () -> Either.left(1).right().get()); + } - @Test - public void shouldGetOnRightProjectionOfRight() { - assertThat(Either.right(1).right().get()).isEqualTo(1); + @Test + public void shouldGetOnRightProjectionOfRight() { + assertThat(Either.right(1).right().get()).isEqualTo(1); + } } - // orElse + @Nested + @DisplayName("orElse") + class OrElseTest { - @Test - public void shouldRightProjectionOrElseRightProjection() { - final Either.RightProjection elseProjection = API.Right(2).right(); - assertThat(Right(1).right().orElse(elseProjection).get()).isEqualTo(1); - assertThat(Left(1).right().orElse(elseProjection).get()).isEqualTo(2); - } + @Test + public void shouldRightProjectionOrElseRightProjection() { + final Either.RightProjection elseProjection = API.Right(2).right(); + assertThat(Right(1).right().orElse(elseProjection).get()).isEqualTo(1); + assertThat(Left(1).right().orElse(elseProjection).get()).isEqualTo(2); + } + + @Test + public void shouldRightProjectionOrElseRightProjectionFromSupplier() { + final Either.RightProjection elseProjection = API.Right(2).right(); + assertThat(Right(1).right().orElse(() -> elseProjection).get()).isEqualTo(1); + assertThat(Left(1).right().orElse(() -> elseProjection).get()).isEqualTo(2); + } - @Test - public void shouldRightProjectionOrElseRightProjectionFromSupplier() { - final Either.RightProjection elseProjection = API.Right(2).right(); - assertThat(Right(1).right().orElse(() -> elseProjection).get()).isEqualTo(1); - assertThat(Left(1).right().orElse(() -> elseProjection).get()).isEqualTo(2); } - // getOrElse + @Nested + @DisplayName("getOrElse") + class GetOrElseTest { - @Test - public void shouldReturnRightWhenOrElseOnRightProjectionOfRight() { - final Integer actual = Either. right(1).right().getOrElse(2); - assertThat(actual).isEqualTo(1); - } + @Test + public void shouldReturnRightWhenOrElseOnRightProjectionOfRight() { + final Integer actual = Either.right(1).right().getOrElse(2); + assertThat(actual).isEqualTo(1); + } + + @Test + public void shouldReturnOtherWhenOrElseOnRightProjectionOfLeft() { + final Integer actual = Either.left("1").right().getOrElse(2); + assertThat(actual).isEqualTo(2); + } - @Test - public void shouldReturnOtherWhenOrElseOnRightProjectionOfLeft() { - final Integer actual = Either. left("1").right().getOrElse(2); - assertThat(actual).isEqualTo(2); } - // getOrElse(Function) + @Nested + @DisplayName("getOrElse(Function)") + class GetOrElseFunctionTest { - @Test - public void shouldReturnRightWhenOrElseGetGivenFunctionOnRightProjectionOfRight() { - final Integer actual = Either. right(1).right().getOrElseGet(l -> 2); - assertThat(actual).isEqualTo(1); - } + @Test + public void shouldReturnRightWhenOrElseGetGivenFunctionOnRightProjectionOfRight() { + final Integer actual = Either.right(1).right().getOrElseGet(l -> 2); + assertThat(actual).isEqualTo(1); + } + + @Test + public void shouldReturnOtherWhenOrElseGetGivenFunctionOnRightProjectionOfLeft() { + final Integer actual = Either.left("1").right().getOrElseGet(l -> 2); + assertThat(actual).isEqualTo(2); + } - @Test - public void shouldReturnOtherWhenOrElseGetGivenFunctionOnRightProjectionOfLeft() { - final Integer actual = Either. left("1").right().getOrElseGet(l -> 2); - assertThat(actual).isEqualTo(2); } - // orElseRun + @Nested + @DisplayName("orElseRun(Function)") + class OrElseRunTest { - @Test - public void shouldReturnRightWhenOrElseRunOnRightProjectionOfRight() { - final boolean[] actual = new boolean[] { true }; - Either. right(1).right().orElseRun(s -> { - actual[0] = false; - }); - assertThat(actual[0]).isTrue(); - } + @Test + public void shouldReturnRightWhenOrElseRunOnRightProjectionOfRight() { + final boolean[] actual = new boolean[]{true}; + Either.right(1).right().orElseRun(s -> { + actual[0] = false; + }); + assertThat(actual[0]).isTrue(); + } + + @Test + public void shouldReturnOtherWhenOrElseRunOnRightProjectionOfLeft() { + final boolean[] actual = new boolean[]{false}; + Either.left("1").right().orElseRun(s -> { + actual[0] = true; + }); + assertThat(actual[0]).isTrue(); + } - @Test - public void shouldReturnOtherWhenOrElseRunOnRightProjectionOfLeft() { - final boolean[] actual = new boolean[] { false }; - Either. left("1").right().orElseRun(s -> { - actual[0] = true; - }); - assertThat(actual[0]).isTrue(); } - // getOrElseThrow(Function) + @Nested + @DisplayName("getOrElseThrow(Function)") + class GetOrElseThrowFunctionTest { - @Test - public void shouldReturnRightWhenOrElseThrowWithFunctionOnRightProjectionOfRight() { - final Integer actual = Either. right(1).right().getOrElseThrow(s -> new RuntimeException(s)); - assertThat(actual).isEqualTo(1); - } + @Test + public void shouldReturnRightWhenOrElseThrowWithFunctionOnRightProjectionOfRight() { + final Integer actual = Either.right(1).right().getOrElseThrow(s -> new RuntimeException(s)); + assertThat(actual).isEqualTo(1); + } + + @Test + public void shouldThrowWhenOrElseThrowWithFunctionOnRightProjectionOfLeft() { + assertThrows(RuntimeException.class, () -> Either.left("1").right().getOrElseThrow(i -> new RuntimeException(String.valueOf(i)))); + } - @Test - public void shouldThrowWhenOrElseThrowWithFunctionOnRightProjectionOfLeft() { - assertThrows(RuntimeException.class, () -> Either.left("1").right().getOrElseThrow(i -> new RuntimeException(String.valueOf(i)))); } - // toOption + @Nested + @DisplayName("toOption") + class ToOptionTest { - @Test - public void shouldConvertRightProjectionOfLeftToNone() { - assertThat(Either.left(0).right().toOption()).isEqualTo(Option.none()); - } + @Test + public void shouldConvertRightProjectionOfLeftToNone() { + assertThat(Either.left(0).right().toOption()).isEqualTo(Option.none()); + } + + @Test + public void shouldConvertRightProjectionOfRightToSome() { + assertThat(Either.right("1").right().toOption()).isEqualTo(Option.of("1")); + } - @Test - public void shouldConvertRightProjectionOfRightToSome() { - assertThat(Either. right("1").right().toOption()).isEqualTo(Option.of("1")); } - // toEither + @Nested + @DisplayName("toEither") + class ToEitherTest { - @Test - public void shouldConvertRightProjectionOfLeftToEither() { - final Either self = Either.left(1); - assertThat(self.right().toEither()).isEqualTo(self); - } + @Test + public void shouldConvertRightProjectionOfLeftToEither() { + final Either self = Either.left(1); + assertThat(self.right().toEither()).isEqualTo(self); + } + + @Test + public void shouldConvertRightProjectionOfRightToEither() { + final Either self = Either.right("1"); + assertThat(self.right().toEither()).isEqualTo(self); + } - @Test - public void shouldConvertRightProjectionOfRightToEither() { - final Either self = Either.right("1"); - assertThat(self.right().toEither()).isEqualTo(self); } - // toJavaOptional + @Nested + @DisplayName("toJavaOptional") + class ToJavaOptionalTest { - @Test - public void shouldConvertRightProjectionOfLeftToJavaOptional() { - assertThat(Either.left(0).right().toJavaOptional()).isEqualTo(Optional.empty()); - } + @Test + public void shouldConvertRightProjectionOfLeftToJavaOptional() { + assertThat(Either.left(0).right().toJavaOptional()).isEqualTo(Optional.empty()); + } + + @Test + public void shouldConvertRightProjectionOfRightToJavaOptional() { + assertThat(Either.right("1").right().toJavaOptional()).isEqualTo(Optional.of("1")); + } - @Test - public void shouldConvertRightProjectionOfRightToJavaOptional() { - assertThat(Either. right("1").right().toJavaOptional()).isEqualTo(Optional.of("1")); } - // -- transform() + @Nested + @DisplayName("transform") + class TransformTest { + + @Test + public void shouldTransform() { + final String transformed = of(1).transform(v -> String.valueOf(v.get())); + assertThat(transformed).isEqualTo("1"); + } - @Test - public void shouldTransform() { - final String transformed = of(1).transform(v -> String.valueOf(v.get())); - assertThat(transformed).isEqualTo("1"); } - // filter + @Nested + @DisplayName("filter") + class FilterTest { - @Test - public void shouldFilterSomeOnRightProjectionOfRightIfPredicateMatches() { - final boolean actual = Either. right(1).right().filter(i -> true).toOption().isDefined(); - assertThat(actual).isTrue(); - } + @Test + public void shouldFilterSomeOnRightProjectionOfRightIfPredicateMatches() { + final boolean actual = Either.right(1).right().filter(i -> true).toOption().isDefined(); + assertThat(actual).isTrue(); + } - @Test - public void shouldFilterNoneOnRightProjectionOfRightIfPredicateNotMatches() { - assertThat(Either. right(1).right().filter(i -> false)).isEqualTo(Option.none()); - } + @Test + public void shouldFilterNoneOnRightProjectionOfRightIfPredicateNotMatches() { + assertThat(Either.right(1).right().filter(i -> false)).isEqualTo(Option.none()); + } - @Test - public void shouldFilterSomeOnRightProjectionOfLeftIfPredicateMatches() { - final boolean actual = Either. left("1").right().filter(i -> true).isDefined(); - assertThat(actual).isTrue(); - } + @Test + public void shouldFilterSomeOnRightProjectionOfLeftIfPredicateMatches() { + final boolean actual = Either.left("1").right().filter(i -> true).isDefined(); + assertThat(actual).isTrue(); + } + + @Test + public void shouldFilterNoneOnRightProjectionOfLeftIfPredicateNotMatches() { + final boolean actual = Either.left("1").right().filter(i -> false).isDefined(); + assertThat(actual).isTrue(); + } - @Test - public void shouldFilterNoneOnRightProjectionOfLeftIfPredicateNotMatches() { - final boolean actual = Either. left("1").right().filter(i -> false).isDefined(); - assertThat(actual).isTrue(); } - // flatMap + @Nested + @DisplayName("flatMap") + class FlatMapTest { - @Test - public void shouldFlatMapOnRightProjectionOfRight() { - final Either actual = Either. right(1).right().flatMap(i -> Either. right(i + 1).right()).toEither(); - assertThat(actual).isEqualTo(Either.right(2)); - } + @Test + public void shouldFlatMapOnRightProjectionOfRight() { + final Either actual = Either.right(1).right().flatMap(i -> Either.right(i + 1).right()).toEither(); + assertThat(actual).isEqualTo(Either.right(2)); + } - @Test - public void shouldFlatMapOnRightProjectionOfLeft() { - final Either actual = Either. left("1").right().flatMap(i -> Either. right(i + 1).right()).toEither(); - assertThat(actual).isEqualTo(Either.left("1")); - } + @Test + public void shouldFlatMapOnRightProjectionOfLeft() { + final Either actual = Either.left("1").right().flatMap(i -> Either.right(i + 1).right()).toEither(); + assertThat(actual).isEqualTo(Either.left("1")); + } + + @Test + public void shouldFlatMapRightProjectionOfLeftOnRightProjectionOfRight() { + final Either good = Either.right("good"); + final Either bad = Either.left("bad"); + final Either.RightProjection> actual = good.right().flatMap(g -> bad.right().map(b -> Tuple.of(g, b))); + assertThat(actual.toEither()).isEqualTo(Either.left("bad")); + } - @Test - public void shouldFlatMapRightProjectionOfLeftOnRightProjectionOfRight() { - final Either good = Either.right("good"); - final Either bad = Either.left("bad"); - final Either.RightProjection> actual = good.right().flatMap(g -> bad.right().map(b -> Tuple.of(g, b))); - assertThat(actual.toEither()).isEqualTo(Either.left("bad")); } - // -- exists + @Nested + @DisplayName("exists") + class ExistsTest { - @Test - public void shouldBeAwareOfPropertyThatHoldsExistsOfRightProjectionOfRight() { - assertThat(Either.right(1).right().exists(i -> i == 1)).isTrue(); - } + @Test + public void shouldBeAwareOfPropertyThatHoldsExistsOfRightProjectionOfRight() { + assertThat(Either.right(1).right().exists(i -> i == 1)).isTrue(); + } - @Test - public void shouldBeAwareOfPropertyThatNotHoldsExistsOfRightProjectionOfRight() { - assertThat(Either.right(1).right().exists(i -> i == 2)).isFalse(); - } + @Test + public void shouldBeAwareOfPropertyThatNotHoldsExistsOfRightProjectionOfRight() { + assertThat(Either.right(1).right().exists(i -> i == 2)).isFalse(); + } + + @Test + public void shouldNotHoldPropertyExistsOfRightProjectionOfLeft() { + assertThat(Either.right(1).left().exists(e -> true)).isFalse(); + } - @Test - public void shouldNotHoldPropertyExistsOfRightProjectionOfLeft() { - assertThat(Either.right(1).left().exists(e -> true)).isFalse(); } - // -- forall + @Nested + @DisplayName("forall") + class ForallTest { - @Test - public void shouldBeAwareOfPropertyThatHoldsForAllOfRightProjectionOfRight() { - assertThat(Either.right(1).right().forAll(i -> i == 1)).isTrue(); - } + @Test + public void shouldBeAwareOfPropertyThatHoldsForAllOfRightProjectionOfRight() { + assertThat(Either.right(1).right().forAll(i -> i == 1)).isTrue(); + } - @Test - public void shouldBeAwareOfPropertyThatNotHoldsForAllOfRightProjectionOfRight() { - assertThat(Either.right(1).right().forAll(i -> i == 2)).isFalse(); - } + @Test + public void shouldBeAwareOfPropertyThatNotHoldsForAllOfRightProjectionOfRight() { + assertThat(Either.right(1).right().forAll(i -> i == 2)).isFalse(); + } + + @Test // a property holds for all elements of no elements + public void shouldNotHoldPropertyForAllOfRightProjectionOfLeft() { + assertThat(Either.right(1).left().forAll(e -> true)).isTrue(); + } - @Test // a property holds for all elements of no elements - public void shouldNotHoldPropertyForAllOfRightProjectionOfLeft() { - assertThat(Either.right(1).left().forAll(e -> true)).isTrue(); } - // forEach + @Nested + @DisplayName("forEach") + class ForEachTest { - @Test - public void shouldForEachOnRightProjectionOfRight() { - final List actual = new ArrayList<>(); - Either. right(1).right().forEach(actual::add); - assertThat(actual).isEqualTo(Collections.singletonList(1)); - } + @Test + public void shouldForEachOnRightProjectionOfRight() { + final List actual = new ArrayList<>(); + Either.right(1).right().forEach(actual::add); + assertThat(actual).isEqualTo(Collections.singletonList(1)); + } + + @Test + public void shouldForEachOnRightProjectionOfLeft() { + final List actual = new ArrayList<>(); + Either.left("1").right().forEach(actual::add); + assertThat(actual.isEmpty()).isTrue(); + } - @Test - public void shouldForEachOnRightProjectionOfLeft() { - final List actual = new ArrayList<>(); - Either. left("1").right().forEach(actual::add); - assertThat(actual.isEmpty()).isTrue(); } - // peek + @Nested + @DisplayName("peek") + class PeekTest { - @Test - public void shouldPeekOnRightProjectionOfRight() { - final List actual = new ArrayList<>(); - final Either testee = Either. right(1).right().peek(actual::add).toEither(); - assertThat(actual).isEqualTo(Collections.singletonList(1)); - assertThat(testee).isEqualTo(Either.right(1)); - } + @Test + public void shouldPeekOnRightProjectionOfRight() { + final List actual = new ArrayList<>(); + final Either testee = Either.right(1).right().peek(actual::add).toEither(); + assertThat(actual).isEqualTo(Collections.singletonList(1)); + assertThat(testee).isEqualTo(Either.right(1)); + } + + @Test + public void shouldPeekOnRightProjectionOfLeft() { + final List actual = new ArrayList<>(); + final Either testee = Either.left("1").right().peek(actual::add).toEither(); + assertThat(actual.isEmpty()).isTrue(); + assertThat(testee).isEqualTo(Either.left("1")); + } - @Test - public void shouldPeekOnRightProjectionOfLeft() { - final List actual = new ArrayList<>(); - final Either testee = Either. left("1").right().peek(actual::add).toEither(); - assertThat(actual.isEmpty()).isTrue(); - assertThat(testee).isEqualTo(Either. left("1")); } - // map + @Nested + @DisplayName("map") + class MapTest { - @Test - public void shouldMapOnRightProjectionOfRight() { - final Either actual = Either. right(1).right().map(i -> i + 1).toEither(); - assertThat(actual).isEqualTo(Either.right(2)); - } + @Test + public void shouldMapOnRightProjectionOfRight() { + final Either actual = Either.right(1).right().map(i -> i + 1).toEither(); + assertThat(actual).isEqualTo(Either.right(2)); + } + + @Test + public void shouldMapOnRightProjectionOfLeft() { + final Either actual = Either.left("1").right().map(i -> i + 1).toEither(); + assertThat(actual).isEqualTo(Either.left("1")); + } - @Test - public void shouldMapOnRightProjectionOfLeft() { - final Either actual = Either. left("1").right().map(i -> i + 1).toEither(); - assertThat(actual).isEqualTo(Either.left("1")); } - // iterator + @Nested + @DisplayName("iterator") + class TteratorTest { - @Test - public void shouldReturnIteratorOfRightOfRightProjection() { - assertThat((Iterator) Either.right(1).right().iterator()).isNotNull(); - } + @Test + public void shouldReturnIteratorOfRightOfRightProjection() { + assertThat((Iterator) Either.right(1).right().iterator()).isNotNull(); + } + + @Test + public void shouldReturnIteratorOfLeftOfRightProjection() { + assertThat((Iterator) Either.left(1).right().iterator()).isNotNull(); + } - @Test - public void shouldReturnIteratorOfLeftOfRightProjection() { - assertThat((Iterator) Either.left(1).right().iterator()).isNotNull(); } - // equals + @Nested + @DisplayName("equals") + class EqualsTest { - @Test - public void shouldEqualRightProjectionOfRightIfObjectIsSame() { - final Either.RightProjection r = Either.right(1).right(); - assertThat(r.equals(r)).isTrue(); - } + @Test + public void shouldEqualRightProjectionOfRightIfObjectIsSame() { + final Either.RightProjection r = Either.right(1).right(); + assertThat(r.equals(r)).isTrue(); + } - @Test - public void shouldEqualRightProjectionOfLeftIfObjectIsSame() { - final Either.RightProjection r = Either.left(1).right(); - assertThat(r.equals(r)).isTrue(); - } + @Test + public void shouldEqualRightProjectionOfLeftIfObjectIsSame() { + final Either.RightProjection r = Either.left(1).right(); + assertThat(r.equals(r)).isTrue(); + } - @Test - public void shouldNotEqualRightProjectionOfRightIfObjectIsNull() { - assertThat(Either.right(1).right().equals(null)).isFalse(); - } + @Test + public void shouldNotEqualRightProjectionOfRightIfObjectIsNull() { + assertThat(Either.right(1).right().equals(null)).isFalse(); + } - @Test - public void shouldNotEqualRightProjectionOfLeftIfObjectIsNull() { - assertThat(Either.left(1).right().equals(null)).isFalse(); - } + @Test + public void shouldNotEqualRightProjectionOfLeftIfObjectIsNull() { + assertThat(Either.left(1).right().equals(null)).isFalse(); + } - @Test - public void shouldNotEqualRightProjectionOfRightIfObjectIsOfDifferentType() { - assertThat(Either.right(1).right().equals(new Object())).isFalse(); - } + @Test + public void shouldNotEqualRightProjectionOfRightIfObjectIsOfDifferentType() { + assertThat(Either.right(1).right().equals(new Object())).isFalse(); + } - @Test - public void shouldNotEqualRightProjectionOfLeftIfObjectIsOfDifferentType() { - assertThat(Either.left(1).right().equals(new Object())).isFalse(); - } + @Test + public void shouldNotEqualRightProjectionOfLeftIfObjectIsOfDifferentType() { + assertThat(Either.left(1).right().equals(new Object())).isFalse(); + } - @Test - public void shouldEqualRightProjectionOfRight() { - assertThat(Either.right(1).right()).isEqualTo(Either.right(1).right()); - } + @Test + public void shouldEqualRightProjectionOfRight() { + assertThat(Either.right(1).right()).isEqualTo(Either.right(1).right()); + } + + @Test + public void shouldEqualRightProjectionOfLeft() { + assertThat(Either.left(1).right()).isEqualTo(Either.left(1).right()); + } - @Test - public void shouldEqualRightProjectionOfLeft() { - assertThat(Either.left(1).right()).isEqualTo(Either.left(1).right()); } - // hashCode + @Nested + @DisplayName("hashCode") + class HashCodeTest { - @Test - public void shouldHashRightProjectionOfRight() { - assertThat(Either.right(1).right().hashCode()).isEqualTo(Objects.hashCode(Either.right(1))); - } + @Test + public void shouldHashRightProjectionOfRight() { + assertThat(Either.right(1).right().hashCode()).isEqualTo(Objects.hashCode(Either.right(1))); + } + + @Test + public void shouldHashRightProjectionOfLeft() { + assertThat(Either.left(1).right().hashCode()).isEqualTo(Objects.hashCode(Either.left(1))); + } - @Test - public void shouldHashRightProjectionOfLeft() { - assertThat(Either.left(1).right().hashCode()).isEqualTo(Objects.hashCode(Either.left(1))); } - // toString + @Nested + @DisplayName("toString") + class ToStringTest { - @Test - public void shouldConvertRightProjectionOfLeftToString() { - assertThat(Either.left(1).right().toString()).isEqualTo("RightProjection(Left(1))"); - } + @Test + public void shouldConvertRightProjectionOfLeftToString() { + assertThat(Either.left(1).right().toString()).isEqualTo("RightProjection(Left(1))"); + } + + @Test + public void shouldConvertRightProjectionOfRightToString() { + assertThat(Either.right(1).right().toString()).isEqualTo("RightProjection(Right(1))"); + } - @Test - public void shouldConvertRightProjectionOfRightToString() { - assertThat(Either.right(1).right().toString()).isEqualTo("RightProjection(Right(1))"); } - // -- spliterator + @Nested + @DisplayName("spliterator") + class SpliteratorTest { - @Test - public void shouldHaveSizedSpliterator() { - assertThat(of(1).spliterator().hasCharacteristics(Spliterator.SIZED | Spliterator.SUBSIZED)).isTrue(); - } + @Test + public void shouldHaveSizedSpliterator() { + assertThat(of(1).spliterator().hasCharacteristics(Spliterator.SIZED | Spliterator.SUBSIZED)).isTrue(); + } - @Test - public void shouldHaveOrderedSpliterator() { - assertThat(of(1).spliterator().hasCharacteristics(Spliterator.ORDERED)).isTrue(); - } + @Test + public void shouldHaveOrderedSpliterator() { + assertThat(of(1).spliterator().hasCharacteristics(Spliterator.ORDERED)).isTrue(); + } - @Test - public void shouldReturnSizeWhenSpliterator() { - assertThat(of(1).spliterator().getExactSizeIfKnown()).isEqualTo(1); - } + @Test + public void shouldReturnSizeWhenSpliterator() { + assertThat(of(1).spliterator().getExactSizeIfKnown()).isEqualTo(1); + } + } } From 51f93ecb744cd437c2fd22808cc87cd4669aedfd Mon Sep 17 00:00:00 2001 From: Boris van Katwijk Date: Mon, 20 Jan 2025 09:22:34 +0100 Subject: [PATCH 60/61] skip tests of deprecated classes --- nested.sh | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/nested.sh b/nested.sh index 690992f5ed..c97cef3b52 100755 --- a/nested.sh +++ b/nested.sh @@ -6,4 +6,13 @@ find ./src -name "*Test.java" -type f -exec sed -E -i '' \ find ./src -type f -name '*Test.java' -exec perl -i -pe 's/\bCAPS([a-zA-Z])([a-zA-Z]*)/\u\1\2/g' {} + +git restore **/LazyTest.java \ + **/PredicatesTest.java \ + **/PartialFunctionTest.java \ + **/MatchErrorTest.java \ + **/ValueTest.java \ + **/BitSetTest.java \ + **/LinkedHashMultimapTest.java \ + **/LinkedHashMultimapOfEntriesTest.java + #./gradlew test \ No newline at end of file From 9a63a66cad30ce8aea2eae8018b8b3c5ed8e12b1 Mon Sep 17 00:00:00 2001 From: Boris van Katwijk Date: Mon, 20 Jan 2025 09:31:20 +0100 Subject: [PATCH 61/61] Update TryTest.java --- nested.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/nested.sh b/nested.sh index c97cef3b52..d412ecadea 100755 --- a/nested.sh +++ b/nested.sh @@ -6,6 +6,7 @@ find ./src -name "*Test.java" -type f -exec sed -E -i '' \ find ./src -type f -name '*Test.java' -exec perl -i -pe 's/\bCAPS([a-zA-Z])([a-zA-Z]*)/\u\1\2/g' {} + +# skip tests of deprecated classes git restore **/LazyTest.java \ **/PredicatesTest.java \ **/PartialFunctionTest.java \