diff options
Diffstat (limited to 'crates/ra_mbe/src/tests.rs')
-rw-r--r-- | crates/ra_mbe/src/tests.rs | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/crates/ra_mbe/src/tests.rs b/crates/ra_mbe/src/tests.rs index 312fa4626..d7482c63d 100644 --- a/crates/ra_mbe/src/tests.rs +++ b/crates/ra_mbe/src/tests.rs | |||
@@ -3,6 +3,54 @@ use test_utils::assert_eq_text; | |||
3 | 3 | ||
4 | use super::*; | 4 | use super::*; |
5 | 5 | ||
6 | mod rule_parsing { | ||
7 | use ra_syntax::{ast, AstNode}; | ||
8 | |||
9 | use super::*; | ||
10 | use crate::ast_to_token_tree; | ||
11 | |||
12 | #[test] | ||
13 | fn test_valid_arms() { | ||
14 | fn check(macro_body: &str) { | ||
15 | let m = parse_macro_arm(macro_body); | ||
16 | m.unwrap(); | ||
17 | } | ||
18 | |||
19 | check("($i:ident) => ()"); | ||
20 | check("($($i:ident)*) => ($_)"); | ||
21 | check("($($true:ident)*) => ($true)"); | ||
22 | check("($($false:ident)*) => ($false)"); | ||
23 | } | ||
24 | |||
25 | #[test] | ||
26 | fn test_invalid_arms() { | ||
27 | fn check(macro_body: &str, err: &str) { | ||
28 | let m = parse_macro_arm(macro_body); | ||
29 | assert_eq!(m, Err(ParseError::Expected(String::from(err)))); | ||
30 | } | ||
31 | |||
32 | check("invalid", "expected subtree"); | ||
33 | |||
34 | check("$i:ident => ()", "expected subtree"); | ||
35 | check("($i:ident) ()", "expected `=`"); | ||
36 | check("($($i:ident)_) => ()", "invalid repeat"); | ||
37 | |||
38 | check("($i) => ($i)", "invalid macro definition"); | ||
39 | check("($i:) => ($i)", "invalid macro definition"); | ||
40 | } | ||
41 | |||
42 | fn parse_macro_arm(arm_definition: &str) -> Result<crate::MacroRules, ParseError> { | ||
43 | let macro_definition = format!(" macro_rules! m {{ {} }} ", arm_definition); | ||
44 | let source_file = ast::SourceFile::parse(¯o_definition).ok().unwrap(); | ||
45 | let macro_definition = | ||
46 | source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap(); | ||
47 | |||
48 | let (definition_tt, _) = | ||
49 | ast_to_token_tree(¯o_definition.token_tree().unwrap()).unwrap(); | ||
50 | crate::MacroRules::parse(&definition_tt) | ||
51 | } | ||
52 | } | ||
53 | |||
6 | // Good first issue (although a slightly challenging one): | 54 | // Good first issue (although a slightly challenging one): |
7 | // | 55 | // |
8 | // * Pick a random test from here | 56 | // * Pick a random test from here |