aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/ra_mbe/src/lib.rs32
1 files changed, 18 insertions, 14 deletions
diff --git a/crates/ra_mbe/src/lib.rs b/crates/ra_mbe/src/lib.rs
index bafa301ea..922256c03 100644
--- a/crates/ra_mbe/src/lib.rs
+++ b/crates/ra_mbe/src/lib.rs
@@ -161,6 +161,20 @@ impl_froms!(TokenTree: Leaf, Subtree);
161 ) 161 )
162 } 162 }
163 163
164 fn assert_expansion(rules: &MacroRules, invocation: &str, expansion: &str) {
165 let source_file = ast::SourceFile::parse(invocation);
166 let macro_invocation = source_file
167 .syntax()
168 .descendants()
169 .find_map(ast::MacroCall::cast)
170 .unwrap();
171
172 let invocation_tt = ast_to_token_tree(macro_invocation.token_tree().unwrap()).unwrap();
173
174 let expaned = rules.expand(&invocation_tt).unwrap();
175 assert_eq!(expaned.to_string(), expansion);
176 }
177
164 #[test] 178 #[test]
165 fn test_fail_match_pattern_by_token() { 179 fn test_fail_match_pattern_by_token() {
166 let macro_definition = r#" 180 let macro_definition = r#"
@@ -177,10 +191,6 @@ impl_froms!(TokenTree: Leaf, Subtree);
177 } 191 }
178"#; 192"#;
179 193
180 let macro_invocation = r#"
181foo! { foo }
182"#;
183
184 let source_file = ast::SourceFile::parse(macro_definition); 194 let source_file = ast::SourceFile::parse(macro_definition);
185 let macro_definition = source_file 195 let macro_definition = source_file
186 .syntax() 196 .syntax()
@@ -188,18 +198,12 @@ foo! { foo }
188 .find_map(ast::MacroCall::cast) 198 .find_map(ast::MacroCall::cast)
189 .unwrap(); 199 .unwrap();
190 200
191 let source_file = ast::SourceFile::parse(macro_invocation);
192 let macro_invocation = source_file
193 .syntax()
194 .descendants()
195 .find_map(ast::MacroCall::cast)
196 .unwrap();
197
198 let definition_tt = ast_to_token_tree(macro_definition.token_tree().unwrap()).unwrap(); 201 let definition_tt = ast_to_token_tree(macro_definition.token_tree().unwrap()).unwrap();
199 let invocation_tt = ast_to_token_tree(macro_invocation.token_tree().unwrap()).unwrap();
200 let rules = crate::MacroRules::parse(&definition_tt).unwrap(); 202 let rules = crate::MacroRules::parse(&definition_tt).unwrap();
201 let expansion = rules.expand(&invocation_tt).unwrap(); 203
202 assert_eq!(expansion.to_string(), "mod foo {}") 204 assert_expansion(&rules, "foo! { foo }", "mod foo {}");
205 assert_expansion(&rules, "foo! { = bar }", "fn bar () {}");
206 assert_expansion(&rules, "foo! { + Baz }", "struct Baz ;");
203 } 207 }
204 208
205} 209}