@@ -161,6 +161,18 @@ impl_froms!(TokenTree: Leaf, Subtree);
161
161
)
162
162
}
163
163
164
+ fn create_rules ( macro_definition : & str ) -> MacroRules {
165
+ let source_file = ast:: SourceFile :: parse ( macro_definition) ;
166
+ let macro_definition = source_file
167
+ . syntax ( )
168
+ . descendants ( )
169
+ . find_map ( ast:: MacroCall :: cast)
170
+ . unwrap ( ) ;
171
+
172
+ let definition_tt = ast_to_token_tree ( macro_definition. token_tree ( ) . unwrap ( ) ) . unwrap ( ) ;
173
+ crate :: MacroRules :: parse ( & definition_tt) . unwrap ( )
174
+ }
175
+
164
176
fn assert_expansion ( rules : & MacroRules , invocation : & str , expansion : & str ) {
165
177
let source_file = ast:: SourceFile :: parse ( invocation) ;
166
178
let macro_invocation = source_file
@@ -177,7 +189,8 @@ impl_froms!(TokenTree: Leaf, Subtree);
177
189
178
190
#[ test]
179
191
fn test_fail_match_pattern_by_first_token ( ) {
180
- let macro_definition = r#"
192
+ let rules = create_rules (
193
+ r#"
181
194
macro_rules! foo {
182
195
($ i:ident) => (
183
196
mod $ i {}
@@ -189,17 +202,8 @@ impl_froms!(TokenTree: Leaf, Subtree);
189
202
struct $ i;
190
203
)
191
204
}
192
- "# ;
193
-
194
- let source_file = ast:: SourceFile :: parse ( macro_definition) ;
195
- let macro_definition = source_file
196
- . syntax ( )
197
- . descendants ( )
198
- . find_map ( ast:: MacroCall :: cast)
199
- . unwrap ( ) ;
200
-
201
- let definition_tt = ast_to_token_tree ( macro_definition. token_tree ( ) . unwrap ( ) ) . unwrap ( ) ;
202
- let rules = crate :: MacroRules :: parse ( & definition_tt) . unwrap ( ) ;
205
+ "# ,
206
+ ) ;
203
207
204
208
assert_expansion ( & rules, "foo! { foo }" , "mod foo {}" ) ;
205
209
assert_expansion ( & rules, "foo! { = bar }" , "fn bar () {}" ) ;
@@ -208,7 +212,8 @@ impl_froms!(TokenTree: Leaf, Subtree);
208
212
209
213
#[ test]
210
214
fn test_fail_match_pattern_by_last_token ( ) {
211
- let macro_definition = r#"
215
+ let rules = create_rules (
216
+ r#"
212
217
macro_rules! foo {
213
218
($ i:ident) => (
214
219
mod $ i {}
@@ -220,17 +225,8 @@ impl_froms!(TokenTree: Leaf, Subtree);
220
225
struct $ i;
221
226
)
222
227
}
223
- "# ;
224
-
225
- let source_file = ast:: SourceFile :: parse ( macro_definition) ;
226
- let macro_definition = source_file
227
- . syntax ( )
228
- . descendants ( )
229
- . find_map ( ast:: MacroCall :: cast)
230
- . unwrap ( ) ;
231
-
232
- let definition_tt = ast_to_token_tree ( macro_definition. token_tree ( ) . unwrap ( ) ) . unwrap ( ) ;
233
- let rules = crate :: MacroRules :: parse ( & definition_tt) . unwrap ( ) ;
228
+ "# ,
229
+ ) ;
234
230
235
231
assert_expansion ( & rules, "foo! { foo }" , "mod foo {}" ) ;
236
232
assert_expansion ( & rules, "foo! { bar = }" , "fn bar () {}" ) ;
0 commit comments