Fill out test a little more

This factors out an assert_expansion function to make things
more managable.
This commit is contained in:
Jeff Muizelaar 2019-02-02 22:36:26 -05:00
parent 31d143ba18
commit 0bb8456e7d

View File

@ -161,6 +161,20 @@ impl_froms!(TokenTree: Leaf, Subtree);
)
}
fn assert_expansion(rules: &MacroRules, invocation: &str, expansion: &str) {
let source_file = ast::SourceFile::parse(invocation);
let macro_invocation = source_file
.syntax()
.descendants()
.find_map(ast::MacroCall::cast)
.unwrap();
let invocation_tt = ast_to_token_tree(macro_invocation.token_tree().unwrap()).unwrap();
let expaned = rules.expand(&invocation_tt).unwrap();
assert_eq!(expaned.to_string(), expansion);
}
#[test]
fn test_fail_match_pattern_by_token() {
let macro_definition = r#"
@ -177,10 +191,6 @@ impl_froms!(TokenTree: Leaf, Subtree);
}
"#;
let macro_invocation = r#"
foo! { foo }
"#;
let source_file = ast::SourceFile::parse(macro_definition);
let macro_definition = source_file
.syntax()
@ -188,18 +198,12 @@ foo! { foo }
.find_map(ast::MacroCall::cast)
.unwrap();
let source_file = ast::SourceFile::parse(macro_invocation);
let macro_invocation = source_file
.syntax()
.descendants()
.find_map(ast::MacroCall::cast)
.unwrap();
let definition_tt = ast_to_token_tree(macro_definition.token_tree().unwrap()).unwrap();
let invocation_tt = ast_to_token_tree(macro_invocation.token_tree().unwrap()).unwrap();
let rules = crate::MacroRules::parse(&definition_tt).unwrap();
let expansion = rules.expand(&invocation_tt).unwrap();
assert_eq!(expansion.to_string(), "mod foo {}")
assert_expansion(&rules, "foo! { foo }", "mod foo {}");
assert_expansion(&rules, "foo! { = bar }", "fn bar () {}");
assert_expansion(&rules, "foo! { + Baz }", "struct Baz ;");
}
}