forked from ibanknatoPrad/opel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add logical negation operation (allegro#27)
* add logical negation operation
- Loading branch information
Showing
13 changed files
with
177 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
#Thu Jun 09 09:02:39 CEST 2016 | ||
distributionBase=GRADLE_USER_HOME | ||
distributionPath=wrapper/dists | ||
zipStoreBase=GRADLE_USER_HOME | ||
zipStorePath=wrapper/dists | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-2.13-bin.zip | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-4.4.1-bin.zip |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/main/java/pl/allegro/tech/opel/LogicalNegationOperatorExpressionNode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package pl.allegro.tech.opel; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
|
||
class LogicalNegationOperatorExpressionNode implements OpelNode { | ||
|
||
private final OpelNode opelNode; | ||
private final ImplicitConversion conversion; | ||
|
||
LogicalNegationOperatorExpressionNode(OpelNode opelNode, ImplicitConversion conversion) { | ||
this.opelNode = opelNode; | ||
this.conversion = conversion; | ||
} | ||
|
||
@Override | ||
public CompletableFuture<?> getValue(EvalContext context) { | ||
return opelNode.getValue(context) | ||
.thenApply(this::getValue); | ||
} | ||
|
||
private Boolean getValue(Object value) { | ||
if (value == null) { | ||
throw new OpelException("Can't negate null value"); | ||
} if (value instanceof Boolean) { | ||
return !((boolean)value); | ||
} else if (conversion.hasConverter(value, Boolean.class)) { | ||
return !conversion.convert(value, Boolean.class); | ||
} | ||
throw new OpelException("Can't negate " + value.getClass().getSimpleName() + " type"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
src/test/groovy/pl/allegro/tech/opel/LogicalNegationOperatorExpressionNodeSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package pl.allegro.tech.opel | ||
|
||
import spock.lang.Ignore | ||
import spock.lang.Specification | ||
import spock.lang.Unroll | ||
|
||
import java.util.concurrent.CompletionException | ||
|
||
class LogicalNegationOperatorExpressionNodeSpec extends Specification { | ||
|
||
@Unroll | ||
def 'should return negation of boolean'() { | ||
given: | ||
def conversion = new ImplicitConversion() | ||
def node = new LogicalNegationOperatorExpressionNode(new LiteralExpressionNode(booleanValue), conversion) | ||
|
||
expect: | ||
node.getValue(EvalContext.empty()).get() == !booleanValue | ||
|
||
where: | ||
booleanValue << [true, false] | ||
} | ||
|
||
@Unroll | ||
def 'should return negation of converted value'() { | ||
given: | ||
def conversion = new ImplicitConversion() | ||
conversion.register(convertion) | ||
def node = new LogicalNegationOperatorExpressionNode(new LiteralExpressionNode(new Foo()), conversion) | ||
|
||
expect: | ||
node.getValue(EvalContext.empty()).get() == expectedValue | ||
|
||
where: | ||
convertion || expectedValue | ||
new ImplicitConversionUnit<>(Foo, Boolean, { true }) || false | ||
new ImplicitConversionUnit<>(Foo, Boolean, { false }) || true | ||
} | ||
|
||
def 'should thrown exception on negation of nulls'() { | ||
given: | ||
def conversion = new ImplicitConversion() | ||
def node = new LogicalNegationOperatorExpressionNode(new LiteralExpressionNode(null), conversion) | ||
|
||
when: | ||
node.getValue(EvalContext.empty()).join() | ||
|
||
then: | ||
def ex = thrown CompletionException | ||
ex.cause.class == OpelException | ||
} | ||
|
||
def 'should throw exception on negation of not boolean type'() { | ||
given: | ||
def conversion = new ImplicitConversion() | ||
def node = new LogicalNegationOperatorExpressionNode(new LiteralExpressionNode("some string"), conversion) | ||
|
||
when: | ||
node.getValue(EvalContext.empty()).join() | ||
|
||
then: | ||
def ex = thrown CompletionException | ||
ex.cause.class == OpelException | ||
} | ||
|
||
static class Foo { | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters