Skip to content

Commit

Permalink
(#1445) Improve test coverage of ProcOf, ScalarOf and co
Browse files Browse the repository at this point in the history
  • Loading branch information
victornoel committed Jan 16, 2021
1 parent 7e34874 commit 1cb1b88
Show file tree
Hide file tree
Showing 7 changed files with 217 additions and 104 deletions.
63 changes: 47 additions & 16 deletions src/test/java/org/cactoos/func/BiFuncOfTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,57 +23,88 @@
*/
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
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<Object> done = new AtomicReference<>();
final Object result = new Object();
new Assertion<>(
"Must convert procedure into bi-function",
new BiFuncOf<String, Integer, Boolean>(
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<Boolean, Boolean, Boolean>(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<BiFunc<Object, Object, Object[]>>(
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();
}
}
54 changes: 40 additions & 14 deletions src/test/java/org/cactoos/func/FuncOfTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Object> done = new AtomicReference<>();
final Object result = new Object();
new Assertion<>(
"Must convert procedure into function",
new FuncOf<String, Boolean>(
"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();
}
}
52 changes: 37 additions & 15 deletions src/test/java/org/cactoos/proc/ProcOfTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> list = new ArrayList<>(1);
new ProcOf<String>(
new FuncOf<>(
input -> {
list.add(input);
return list.size();
final AtomicReference<Object> 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<Object> 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();
}
}
54 changes: 37 additions & 17 deletions src/test/java/org/cactoos/proc/RunnableOfTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -33,50 +33,70 @@
* 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<Object> 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<Runnable>(
input -> {
input.run();
return done.get();
new MatcherOf<>(
runnable -> {
runnable.run();
return done.get().equals(obj);
}
)
).affirm();
}

@Test
void convertsScalarIntoRunnable() {
final AtomicBoolean done = new AtomicBoolean();
final AtomicReference<Object> 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<Object> done = new AtomicReference<>();
final Object obj = new Object();
new Assertion<>(
"Must execute Runnable with Lambda",
new RunnableOf(
() -> {
done.set(obj);
}
),
new MatcherOf<Runnable>(
input -> {
input.run();
return done.get();
runnable -> {
runnable.run();
return done.get().equals(obj);
}
)
).affirm();
Expand Down
Loading

0 comments on commit 1cb1b88

Please sign in to comment.