RulesDsl { public static List<Rule> makeRules(final String source) throws DslAggregateException { final List<DslError> errors = new ArrayList<>(); final Lexer lexer = new Lexer(source, errors::add); final Parser parser = new Parser(lexer.lex(), errors::add); final List<PatternRule> rules = parser.parse(); for (final PatternRule rule : rules) { undefinedSubFunctions(rule) .map(subFunc -> new UndefinedSubFunction( parser.getSubFunctionIdentifier(subFunc) )) .forEach(errors::add); } if (!errors.isEmpty()) { throw new DslAggregateException(errors); } return Collections.unmodifiableList(rules); } private  RulesDsl(); static List<Rule> makeRules(final String source);  }
@Test void validRules() throws DslAggregateException { final List<Rule> rules = RulesDsl.makeRules( "reduction test1: x -> x\n" + "expansion test2:\n" + " x -> --x\n" + "calculation test3:\n" + " 1 + 1 -> 2\n" ); final List<Rule> expected = Arrays.asList( new PatternRule( "test1", RuleType.REDUCTION, new SubFunctionPattern("x"), new SubFunctionPattern("x") ), new PatternRule( "test2", RuleType.EXPANSION, new SubFunctionPattern("x"), new NegativePattern(new NegativePattern(new SubFunctionPattern("x"))) ), new PatternRule( "test3", RuleType.CALCULATION, new SumPattern( new NumberPattern(new BigDecimal(1)), new NumberPattern(new BigDecimal(1)) ), new NumberPattern(new BigDecimal(2)) ) ); assertEquals(expected, rules); }
@Test void lexerError() { final var exception = assertThrows(DslAggregateException.class, () -> RulesDsl.makeRules("reduction test: 2. 5 -> 1") ); final var expectedErrors = Collections.singletonList( new IncompleteNumberLiteral(16, "2.") ); assertEquals(expectedErrors, exception.getErrors()); }
@Test void parserError() { final var exception = assertThrows(DslAggregateException.class, () -> RulesDsl.makeRules("existence test: x + y ->") ); final var expectedErrors = Collections.singletonList( new UnexpectedToken(new Token(TokenType.EOF, "", 24)) ); assertEquals(expectedErrors, exception.getErrors()); }
@Test void undefinedSubFunction() { final var exception = assertThrows(DslAggregateException.class, () -> RulesDsl.makeRules("expansion test: x -> x + y") ); final var expectedErrors = Collections.singletonList( new UndefinedSubFunction(new Token(TokenType.IDENTIFIER, "y", 25)) ); assertEquals(expectedErrors, exception.getErrors()); }