internal: allow macro tests to inspect parse tree

This commit is contained in:
Aleksey Kladov 2021-10-09 17:58:17 +03:00
parent aac23f7832
commit c41b7bbe69
3 changed files with 53 additions and 42 deletions

View File

@ -69,6 +69,13 @@ fn check(ra_fixture: &str, mut expect: Expect) {
let indent = IndentLevel::from_node(call.syntax());
let pp = reindent(indent, pp);
format_to!(expn_text, "{}", pp);
if call.to_string().contains("// +tree") {
let tree = format!("{:#?}", parse.syntax_node())
.split_inclusive("\n")
.map(|line| format!("// {}", line))
.collect::<String>();
format_to!(expn_text, "\n{}", tree)
}
}
let range = call.syntax().text_range();
let range: Range<usize> = range.into();

View File

@ -261,3 +261,49 @@ fn baz() {
"#]],
)
}
#[test]
fn test_expr_order() {
check(
r#"
macro_rules! m {
($ i:expr) => { fn bar() { $ i * 3; } }
}
// +tree
m! { 1 + 2 }
"#,
expect![[r#"
macro_rules! m {
($ i:expr) => { fn bar() { $ i * 3; } }
}
fn bar() {
1+2*3;
}
// MACRO_ITEMS@0..15
// FN@0..15
// FN_KW@0..2 "fn"
// NAME@2..5
// IDENT@2..5 "bar"
// PARAM_LIST@5..7
// L_PAREN@5..6 "("
// R_PAREN@6..7 ")"
// BLOCK_EXPR@7..15
// STMT_LIST@7..15
// L_CURLY@7..8 "{"
// EXPR_STMT@8..14
// BIN_EXPR@8..13
// BIN_EXPR@8..11
// LITERAL@8..9
// INT_NUMBER@8..9 "1"
// PLUS@9..10 "+"
// LITERAL@10..11
// INT_NUMBER@10..11 "2"
// STAR@11..12 "*"
// LITERAL@12..13
// INT_NUMBER@12..13 "3"
// SEMICOLON@13..14 ";"
// R_CURLY@14..15 "}"
"#]],
)
}

View File

@ -166,48 +166,6 @@ SUBTREE $
);
}
#[test]
fn test_expr_order() {
let expanded = parse_macro(
r#"
macro_rules! foo {
($ i:expr) => {
fn bar() { $ i * 2; }
}
}
"#,
)
.expand_items("foo! { 1 + 1}");
let dump = format!("{:#?}", expanded);
assert_eq_text!(
r#"MACRO_ITEMS@0..15
FN@0..15
FN_KW@0..2 "fn"
NAME@2..5
IDENT@2..5 "bar"
PARAM_LIST@5..7
L_PAREN@5..6 "("
R_PAREN@6..7 ")"
BLOCK_EXPR@7..15
STMT_LIST@7..15
L_CURLY@7..8 "{"
EXPR_STMT@8..14
BIN_EXPR@8..13
BIN_EXPR@8..11
LITERAL@8..9
INT_NUMBER@8..9 "1"
PLUS@9..10 "+"
LITERAL@10..11
INT_NUMBER@10..11 "1"
STAR@11..12 "*"
LITERAL@12..13
INT_NUMBER@12..13 "2"
SEMICOLON@13..14 ";"
R_CURLY@14..15 "}""#,
dump.trim()
);
}
#[test]
fn test_match_group_with_multichar_sep() {