libslide/parser/
test_utils.rs

1#![allow(unused_macros)]
2macro_rules! __parse {
3    ($parser:ident, $inout:expr) => {
4        let inout: Vec<&str> = $inout.split(" => ").collect();
5        let pin = inout[0];
6        let pout = if inout.len() > 1 {
7            inout[1].to_owned()
8        } else {
9            pin.to_owned()
10        };
11        let tokens = scan(pin).tokens;
12        let (parsed, _) = $parser(tokens);
13        assert_eq!(parsed.to_string(), pout);
14    };
15}
16
17macro_rules! common_parser_tests {
18    ($($name:ident: $inout:expr)*) => {
19    $(
20        #[test]
21        fn $name() {
22            use crate::scanner::{scan};
23            use crate::parser::{parse_expression, parse_expression_pattern};
24
25            __parse!(parse_expression, $inout);
26            __parse!(parse_expression_pattern, $inout);
27        }
28    )*
29    }
30}
31
32macro_rules! parser_tests {
33    ($parser:ident $($name:ident: $program:expr)*) => {
34    $(
35        #[test]
36        fn $name() {
37            use crate::scanner::{scan};
38            use crate::parser::{$parser};
39
40            __parse!($parser, $program);
41        }
42    )*
43    }
44}
45
46macro_rules! parser_error_tests {
47    ($parser:ident $($name:ident: $program:expr => $error:expr)*) => {
48    $(
49        #[test]
50        fn $name() {
51            use crate::scanner::{scan};
52            use crate::parser::{$parser};
53
54            let tokens = scan($program).tokens;
55            let (_, errors) = $parser(tokens);
56            assert_eq!(errors.join("\n"), $error);
57        }
58    )*
59    }
60}