From 74dc2a3d88a41b2e14fd99b1cfdd2eb0955ddf91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Victor=20No=C3=ABl?= Date: Sun, 10 Jan 2021 15:07:46 +0100 Subject: [PATCH 1/3] (#1445) Cleanup around Proc and co --- src/main/java/org/cactoos/BiProc.java | 4 ++++ src/main/java/org/cactoos/proc/ForEach.java | 11 ++++------- .../java/org/cactoos/proc/ForEachInThreads.java | 11 ++++------- .../java/org/cactoos/proc/ForEachWithIndex.java | 10 +++------- src/main/java/org/cactoos/proc/ProcOf.java | 6 +++++- src/main/java/org/cactoos/scalar/And.java | 13 ++++++++++++- src/test/java/org/cactoos/io/TempFolderTest.java | 4 ---- 7 files changed, 32 insertions(+), 27 deletions(-) diff --git a/src/main/java/org/cactoos/BiProc.java b/src/main/java/org/cactoos/BiProc.java index fceb567d77..8ffeafc3d4 100644 --- a/src/main/java/org/cactoos/BiProc.java +++ b/src/main/java/org/cactoos/BiProc.java @@ -39,6 +39,10 @@ * @param Type of input * @param Type of input * @since 0.20 + * @todo #1445:30min Introduce BiProcOf on the model of ProcOf, FuncOf, etc + * with constructors taking Func, BiFunc, Proc and BiProc as primary. + * Add tests similar to those of ProcOfTest, FuncOfTest, etc to fully cover + * the implementation. Use it where needed, for example in AndWithIndexTest. */ public interface BiProc { diff --git a/src/main/java/org/cactoos/proc/ForEach.java b/src/main/java/org/cactoos/proc/ForEach.java index 67d2fa07f4..0d7a5db3e1 100644 --- a/src/main/java/org/cactoos/proc/ForEach.java +++ b/src/main/java/org/cactoos/proc/ForEach.java @@ -23,7 +23,6 @@ */ package org.cactoos.proc; -import org.cactoos.Func; import org.cactoos.Proc; import org.cactoos.func.FuncOf; import org.cactoos.scalar.And; @@ -47,7 +46,7 @@ *

* There is no thread-safety guarantee. * - * @param The type to itetare over + * @param The type to iterate over * @since 1.0 */ public final class ForEach implements Proc> { @@ -55,7 +54,7 @@ public final class ForEach implements Proc> { /** * The proc. */ - private final Func func; + private final Proc proc; /** * Ctor. @@ -63,15 +62,13 @@ public final class ForEach implements Proc> { * @param proc The proc to execute */ public ForEach(final Proc proc) { - this.func = new FuncOf<>( - proc, true - ); + this.proc = proc; } @Override public void exec(final Iterable input) throws Exception { new And( - this.func, input + new FuncOf<>(this.proc, true), input ).value(); } diff --git a/src/main/java/org/cactoos/proc/ForEachInThreads.java b/src/main/java/org/cactoos/proc/ForEachInThreads.java index 37dd70b1bc..8698463318 100644 --- a/src/main/java/org/cactoos/proc/ForEachInThreads.java +++ b/src/main/java/org/cactoos/proc/ForEachInThreads.java @@ -23,7 +23,6 @@ */ package org.cactoos.proc; -import org.cactoos.Func; import org.cactoos.Proc; import org.cactoos.func.FuncOf; import org.cactoos.scalar.AndInThreads; @@ -49,7 +48,7 @@ *

* There is no thread-safety guarantee. * - * @param The type to itetare over + * @param The type to iterate over * @since 1.0 */ public final class ForEachInThreads implements Proc> { @@ -57,7 +56,7 @@ public final class ForEachInThreads implements Proc> { /** * The proc. */ - private final Func func; + private final Proc proc; /** * Ctor. @@ -65,15 +64,13 @@ public final class ForEachInThreads implements Proc> { * @param proc The proc to execute */ public ForEachInThreads(final Proc proc) { - this.func = new FuncOf<>( - proc, true - ); + this.proc = proc; } @Override public void exec(final Iterable input) throws Exception { new AndInThreads( - this.func, input + new FuncOf<>(this.proc, true), input ).value(); } diff --git a/src/main/java/org/cactoos/proc/ForEachWithIndex.java b/src/main/java/org/cactoos/proc/ForEachWithIndex.java index 725bdc7854..025a13af71 100644 --- a/src/main/java/org/cactoos/proc/ForEachWithIndex.java +++ b/src/main/java/org/cactoos/proc/ForEachWithIndex.java @@ -23,10 +23,8 @@ */ package org.cactoos.proc; -import org.cactoos.BiFunc; import org.cactoos.BiProc; import org.cactoos.Proc; -import org.cactoos.func.BiFuncOf; import org.cactoos.scalar.AndWithIndex; /** @@ -57,7 +55,7 @@ public final class ForEachWithIndex implements Proc> { /** * The proc. */ - private final BiFunc func; + private final BiProc proc; /** * Ctor. @@ -65,15 +63,13 @@ public final class ForEachWithIndex implements Proc> { * @param proc The proc to execute */ public ForEachWithIndex(final BiProc proc) { - this.func = new BiFuncOf<>( - proc, true - ); + this.proc = proc; } @Override public void exec(final Iterable input) throws Exception { new AndWithIndex( - this.func, input + this.proc, input ).value(); } } diff --git a/src/main/java/org/cactoos/proc/ProcOf.java b/src/main/java/org/cactoos/proc/ProcOf.java index 8fe9fdca5d..b65248362f 100644 --- a/src/main/java/org/cactoos/proc/ProcOf.java +++ b/src/main/java/org/cactoos/proc/ProcOf.java @@ -46,7 +46,11 @@ public final class ProcOf implements Proc { * @param fnc The proc */ public ProcOf(final Func fnc) { - this((Proc) fnc::apply); + this( + input -> { + fnc.apply(input); + } + ); } /** diff --git a/src/main/java/org/cactoos/scalar/And.java b/src/main/java/org/cactoos/scalar/And.java index 8755f8aff4..ba2d370f32 100644 --- a/src/main/java/org/cactoos/scalar/And.java +++ b/src/main/java/org/cactoos/scalar/And.java @@ -105,10 +105,21 @@ public And(final Func func, final Iterable src) { */ @SafeVarargs public And(final X subject, final Func... conditions) { + this(subject, new IterableOf<>(conditions)); + } + + /** + * Ctor. + * @param subject The subject + * @param conditions Funcs to map + * @param Type of items in the iterable + * @since 0.49 + */ + public And(final X subject, final Iterable> conditions) { this( new Mapped<>( item -> (Scalar) () -> item.apply(subject), - new IterableOf<>(conditions) + conditions ) ); } diff --git a/src/test/java/org/cactoos/io/TempFolderTest.java b/src/test/java/org/cactoos/io/TempFolderTest.java index c3be895f47..2aa8b43a9a 100644 --- a/src/test/java/org/cactoos/io/TempFolderTest.java +++ b/src/test/java/org/cactoos/io/TempFolderTest.java @@ -87,7 +87,6 @@ void deletesNonEmptyDirectory() throws Exception { filename, "" ).value(); - return true; } ) ).exec( @@ -95,7 +94,6 @@ void deletesNonEmptyDirectory() throws Exception { "file1.txt", "file2.txt", "file3.txt" ) ); - return true; } ) ).exec( @@ -129,7 +127,6 @@ void createDirectoryWithDirectoriesAndFiles() throws Exception { filename, "" ).value(); - return true; } ) ).exec( @@ -137,7 +134,6 @@ void createDirectoryWithDirectoriesAndFiles() throws Exception { "1.txt", "2.txt", "3.txt" ) ); - return true; } ) ).exec( From 7e348742577bb77bfc01b9195271a8f54069c433 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Victor=20No=C3=ABl?= Date: Sun, 10 Jan 2021 15:08:25 +0100 Subject: [PATCH 2/3] (#1445) Remove casts where possible --- src/main/java/org/cactoos/scalar/And.java | 5 +++-- src/main/java/org/cactoos/scalar/AndInThreads.java | 8 +++++--- src/main/java/org/cactoos/scalar/AndWithIndex.java | 5 +++-- src/main/java/org/cactoos/scalar/Or.java | 5 +++-- 4 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/cactoos/scalar/And.java b/src/main/java/org/cactoos/scalar/And.java index ba2d370f32..dd33598d18 100644 --- a/src/main/java/org/cactoos/scalar/And.java +++ b/src/main/java/org/cactoos/scalar/And.java @@ -91,7 +91,8 @@ public And(final Func func, final X... src) { public And(final Func func, final Iterable src) { this( new Mapped<>( - item -> (Scalar) () -> func.apply(item), src + item -> new ScalarOf<>(() -> func.apply(item)), + src ) ); } @@ -118,7 +119,7 @@ public And(final X subject, final Func... conditions) { public And(final X subject, final Iterable> conditions) { this( new Mapped<>( - item -> (Scalar) () -> item.apply(subject), + item -> new ScalarOf<>(() -> item.apply(subject)), conditions ) ); diff --git a/src/main/java/org/cactoos/scalar/AndInThreads.java b/src/main/java/org/cactoos/scalar/AndInThreads.java index 37d896d1ef..90b226844c 100644 --- a/src/main/java/org/cactoos/scalar/AndInThreads.java +++ b/src/main/java/org/cactoos/scalar/AndInThreads.java @@ -92,7 +92,8 @@ public AndInThreads(final Func func, final Iterable src) { this( new Mapped<>( - item -> (Scalar) () -> func.apply(item), src + item -> new ScalarOf<>(() -> func.apply(item)), + src ) ); } @@ -164,7 +165,8 @@ public AndInThreads(final ExecutorService svc, this( svc, new Mapped<>( - item -> (Scalar) () -> func.apply(item), src + item -> new ScalarOf<>(() -> func.apply(item)), + src ) ); } @@ -210,7 +212,7 @@ public Boolean value() throws Exception { futures.add(this.service.submit(item::value)); } final boolean result = new And( - (Func, Boolean>) Future::get, + Future::get, futures ).value(); if (this.shut) { diff --git a/src/main/java/org/cactoos/scalar/AndWithIndex.java b/src/main/java/org/cactoos/scalar/AndWithIndex.java index a1f9103dc6..7997202872 100644 --- a/src/main/java/org/cactoos/scalar/AndWithIndex.java +++ b/src/main/java/org/cactoos/scalar/AndWithIndex.java @@ -29,6 +29,7 @@ import org.cactoos.Proc; import org.cactoos.Scalar; import org.cactoos.func.BiFuncOf; +import org.cactoos.func.FuncOf; import org.cactoos.iterable.IterableOf; import org.cactoos.iterable.Mapped; @@ -115,8 +116,8 @@ public AndWithIndex(final BiFunc func, final Iterable src) { this( new Mapped<>( - item -> (Func) input - -> func.apply(item, input), src + item -> new FuncOf<>(input -> func.apply(item, input)), + src ) ); } diff --git a/src/main/java/org/cactoos/scalar/Or.java b/src/main/java/org/cactoos/scalar/Or.java index c3438938ca..fefb1fc8ab 100644 --- a/src/main/java/org/cactoos/scalar/Or.java +++ b/src/main/java/org/cactoos/scalar/Or.java @@ -125,7 +125,8 @@ public Or(final Proc proc, final Iterable src) { public Or(final Func func, final Iterable src) { this( new Mapped<>( - item -> (Scalar) () -> func.apply(item), src + item -> new ScalarOf<>(() -> func.apply(item)), + src ) ); } @@ -140,7 +141,7 @@ public Or(final Func func, final Iterable src) { public Or(final X subject, final Func... conditions) { this( new Mapped<>( - item -> (Scalar) () -> item.apply(subject), + item -> new ScalarOf<>(() -> item.apply(subject)), new IterableOf<>(conditions) ) ); From 1cb1b88c989c68e445772b4f423b2f588d4aba53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Victor=20No=C3=ABl?= Date: Sun, 10 Jan 2021 15:08:53 +0100 Subject: [PATCH 3/3] (#1445) Improve test coverage of ProcOf, ScalarOf and co --- .../java/org/cactoos/func/BiFuncOfTest.java | 63 ++++++++++++++----- .../java/org/cactoos/func/FuncOfTest.java | 54 +++++++++++----- .../java/org/cactoos/proc/ProcOfTest.java | 52 ++++++++++----- .../java/org/cactoos/proc/RunnableOfTest.java | 54 +++++++++++----- .../org/cactoos/scalar/AndInThreadsTest.java | 35 ++--------- .../java/org/cactoos/scalar/RepeatedTest.java | 2 +- .../java/org/cactoos/scalar/ScalarOfTest.java | 61 +++++++++++++++--- 7 files changed, 217 insertions(+), 104 deletions(-) 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..decc2ae9bb 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,9 +93,9 @@ void allFalse() throws Exception { } @Test - void emptyIterator() throws Exception { + void emptyIterable() throws Exception { new Assertion<>( - "Iterator must be empty", + "Must iterate over empty iterable", new AndInThreads(new IterableOf>()), new ScalarHasValue<>(true) ).affirm(); @@ -106,10 +105,10 @@ void emptyIterator() throws Exception { void iteratesList() { final List list = new Synced<>(new ListOf<>()); new Assertion<>( - "Must iterate a list with a procedure", + "Must iterate a list with a function", 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..1a19164fbe 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 Proc 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(); }