From 80e813e9336396803bf165b3e770f0f3028ef6c5 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Wed, 25 Jul 2018 11:00:53 +0100 Subject: [PATCH 1/6] Condition.{and(Condition),or(Condition),not()}: added Deprecate the whereNot(), andNot() and orNot() methods infavour of these new methods, which they all not use internally.. --- CHANGELOG | 10 ++ .../net/kemitix/conditional/Condition.java | 64 ++++++-- .../kemitix/conditional/FalseCondition.java | 11 ++ .../kemitix/conditional/TrueCondition.java | 10 ++ .../kemitix/conditional/ConditionalTest.java | 143 +++++++++++++++--- 5 files changed, 206 insertions(+), 32 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 09046a8..2af9d3d 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,16 @@ CHANGELOG ========= +0.6.0 +----- + +* Add `Condition.and(Condition)` +* Add `Condition.or(Condition)` +* Add `Condition.not()` +* Deprecate `Condition.whereNot(boolean)` +* Deprecate `Condition.andNot(boolean)` +* Deprecate `Condition.orNot(boolean)` + 0.5.0 ----- diff --git a/src/main/java/net/kemitix/conditional/Condition.java b/src/main/java/net/kemitix/conditional/Condition.java index aa4669b..8bfee11 100644 --- a/src/main/java/net/kemitix/conditional/Condition.java +++ b/src/main/java/net/kemitix/conditional/Condition.java @@ -35,7 +35,6 @@ public interface Condition { * Create a new {@code Condition} for the clause. * * @param clause the condition to test - * * @return the Condition */ static Condition where(final boolean clause) { @@ -49,59 +48,102 @@ public interface Condition { * Create a new {@code Condition} for the boolean opposite of the clause. * * @param clause the condition to test - * * @return the Condition + * @deprecated use {@link #not()} */ + @Deprecated static Condition whereNot(final boolean clause) { - return where(!clause); + return where(clause).not(); + } + + /** + * Checks if the Condition is true or not. + * + * @return true if the Condition is true + */ + boolean isTrue(); + + /** + * Checks of the Condition is false or not. + * + * @return true if the Condition is false + */ + boolean isFalse(); + + /** + * Negates the Condtion. + * + * @return a false Condition if the Condition is true, or a true Condition if the Condition is false. + */ + default Condition not() { + return Condition.where(isFalse()); } /** * Logically AND combine the current {@code Condition} with the clause. * * @param clause the condition to test - * * @return the Condition */ Condition and(Supplier clause); + /** + * Logicaly OR current {@code Condition} with the other {@code Condition}. + * + * @param other the other Condition + * @return true if both Conditions are true + */ + default Condition and(Condition other) { + return Condition.where(isTrue()).and(other::isTrue); + } + /** * Logically AND combine the current {@code Condition} with boolean opposite of the clause. * * @param clause the condition to test - * * @return the Condition + * @deprecated use {@link #not()} */ + @Deprecated default Condition andNot(final Supplier clause) { - return and(() -> !clause.get()); + return and(clause).not(); } /** * Logically OR combine the current {@code Condition} with the clause. * * @param clause the condition to test - * * @return the Condition */ @SuppressWarnings("PMD.ShortMethodName") Condition or(Supplier clause); + /** + * Logically OR the current {@code Condition} with the other {@code Condition}. + * + * @param other the other Condition + * @return true if either Condition is true + */ + default Condition or(Condition other) { + return where(isTrue()).or(other::isTrue); + } + /** * Logically OR combine the current {@code Condition} with the boolean opposite of the clause. * * @param clause the condition to test - * * @return the Condition + * @deprecated use {@link #not()} */ + @Deprecated default Condition orNot(final Supplier clause) { - return or(() -> !clause.get()); + return or(clause).not(); } /** * Perform this response if the {@code Condition} is {@code true}. * * @param response the response to perform - * * @return the Condition */ Condition then(Action response); @@ -117,7 +159,6 @@ public interface Condition { * Create a new {@code Condition} for the clause as a continuation to an existing {@code Condition}. * * @param clause the condition to test - * * @return the Condition */ default Condition otherwise(final Supplier clause) { @@ -141,4 +182,5 @@ public interface Condition { */ @SuppressWarnings(SuppressHelper.CS_ILLEGALTHROWS) void otherwiseThrow(Exception exception) throws Exception; + } diff --git a/src/main/java/net/kemitix/conditional/FalseCondition.java b/src/main/java/net/kemitix/conditional/FalseCondition.java index f47d8a4..b98f058 100644 --- a/src/main/java/net/kemitix/conditional/FalseCondition.java +++ b/src/main/java/net/kemitix/conditional/FalseCondition.java @@ -62,4 +62,15 @@ final class FalseCondition implements Condition { public void otherwiseThrow(final Exception exception) throws Exception { throw exception; } + + @Override + public boolean isTrue() { + return false; + } + + @Override + public boolean isFalse() { + return true; + } + } diff --git a/src/main/java/net/kemitix/conditional/TrueCondition.java b/src/main/java/net/kemitix/conditional/TrueCondition.java index 3597cb3..a261441 100644 --- a/src/main/java/net/kemitix/conditional/TrueCondition.java +++ b/src/main/java/net/kemitix/conditional/TrueCondition.java @@ -64,4 +64,14 @@ final class TrueCondition implements Condition { // do nothing } + @Override + public boolean isTrue() { + return true; + } + + @Override + public boolean isFalse() { + return false; + } + } diff --git a/src/test/java/net/kemitix/conditional/ConditionalTest.java b/src/test/java/net/kemitix/conditional/ConditionalTest.java index 7cc8465..423b930 100644 --- a/src/test/java/net/kemitix/conditional/ConditionalTest.java +++ b/src/test/java/net/kemitix/conditional/ConditionalTest.java @@ -7,18 +7,21 @@ import org.junit.Test; import java.io.IOException; import java.util.concurrent.atomic.AtomicInteger; +import static net.kemitix.conditional.Condition.where; + /** * @author Paul Campbell (pcampbell@kemitix.net). */ public class ConditionalTest implements WithAssertions { private Action thenResponse; - private Action otherwiseResponse; - private boolean thenFlag; - private boolean otherwiseFlag; + private final org.assertj.core.api.Condition trueCondition = + new org.assertj.core.api.Condition<>(Condition::isTrue, "is true"); + private final org.assertj.core.api.Condition falseCondition = + new org.assertj.core.api.Condition<>(Condition::isFalse, "is false"); @Before public void setUp() { @@ -77,7 +80,7 @@ public class ConditionalTest implements WithAssertions { @Test public void whereTrueThenDoSomethingAndThenDoSomethingElse() { //when - Condition.where(true) + where(true) .then(thenResponse) .and(() -> true) .then(otherwiseResponse); @@ -139,7 +142,7 @@ public class ConditionalTest implements WithAssertions { @Test public void whereTrueAndNotFalseThenRuns() { //when - Condition.where(true) + where(true) .andNot(() -> false) .then(thenResponse); //then @@ -149,7 +152,7 @@ public class ConditionalTest implements WithAssertions { @Test public void whereTrueAndNotTrueThenOtherwiseRuns() { //when - Condition.where(true) + where(true) .andNot(() -> true) .then(thenResponse) .otherwise(otherwiseResponse); @@ -160,7 +163,7 @@ public class ConditionalTest implements WithAssertions { @Test public void whereFalseOrNotFalseThenRuns() { //when - Condition.where(false) + where(false) .orNot(() -> false) .then(thenResponse); //then @@ -170,7 +173,7 @@ public class ConditionalTest implements WithAssertions { @Test public void whereFalseOrNotTrueThenOtherwiseRuns() { //when - Condition.where(false) + where(false) .orNot(() -> true) .then(thenResponse) .otherwise(otherwiseResponse); @@ -181,7 +184,7 @@ public class ConditionalTest implements WithAssertions { @Test public void whereFalseElseTrueThenOtherwiseRuns() { //when - Condition.where(false) + where(false) .then(thenResponse) .otherwise(() -> true) .then(otherwiseResponse); @@ -192,7 +195,7 @@ public class ConditionalTest implements WithAssertions { @Test public void whereFalseElseFalseThenNothingRuns() { //when - Condition.where(false) + where(false) .then(thenResponse) .otherwise(() -> false) .then(otherwiseResponse); @@ -203,7 +206,7 @@ public class ConditionalTest implements WithAssertions { @Test public void whereTrueChainedThensBothRuns() { //when - Condition.where(true) + where(true) .then(thenResponse) .then(otherwiseResponse); //then @@ -213,7 +216,7 @@ public class ConditionalTest implements WithAssertions { @Test public void whereFalseChainedThensNothingRuns() { //when - Condition.where(false) + where(false) .then(thenResponse) .then(otherwiseResponse); //then @@ -261,7 +264,7 @@ public class ConditionalTest implements WithAssertions { } private void when(final boolean clause) { - Condition.where(clause) + where(clause) .then(thenResponse) .otherwise(otherwiseResponse); } @@ -270,7 +273,7 @@ public class ConditionalTest implements WithAssertions { final boolean firstClause, final boolean secondClause ) { - Condition.where(firstClause) + where(firstClause) .and(() -> secondClause) .then(thenResponse) .otherwise(otherwiseResponse); @@ -280,7 +283,7 @@ public class ConditionalTest implements WithAssertions { final boolean firstClause, final boolean secondClause ) { - Condition.where(firstClause) + where(firstClause) .or(() -> secondClause) .then(thenResponse) .otherwise(otherwiseResponse); @@ -291,7 +294,7 @@ public class ConditionalTest implements WithAssertions { //given final AtomicInteger atomicInteger = new AtomicInteger(); //when - Condition.where(true) + where(true) .or(() -> atomicInteger.compareAndSet(0, 2)) .then(thenResponse); //then @@ -304,7 +307,7 @@ public class ConditionalTest implements WithAssertions { //given final AtomicInteger atomicInteger = new AtomicInteger(); //when - Condition.where(false) + where(false) .and(() -> atomicInteger.compareAndSet(0, 2)) .then(thenResponse); //then @@ -316,14 +319,14 @@ public class ConditionalTest implements WithAssertions { public void whereTrueThenThrowException() { //given assertThatExceptionOfType(IOException.class) - .isThrownBy(() -> Condition.where(true) + .isThrownBy(() -> where(true) .thenThrow(new IOException())); } @Test public void whereFalseThenDoNotThrowException() throws Exception { assertThatCode(() -> - Condition.where(false) + where(false) .thenThrow(new IOException())) .doesNotThrowAnyException(); } @@ -332,15 +335,113 @@ public class ConditionalTest implements WithAssertions { public void whereFalseOtherwiseThenThrowException() { //given assertThatExceptionOfType(IOException.class) - .isThrownBy(() -> Condition.where(false) + .isThrownBy(() -> where(false) .otherwiseThrow(new IOException())); } @Test public void whereTrueOtherwiseThenDoNotThrowException() throws Exception { assertThatCode(() -> - Condition.where(true) + where(true) .otherwiseThrow(new IOException())) .doesNotThrowAnyException(); } + + @Test + public void whereTrueConditionAndTrueConditionThenTrueCondition() { + //given + final Condition condition1 = Condition.where(true); + final Condition condition2 = Condition.where(true); + //when + final Condition result = condition1.and(condition2); + //then + assertThat(result).is(trueCondition); + } + + @Test + public void whereTrueConditionAndFalseConditionThenFalseCondition() { + //given + final Condition condition1 = Condition.where(true); + final Condition condition2 = Condition.where(false); + //when + final Condition result = condition1.and(condition2); + //then + assertThat(result).is(falseCondition); + } + + @Test + public void whereFalseConditionAndTrueConditionThenFalseCondition() { + //given + final Condition condition1 = Condition.where(false); + final Condition condition2 = Condition.where(true); + //when + final Condition result = condition1.and(condition2); + //then + assertThat(result).is(falseCondition); + } + + @Test + public void whereFalseConditionAndFalseConditionThenFalseCondition() { + //given + final Condition condition1 = Condition.where(false); + final Condition condition2 = Condition.where(false); + //when + final Condition result = condition1.and(condition2); + //then + assertThat(result).is(falseCondition); + } + + @Test + public void whereTrueConditionOrTrueConditionThenTrueCondition() { + //given + final Condition condition1 = Condition.where(true); + final Condition condition2 = Condition.where(true); + //when + final Condition result = condition1.or(condition2); + //then + assertThat(result).is(trueCondition); + } + + @Test + public void whereTrueConditionOrFalseConditionThenTrueCondition() { + //given + final Condition condition1 = Condition.where(true); + final Condition condition2 = Condition.where(false); + //when + final Condition result = condition1.or(condition2); + //then + assertThat(result).is(trueCondition); + } + + @Test + public void whereFalseConditionOrTrueConditionThenTrueCondition() { + //given + final Condition condition1 = Condition.where(false); + final Condition condition2 = Condition.where(true); + //when + final Condition result = condition1.or(condition2); + //then + assertThat(result).is(trueCondition); + } + + @Test + public void whereFalseConditionOrFalseConditionThenFalseCondition() { + //given + final Condition condition1 = Condition.where(false); + final Condition condition2 = Condition.where(false); + //when + final Condition result = condition1.or(condition2); + //then + assertThat(result).is(falseCondition); + } + + @Test + public void whereTrueWhenNotThenFalse() { + assertThat(Condition.where(true).not()).is(falseCondition); + } + + @Test + public void whereFalseWhenNotThenTriue() { + assertThat(Condition.where(false).not()).is(trueCondition); + } } From e853894382a1992deae5e4b90c80535ad9354a20 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sat, 28 Jul 2018 16:35:40 +0100 Subject: [PATCH 2/6] Condition.flatMap() added with Monad tests --- .../net/kemitix/conditional/Condition.java | 10 +++++ .../conditional/ConditionMonadTest.java | 45 +++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 src/test/java/net/kemitix/conditional/ConditionMonadTest.java diff --git a/src/main/java/net/kemitix/conditional/Condition.java b/src/main/java/net/kemitix/conditional/Condition.java index 8bfee11..4fe2d15 100644 --- a/src/main/java/net/kemitix/conditional/Condition.java +++ b/src/main/java/net/kemitix/conditional/Condition.java @@ -21,6 +21,7 @@ package net.kemitix.conditional; +import java.util.function.Function; import java.util.function.Supplier; /** @@ -183,4 +184,13 @@ public interface Condition { @SuppressWarnings(SuppressHelper.CS_ILLEGALTHROWS) void otherwiseThrow(Exception exception) throws Exception; + /** + * Apply the function to the Condtion, resulting an another Condition. + * + * @param f the function to apply + * @return a new Condition + */ + default Condition flatMap(final Function f) { + return f.apply(isTrue()); + } } diff --git a/src/test/java/net/kemitix/conditional/ConditionMonadTest.java b/src/test/java/net/kemitix/conditional/ConditionMonadTest.java new file mode 100644 index 0000000..74307b2 --- /dev/null +++ b/src/test/java/net/kemitix/conditional/ConditionMonadTest.java @@ -0,0 +1,45 @@ +package net.kemitix.conditional; + +import org.assertj.core.api.WithAssertions; +import org.junit.Test; + +import java.util.function.Function; + +public class ConditionMonadTest implements WithAssertions { + + private final boolean v = true; + private final Function f = i -> r(true); + private final Function g = i -> r(i); + + private static Condition r(boolean v) { + return Condition.where(v); + } + + @Test + public void leftIdentity() { + assertThat( + r(v).flatMap(f) + ).isEqualTo( + f.apply(v) + ); + } + + @Test + public void rightIdentity() { + assertThat( + r(v).flatMap(x -> r(x)) + ).isEqualTo( + r(v) + ); + } + + @Test + public void associativity() { + assertThat( + r(v).flatMap(f).flatMap(g) + ).isEqualTo( + r(v).flatMap(x -> f.apply(x).flatMap(g)) + ); + } + +} From 0324cb8929e50f3cee52a84c008f61fe55960faa Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sat, 28 Jul 2018 16:36:33 +0100 Subject: [PATCH 3/6] Value.where(Condition,...), Value.not() added --- .../kemitix/conditional/FalseValueClause.java | 6 +- .../kemitix/conditional/TrueValueClause.java | 6 +- .../java/net/kemitix/conditional/Value.java | 79 ++++++++++++++----- .../net/kemitix/conditional/ValueTest.java | 77 +++++++++++------- 4 files changed, 120 insertions(+), 48 deletions(-) diff --git a/src/main/java/net/kemitix/conditional/FalseValueClause.java b/src/main/java/net/kemitix/conditional/FalseValueClause.java index f4283c3..43e121d 100644 --- a/src/main/java/net/kemitix/conditional/FalseValueClause.java +++ b/src/main/java/net/kemitix/conditional/FalseValueClause.java @@ -28,13 +28,17 @@ import java.util.function.Supplier; * An intermediate state where the clause has evaluated to false. * * @param the type of the value - * * @author Paul Campbell (pcampbell@kemitix.net). */ class FalseValueClause implements Value.ValueClause { protected static final Value.ValueClause FALSE = new FalseValueClause<>(); + @Override + public Value.ValueClause not() { + return Value.where(true); + } + @Override public ValueSupplier then(final Supplier trueSupplier) { return new FalseValueSupplier<>(); diff --git a/src/main/java/net/kemitix/conditional/TrueValueClause.java b/src/main/java/net/kemitix/conditional/TrueValueClause.java index 46903c5..55264b2 100644 --- a/src/main/java/net/kemitix/conditional/TrueValueClause.java +++ b/src/main/java/net/kemitix/conditional/TrueValueClause.java @@ -30,13 +30,17 @@ import java.util.function.Supplier; * An intermediate state where the clause has evaluated to true. * * @param the type of the value - * * @author Paul Campbell (pcampbell@kemitix.net). */ class TrueValueClause implements Value.ValueClause { protected static final Value.ValueClause TRUE = new TrueValueClause<>(); + @Override + public Value.ValueClause not() { + return Value.where(false); + } + @Override public ValueSupplier then(final Supplier trueSupplier) { return new TrueValueSupplier<>(trueSupplier); diff --git a/src/main/java/net/kemitix/conditional/Value.java b/src/main/java/net/kemitix/conditional/Value.java index 8837095..81c8d01 100644 --- a/src/main/java/net/kemitix/conditional/Value.java +++ b/src/main/java/net/kemitix/conditional/Value.java @@ -38,41 +38,70 @@ public interface Value { * @param trueSupplier The supplier to provide the value when the clause is true * @param falseSupplier The supplier to provide the value when the clause is false * @param The type of the value - * * @return the value from either the trueSupplier or the falseSupplier */ - @SuppressWarnings("PMD.LawOfDemeter") static T where( final boolean clause, final Supplier trueSupplier, final Supplier falseSupplier - ) { - return Value.where(clause).then(trueSupplier) - .otherwise(falseSupplier); + ) { + return Value.where(clause) + .then(trueSupplier) + .otherwise(falseSupplier); + } + + /** + * Return one of two values depending on the value of a clause. + * + * @param clause The deciding clause + * @param trueSupplier The supplier to provide the value when the clause is true + * @param falseSupplier The supplier to provide the value when the clause is false + * @param The type of the value + * @return the value from either the trueSupplier or the falseSupplier + */ + static T where( + final Condition clause, + final Supplier trueSupplier, + final Supplier falseSupplier + ) { + return Value.where(clause.isTrue(), trueSupplier, falseSupplier); } /** * Return an Optional either containing a value, if the clause is true, or empty. * - * @param clause The deciding clause - * @param trueSupplier The supplier to provide the value when the clause is true - * @param The type of the value - * + * @param clause The deciding clause + * @param trueSupplier The supplier to provide the value when the clause is true + * @param The type of the value * @return an Optional either containing the value from the trueSupplier or empty */ static Optional where( final boolean clause, final Supplier trueSupplier - ) { + ) { return Optional.ofNullable(Value.where(clause, trueSupplier, () -> null)); } + /** + * Return an Optional either containing a value, if the clause is true, or empty. + * + * @param clause The deciding clause + * @param trueSupplier The supplier to provide the value when the clause is true + * @param The type of the value + * @return an Optional either containing the value from the trueSupplier or empty + */ + static Optional where( + final Condition clause, + final Supplier trueSupplier + ) { + return Value.where(clause.isTrue(), trueSupplier); + } + /** * Create a new {@link ValueClause} for the clause. * * @param clause the condition to test * @param the type of the value - * * @return a true or false value clause */ @SuppressWarnings("unchecked") @@ -83,14 +112,27 @@ public interface Value { return (ValueClause) FalseValueClause.FALSE; } + /** + * Create a new {@link ValueClause} for the clause. + * + * @param clause the condition to test + * @param the type of the value + * @return a true or false value clause + */ + @SuppressWarnings("unchecked") + static ValueClause where(final Condition clause) { + return Value.where(clause.isTrue()); + } + /** * Create a new {@link ValueClause} for the boolean opposite of the clause. * * @param clause the condition to test * @param the type of the value - * * @return a true or false value clause + * @deprecated use {@link #where(boolean)}.{@link ValueClause#not()} */ + @Deprecated static ValueClause whereNot(final boolean clause) { return where(!clause); } @@ -102,11 +144,17 @@ public interface Value { */ /* default */ interface ValueClause { + /** + * Negate the Value. + * + * @return a new ValueClause with a negated value + */ + ValueClause not(); + /** * Create a {@link ValueSupplier} with the {@link Supplier} should the {@link ValueClause} be true. * * @param trueSupplier the Supplier for the true value - * * @return the value supplier */ ValueSupplier then(Supplier trueSupplier); @@ -115,7 +163,6 @@ public interface Value { * Logically AND combine the current {@link ValueClause} with clause. * * @param clause the condition to test - * * @return a true or false value clause */ ValueClause and(Supplier clause); @@ -124,7 +171,6 @@ public interface Value { * Logically OR combine the current {@link ValueClause} with clause. * * @param clause the condition to test - * * @return a true or false value clause */ @SuppressWarnings("PMD.ShortMethodName") @@ -134,7 +180,6 @@ public interface Value { * Logically AND combine the current {@link ValueClause} with boolean opposite of the clause. * * @param clause the condition to test - * * @return a true or false value clause */ default ValueClause andNot(final Supplier clause) { @@ -145,7 +190,6 @@ public interface Value { * Logically OR combine the current {@link ValueClause} with boolean opposite of the clause. * * @param clause the condition to test - * * @return a true or false value clause */ default ValueClause orNot(final Supplier clause) { @@ -163,7 +207,6 @@ public interface Value { * Determine the value by whether the {@link ValueClause} was true or false. * * @param falseSupplier the Supplier for the false value - * * @return the value */ T otherwise(Supplier falseSupplier); diff --git a/src/test/java/net/kemitix/conditional/ValueTest.java b/src/test/java/net/kemitix/conditional/ValueTest.java index 3c72c6e..960ec02 100644 --- a/src/test/java/net/kemitix/conditional/ValueTest.java +++ b/src/test/java/net/kemitix/conditional/ValueTest.java @@ -14,13 +14,15 @@ import static org.assertj.core.api.Assertions.assertThat; public class ValueTest { private static final String TRUE = "true"; - private static final String FALSE = "false"; + private static final Condition TRUE_CONDITION = Condition.where(true); + private static final Condition FALSE_CONDITION = Condition.where(false); + @Test public void valueWhereClauseIsTrueTypeSafe() { //when - final String result = Value.where(true, () -> TRUE, () -> FALSE); + final String result = Value.where(TRUE_CONDITION, () -> TRUE, () -> FALSE); //then assertThat(result).isEqualTo(TRUE); } @@ -28,7 +30,7 @@ public class ValueTest { @Test public void valueWhereClauseIsFalseTypeSafe() { //when - final String result = Value.where(false, () -> TRUE, () -> FALSE); + final String result = Value.where(FALSE_CONDITION, () -> TRUE, () -> FALSE); //then assertThat(result).isEqualTo(FALSE); } @@ -36,7 +38,7 @@ public class ValueTest { @Test public void valueWhereClauseIsTrueIsOptional() { //when - final Optional result = Value.where(true, () -> TRUE); + final Optional result = Value.where(TRUE_CONDITION, () -> TRUE); //then assertThat(result).contains(TRUE); } @@ -44,7 +46,7 @@ public class ValueTest { @Test public void valueWhereClauseIsFalseIsEmptyOptional() { //when - final Optional result = Value.where(false, () -> TRUE); + final Optional result = Value.where(FALSE_CONDITION, () -> TRUE); //then assertThat(result).isEmpty(); } @@ -52,7 +54,7 @@ public class ValueTest { @Test public void valueWhereClauseIsTrue() { //when - val result = Value.where(true).then(() -> TRUE) + val result = Value.where(TRUE_CONDITION).then(() -> TRUE) .otherwise(() -> FALSE); //then assertThat(result).isEqualTo(TRUE); @@ -61,7 +63,7 @@ public class ValueTest { @Test public void valueWhereClauseIsFalse() { //when - val result = Value.where(false).then(() -> TRUE) + val result = Value.where(FALSE_CONDITION).then(() -> TRUE) .otherwise(() -> FALSE); //then assertThat(result).isEqualTo(FALSE); @@ -70,7 +72,7 @@ public class ValueTest { @Test public void valueWhereTrueAndTrueIsTrue() { //when - val result = Value.where(true).and(() -> true) + val result = Value.where(TRUE_CONDITION).and(() -> true) .then(() -> TRUE) .otherwise(() -> FALSE); //then @@ -80,7 +82,7 @@ public class ValueTest { @Test public void valueWhereTrueAndFalseIsFalse() { //when - val result = Value.where(true).and(() -> false) + val result = Value.where(TRUE_CONDITION).and(() -> false) .then(() -> TRUE) .otherwise(() -> FALSE); //then @@ -90,7 +92,7 @@ public class ValueTest { @Test public void valueWhereFalseAndTrueIsFalse() { //when - val result = Value.where(false).and(() -> true) + val result = Value.where(FALSE_CONDITION).and(() -> true) .then(() -> TRUE) .otherwise(() -> FALSE); //then @@ -100,7 +102,7 @@ public class ValueTest { @Test public void valueWhereFalseAndFalseIsFalse() { //when - val result = Value.where(false).and(() -> false) + val result = Value.where(FALSE_CONDITION).and(() -> false) .then(() -> TRUE) .otherwise(() -> FALSE); //then @@ -110,7 +112,7 @@ public class ValueTest { @Test public void valueWhereTrueOrTrueIsTrue() { //when - val result = Value.where(true).or(() -> true) + val result = Value.where(TRUE_CONDITION).or(() -> true) .then(() -> TRUE) .otherwise(() -> FALSE); //then @@ -120,7 +122,7 @@ public class ValueTest { @Test public void valueWhereTrueOrFalseIsTrue() { //when - val result = Value.where(true).or(() -> false) + val result = Value.where(TRUE_CONDITION).or(() -> false) .then(() -> TRUE) .otherwise(() -> FALSE); //then @@ -130,7 +132,7 @@ public class ValueTest { @Test public void valueWhereFalseOrTrueIsTrue() { //when - val result = Value.where(false).or(() -> true) + val result = Value.where(FALSE_CONDITION).or(() -> true) .then(() -> TRUE) .otherwise(() -> FALSE); //then @@ -140,7 +142,7 @@ public class ValueTest { @Test public void valueWhereFalseOrFalseIsFalse() { //when - val result = Value.where(false).or(() -> false) + val result = Value.where(FALSE_CONDITION).or(() -> false) .then(() -> TRUE) .otherwise(() -> FALSE); //then @@ -157,7 +159,7 @@ public class ValueTest { } @Test - public void valueWhereNotFalseIsTrue() { + public void valueWhereNotFalseIsTrue_deprecated() { //when val result = Value.whereNot(false).then(() -> TRUE) .otherwise(() -> FALSE); @@ -165,10 +167,19 @@ public class ValueTest { assertThat(result).isEqualTo(TRUE); } + @Test + public void valueWhereNotFalseIsTrue() { + //when + val result = Value.where(false).not().then(() -> TRUE) + .otherwise(() -> FALSE); + //then + assertThat(result).isEqualTo(TRUE); + } + @Test public void valueWhereTrueAndNotTrueIsFalse() { //when - val result = Value.where(true).andNot(() -> true) + val result = Value.where(TRUE_CONDITION).andNot(() -> true) .then(() -> TRUE) .otherwise(() -> FALSE); //then @@ -178,7 +189,7 @@ public class ValueTest { @Test public void valueWhereTrueAndNotFalseIsTrue() { //when - val result = Value.where(true).andNot(() -> false) + val result = Value.where(TRUE_CONDITION).andNot(() -> false) .then(() -> TRUE) .otherwise(() -> FALSE); //then @@ -188,7 +199,7 @@ public class ValueTest { @Test public void valueWhereFalseAndNotTrueIsFalse() { //when - val result = Value.where(false).andNot(() -> true) + val result = Value.where(FALSE_CONDITION).and(() -> true) .then(() -> TRUE) .otherwise(() -> FALSE); //then @@ -198,7 +209,7 @@ public class ValueTest { @Test public void valueWhereFalseAndNotFalseIsFalse() { //when - val result = Value.where(false).andNot(() -> false) + val result = Value.where(FALSE_CONDITION).and(() -> false) .then(() -> TRUE) .otherwise(() -> FALSE); //then @@ -218,17 +229,27 @@ public class ValueTest { @Test public void valueWhereTrueOrNotFalseIsTrue() { //when - val result = Value.where(true).orNot(() -> false) + val result = Value.where(TRUE_CONDITION).orNot(() -> false) .then(() -> TRUE) .otherwise(() -> FALSE); //then assertThat(result).isEqualTo(TRUE); } + @Test + public void valueWhereFalseOrNotTrueIsFalse_deprecated() { + //when + val result = Value.where(FALSE_CONDITION).orNot(() -> true) + .then(() -> TRUE) + .otherwise(() -> FALSE); + //then + assertThat(result).isEqualTo(FALSE); + } + @Test public void valueWhereFalseOrNotTrueIsFalse() { //when - val result = Value.where(false).orNot(() -> true) + val result = Value.where(FALSE_CONDITION).or(() -> true).not() .then(() -> TRUE) .otherwise(() -> FALSE); //then @@ -248,7 +269,7 @@ public class ValueTest { @Test public void valueWhereTrueThenIsNotEmpty() { //given - final Optional result = Value.where(true).then(() -> "value").optional(); + final Optional result = Value.where(TRUE_CONDITION).then(() -> "value").optional(); //then assertThat(result).contains("value"); } @@ -256,17 +277,17 @@ public class ValueTest { @Test public void valueWhereFalseThenIsEmpty() { //given - final Optional result = Value.where(false).then(() -> "value").optional(); + final Optional result = Value.where(FALSE_CONDITION).then(() -> "value").optional(); //when assertThat(result).isEmpty(); } @Test - public void shortCurcuitOr() { + public void shortCircuitOr() { //given final AtomicInteger atomicInteger = new AtomicInteger(); //when - final Optional result = Value.where(true) + final Optional result = Value.where(TRUE_CONDITION) .or(() -> atomicInteger.compareAndSet(0, 2)) .then(() -> "Pass") .optional(); @@ -276,11 +297,11 @@ public class ValueTest { } @Test - public void shortCurcuitAnd() { + public void shortCircuitAnd() { //given final AtomicInteger atomicInteger = new AtomicInteger(); //when - final Optional result = Value.where(false) + final Optional result = Value.where(FALSE_CONDITION) .and(() -> atomicInteger.compareAndSet(0, 2)) .then(() -> "Pass") .optional(); From 64bbf6947475abc47b88fa9a5bd188e9725f0031 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sat, 28 Jul 2018 17:21:35 +0100 Subject: [PATCH 4/6] Remove full travis support --- .gitmodules | 3 --- .travis-support | 1 - .travis.yml | 12 +----------- 3 files changed, 1 insertion(+), 15 deletions(-) delete mode 100644 .gitmodules delete mode 160000 .travis-support diff --git a/.gitmodules b/.gitmodules deleted file mode 100644 index c50d110..0000000 --- a/.gitmodules +++ /dev/null @@ -1,3 +0,0 @@ -[submodule ".travis-support"] - path = .travis-support - url = https://github.com/kemitix/kemitix-travis-support.git diff --git a/.travis-support b/.travis-support deleted file mode 160000 index b8593e5..0000000 --- a/.travis-support +++ /dev/null @@ -1 +0,0 @@ -Subproject commit b8593e541ba9a11447fa9559a83e5f99097ca4d2 diff --git a/.travis.yml b/.travis.yml index ea42614..65b9122 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,14 +5,4 @@ cache: directories: - "$HOME/.m2" install: true -script: "./mvnw -B -U clean install" -after_success: -- sh .travis-support/coveralls.sh -- bash <(curl -s https://codecov.io/bash) -deploy: - provider: script - script: sh .travis-support/deploy.sh - on: - branch: master -env: - global: +script: "mvn -B -U clean install" From 0fe8e14c7e62326be3e933eff2289cdc7cfabb10 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sat, 28 Jul 2018 17:30:15 +0100 Subject: [PATCH 5/6] ConditionalTest: remove static import of Condition.where() --- .../kemitix/conditional/ConditionalTest.java | 38 +++++++++---------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/src/test/java/net/kemitix/conditional/ConditionalTest.java b/src/test/java/net/kemitix/conditional/ConditionalTest.java index 423b930..6ddc282 100644 --- a/src/test/java/net/kemitix/conditional/ConditionalTest.java +++ b/src/test/java/net/kemitix/conditional/ConditionalTest.java @@ -7,8 +7,6 @@ import org.junit.Test; import java.io.IOException; import java.util.concurrent.atomic.AtomicInteger; -import static net.kemitix.conditional.Condition.where; - /** * @author Paul Campbell (pcampbell@kemitix.net). */ @@ -80,7 +78,7 @@ public class ConditionalTest implements WithAssertions { @Test public void whereTrueThenDoSomethingAndThenDoSomethingElse() { //when - where(true) + Condition.where(true) .then(thenResponse) .and(() -> true) .then(otherwiseResponse); @@ -142,7 +140,7 @@ public class ConditionalTest implements WithAssertions { @Test public void whereTrueAndNotFalseThenRuns() { //when - where(true) + Condition.where(true) .andNot(() -> false) .then(thenResponse); //then @@ -152,7 +150,7 @@ public class ConditionalTest implements WithAssertions { @Test public void whereTrueAndNotTrueThenOtherwiseRuns() { //when - where(true) + Condition.where(true) .andNot(() -> true) .then(thenResponse) .otherwise(otherwiseResponse); @@ -163,7 +161,7 @@ public class ConditionalTest implements WithAssertions { @Test public void whereFalseOrNotFalseThenRuns() { //when - where(false) + Condition.where(false) .orNot(() -> false) .then(thenResponse); //then @@ -173,7 +171,7 @@ public class ConditionalTest implements WithAssertions { @Test public void whereFalseOrNotTrueThenOtherwiseRuns() { //when - where(false) + Condition.where(false) .orNot(() -> true) .then(thenResponse) .otherwise(otherwiseResponse); @@ -184,7 +182,7 @@ public class ConditionalTest implements WithAssertions { @Test public void whereFalseElseTrueThenOtherwiseRuns() { //when - where(false) + Condition.where(false) .then(thenResponse) .otherwise(() -> true) .then(otherwiseResponse); @@ -195,7 +193,7 @@ public class ConditionalTest implements WithAssertions { @Test public void whereFalseElseFalseThenNothingRuns() { //when - where(false) + Condition.where(false) .then(thenResponse) .otherwise(() -> false) .then(otherwiseResponse); @@ -206,7 +204,7 @@ public class ConditionalTest implements WithAssertions { @Test public void whereTrueChainedThensBothRuns() { //when - where(true) + Condition.where(true) .then(thenResponse) .then(otherwiseResponse); //then @@ -216,7 +214,7 @@ public class ConditionalTest implements WithAssertions { @Test public void whereFalseChainedThensNothingRuns() { //when - where(false) + Condition.where(false) .then(thenResponse) .then(otherwiseResponse); //then @@ -264,7 +262,7 @@ public class ConditionalTest implements WithAssertions { } private void when(final boolean clause) { - where(clause) + Condition.where(clause) .then(thenResponse) .otherwise(otherwiseResponse); } @@ -273,7 +271,7 @@ public class ConditionalTest implements WithAssertions { final boolean firstClause, final boolean secondClause ) { - where(firstClause) + Condition.where(firstClause) .and(() -> secondClause) .then(thenResponse) .otherwise(otherwiseResponse); @@ -283,7 +281,7 @@ public class ConditionalTest implements WithAssertions { final boolean firstClause, final boolean secondClause ) { - where(firstClause) + Condition.where(firstClause) .or(() -> secondClause) .then(thenResponse) .otherwise(otherwiseResponse); @@ -294,7 +292,7 @@ public class ConditionalTest implements WithAssertions { //given final AtomicInteger atomicInteger = new AtomicInteger(); //when - where(true) + Condition.where(true) .or(() -> atomicInteger.compareAndSet(0, 2)) .then(thenResponse); //then @@ -307,7 +305,7 @@ public class ConditionalTest implements WithAssertions { //given final AtomicInteger atomicInteger = new AtomicInteger(); //when - where(false) + Condition.where(false) .and(() -> atomicInteger.compareAndSet(0, 2)) .then(thenResponse); //then @@ -319,14 +317,14 @@ public class ConditionalTest implements WithAssertions { public void whereTrueThenThrowException() { //given assertThatExceptionOfType(IOException.class) - .isThrownBy(() -> where(true) + .isThrownBy(() -> Condition.where(true) .thenThrow(new IOException())); } @Test public void whereFalseThenDoNotThrowException() throws Exception { assertThatCode(() -> - where(false) + Condition.where(false) .thenThrow(new IOException())) .doesNotThrowAnyException(); } @@ -335,14 +333,14 @@ public class ConditionalTest implements WithAssertions { public void whereFalseOtherwiseThenThrowException() { //given assertThatExceptionOfType(IOException.class) - .isThrownBy(() -> where(false) + .isThrownBy(() -> Condition.where(false) .otherwiseThrow(new IOException())); } @Test public void whereTrueOtherwiseThenDoNotThrowException() throws Exception { assertThatCode(() -> - where(true) + Condition.where(true) .otherwiseThrow(new IOException())) .doesNotThrowAnyException(); } From 5bcd9bdac2673a0ddeabb3d0db3dc414ee70bd60 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sat, 28 Jul 2018 17:30:35 +0100 Subject: [PATCH 6/6] ValueTest: remove underscores from test method names and user WithAssertions --- src/test/java/net/kemitix/conditional/ValueTest.java | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/test/java/net/kemitix/conditional/ValueTest.java b/src/test/java/net/kemitix/conditional/ValueTest.java index 960ec02..d32843d 100644 --- a/src/test/java/net/kemitix/conditional/ValueTest.java +++ b/src/test/java/net/kemitix/conditional/ValueTest.java @@ -1,17 +1,16 @@ package net.kemitix.conditional; import lombok.val; +import org.assertj.core.api.WithAssertions; import org.junit.Test; import java.util.Optional; import java.util.concurrent.atomic.AtomicInteger; -import static org.assertj.core.api.Assertions.assertThat; - /** * @author Paul Campbell (pcampbell@kemitix.net). */ -public class ValueTest { +public class ValueTest implements WithAssertions { private static final String TRUE = "true"; private static final String FALSE = "false"; @@ -159,7 +158,7 @@ public class ValueTest { } @Test - public void valueWhereNotFalseIsTrue_deprecated() { + public void deprecatedValueWhereNotFalseIsTrue() { //when val result = Value.whereNot(false).then(() -> TRUE) .otherwise(() -> FALSE); @@ -237,7 +236,7 @@ public class ValueTest { } @Test - public void valueWhereFalseOrNotTrueIsFalse_deprecated() { + public void deprecatedValueWhereFalseOrNotTrueIsFalse() { //when val result = Value.where(FALSE_CONDITION).orNot(() -> true) .then(() -> TRUE)