diff --git a/core/src/main/java/it/cavallium/warppi/math/rules/dsl/DslAggregateException.java b/core/src/main/java/it/cavallium/warppi/math/rules/dsl/DslAggregateException.java index 1dcf7019..38d5c05b 100644 --- a/core/src/main/java/it/cavallium/warppi/math/rules/dsl/DslAggregateException.java +++ b/core/src/main/java/it/cavallium/warppi/math/rules/dsl/DslAggregateException.java @@ -1,6 +1,7 @@ package it.cavallium.warppi.math.rules.dsl; import java.util.List; +import java.util.Objects; /** * Thrown when processing DSL code which contains one or more errors. @@ -24,4 +25,18 @@ public class DslAggregateException extends Exception { public List getErrors() { return errors; } + + @Override + public boolean equals(final Object o) { + if (!(o instanceof DslAggregateException)) { + return false; + } + final DslAggregateException other = (DslAggregateException) o; + return this.errors.equals(other.errors); + } + + @Override + public int hashCode() { + return Objects.hash(errors); + } } diff --git a/core/src/main/java/it/cavallium/warppi/math/rules/dsl/UndefinedSubFunction.java b/core/src/main/java/it/cavallium/warppi/math/rules/dsl/UndefinedSubFunction.java index 8816bd47..955b29df 100644 --- a/core/src/main/java/it/cavallium/warppi/math/rules/dsl/UndefinedSubFunction.java +++ b/core/src/main/java/it/cavallium/warppi/math/rules/dsl/UndefinedSubFunction.java @@ -2,6 +2,8 @@ package it.cavallium.warppi.math.rules.dsl; import it.cavallium.warppi.math.rules.dsl.frontend.Token; +import java.util.Objects; + /** * Occurs when a sub-function is used in one of the replacement pattern of a PatternRule, * but not defined (captured) in the target pattern. @@ -29,4 +31,18 @@ public class UndefinedSubFunction implements DslError { public String getName() { return identifier.lexeme; } + + @Override + public boolean equals(final Object o) { + if (!(o instanceof UndefinedSubFunction)) { + return false; + } + final UndefinedSubFunction other = (UndefinedSubFunction) o; + return this.identifier.equals(other.identifier); + } + + @Override + public int hashCode() { + return Objects.hash(identifier); + } } diff --git a/core/src/test/java/it/cavallium/warppi/math/rules/dsl/RulesDslTest.java b/core/src/test/java/it/cavallium/warppi/math/rules/dsl/RulesDslTest.java index d27499c2..9eb52d8c 100644 --- a/core/src/test/java/it/cavallium/warppi/math/rules/dsl/RulesDslTest.java +++ b/core/src/test/java/it/cavallium/warppi/math/rules/dsl/RulesDslTest.java @@ -2,19 +2,29 @@ package it.cavallium.warppi.math.rules.dsl; import it.cavallium.warppi.math.rules.Rule; import it.cavallium.warppi.math.rules.RuleType; +import it.cavallium.warppi.math.rules.dsl.frontend.IncompleteNumberLiteral; +import it.cavallium.warppi.math.rules.dsl.frontend.Token; +import it.cavallium.warppi.math.rules.dsl.frontend.TokenType; +import it.cavallium.warppi.math.rules.dsl.frontend.UnexpectedToken; import it.cavallium.warppi.math.rules.dsl.patterns.NegativePattern; import it.cavallium.warppi.math.rules.dsl.patterns.NumberPattern; import it.cavallium.warppi.math.rules.dsl.patterns.SubFunctionPattern; import it.cavallium.warppi.math.rules.dsl.patterns.SumPattern; import org.junit.Test; +import org.junit.rules.ExpectedException; import java.math.BigDecimal; import java.util.Arrays; +import java.util.Collections; import java.util.List; +import static org.hamcrest.CoreMatchers.equalTo; import static org.junit.Assert.*; public class RulesDslTest { + @org.junit.Rule + public ExpectedException thrown = ExpectedException.none(); + @Test public void validRules() throws DslAggregateException { final List rules = RulesDsl.makeRules( @@ -52,18 +62,39 @@ public class RulesDslTest { assertEquals(expected, rules); } - @Test(expected = DslAggregateException.class) + @Test public void lexerError() throws DslAggregateException { + thrown.expect(DslAggregateException.class); + thrown.expect(equalTo( + new DslAggregateException(Collections.singletonList( + new IncompleteNumberLiteral(16, "2.") + )) + )); + RulesDsl.makeRules("reduction test: 2. 5 -> 1"); } - @Test(expected = DslAggregateException.class) + @Test public void parserError() throws DslAggregateException { + thrown.expect(DslAggregateException.class); + thrown.expect(equalTo( + new DslAggregateException(Collections.singletonList( + new UnexpectedToken(new Token(TokenType.EOF, "", 24)) + )) + )); + RulesDsl.makeRules("existence test: x + y ->"); } - @Test(expected = DslAggregateException.class) + @Test public void undefinedSubFunction() throws DslAggregateException { + thrown.expect(DslAggregateException.class); + thrown.expect(equalTo( + new DslAggregateException(Collections.singletonList( + new UndefinedSubFunction(new Token(TokenType.IDENTIFIER, "y", 25)) + )) + )); + RulesDsl.makeRules("expansion test: x -> x + y"); } }