diff --git a/src/test/java/org/cactoos/func/BiFuncOfTest.java b/src/test/java/org/cactoos/func/BiFuncOfTest.java index 566db12587..c67eab3398 100644 --- a/src/test/java/org/cactoos/func/BiFuncOfTest.java +++ b/src/test/java/org/cactoos/func/BiFuncOfTest.java @@ -23,21 +23,22 @@ */ package org.cactoos.func; -import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicReference; +import org.cactoos.BiFunc; import org.cactoos.proc.ProcOf; -import org.cactoos.scalar.True; -import org.hamcrest.core.IsEqual; +import org.cactoos.scalar.Constant; +import org.hamcrest.core.IsSame; import org.junit.jupiter.api.Test; import org.llorllale.cactoos.matchers.Assertion; -import org.llorllale.cactoos.matchers.IsTrue; +import org.llorllale.cactoos.matchers.MatcherOf; /** * Test case for {@link BiFuncOf}. * * @since 0.20 - * @checkstyle JavadocMethodCheck (500 lines) * @checkstyle ClassDataAbstractionCouplingCheck (500 lines) */ +@SuppressWarnings("PMD.AvoidDuplicateLiterals") final class BiFuncOfTest { @Test @@ -45,35 +46,65 @@ void convertsFuncIntoBiFunc() throws Exception { new Assertion<>( "Must convert function into bi-function", new BiFuncOf<>( - new FuncOf<>(input -> 1) - ).apply(1, 2), - new IsEqual<>(1) + new FuncOf<>(input -> input) + ), + new MatcherOf<>( + func -> { + final Object first = new Object(); + final Object res = func.apply(first, "discarded"); + return res.equals(first); + } + ) ).affirm(); } @Test void convertsProcIntoBiFunc() throws Exception { - final AtomicBoolean done = new AtomicBoolean(false); + final AtomicReference done = new AtomicReference<>(); + final Object result = new Object(); new Assertion<>( "Must convert procedure into bi-function", - new BiFuncOf( + new BiFuncOf<>( new ProcOf<>( input -> { - done.set(true); + done.set(input); } ), - true - ).apply("hello world", 1), - new IsEqual<>(done.get()) + result + ), + new MatcherOf<>( + func -> { + final Object first = new Object(); + final Object res = func.apply(first, "discarded"); + return res.equals(result) && done.get().equals(first); + } + ) ).affirm(); } @Test void convertsScalarIntoBiFunc() throws Exception { + final Object obj = new Object(); new Assertion<>( "Must convert scalar into bi-function", - new BiFuncOf(new True()).apply(false, false), - new IsTrue() + new BiFuncOf<>(new Constant<>(obj)).apply("discarded", "discarded"), + new IsSame<>(obj) + ).affirm(); + } + + @Test + void convertsLambdaIntoBiFunc() throws Exception { + new Assertion<>( + "Must convert lambda into bi-function", + new BiFuncOf<>((first, second) -> new Object[] {first, second}), + new MatcherOf>( + func -> { + final Object first = new Object(); + final Object second = new Object(); + final Object[] res = func.apply(first, second); + return res.length == 2 && res[0].equals(first) && res[1].equals(second); + } + ) ).affirm(); } } diff --git a/src/test/java/org/cactoos/func/FuncOfTest.java b/src/test/java/org/cactoos/func/FuncOfTest.java index 180668e320..78adff9e76 100644 --- a/src/test/java/org/cactoos/func/FuncOfTest.java +++ b/src/test/java/org/cactoos/func/FuncOfTest.java @@ -23,44 +23,70 @@ */ package org.cactoos.func; -import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicReference; import org.cactoos.proc.ProcOf; import org.cactoos.scalar.Constant; -import org.hamcrest.core.IsEqual; import org.junit.jupiter.api.Test; import org.llorllale.cactoos.matchers.Assertion; +import org.llorllale.cactoos.matchers.MatcherOf; /** * Test case for {@link FuncOf}. * * @since 0.20 - * @checkstyle JavadocMethodCheck (500 lines) */ final class FuncOfTest { - @Test void convertsProcIntoFunc() throws Exception { - final AtomicBoolean done = new AtomicBoolean(false); + final AtomicReference done = new AtomicReference<>(); + final Object result = new Object(); new Assertion<>( - "Must convert procedure into function", - new FuncOf( + "Must convert Proc into Func", + new FuncOf<>( new ProcOf<>( input -> { - done.set(true); + done.set(input); } ), - true - ).apply("hello world"), - new IsEqual<>(done.get()) + result + ), + new MatcherOf<>( + func -> { + final Object input = new Object(); + final Object res = func.apply(input); + return res.equals(result) && done.get().equals(input); + } + ) ).affirm(); } @Test void convertsScalarIntoFunc() throws Exception { + final Object result = new Object(); + new Assertion<>( + "Must convert Scalar into Func", + new FuncOf<>(new Constant<>(result)), + new MatcherOf<>( + func -> { + final Object res = func.apply("discarded"); + return res.equals(result); + } + ) + ).affirm(); + } + + @Test + void convertsLambdaIntoFunc() throws Exception { new Assertion<>( - "Result of func must be equal to the original value", - new FuncOf<>(new Constant<>(1)).apply(new Object()), - new IsEqual<>(1) + "Must convert Lambda into Func", + new FuncOf<>(input -> input), + new MatcherOf<>( + func -> { + final Object input = new Object(); + final Object res = func.apply(input); + return res.equals(input); + } + ) ).affirm(); } } diff --git a/src/test/java/org/cactoos/proc/ProcOfTest.java b/src/test/java/org/cactoos/proc/ProcOfTest.java index ffd5bceadf..16dd560fdb 100644 --- a/src/test/java/org/cactoos/proc/ProcOfTest.java +++ b/src/test/java/org/cactoos/proc/ProcOfTest.java @@ -23,36 +23,58 @@ */ package org.cactoos.proc; -import java.util.ArrayList; -import java.util.List; +import java.util.concurrent.atomic.AtomicReference; import org.cactoos.func.FuncOf; import org.junit.jupiter.api.Test; import org.llorllale.cactoos.matchers.Assertion; -import org.llorllale.cactoos.matchers.HasValues; +import org.llorllale.cactoos.matchers.MatcherOf; /** * Test case for {@link ProcOf}. * * @since 0.3 - * @checkstyle JavadocMethodCheck (500 lines) */ final class ProcOfTest { @Test void worksWithFunc() throws Exception { - final String str = "test input"; - final List list = new ArrayList<>(1); - new ProcOf( - new FuncOf<>( - input -> { - list.add(input); - return list.size(); + final AtomicReference done = new AtomicReference<>(); + new Assertion<>( + "Must execute Proc with Func", + new ProcOf<>( + new FuncOf<>( + input -> { + done.set(input); + return true; + } + ) + ), + new MatcherOf<>( + proc -> { + final Object input = new Object(); + proc.exec(input); + return done.get() == input; } ) - ).exec(str); + ).affirm(); + } + + @Test + void worksWithLambda() throws Exception { + final AtomicReference done = new AtomicReference<>(); new Assertion<>( - "Must contains the expected value from func", - list, - new HasValues<>(str) + "Must execute Proc with Lambda", + new ProcOf<>( + input -> { + done.set(input); + } + ), + new MatcherOf<>( + proc -> { + final Object input = new Object(); + proc.exec(input); + return done.get() == input; + } + ) ).affirm(); } } diff --git a/src/test/java/org/cactoos/proc/RunnableOfTest.java b/src/test/java/org/cactoos/proc/RunnableOfTest.java index 38c5ddb0b6..9f5aa94cf7 100644 --- a/src/test/java/org/cactoos/proc/RunnableOfTest.java +++ b/src/test/java/org/cactoos/proc/RunnableOfTest.java @@ -23,7 +23,7 @@ */ package org.cactoos.proc; -import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicReference; import org.cactoos.scalar.ScalarOf; import org.junit.jupiter.api.Test; import org.llorllale.cactoos.matchers.Assertion; @@ -33,28 +33,27 @@ * Test case for {@link RunnableOf}. * * @since 0.2 - * @checkstyle JavadocMethodCheck (500 lines) */ -@SuppressWarnings("PMD.AvoidDuplicateLiterals") final class RunnableOfTest { @Test void convertsProcIntoRunnable() { - final AtomicBoolean done = new AtomicBoolean(); + final AtomicReference done = new AtomicReference<>(); + final Object obj = new Object(); new Assertion<>( "Must execute Runnable with Proc", new RunnableOf( new ProcOf<>( - ignored -> { - done.set(true); + input -> { + done.set(input); } ), - "ignored" + obj ), - new MatcherOf( - input -> { - input.run(); - return done.get(); + new MatcherOf<>( + runnable -> { + runnable.run(); + return done.get().equals(obj); } ) ).affirm(); @@ -62,21 +61,42 @@ void convertsProcIntoRunnable() { @Test void convertsScalarIntoRunnable() { - final AtomicBoolean done = new AtomicBoolean(); + final AtomicReference done = new AtomicReference<>(); + final Object obj = new Object(); new Assertion<>( "Must execute Runnable with Scalar", new RunnableOf( new ScalarOf<>( () -> { - done.set(true); - return null; + done.set(obj); + return "discarded"; } ) ), + new MatcherOf<>( + runnable -> { + runnable.run(); + return done.get().equals(obj); + } + ) + ).affirm(); + } + + @Test + void convertsLambdaIntoRunnable() { + final AtomicReference done = new AtomicReference<>(); + final Object obj = new Object(); + new Assertion<>( + "Must execute Runnable with Lambda", + new RunnableOf( + () -> { + done.set(obj); + } + ), new MatcherOf( - input -> { - input.run(); - return done.get(); + runnable -> { + runnable.run(); + return done.get().equals(obj); } ) ).affirm(); diff --git a/src/test/java/org/cactoos/scalar/AndInThreadsTest.java b/src/test/java/org/cactoos/scalar/AndInThreadsTest.java index c4eddfed23..df610eda06 100644 --- a/src/test/java/org/cactoos/scalar/AndInThreadsTest.java +++ b/src/test/java/org/cactoos/scalar/AndInThreadsTest.java @@ -23,7 +23,6 @@ */ package org.cactoos.scalar; -import java.util.ArrayList; import java.util.List; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -94,7 +93,7 @@ void allFalse() throws Exception { } @Test - void emptyIterator() throws Exception { + void emptyIterable() throws Exception { new Assertion<>( "Iterator must be empty", new AndInThreads(new IterableOf>()), @@ -109,7 +108,7 @@ void iteratesList() { "Must iterate a list with a procedure", new AndInThreads( new Mapped<>( - new FuncOf>(list::add, () -> true), + new FuncOf>(list::add, new True()), new IterableOf<>("hello", "world") ) ), @@ -135,32 +134,6 @@ void iteratesList() { ).affirm(); } - @Test - void iteratesEmptyList() { - final List list = new Synced<>( - new ArrayList<>(2) - ); - new Assertion<>( - "Must iterate an empty list", - new AndInThreads( - new Mapped<>( - new FuncOf>(list::add, () -> true), - new IterableOf() - ) - ), - new ScalarHasValue<>( - Matchers.allOf( - Matchers.equalTo(true), - new MatcherOf<>( - value -> { - return list.isEmpty(); - } - ) - ) - ) - ).affirm(); - } - @Test void worksWithFunc() throws Exception { MatcherAssert.assertThat( diff --git a/src/test/java/org/cactoos/scalar/RepeatedTest.java b/src/test/java/org/cactoos/scalar/RepeatedTest.java index 89f11250c5..6c335a0e36 100644 --- a/src/test/java/org/cactoos/scalar/RepeatedTest.java +++ b/src/test/java/org/cactoos/scalar/RepeatedTest.java @@ -61,7 +61,7 @@ void throwsIfZero() { () -> { throw new IllegalStateException("intended to fail"); }, - true + "discarded" ), 0 ).value(), diff --git a/src/test/java/org/cactoos/scalar/ScalarOfTest.java b/src/test/java/org/cactoos/scalar/ScalarOfTest.java index 86750db836..2e2625210c 100644 --- a/src/test/java/org/cactoos/scalar/ScalarOfTest.java +++ b/src/test/java/org/cactoos/scalar/ScalarOfTest.java @@ -23,11 +23,13 @@ */ package org.cactoos.scalar; +import java.util.concurrent.atomic.AtomicReference; import org.cactoos.func.FuncOf; import org.cactoos.proc.ProcOf; import org.cactoos.proc.RunnableOf; import org.junit.jupiter.api.Test; import org.llorllale.cactoos.matchers.Assertion; +import org.llorllale.cactoos.matchers.MatcherOf; import org.llorllale.cactoos.matchers.ScalarHasValue; /** @@ -42,7 +44,7 @@ final class ScalarOfTest { void worksWithCallable() { final Object obj = new Object(); new Assertion<>( - "must hold the same value as given by callable", + "Must convert Callable into Scalar", new ScalarOf<>(new CallableOf<>(new Constant<>(obj))), new ScalarHasValue<>(obj) ).affirm(); @@ -51,29 +53,68 @@ void worksWithCallable() { @Test void worksWithRunnable() { final Object obj = new Object(); + final Object result = new Object(); + final AtomicReference done = new AtomicReference<>(); new Assertion<>( - "must hold the same value as given", - new ScalarOf<>(new RunnableOf(ignored -> { }, "ignored"), obj), - new ScalarHasValue<>(obj) + "Must convert Runnable into Scalar", + new ScalarOf<>( + new RunnableOf( + () -> { + done.set(result); + } + ), + obj + ), + new MatcherOf<>( + scalar -> { + final Object res = scalar.value(); + return res.equals(obj) && done.get().equals(result); + } + ) ).affirm(); } @Test void worksWithFunc() { - final Object obj = new Object(); + final Object ipt = new Object(); new Assertion<>( - "must hold the same value as given by func", - new ScalarOf<>(new FuncOf<>(new Constant<>(obj)), "ignored"), - new ScalarHasValue<>(obj) + "Must convert Func into Scalar", + new ScalarOf<>(new FuncOf<>(input -> input), ipt), + new ScalarHasValue<>(ipt) ).affirm(); } @Test void worksWithProc() { + final Object ipt = new Object(); + final Object result = new Object(); + final AtomicReference done = new AtomicReference<>(); + new Assertion<>( + "Must convert Func into Scalar", + new ScalarOf<>( + new ProcOf<>( + input -> { + done.set(input); + } + ), + ipt, + result + ), + new MatcherOf<>( + scalar -> { + final Object res = scalar.value(); + return res.equals(result) && done.get().equals(ipt); + } + ) + ).affirm(); + } + + @Test + void worksWithLambda() { final Object obj = new Object(); new Assertion<>( - "must hold the expected value", - new ScalarOf<>(new ProcOf<>(ignored -> { }), "ignored", obj), + "Must convert Lambda into Scalar", + new ScalarOf<>(() -> obj), new ScalarHasValue<>(obj) ).affirm(); }