aboutsummaryrefslogtreecommitdiff
path: root/crates/mbe/src/tests/rule.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/mbe/src/tests/rule.rs')
-rw-r--r--crates/mbe/src/tests/rule.rs45
1 files changed, 45 insertions, 0 deletions
diff --git a/crates/mbe/src/tests/rule.rs b/crates/mbe/src/tests/rule.rs
new file mode 100644
index 000000000..07277966d
--- /dev/null
+++ b/crates/mbe/src/tests/rule.rs
@@ -0,0 +1,45 @@
1use syntax::{ast, AstNode};
2
3use crate::ast_to_token_tree;
4
5use super::*;
6
7#[test]
8fn test_valid_arms() {
9 fn check(macro_body: &str) {
10 let m = parse_macro_arm(macro_body);
11 m.unwrap();
12 }
13
14 check("($i:ident) => ()");
15 check("($($i:ident)*) => ($_)");
16 check("($($true:ident)*) => ($true)");
17 check("($($false:ident)*) => ($false)");
18 check("($) => ($)");
19}
20
21#[test]
22fn test_invalid_arms() {
23 fn check(macro_body: &str, err: ParseError) {
24 let m = parse_macro_arm(macro_body);
25 assert_eq!(m, Err(err));
26 }
27 check("invalid", ParseError::Expected("expected subtree".into()));
28
29 check("$i:ident => ()", ParseError::Expected("expected subtree".into()));
30 check("($i:ident) ()", ParseError::Expected("expected `=`".into()));
31 check("($($i:ident)_) => ()", ParseError::InvalidRepeat);
32
33 check("($i) => ($i)", ParseError::UnexpectedToken("bad fragment specifier 1".into()));
34 check("($i:) => ($i)", ParseError::UnexpectedToken("bad fragment specifier 1".into()));
35}
36
37fn parse_macro_arm(arm_definition: &str) -> Result<crate::MacroRules, ParseError> {
38 let macro_definition = format!(" macro_rules! m {{ {} }} ", arm_definition);
39 let source_file = ast::SourceFile::parse(&macro_definition).ok().unwrap();
40 let macro_definition =
41 source_file.syntax().descendants().find_map(ast::MacroRules::cast).unwrap();
42
43 let (definition_tt, _) = ast_to_token_tree(&macro_definition.token_tree().unwrap()).unwrap();
44 crate::MacroRules::parse(&definition_tt)
45}