From f3bcc6c638b6a6af8b44e81e366a08c70e2a8297 Mon Sep 17 00:00:00 2001 From: Jerry Lee Date: Sat, 3 Aug 2024 19:17:05 +0800 Subject: [PATCH] test: rewrite `TupleTest` to `Java`, increase test coverage --- .../java/io/foldright/cffu/tuple/Tuple5.java | 5 +- .../io/foldright/cffu/tuple/TupleTest.java | 116 ++++++++++++++++++ .../java/io/foldright/cffu/tuple/TupleTest.kt | 86 ------------- 3 files changed, 118 insertions(+), 89 deletions(-) create mode 100644 cffu-core/src/test/java/io/foldright/cffu/tuple/TupleTest.java delete mode 100644 cffu-core/src/test/java/io/foldright/cffu/tuple/TupleTest.kt diff --git a/cffu-core/src/main/java/io/foldright/cffu/tuple/Tuple5.java b/cffu-core/src/main/java/io/foldright/cffu/tuple/Tuple5.java index e6fc12f4..c8bcb4fd 100644 --- a/cffu-core/src/main/java/io/foldright/cffu/tuple/Tuple5.java +++ b/cffu-core/src/main/java/io/foldright/cffu/tuple/Tuple5.java @@ -32,9 +32,8 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Tuple5 tuple5 = (Tuple5) o; - return Objects.equals(_1, tuple5._1) && Objects.equals(_2, tuple5._2) - && Objects.equals(_3, tuple5._3) && Objects.equals(_4, tuple5._4) - && Objects.equals(_5, tuple5._5); + return Objects.equals(_1, tuple5._1) && Objects.equals(_2, tuple5._2) && Objects.equals(_3, tuple5._3) + && Objects.equals(_4, tuple5._4) && Objects.equals(_5, tuple5._5); } @Override diff --git a/cffu-core/src/test/java/io/foldright/cffu/tuple/TupleTest.java b/cffu-core/src/test/java/io/foldright/cffu/tuple/TupleTest.java new file mode 100644 index 00000000..0e3e650c --- /dev/null +++ b/cffu-core/src/test/java/io/foldright/cffu/tuple/TupleTest.java @@ -0,0 +1,116 @@ +package io.foldright.cffu.tuple; + +import org.junit.jupiter.api.Test; + +import java.util.HashMap; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.*; + + +@SuppressWarnings({"AssertBetweenInconvertibleTypes", "EqualsWithItself", "SimplifiableAssertion", "ConstantValue", "EqualsBetweenInconvertibleTypes"}) +public class TupleTest { + int e1 = 1; + String e2 = "2"; + double e3 = 3.14; + int e4 = 4; + char e5 = '5'; + + String value = "42"; + + @Test + void tuple2() { + final Tuple2 tuple = Tuple2.of(e1, e2); + assertEquals(e1, tuple._1); + assertEquals(e2, tuple._2); + + assertEquals(Tuple2.of(e1, e2), tuple); + assertTrue(tuple.equals(tuple)); + assertNotEquals(Tuple2.of(e1 + 1, e2), tuple); + assertNotEquals(Tuple2.of(e1, e2 + "X"), tuple); + + assertFalse(tuple.equals("foo")); + assertFalse(tuple.equals(null)); + + assertEquals("Tuple2(1, 2)", tuple.toString()); + + Map, String> map = new HashMap<>(); + map.put(tuple, value); + assertEquals(value, map.get(tuple)); + } + + @Test + void tuple3() { + final Tuple3 tuple = Tuple3.of(e1, e2, e3); + assertEquals(e1, tuple._1); + assertEquals(e2, tuple._2); + assertEquals(e3, tuple._3); + + assertEquals(Tuple3.of(e1, e2, e3), tuple); + assertTrue(tuple.equals(tuple)); + assertNotEquals(Tuple3.of(e1 + 1, e2, e3), tuple); + assertNotEquals(Tuple3.of(e1, e2 + "X", e3), tuple); + assertNotEquals(Tuple3.of(e1, e2, e3 + 1), tuple); + + assertFalse(tuple.equals("foo")); + assertFalse(tuple.equals(null)); + + assertEquals("Tuple3(1, 2, 3.14)", tuple.toString()); + + Map, String> map = new HashMap<>(); + map.put(tuple, value); + assertEquals(value, map.get(tuple)); + } + + @Test + void tuple4() { + final Tuple4 tuple = Tuple4.of(e1, e2, e3, e4); + assertEquals(e1, tuple._1); + assertEquals(e2, tuple._2); + assertEquals(e3, tuple._3); + assertEquals(e4, tuple._4); + + assertEquals(Tuple4.of(e1, e2, e3, e4), tuple); + assertTrue(tuple.equals(tuple)); + assertNotEquals(Tuple4.of(e1 + 1, e2, e3, e4), tuple); + assertNotEquals(Tuple4.of(e1, e2 + "X", e3, e4), tuple); + assertNotEquals(Tuple4.of(e1, e2, e3 + 1, e4), tuple); + assertNotEquals(Tuple4.of(e1, e2, e3, e4 + 1), tuple); + + assertFalse(tuple.equals("foo")); + assertFalse(tuple.equals(null)); + + assertEquals("Tuple4(1, 2, 3.14, 4)", tuple.toString()); + + Map, String> map = new HashMap<>(); + map.put(tuple, value); + assertEquals(value, map.get(tuple)); + } + + @Test + void tuple5() { + final Tuple5 tuple = Tuple5.of(e1, e2, e3, e4, e5); + assertEquals(e1, tuple._1); + assertEquals(e2, tuple._2); + assertEquals(e3, tuple._3); + assertEquals(e4, tuple._4); + assertEquals(e5, tuple._5); + + assertEquals(Tuple5.of(e1, e2, e3, e4, e5), tuple); + assertTrue(tuple.equals(tuple)); + assertNotEquals(Tuple5.of(e1 + 1, e2, e3, e4, e5), tuple); + assertNotEquals(Tuple5.of(e1, e2 + "X", e3, e4, e5), tuple); + assertNotEquals(Tuple5.of(e1, e2, e3 + 1, e4, e5), tuple); + assertNotEquals(Tuple5.of(e1, e2, e3, e4 + 1, e5), tuple); + assertNotEquals(Tuple5.of(e1, e2, e3, e4, '6'), tuple); + + assertFalse(tuple.equals("foo")); + assertFalse(tuple.equals(null)); + + assertEquals("Tuple5(1, 2, 3.14, 4, 5)", tuple.toString()); + + Map, String> map = new HashMap<>(); + map.put(tuple, value); + assertEquals(value, map.get(tuple)); + } +} diff --git a/cffu-core/src/test/java/io/foldright/cffu/tuple/TupleTest.kt b/cffu-core/src/test/java/io/foldright/cffu/tuple/TupleTest.kt deleted file mode 100644 index 2e144c28..00000000 --- a/cffu-core/src/test/java/io/foldright/cffu/tuple/TupleTest.kt +++ /dev/null @@ -1,86 +0,0 @@ -package io.foldright.cffu.tuple - -import io.kotest.core.spec.style.FunSpec -import io.kotest.matchers.shouldBe -import io.kotest.matchers.shouldNotBe - -class TupleTest : FunSpec({ - val e1 = 1 - val e2 = "2" - val e3 = 3.14 - val e4 = 4 - val e5 = '5' - - val value = "42" - val pair2 = "foo" to "4242" - - test("tuple 2") { - val tuple2 = Tuple2.of(e1, e2) - tuple2._1 shouldBe e1 - tuple2._2 shouldBe e2 - - tuple2 shouldBe Tuple2.of(e1, e2) - tuple2 shouldBe tuple2 - - tuple2 shouldNotBe Tuple2.of(e1, e2 + "X") - tuple2 shouldNotBe null - tuple2 shouldNotBe "foo" - - mapOf(tuple2 to value, pair2)[tuple2] shouldBe value - tuple2.toString() shouldBe "Tuple2(1, 2)" - } - - test("tuple 3") { - val tuple3 = Tuple3.of(e1, e2, e3) - tuple3._1 shouldBe e1 - tuple3._2 shouldBe e2 - tuple3._3 shouldBe e3 - - tuple3 shouldBe Tuple3.of(e1, e2, e3) - tuple3 shouldBe tuple3 - - tuple3 shouldNotBe Tuple3.of(e1, e2, e3 + 1.0) - tuple3 shouldNotBe null - tuple3 shouldNotBe "foo" - - mapOf(tuple3 to value, pair2)[tuple3] shouldBe value - tuple3.toString() shouldBe "Tuple3(1, 2, 3.14)" - } - - test("tuple 4") { - val tuple4 = Tuple4.of(e1, e2, e3, e4) - tuple4._1 shouldBe e1 - tuple4._2 shouldBe e2 - tuple4._3 shouldBe e3 - tuple4._4 shouldBe e4 - - tuple4 shouldBe Tuple4.of(e1, e2, e3, e4) - tuple4 shouldBe tuple4 - - tuple4 shouldNotBe Tuple4.of(e1, e2, e3, e4 + 1) - tuple4 shouldNotBe null - tuple4 shouldNotBe "foo" - - mapOf(tuple4 to value, pair2)[tuple4] shouldBe value - tuple4.toString() shouldBe "Tuple4(1, 2, 3.14, 4)" - } - - test("tuple 5") { - val tuple5 = Tuple5.of(e1, e2, e3, e4, e5) - tuple5._1 shouldBe e1 - tuple5._2 shouldBe e2 - tuple5._3 shouldBe e3 - tuple5._4 shouldBe e4 - tuple5._5 shouldBe e5 - - tuple5 shouldBe Tuple5.of(e1, e2, e3, e4, e5) - tuple5 shouldBe tuple5 - - tuple5 shouldNotBe Tuple5.of(e1, e2, e3, e4, '6') - tuple5 shouldNotBe null - tuple5 shouldNotBe "foo" - - mapOf(tuple5 to value, pair2)[tuple5] shouldBe value - tuple5.toString() shouldBe "Tuple5(1, 2, 3.14, 4, 5)" - } -})