From f9eb0d14971b040b73ee0b14c737c960b375dc1a Mon Sep 17 00:00:00 2001 From: Glavo Date: Thu, 7 Nov 2024 18:00:25 +0800 Subject: [PATCH] Create EitherTest --- src/test/java/kala/control/EitherTest.java | 62 ++++++++++++++++++++++ src/test/java/kala/control/ResultTest.java | 1 - src/test/util/kala/TestValue.java | 23 ++++++++ 3 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 src/test/java/kala/control/EitherTest.java create mode 100644 src/test/util/kala/TestValue.java diff --git a/src/test/java/kala/control/EitherTest.java b/src/test/java/kala/control/EitherTest.java new file mode 100644 index 00000000..96a11783 --- /dev/null +++ b/src/test/java/kala/control/EitherTest.java @@ -0,0 +1,62 @@ +/* + * Copyright 2024 Glavo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package kala.control; + +import kala.TestValue; +import org.junit.jupiter.api.*; + +import static org.junit.jupiter.api.Assertions.*; + +public final class EitherTest { + private final TestValue value = new TestValue<>("value"); + private final TestValue otherValue = new TestValue<>("otherValue"); + + private final Either.Left, ?> left = Either.left(value); + private final Either.Right> right = Either.right(value); + + @Test + public void swapTest() { + assertEquals(left, right.swap()); + assertEquals(right, left.swap()); + } + + @Test + public void toResultTest() { + assertEquals(Result.ok(value), right.toResult()); + assertEquals(Result.err(value), left.toResult()); + } + + @Test + public void leftProjectionTest() { + assertTrue(left.leftProjection().isDefined()); + assertFalse(right.leftProjection().isDefined()); + + assertEquals(Either.left(otherValue), left.leftProjection().map(it -> otherValue).getEither()); + assertEquals(right, right.leftProjection().map(it -> otherValue).getEither()); + } + + @Test + public void rightProjectionTest() { + assertFalse(left.rightProjection().isDefined()); + assertTrue(right.rightProjection().isDefined()); + + assertEquals(left, left.rightProjection().map(it -> otherValue).getEither()); + assertEquals(Either.right(otherValue), right.rightProjection().map(it -> otherValue).getEither()); + } + + private EitherTest() { + } +} diff --git a/src/test/java/kala/control/ResultTest.java b/src/test/java/kala/control/ResultTest.java index 16bbb745..5501822c 100644 --- a/src/test/java/kala/control/ResultTest.java +++ b/src/test/java/kala/control/ResultTest.java @@ -20,7 +20,6 @@ import java.io.IOException; import java.util.NoSuchElementException; -import java.util.Objects; import static org.junit.jupiter.api.Assertions.*; diff --git a/src/test/util/kala/TestValue.java b/src/test/util/kala/TestValue.java new file mode 100644 index 00000000..4f117e57 --- /dev/null +++ b/src/test/util/kala/TestValue.java @@ -0,0 +1,23 @@ +/* + * Copyright 2024 Glavo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package kala; + +public record TestValue(T value) { + @Override + public boolean equals(Object obj) { + return this == obj; + } +}