Skip to content

Commit

Permalink
Create EitherTest
Browse files Browse the repository at this point in the history
  • Loading branch information
Glavo committed Nov 7, 2024
1 parent 881963d commit f9eb0d1
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 1 deletion.
62 changes: 62 additions & 0 deletions src/test/java/kala/control/EitherTest.java
Original file line number Diff line number Diff line change
@@ -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<TestValue<?>, ?> left = Either.left(value);
private final Either.Right<?, TestValue<?>> 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() {
}
}
1 change: 0 additions & 1 deletion src/test/java/kala/control/ResultTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import java.io.IOException;
import java.util.NoSuchElementException;
import java.util.Objects;

import static org.junit.jupiter.api.Assertions.*;

Expand Down
23 changes: 23 additions & 0 deletions src/test/util/kala/TestValue.java
Original file line number Diff line number Diff line change
@@ -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>(T value) {
@Override
public boolean equals(Object obj) {
return this == obj;
}
}

0 comments on commit f9eb0d1

Please sign in to comment.