From c41b7bbe698a504b6c66cce744c2a8c0181c9b85 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 9 Oct 2021 17:58:17 +0300 Subject: [PATCH] internal: allow macro tests to inspect parse tree --- crates/hir_def/src/macro_expansion_tests.rs | 7 +++ .../hir_def/src/macro_expansion_tests/mbe.rs | 46 +++++++++++++++++++ crates/mbe/src/tests/expand.rs | 42 ----------------- 3 files changed, 53 insertions(+), 42 deletions(-) diff --git a/crates/hir_def/src/macro_expansion_tests.rs b/crates/hir_def/src/macro_expansion_tests.rs index ac0a2d27b3..0a9ab93157 100644 --- a/crates/hir_def/src/macro_expansion_tests.rs +++ b/crates/hir_def/src/macro_expansion_tests.rs @@ -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::(); + format_to!(expn_text, "\n{}", tree) + } } let range = call.syntax().text_range(); let range: Range = range.into(); diff --git a/crates/hir_def/src/macro_expansion_tests/mbe.rs b/crates/hir_def/src/macro_expansion_tests/mbe.rs index f1e979503e..7c9a19613b 100644 --- a/crates/hir_def/src/macro_expansion_tests/mbe.rs +++ b/crates/hir_def/src/macro_expansion_tests/mbe.rs @@ -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 "}" + +"#]], + ) +} diff --git a/crates/mbe/src/tests/expand.rs b/crates/mbe/src/tests/expand.rs index 3655cb5a63..85a51588eb 100644 --- a/crates/mbe/src/tests/expand.rs +++ b/crates/mbe/src/tests/expand.rs @@ -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() {