
org.parboiled.testing.ParboiledTest Maven / Gradle / Ivy
/*
* Copyright (C) 2009-2011 Mathias Doenitz
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.parboiled.testing;
import org.parboiled.Node;
import org.parboiled.Rule;
import org.parboiled.buffers.InputBuffer;
import org.parboiled.common.Predicate;
import org.parboiled.parserunners.RecoveringParseRunner;
import org.parboiled.parserunners.ReportingParseRunner;
import org.parboiled.support.ParsingResult;
import org.parboiled.parserunners.RecoveringParseRunner;
import org.parboiled.parserunners.ReportingParseRunner;
import org.parboiled.support.ParseTreeUtils;
import org.parboiled.support.ParsingResult;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import static org.parboiled.errors.ErrorUtils.printParseErrors;
import static org.parboiled.support.ParseTreeUtils.printNodeTree;
public abstract class ParboiledTest {
public class TestResult {
public final ParsingResult result;
public TestResult(ParsingResult result) {
this.result = result;
}
public TestResult hasNoErrors() {
if (result.hasErrors()) {
fail("\n--- ParseErrors ---\n" +
printParseErrors(result) +
"\n--- ParseTree ---\n" +
ParseTreeUtils.printNodeTree(result)
);
}
return this;
}
public TestResult hasErrors(String expectedErrors) {
assertEquals(printParseErrors(result), expectedErrors);
return this;
}
public TestResult hasParseTree(String expectedTree) {
assertEquals(ParseTreeUtils.printNodeTree(result), expectedTree);
return this;
}
public TestResult hasParseTree(Predicate> nodeFilter, Predicate> subTreeFilter,
String expectedTree) {
assertEquals(ParseTreeUtils.printNodeTree(result, nodeFilter, subTreeFilter), expectedTree);
return this;
}
public TestResult hasResult(V... expectedResults) {
assertEquals(toListReversed(result.valueStack), Arrays.asList(expectedResults));
return this;
}
private List toListReversed(Iterable iterable) {
List list = new ArrayList();
for (T t : iterable) list.add(t);
Collections.reverse(list);
return list;
}
}
public TestResult test(Rule rule, String input) {
return new TestResult(new ReportingParseRunner(rule).run(input));
}
public TestResult test(Rule rule, InputBuffer inputBuffer) {
return new TestResult(new ReportingParseRunner(rule).run(inputBuffer));
}
public TestResult testWithRecovery(Rule rule, String input) {
return new TestResult(new RecoveringParseRunner(rule).run(input));
}
public TestResult testWithRecovery(Rule rule, InputBuffer inputBuffer) {
return new TestResult(new RecoveringParseRunner(rule).run(inputBuffer));
}
protected abstract void fail(String message);
protected abstract void assertEquals(Object actual, Object expected);
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy