macro_rules! assert_parse {
($ty: ty, $str: literal, $str2: literal, $($ast: pat)+) => { ... };
($ty: ty, $str: literal) => { ... };
($ty: ty, $str: literal, $str2: literal) => { ... };
($ty: ty, $str: literal, $($ast: pat)+) => { ... };
}
Expand description
(Requires feature “testing”) Given a Node, and a string, this will expand to code that sets up a parser, and parses the given string against the given node. If the parse failed this macro will panic with a readable failure. It then writes the result out using crate::CursorWriteSink, writing the parsed Node back out to a string. If resulting string from the given string, then the macro will panic with a readable failure.
In rare cases it might be necessary to ensure the resulting string differs from the input, for example if a grammar is serialized in a specific order but allows parsing in any order (many style values do this). In these cases, a second string can be provided which will be asserted gainst the output instead.
use css_parse::*;
assert_parse!(T![Ident], "foo");
// Equivalent to:
assert_parse!(T![Ident], "foo", "foo");
For more complex types (for example enum variants), you might want to assert that the given AST node matches an expected pattern (for example, one enum variant was chosen over another). In these cases, passing the match pattern as the third (or fourth) argument will assert that the parsed output struct matches the given pattern:
use css_parse::*;
use csskit_derives::*;
#[derive(Parse, ToCursors, Debug)]
enum IdentOrNumber {
Ident(T![Ident]),
Number(T![Number]),
}
assert_parse!(IdentOrNumber, "foo", IdentOrNumber::Ident(_));
assert_parse!(IdentOrNumber, "12", IdentOrNumber::Number(_));