Skip to content

Commit

Permalink
Introduce tests for Checked class yegor256#1533
Browse files Browse the repository at this point in the history
  • Loading branch information
Olivier B. OURA committed Mar 9, 2021
1 parent 894cc11 commit caaf0d9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/main/java/org/cactoos/bytes/UncheckedBytes.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public final class UncheckedBytes implements Bytes {
/**
* Fallback.
*/
private final Func<? super Exception, ? extends byte[]> fallback;
private final Func<? super Exception, byte[]> fallback;

/**
* Ctor.
Expand All @@ -67,7 +67,7 @@ public UncheckedBytes(final Bytes bts) {
* @since 0.5
*/
public UncheckedBytes(final Bytes bts,
final Func<? super Exception, ? extends byte[]> fbk) {
final Func<? super Exception, byte[]> fbk) {
this.bytes = bts;
this.fallback = fbk;
}
Expand Down
27 changes: 27 additions & 0 deletions src/test/java/org/cactoos/scalar/CheckedTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@

import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.channels.AcceptPendingException;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.hamcrest.core.IsEqual;
import org.junit.Test;
import org.llorllale.cactoos.matchers.Assertion;

/**
* Test case for {@link Checked}.
Expand All @@ -46,6 +49,30 @@ public void runtimeExceptionGoesOut() throws Exception {
).value();
}

@Test(expected = IllegalStateException.class)
public void usesGenericVarianceOnExceptionTypes() throws Exception {
new Checked<String, IllegalStateException>(
() -> {
throw new IllegalStateException();
},
(Throwable ex) -> {
return new AcceptPendingException();
}
).value();
}

@SuppressWarnings("PMD.AvoidDuplicateLiterals")
public void usesContravarianceOnResultType() throws Exception {
new Assertion<>(
"Must use contravariance on result",
new Checked<CharSequence, IOException>(
() -> new String("contravariance"),
IOException::new
).value(),
new IsEqual<>("contravariance")
);
}

@Test(expected = IOException.class)
public void throwsIoException() throws Exception {
new Checked<>(
Expand Down

0 comments on commit caaf0d9

Please sign in to comment.