aboutsummaryrefslogtreecommitdiff
path: root/crates/mbe/src/tests/rule.rs
blob: 07277966d49956368efe6663ce1905e049a718c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
use syntax::{ast, AstNode};

use crate::ast_to_token_tree;

use super::*;

#[test]
fn test_valid_arms() {
    fn check(macro_body: &str) {
        let m = parse_macro_arm(macro_body);
        m.unwrap();
    }

    check("($i:ident) => ()");
    check("($($i:ident)*) => ($_)");
    check("($($true:ident)*) => ($true)");
    check("($($false:ident)*) => ($false)");
    check("($) => ($)");
}

#[test]
fn test_invalid_arms() {
    fn check(macro_body: &str, err: ParseError) {
        let m = parse_macro_arm(macro_body);
        assert_eq!(m, Err(err));
    }
    check("invalid", ParseError::Expected("expected subtree".into()));

    check("$i:ident => ()", ParseError::Expected("expected subtree".into()));
    check("($i:ident) ()", ParseError::Expected("expected `=`".into()));
    check("($($i:ident)_) => ()", ParseError::InvalidRepeat);

    check("($i) => ($i)", ParseError::UnexpectedToken("bad fragment specifier 1".into()));
    check("($i:) => ($i)", ParseError::UnexpectedToken("bad fragment specifier 1".into()));
}

fn parse_macro_arm(arm_definition: &str) -> Result<crate::MacroRules, ParseError> {
    let macro_definition = format!(" macro_rules! m {{ {} }} ", arm_definition);
    let source_file = ast::SourceFile::parse(&macro_definition).ok().unwrap();
    let macro_definition =
        source_file.syntax().descendants().find_map(ast::MacroRules::cast).unwrap();

    let (definition_tt, _) = ast_to_token_tree(&macro_definition.token_tree().unwrap()).unwrap();
    crate::MacroRules::parse(&definition_tt)
}