Skip to content

Commit

Permalink
* More tests for Try
Browse files Browse the repository at this point in the history
  • Loading branch information
xyzsd committed Dec 12, 2023
1 parent 1aeccb0 commit a0b75e1
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 10 deletions.
5 changes: 4 additions & 1 deletion src/main/java/net/xyzsd/dichotomy/trying/Try.java
Original file line number Diff line number Diff line change
Expand Up @@ -566,11 +566,14 @@ static <T, AC1 extends AutoCloseable, AC2 extends AutoCloseable> Try<T> withReso
* {@snippet :
* getOrThrow(IOException::new); // equivalent to new IOException(Throwable)
*}
* If the exception is a checked exception, it must be caught or
* declared within a methods {@code throws} clause.
* <p>
* If setting the throwable as a cause is desirable, this form could be used:
* {@snippet :
* getOrThrow( (x) -> new IOException("This is my exception message"));
*}
* </p>
*
*
* @param exFn Exception (Throwable) mapping function
* @param <X> Exception to throw; if not a (subclass of) RuntimeException, it must be rethrown or caught
Expand Down
113 changes: 104 additions & 9 deletions src/test/java/net/xyzsd/dichotomy/trying/TryTest.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
package net.xyzsd.dichotomy.trying;

import net.xyzsd.dichotomy.TestUtils.SingleUseConsumer;
import net.xyzsd.dichotomy.trying.function.ExFunction;
import net.xyzsd.dichotomy.trying.function.ExSupplier;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.io.StringWriter;
import java.util.Arrays;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

import static java.util.Objects.requireNonNull;
import static net.xyzsd.dichotomy.TestUtils.neverConsumer;
import static net.xyzsd.dichotomy.TestUtils.neverFunction;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;

class TryTest {

Expand Down Expand Up @@ -167,15 +172,15 @@ void ofBiFn() {
}

@Test
void from() {
void fromSupplier() {
}

@Test
void testFrom() {
void fromFunction() {
}

@Test
void testFrom1() {
void fromBiFunction() {
}

@Test
Expand Down Expand Up @@ -267,7 +272,8 @@ void fold() {

@Test
void stream() {

assertEquals( 1, TRY_SUCCESS.stream().count() );
assertEquals( 0, TRY_FAILURE.stream().count() );
}

@Test
Expand All @@ -276,10 +282,31 @@ void filter() {

@Test
void exec() {

}

@Test
void consume() {
assertThrows( NullPointerException.class, () -> TRY_SUCCESS.consume( null ));
assertThrows( NullPointerException.class, () -> TRY_FAILURE.consume( null ));
assertThrows( LinkageError.class, () -> TRY_SUCCESS.consume( neverConsumer() ) );
assertDoesNotThrow( () -> TRY_FAILURE.consume( neverConsumer() ) );
//
SingleUseConsumer<String> consumer = new SingleUseConsumer<>();
TRY_SUCCESS.consume(consumer);
assertTrue( consumer.usedJustOnce() );
}

@Test
void consumeErr() {
assertThrows( NullPointerException.class, () -> TRY_SUCCESS.consumeErr( null ));
assertThrows( NullPointerException.class, () -> TRY_FAILURE.consumeErr( null ));
assertDoesNotThrow( () -> TRY_SUCCESS.consumeErr( neverConsumer() ) );
assertThrows( LinkageError.class, () -> TRY_FAILURE.consumeErr( neverConsumer() ) );
//
SingleUseConsumer<Throwable> consumer = new SingleUseConsumer<>();
TRY_FAILURE.consumeErr(consumer);
assertTrue( consumer.usedJustOnce() );
}

@Test
Expand Down Expand Up @@ -327,13 +354,22 @@ void flatMap() {

@Test
void ifPredicate() {
final Predicate<String> predicateS = x -> x.startsWith( "S" );
assertTrue( TRY_SUCCESS.ifPredicate( predicateS ) );
assertFalse( TRY_SUCCESS.ifPredicate( predicateS.negate() ) );
//
assertFalse( TRY_FAILURE.ifPredicate( predicateS ) );
assertFalse( TRY_FAILURE.ifPredicate( predicateS.negate() ) );
}

@Test
void contains() {
assertTrue( Try.Success.of( SUPPLIED_STRING ).contains( "SuppliedString" ) );
assertFalse( Try.Success.of( SUPPLIED_STRING ).contains( "randomstring#*@(#$" ) );
assertFalse( Try.Failure.of( IOE ).contains( "SuppliedString" ) );
// null : allowed but always false.
assertFalse( TRY_SUCCESS.contains( null ) );
assertFalse( TRY_FAILURE.contains( null ) );
}

@Test
Expand All @@ -346,17 +382,36 @@ void orElseGet() {

@Test
void recover() {
assertThrows( NullPointerException.class,
() -> TRY_SUCCESS.recover( null ));
assertThrows( NullPointerException.class,
() -> TRY_FAILURE.recover( null ));
assertDoesNotThrow( () -> TRY_SUCCESS.recover( (x) -> null )); // because fn not used
assertThrows( NullPointerException.class,
() -> TRY_FAILURE.recover( (x) -> null ));
//
final Function<Throwable,String> e2v = Throwable::getMessage;
assertEquals( MSG_FAIL, TRY_FAILURE.recover( e2v ) );
assertEquals( MSG_SUCCESS, TRY_SUCCESS.recover( e2v ) );
}


@Test
void forfeit() {
assertThrows( NullPointerException.class,
() -> TRY_SUCCESS.forfeit( null ) );
assertThrows( NullPointerException.class,
() -> TRY_FAILURE.forfeit( null ) );
assertThrows( NullPointerException.class,
() -> TRY_SUCCESS.forfeit( (x) -> null ) );
assertDoesNotThrow( () -> TRY_FAILURE.forfeit( (x) -> null ) ); // because fn not used
//
final Function<String,Throwable> e2v = ArithmeticException::new;
assertEquals( MSG_FAIL, TRY_FAILURE.forfeit( e2v ).getMessage() );
assertEquals( MSG_SUCCESS, TRY_SUCCESS.forfeit( e2v ).getMessage() );
}


@Test
void consumeErr() {
}

@Test
void mapErr() {
Expand All @@ -372,12 +427,52 @@ void flatMapErr() {

@Test
void expect() {

assertDoesNotThrow( TRY_SUCCESS::expect );
assertEquals( MSG_SUCCESS, TRY_SUCCESS.expect() );
//
assertThrows( NoSuchElementException.class, () -> TRY_FAILURE.expect() );
try {
TRY_FAILURE.expect();
} catch(NoSuchElementException e) {
// ensure we set the Throwable as a cause for the created NoSuchElementException
assertNotNull( e.getCause() );
assertEquals( MSG_FAIL, e.getCause().getMessage() );
}
}

@Test
void getOrThrow() {
// TRY_FAILURE.getOrThrow( (x) -> new RuntimeException("sdf") );
// null input
assertThrows( NullPointerException.class, () -> TRY_SUCCESS.getOrThrow( null ) );
assertThrows( NullPointerException.class, () -> TRY_FAILURE.getOrThrow( null ) );
assertThrows( NullPointerException.class,
() -> TRY_FAILURE.getOrThrow( (x) -> null ) );
//
//
assertDoesNotThrow( () -> TRY_SUCCESS.getOrThrow( neverFunction()) );
// if we use an IOException::new here, we would have to wrap it in a try-catch.
assertEquals( MSG_SUCCESS, TRY_SUCCESS.getOrThrow( RuntimeException::new ));
//
//
assertThrows( IOException.class, () -> TRY_FAILURE.getOrThrow( IOException::new ) );
try {
// this form will use the IOException(Throwable) constructor
TRY_FAILURE.getOrThrow( IOException::new );
} catch (IOException e) {
// ensure we are applying function correctly
assertNotNull( e.getCause() );
assertEquals( MSG_FAIL, e.getCause().getMessage() );
}

try {
// this form will use the IOException(String) constructor; more like a supplier
// (function input argument is ignored)
TRY_FAILURE.getOrThrow( __ -> new IOException("Here be Exceptions") );
} catch (IOException e) {
// ensure we are applying function correctly
assertNull( e.getCause() );
assertEquals( "Here be Exceptions", e.getMessage() );
}

}

Expand Down

0 comments on commit a0b75e1

Please sign in to comment.