internal: start new macro test suite

I don't like our macro tests -- they are brittle and don't inspire
confidence. I think the reason for that is that we try to unit-test
them, but that is at odds with reality, where macro expansion
fundamentally depends on name resolution.
This commit is contained in:
Aleksey Kladov 2021-10-09 13:42:32 +03:00
parent 545b068a77
commit 093f99b809
4 changed files with 90 additions and 62 deletions

View file

@ -114,64 +114,3 @@ enum Fragment {
/// like `$i * 2` where `$i = 1 + 1` work as expectd.
Ast(tt::TokenTree),
}
#[cfg(test)]
mod tests {
use syntax::{ast, AstNode};
use super::*;
use crate::syntax_node_to_token_tree;
#[test]
fn test_expand_rule() {
assert_err(
"($($i:ident);*) => ($i)",
"foo!{a}",
ExpandError::BindingError(String::from(
"expected simple binding, found nested binding `i`",
)),
);
// FIXME:
// Add an err test case for ($($i:ident)) => ($())
}
fn assert_err(macro_body: &str, invocation: &str, err: ExpandError) {
assert_eq!(
expand_first(&create_rules(&format_macro(macro_body)), invocation).err,
Some(err)
);
}
fn format_macro(macro_body: &str) -> String {
format!(
"
macro_rules! foo {{
{}
}}
",
macro_body
)
}
fn create_rules(macro_definition: &str) -> crate::MacroRules {
let source_file = ast::SourceFile::parse(macro_definition).ok().unwrap();
let macro_definition =
source_file.syntax().descendants().find_map(ast::MacroRules::cast).unwrap();
let (definition_tt, _) =
syntax_node_to_token_tree(macro_definition.token_tree().unwrap().syntax());
crate::MacroRules::parse(&definition_tt).unwrap()
}
fn expand_first(rules: &crate::MacroRules, invocation: &str) -> ExpandResult<tt::Subtree> {
let source_file = ast::SourceFile::parse(invocation).ok().unwrap();
let macro_invocation =
source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap();
let (invocation_tt, _) =
syntax_node_to_token_tree(macro_invocation.token_tree().unwrap().syntax());
expand_rules(&rules.rules, &invocation_tt)
}
}