Skip to content

Commit

Permalink
Add more tests for Try
Browse files Browse the repository at this point in the history
  • Loading branch information
Glavo committed Jan 6, 2025
1 parent 25b6215 commit a83bdd3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 19 deletions.
7 changes: 3 additions & 4 deletions kala-base/src/main/java/kala/control/Try.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,11 @@ static <R, Ex extends Throwable> R throwException(Ex exception) throws Ex {
*/
@Contract("_ -> fail")
static <R> R sneakyThrow(Throwable exception) {
sneakyThrow0(exception);
return null; // make compiler happy
return sneakyThrow0(exception);
}

@Contract("_ -> fail")
private static <Ex extends Throwable> void sneakyThrow0(Throwable exception) throws Ex {
private static <R, Ex extends Throwable> R sneakyThrow0(Throwable exception) throws Ex {
throw (Ex) exception;
}

Expand Down Expand Up @@ -306,7 +305,7 @@ default <Ex extends Throwable> T getOrThrowException(Ex exception) throws Ex {
@Override
default @NotNull Throwable getCause() {
return switch (this) {
case Success<T> __ -> throw new UnsupportedOperationException();
case Success<T> ignored -> throw new UnsupportedOperationException();
case Failure(var cause) -> cause;
};
}
Expand Down
39 changes: 24 additions & 15 deletions src/test/java/kala/control/TryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,27 @@

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

public class TryTest {
public final class TryTest {

@Test
public void throwExceptionTest() {
void throwExceptionTest() {
MyException ex = new MyException();

assertThrows(MyException.class, () -> Try.throwException(ex));
assertThrows(MyException.class, () -> Try.sneakyThrow(ex));
}

@Test
public void ofTest() {
void maybeThrowsTest() {
try {
Try.<IOException>maybeThrows();
} catch (IOException ignored) {
// empty
}
}

@Test
void ofTest() {
assertEquals(Try.success("foo"), Try.of(() -> "foo"));
assertEquals(Try.success("foo"), Try.ofCallable(() -> "foo"));
assertEquals(Try.success(null), Try.runCatching(() -> {
Expand All @@ -55,7 +64,7 @@ public void ofTest() {
}

@Test
public void runTest() {
void runTest() {
assertDoesNotThrow(() -> Try.runIgnoreException(() -> {
throw new MyException();
}));
Expand All @@ -64,7 +73,7 @@ public void runTest() {
}

@Test
public void usingTest() {
void usingTest() {
class CloseableImpl implements AutoCloseable {
final LongAdder adder;

Expand Down Expand Up @@ -176,7 +185,7 @@ public void close() {
}

@Test
public void getTest() {
void getTest() {
MyException ex = new MyException();
Exception defaultValue = new Exception();

Expand Down Expand Up @@ -217,7 +226,7 @@ public void getTest() {
}

@Test
public void recoverTest() {
void recoverTest() {
MyException ex0 = new MyException();
IllegalArgumentException ex1 = new IllegalArgumentException();

Expand Down Expand Up @@ -248,7 +257,7 @@ public void recoverTest() {
}

@Test
public void rethrowTest() {
void rethrowTest() {
MyException ex = new MyException();

Try<String> success = Try.success("foo");
Expand All @@ -268,23 +277,23 @@ public void rethrowTest() {
}

@Test
public void toEitherTest() {
void toEitherTest() {
MyException ex = new MyException();

assertEquals(Either.right("foo"), Try.success("foo").toEither());
assertEquals(Either.left(ex), Try.failure(ex).toEither());
}

@Test
public void toResultTest() {
void toResultTest() {
MyException ex = new MyException();

assertEquals(Result.ok("foo"), Try.success("foo").toResult());
assertEquals(Result.err(ex), Try.failure(ex).toResult());
}

@Test
public void mapTest() {
void mapTest() {
MyException ex = new MyException();

assertEquals(Try.success(3), Try.success("foo").map(String::length));
Expand All @@ -293,7 +302,7 @@ public void mapTest() {
}

@Test
public void flatMapTest() {
void flatMapTest() {
MyException ex = new MyException();

Try<String> success = Try.success("foo");
Expand All @@ -312,13 +321,13 @@ public void flatMapTest() {
}

@Test
public void iteratorTest() {
void iteratorTest() {
assertIterableEquals(List.of("foo"), Try.success("foo"));
assertIterableEquals(List.of(), Try.failure(new MyException()));
}

@Test
public void forEachTest() {
void forEachTest() {
MyException ex = new MyException();

Try<String> success = Try.success("foo");
Expand All @@ -337,7 +346,7 @@ public void forEachTest() {
}

@Test
public void serializationTest() throws IOException, ClassNotFoundException {
void serializationTest() throws IOException, ClassNotFoundException {
assertEquals(Try.success("foo"), SerializationUtils.writeAndRead(Try.success("foo")));
assertInstanceOf(MyException.class, SerializationUtils.writeAndRead(Try.failure(new MyException())).getCause());
}
Expand Down

0 comments on commit a83bdd3

Please sign in to comment.