Test the content of DslAggregateExceptions in RulesDslTest

This commit is contained in:
Riccardo Azzolini 2019-02-07 20:20:58 +01:00
parent e65f20382f
commit d18e2d3708
3 changed files with 65 additions and 3 deletions

View File

@ -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<DslError> 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);
}
}

View File

@ -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 <code>PatternRule</code>,
* 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);
}
}

View File

@ -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<Rule> 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");
}
}