6 releases

0.3.3 May 28, 2022
0.3.2 May 28, 2022
0.2.0 Jan 20, 2022
0.1.0 Jan 16, 2022

#291 in Parser tooling

MIT license

10KB
227 lines

This crate allows you to write parser's tests like follows:

let input = "#( a b #items x y )*";
let spans = "   ^ ^ ^----^ ^ ^   ";
let expected = [
    Rule::token,
    Rule::token,
    Rule::interpolate,
    Rule::token,
    Rule::token,
];

Highlight each token with ^, ^^ or ^---^ to indicate it's span. parser_test::test() will make sure that parser output containts excactly those tokens in specified positions.

Any parser can be used, as long as you implement Token trait for it's output. Generic TestToken is provided and can probably be used for all use cases.

So far this crate is used to test pest parsers:

let mut output = Lexer::parse(Rule::interpolate_repetition, input).unwrap();
let output = output.next().unwrap().into_inner().map(|t| {
    let span = t.as_span();
    TestToken {
        start: span.start(),
        end: span.end() - 1,
        rule: t.as_rule()
    }
});
assert!(parser_test::test(output, expected, spans));

No runtime deps