]> git.lizzy.rs Git - rust.git/commitdiff
Merge #2594
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>
Thu, 19 Dec 2019 15:35:42 +0000 (15:35 +0000)
committerGitHub <noreply@github.com>
Thu, 19 Dec 2019 15:35:42 +0000 (15:35 +0000)
2594: Omit default parameter types r=matklad a=SomeoneToIgnore

Part of https://github.com/rust-analyzer/rust-analyzer/issues/1946

Co-authored-by: Kirill Bulatov <mail4score@gmail.com>
crates/ra_mbe/src/syntax_bridge.rs
crates/ra_mbe/src/tests.rs
crates/ra_parser/src/grammar/expressions/atom.rs
crates/ra_parser/src/lib.rs

index 2c60430d155cd51cfe072551124d276bfb563043..ea2cac069e6432c6b985a714b5fbf4eb254c44c5 100644 (file)
@@ -476,7 +476,7 @@ fn error(&mut self, error: ParseError) {
 #[cfg(test)]
 mod tests {
     use super::*;
-    use crate::tests::{create_rules, expand};
+    use crate::tests::parse_macro;
     use ra_parser::TokenSource;
     use ra_syntax::{
         algo::{insert_children, InsertPosition},
@@ -485,7 +485,7 @@ mod tests {
 
     #[test]
     fn convert_tt_token_source() {
-        let rules = create_rules(
+        let expansion = parse_macro(
             r#"
             macro_rules! literals {
                 ($i:ident) => {
@@ -498,8 +498,8 @@ macro_rules! literals {
                 }
             }
             "#,
-        );
-        let expansion = expand(&rules, "literals!(foo);");
+        )
+        .expand_tt("literals!(foo);");
         let tts = &[expansion.into()];
         let buffer = tt::buffer::TokenBuffer::new(tts);
         let mut tt_src = SubtreeTokenSource::new(&buffer);
@@ -527,7 +527,7 @@ macro_rules! literals {
 
     #[test]
     fn stmts_token_trees_to_expr_is_err() {
-        let rules = create_rules(
+        let expansion = parse_macro(
             r#"
             macro_rules! stmts {
                 () => {
@@ -538,8 +538,8 @@ macro_rules! stmts {
                 }
             }
             "#,
-        );
-        let expansion = expand(&rules, "stmts!();");
+        )
+        .expand_tt("stmts!();");
         assert!(token_tree_to_syntax_node(&expansion, FragmentKind::Expr).is_err());
     }
 
index ff225f0db21d8ba650bc25cb38b187927bcca1b8..e640d115b4513eefc8211432f1f21be4d9d2b1b8 100644 (file)
@@ -1,5 +1,7 @@
+use std::fmt::Write;
+
 use ra_parser::FragmentKind;
-use ra_syntax::{ast, AstNode, NodeOrToken, WalkEvent};
+use ra_syntax::{ast, AstNode, NodeOrToken, SyntaxKind::IDENT, SyntaxNode, WalkEvent, T};
 use test_utils::assert_eq_text;
 
 use super::*;
@@ -61,13 +63,14 @@ fn parse_macro_arm(arm_definition: &str) -> Result<crate::MacroRules, ParseError
 
 #[test]
 fn test_token_id_shift() {
-    let macro_definition = r#"
+    let expansion = parse_macro(
+        r#"
 macro_rules! foobar {
     ($e:ident) => { foo bar $e }
 }
-"#;
-    let rules = create_rules(macro_definition);
-    let expansion = expand(&rules, "foobar!(baz);");
+"#,
+    )
+    .expand_tt("foobar!(baz);");
 
     fn get_id(t: &tt::TokenTree) -> Option<u32> {
         if let tt::TokenTree::Leaf(tt::Leaf::Ident(ident)) = t {
@@ -90,22 +93,23 @@ fn get_id(t: &tt::TokenTree) -> Option<u32> {
 
 #[test]
 fn test_token_map() {
-    use ra_parser::SyntaxKind::*;
-    use ra_syntax::T;
-
-    let macro_definition = r#"
+    let expanded = parse_macro(
+        r#"
 macro_rules! foobar {
     ($e:ident) => { fn $e() {} }
 }
-"#;
-    let rules = create_rules(macro_definition);
-    let (expansion, (token_map, content)) = expand_and_map(&rules, "foobar!(baz);");
+"#,
+    )
+    .expand_tt("foobar!(baz);");
+
+    let (node, token_map) = token_tree_to_syntax_node(&expanded, FragmentKind::Items).unwrap();
+    let content = node.syntax_node().to_string();
 
     let get_text = |id, kind| -> String {
         content[token_map.range_by_token(id).unwrap().by_kind(kind).unwrap()].to_string()
     };
 
-    assert_eq!(expansion.token_trees.len(), 4);
+    assert_eq!(expanded.token_trees.len(), 4);
     // {($e:ident) => { fn $e() {} }}
     // 012345      67 8 9  T12  3
 
@@ -116,7 +120,7 @@ macro_rules! foobar {
 
 #[test]
 fn test_convert_tt() {
-    let macro_definition = r#"
+    parse_macro(r#"
 macro_rules! impl_froms {
     ($e:ident: $($v:ident),*) => {
         $(
@@ -128,24 +132,17 @@ fn from(it: $v) -> $e {
         )*
     }
 }
-"#;
-
-    let macro_invocation = r#"
-impl_froms!(TokenTree: Leaf, Subtree);
-"#;
-
-    let rules = create_rules(macro_definition);
-    let expansion = expand(&rules, macro_invocation);
-    assert_eq!(
-        expansion.to_string(),
-        "impl From <Leaf > for TokenTree {fn from (it : Leaf) -> TokenTree {TokenTree ::Leaf (it)}} \
-         impl From <Subtree > for TokenTree {fn from (it : Subtree) -> TokenTree {TokenTree ::Subtree (it)}}"
-    )
+"#)
+        .assert_expand_tt(
+            "impl_froms!(TokenTree: Leaf, Subtree);",
+            "impl From <Leaf > for TokenTree {fn from (it : Leaf) -> TokenTree {TokenTree ::Leaf (it)}} \
+             impl From <Subtree > for TokenTree {fn from (it : Subtree) -> TokenTree {TokenTree ::Subtree (it)}}"
+        );
 }
 
 #[test]
 fn test_expr_order() {
-    let rules = create_rules(
+    let expanded = parse_macro(
         r#"
         macro_rules! foo {
             ($ i:expr) => {
@@ -153,11 +150,10 @@ macro_rules! foo {
             }
         }
 "#,
-    );
-    let expanded = expand(&rules, "foo! { 1 + 1}");
-    let tree = token_tree_to_syntax_node(&expanded, FragmentKind::Items).unwrap().0.syntax_node();
+    )
+    .expand_items("foo! { 1 + 1}");
 
-    let dump = format!("{:#?}", tree);
+    let dump = format!("{:#?}", expanded);
     assert_eq_text!(
         dump.trim(),
         r#"MACRO_ITEMS@[0; 15)
@@ -189,7 +185,7 @@ macro_rules! foo {
 
 #[test]
 fn test_fail_match_pattern_by_first_token() {
-    let rules = create_rules(
+    parse_macro(
         r#"
         macro_rules! foo {
             ($ i:ident) => (
@@ -203,16 +199,15 @@ fn $ i() {}
             )
         }
 "#,
-    );
-
-    assert_expansion(MacroKind::Items, &rules, "foo! { foo }", "mod foo {}");
-    assert_expansion(MacroKind::Items, &rules, "foo! { = bar }", "fn bar () {}");
-    assert_expansion(MacroKind::Items, &rules, "foo! { + Baz }", "struct Baz ;");
+    )
+    .assert_expand_items("foo! { foo }", "mod foo {}")
+    .assert_expand_items("foo! { = bar }", "fn bar () {}")
+    .assert_expand_items("foo! { + Baz }", "struct Baz ;");
 }
 
 #[test]
 fn test_fail_match_pattern_by_last_token() {
-    let rules = create_rules(
+    parse_macro(
         r#"
         macro_rules! foo {
             ($ i:ident) => (
@@ -226,16 +221,15 @@ fn $ i() {}
             )
         }
 "#,
-    );
-
-    assert_expansion(MacroKind::Items, &rules, "foo! { foo }", "mod foo {}");
-    assert_expansion(MacroKind::Items, &rules, "foo! { bar = }", "fn bar () {}");
-    assert_expansion(MacroKind::Items, &rules, "foo! { Baz + }", "struct Baz ;");
+    )
+    .assert_expand_items("foo! { foo }", "mod foo {}")
+    .assert_expand_items("foo! { bar = }", "fn bar () {}")
+    .assert_expand_items("foo! { Baz + }", "struct Baz ;");
 }
 
 #[test]
 fn test_fail_match_pattern_by_word_token() {
-    let rules = create_rules(
+    parse_macro(
         r#"
         macro_rules! foo {
             ($ i:ident) => (
@@ -249,16 +243,15 @@ fn $ i() {}
             )
         }
 "#,
-    );
-
-    assert_expansion(MacroKind::Items, &rules, "foo! { foo }", "mod foo {}");
-    assert_expansion(MacroKind::Items, &rules, "foo! { spam bar }", "fn bar () {}");
-    assert_expansion(MacroKind::Items, &rules, "foo! { eggs Baz }", "struct Baz ;");
+    )
+    .assert_expand_items("foo! { foo }", "mod foo {}")
+    .assert_expand_items("foo! { spam bar }", "fn bar () {}")
+    .assert_expand_items("foo! { eggs Baz }", "struct Baz ;");
 }
 
 #[test]
 fn test_match_group_pattern_by_separator_token() {
-    let rules = create_rules(
+    parse_macro(
         r#"
         macro_rules! foo {
             ($ ($ i:ident),*) => ($ (
@@ -273,16 +266,15 @@ fn $ i() {}
             )
         }
 "#,
-    );
-
-    assert_expansion(MacroKind::Items, &rules, "foo! { foo, bar }", "mod foo {} mod bar {}");
-    assert_expansion(MacroKind::Items, &rules, "foo! { foo# bar }", "fn foo () {} fn bar () {}");
-    assert_expansion(MacroKind::Items, &rules, "foo! { Foo,# Bar }", "struct Foo ; struct Bar ;");
+    )
+    .assert_expand_items("foo! { foo, bar }", "mod foo {} mod bar {}")
+    .assert_expand_items("foo! { foo# bar }", "fn foo () {} fn bar () {}")
+    .assert_expand_items("foo! { Foo,# Bar }", "struct Foo ; struct Bar ;");
 }
 
 #[test]
 fn test_match_group_pattern_with_multiple_defs() {
-    let rules = create_rules(
+    parse_macro(
         r#"
         macro_rules! foo {
             ($ ($ i:ident),*) => ( struct Bar { $ (
@@ -290,19 +282,13 @@ fn $ i {}
             )*} );
         }
 "#,
-    );
-
-    assert_expansion(
-        MacroKind::Items,
-        &rules,
-        "foo! { foo, bar }",
-        "struct Bar {fn foo {} fn bar {}}",
-    );
+    )
+    .assert_expand_items("foo! { foo, bar }", "struct Bar {fn foo {} fn bar {}}");
 }
 
 #[test]
 fn test_match_group_pattern_with_multiple_statement() {
-    let rules = create_rules(
+    parse_macro(
         r#"
         macro_rules! foo {
             ($ ($ i:ident),*) => ( fn baz { $ (
@@ -310,14 +296,13 @@ macro_rules! foo {
             )*} );
         }
 "#,
-    );
-
-    assert_expansion(MacroKind::Items, &rules, "foo! { foo, bar }", "fn baz {foo () ; bar () ;}");
+    )
+    .assert_expand_items("foo! { foo, bar }", "fn baz {foo () ; bar () ;}");
 }
 
 #[test]
 fn test_match_group_pattern_with_multiple_statement_without_semi() {
-    let rules = create_rules(
+    parse_macro(
         r#"
         macro_rules! foo {
             ($ ($ i:ident),*) => ( fn baz { $ (
@@ -325,14 +310,13 @@ macro_rules! foo {
             );*} );
         }
 "#,
-    );
-
-    assert_expansion(MacroKind::Items, &rules, "foo! { foo, bar }", "fn baz {foo () ;bar ()}");
+    )
+    .assert_expand_items("foo! { foo, bar }", "fn baz {foo () ;bar ()}");
 }
 
 #[test]
 fn test_match_group_empty_fixed_token() {
-    let rules = create_rules(
+    parse_macro(
         r#"
         macro_rules! foo {
             ($ ($ i:ident)* #abc) => ( fn baz { $ (
@@ -340,69 +324,59 @@ macro_rules! foo {
             )*} );
         }
 "#,
-    );
-
-    assert_expansion(MacroKind::Items, &rules, "foo! {#abc}", "fn baz {}");
+    )
+    .assert_expand_items("foo! {#abc}", "fn baz {}");
 }
 
 #[test]
 fn test_match_group_in_subtree() {
-    let rules = create_rules(
+    parse_macro(
         r#"
         macro_rules! foo {
             (fn $name:ident {$($i:ident)*} ) => ( fn $name() { $ (
                 $ i ();
             )*} );
         }"#,
-    );
-
-    assert_expansion(MacroKind::Items, &rules, "foo! {fn baz {a b} }", "fn baz () {a () ; b () ;}");
+    )
+    .assert_expand_items("foo! {fn baz {a b} }", "fn baz () {a () ; b () ;}");
 }
 
 #[test]
 fn test_match_group_with_multichar_sep() {
-    let rules = create_rules(
+    parse_macro(
         r#"
         macro_rules! foo {
             (fn $name:ident {$($i:literal)*} ) => ( fn $name() -> bool { $($i)&&*} );
         }"#,
-    );
-
-    assert_expansion(
-        MacroKind::Items,
-        &rules,
-        "foo! (fn baz {true true} );",
-        "fn baz () -> bool {true &&true}",
-    );
+    )
+    .assert_expand_items("foo! (fn baz {true true} );", "fn baz () -> bool {true &&true}");
 }
 
 #[test]
 fn test_match_group_zero_match() {
-    let rules = create_rules(
+    parse_macro(
         r#"
         macro_rules! foo {
             ( $($i:ident)* ) => ();
         }"#,
-    );
-
-    assert_expansion(MacroKind::Items, &rules, "foo! ();", "");
+    )
+    .assert_expand_items("foo! ();", "");
 }
 
 #[test]
 fn test_match_group_in_group() {
-    let rules = create_rules(
+    parse_macro(
         r#"
         macro_rules! foo {
             { $( ( $($i:ident)* ) )* } => ( $( ( $($i)* ) )* );
         }"#,
-    );
-
-    assert_expansion(MacroKind::Items, &rules, "foo! ( (a b) );", "(a b)");
+    )
+    .assert_expand_items("foo! ( (a b) );", "(a b)");
 }
 
 #[test]
 fn test_expand_to_item_list() {
-    let rules = create_rules(
+    let tree = parse_macro(
         "
             macro_rules! structs {
                 ($($i:ident),*) => {
@@ -410,9 +384,8 @@ macro_rules! structs {
                 }
             }
             ",
-    );
-    let expansion = expand(&rules, "structs!(Foo, Bar);");
-    let tree = token_tree_to_syntax_node(&expansion, FragmentKind::Items).unwrap().0.syntax_node();
+    )
+    .expand_items("structs!(Foo, Bar);");
     assert_eq!(
         format!("{:#?}", tree).trim(),
         r#"
@@ -469,7 +442,7 @@ fn to_literal(tt: &tt::TokenTree) -> &tt::Literal {
         unreachable!("It is not a literal");
     }
 
-    let rules = create_rules(
+    let expansion = parse_macro(
         r#"
             macro_rules! literals {
                 ($i:ident) => {
@@ -482,8 +455,8 @@ macro_rules! literals {
                 }
             }
             "#,
-    );
-    let expansion = expand(&rules, "literals!(foo);");
+    )
+    .expand_tt("literals!(foo);");
     let stm_tokens = &to_subtree(&expansion.token_trees[0]).token_trees;
 
     // [let] [a] [=] ['c'] [;]
@@ -498,7 +471,7 @@ macro_rules! literals {
 
 #[test]
 fn test_two_idents() {
-    let rules = create_rules(
+    parse_macro(
         r#"
         macro_rules! foo {
             ($ i:ident, $ j:ident) => {
@@ -506,18 +479,13 @@ macro_rules! foo {
             }
         }
 "#,
-    );
-    assert_expansion(
-        MacroKind::Items,
-        &rules,
-        "foo! { foo, bar }",
-        "fn foo () {let a = foo ; let b = bar ;}",
-    );
+    )
+    .assert_expand_items("foo! { foo, bar }", "fn foo () {let a = foo ; let b = bar ;}");
 }
 
 #[test]
 fn test_tt_to_stmts() {
-    let rules = create_rules(
+    let stmts = parse_macro(
         r#"
         macro_rules! foo {
             () => {
@@ -527,11 +495,8 @@ macro_rules! foo {
             }
         }
 "#,
-    );
-
-    let expanded = expand(&rules, "foo!{}");
-    let stmts =
-        token_tree_to_syntax_node(&expanded, FragmentKind::Statements).unwrap().0.syntax_node();
+    )
+    .expand_statements("foo!{}");
 
     assert_eq!(
         format!("{:#?}", stmts).trim(),
@@ -571,7 +536,7 @@ macro_rules! foo {
 
 #[test]
 fn test_match_literal() {
-    let rules = create_rules(
+    parse_macro(
         r#"
     macro_rules! foo {
         ('(') => {
@@ -579,8 +544,8 @@ fn foo() {}
         }
     }
 "#,
-    );
-    assert_expansion(MacroKind::Items, &rules, "foo! ['('];", "fn foo () {}");
+    )
+    .assert_expand_items("foo! ['('];", "fn foo () {}");
 }
 
 // The following tests are port from intellij-rust directly
@@ -588,7 +553,7 @@ fn foo() {}
 
 #[test]
 fn test_path() {
-    let rules = create_rules(
+    parse_macro(
         r#"
         macro_rules! foo {
             ($ i:path) => {
@@ -596,11 +561,9 @@ macro_rules! foo {
             }
         }
 "#,
-    );
-    assert_expansion(MacroKind::Items, &rules, "foo! { foo }", "fn foo () {let a = foo ;}");
-    assert_expansion(
-        MacroKind::Items,
-        &rules,
+    )
+    .assert_expand_items("foo! { foo }", "fn foo () {let a = foo ;}")
+    .assert_expand_items(
         "foo! { bar::<u8>::baz::<u8> }",
         "fn foo () {let a = bar ::< u8 >:: baz ::< u8 > ;}",
     );
@@ -608,7 +571,7 @@ macro_rules! foo {
 
 #[test]
 fn test_two_paths() {
-    let rules = create_rules(
+    parse_macro(
         r#"
         macro_rules! foo {
             ($ i:path, $ j:path) => {
@@ -616,18 +579,13 @@ macro_rules! foo {
             }
         }
 "#,
-    );
-    assert_expansion(
-        MacroKind::Items,
-        &rules,
-        "foo! { foo, bar }",
-        "fn foo () {let a = foo ; let b = bar ;}",
-    );
+    )
+    .assert_expand_items("foo! { foo, bar }", "fn foo () {let a = foo ; let b = bar ;}");
 }
 
 #[test]
 fn test_path_with_path() {
-    let rules = create_rules(
+    parse_macro(
         r#"
         macro_rules! foo {
             ($ i:path) => {
@@ -635,13 +593,13 @@ macro_rules! foo {
             }
         }
 "#,
-    );
-    assert_expansion(MacroKind::Items, &rules, "foo! { foo }", "fn foo () {let a = foo :: bar ;}");
+    )
+    .assert_expand_items("foo! { foo }", "fn foo () {let a = foo :: bar ;}");
 }
 
 #[test]
 fn test_expr() {
-    let rules = create_rules(
+    parse_macro(
         r#"
         macro_rules! foo {
             ($ i:expr) => {
@@ -649,11 +607,8 @@ macro_rules! foo {
             }
         }
 "#,
-    );
-
-    assert_expansion(
-        MacroKind::Items,
-        &rules,
+    )
+    .assert_expand_items(
         "foo! { 2 + 2 * baz(3).quux() }",
         "fn bar () {2 + 2 * baz (3) . quux () ;}",
     );
@@ -661,7 +616,7 @@ macro_rules! foo {
 
 #[test]
 fn test_last_expr() {
-    let rules = create_rules(
+    parse_macro(
         r#"
         macro_rules! vec {
             ($($item:expr),*) => {
@@ -675,10 +630,8 @@ macro_rules! vec {
             };
         }
 "#,
-    );
-    assert_expansion(
-        MacroKind::Items,
-        &rules,
+    )
+    .assert_expand_items(
         "vec!(1,2,3);",
         "{let mut v = Vec :: new () ; v . push (1) ; v . push (2) ; v . push (3) ; v}",
     );
@@ -686,7 +639,7 @@ macro_rules! vec {
 
 #[test]
 fn test_ty() {
-    let rules = create_rules(
+    parse_macro(
         r#"
         macro_rules! foo {
             ($ i:ty) => (
@@ -694,18 +647,13 @@ fn bar() -> $ i { unimplemented!() }
             )
         }
 "#,
-    );
-    assert_expansion(
-        MacroKind::Items,
-        &rules,
-        "foo! { Baz<u8> }",
-        "fn bar () -> Baz < u8 > {unimplemented ! ()}",
-    );
+    )
+    .assert_expand_items("foo! { Baz<u8> }", "fn bar () -> Baz < u8 > {unimplemented ! ()}");
 }
 
 #[test]
 fn test_ty_with_complex_type() {
-    let rules = create_rules(
+    parse_macro(
         r#"
         macro_rules! foo {
             ($ i:ty) => (
@@ -713,20 +661,14 @@ fn bar() -> $ i { unimplemented!() }
             )
         }
 "#,
-    );
-
+    )
     // Reference lifetime struct with generic type
-    assert_expansion(
-        MacroKind::Items,
-        &rules,
+    .assert_expand_items(
         "foo! { &'a Baz<u8> }",
         "fn bar () -> & 'a Baz < u8 > {unimplemented ! ()}",
-    );
-
+    )
     // extern "Rust" func type
-    assert_expansion(
-        MacroKind::Items,
-        &rules,
+    .assert_expand_items(
         r#"foo! { extern "Rust" fn() -> Ret }"#,
         r#"fn bar () -> extern "Rust" fn () -> Ret {unimplemented ! ()}"#,
     );
@@ -734,19 +676,19 @@ fn bar() -> $ i { unimplemented!() }
 
 #[test]
 fn test_pat_() {
-    let rules = create_rules(
+    parse_macro(
         r#"
         macro_rules! foo {
             ($ i:pat) => { fn foo() { let $ i; } }
         }
 "#,
-    );
-    assert_expansion(MacroKind::Items, &rules, "foo! { (a, b) }", "fn foo () {let (a , b) ;}");
+    )
+    .assert_expand_items("foo! { (a, b) }", "fn foo () {let (a , b) ;}");
 }
 
 #[test]
 fn test_stmt() {
-    let rules = create_rules(
+    parse_macro(
         r#"
         macro_rules! foo {
             ($ i:stmt) => (
@@ -754,14 +696,14 @@ macro_rules! foo {
             )
         }
 "#,
-    );
-    assert_expansion(MacroKind::Items, &rules, "foo! { 2 }", "fn bar () {2 ;}");
-    assert_expansion(MacroKind::Items, &rules, "foo! { let a = 0 }", "fn bar () {let a = 0 ;}");
+    )
+    .assert_expand_items("foo! { 2 }", "fn bar () {2 ;}")
+    .assert_expand_items("foo! { let a = 0 }", "fn bar () {let a = 0 ;}");
 }
 
 #[test]
 fn test_single_item() {
-    let rules = create_rules(
+    parse_macro(
         r#"
         macro_rules! foo {
             ($ i:item) => (
@@ -769,13 +711,13 @@ macro_rules! foo {
             )
         }
 "#,
-    );
-    assert_expansion(MacroKind::Items, &rules, "foo! {mod c {}}", "mod c {}");
+    )
+    .assert_expand_items("foo! {mod c {}}", "mod c {}");
 }
 
 #[test]
 fn test_all_items() {
-    let rules = create_rules(
+    parse_macro(
         r#"
         macro_rules! foo {
             ($ ($ i:item)*) => ($ (
@@ -783,10 +725,8 @@ macro_rules! foo {
             )*)
         }
 "#,
-    );
-    assert_expansion(
-        MacroKind::Items,
-        &rules,
+    ).
+    assert_expand_items(
         r#"
         foo! {
             extern crate a;
@@ -810,19 +750,19 @@ fn h() {}
 
 #[test]
 fn test_block() {
-    let rules = create_rules(
+    parse_macro(
         r#"
         macro_rules! foo {
             ($ i:block) => { fn foo() $ i }
         }
 "#,
-    );
-    assert_expansion(MacroKind::Stmts, &rules, "foo! { { 1; } }", "fn foo () {1 ;}");
+    )
+    .assert_expand_statements("foo! { { 1; } }", "fn foo () {1 ;}");
 }
 
 #[test]
 fn test_meta() {
-    let rules = create_rules(
+    parse_macro(
         r#"
         macro_rules! foo {
             ($ i:meta) => (
@@ -831,10 +771,8 @@ fn bar() {}
             )
         }
 "#,
-    );
-    assert_expansion(
-        MacroKind::Items,
-        &rules,
+    )
+    .assert_expand_items(
         r#"foo! { cfg(target_os = "windows") }"#,
         r#"# [cfg (target_os = "windows")] fn bar () {}"#,
     );
@@ -842,7 +780,7 @@ fn bar() {}
 
 #[test]
 fn test_meta_doc_comments() {
-    let rules = create_rules(
+    parse_macro(
         r#"
         macro_rules! foo {
             ($(#[$ i:meta])+) => (
@@ -851,10 +789,8 @@ fn bar() {}
             )
         }
 "#,
-    );
-    assert_expansion(
-        MacroKind::Items,
-        &rules,
+    ).
+    assert_expand_items(
         r#"foo! {
             /// Single Line Doc 1
             /**
@@ -867,69 +803,68 @@ fn bar() {}
 
 #[test]
 fn test_tt_block() {
-    let rules = create_rules(
+    parse_macro(
         r#"
             macro_rules! foo {
                 ($ i:tt) => { fn foo() $ i }
             }
     "#,
-    );
-    assert_expansion(MacroKind::Items, &rules, r#"foo! { { 1; } }"#, r#"fn foo () {1 ;}"#);
+    )
+    .assert_expand_items(r#"foo! { { 1; } }"#, r#"fn foo () {1 ;}"#);
 }
 
 #[test]
 fn test_tt_group() {
-    let rules = create_rules(
+    parse_macro(
         r#"
             macro_rules! foo {
                  ($($ i:tt)*) => { $($ i)* }
             }
     "#,
-    );
-    assert_expansion(MacroKind::Items, &rules, r#"foo! { fn foo() {} }"#, r#"fn foo () {}"#);
+    )
+    .assert_expand_items(r#"foo! { fn foo() {} }"#, r#"fn foo () {}"#);
 }
 #[test]
 fn test_lifetime() {
-    let rules = create_rules(
+    parse_macro(
         r#"
         macro_rules! foo {
               ($ lt:lifetime) => { struct Ref<$ lt>{ s: &$ lt str } }
         }
 "#,
-    );
-    assert_expansion(MacroKind::Items, &rules, r#"foo!{'a}"#, r#"struct Ref <'a > {s : &'a str}"#);
+    )
+    .assert_expand_items(r#"foo!{'a}"#, r#"struct Ref <'a > {s : &'a str}"#);
 }
 
 #[test]
 fn test_literal() {
-    let rules = create_rules(
+    parse_macro(
         r#"
         macro_rules! foo {
               ($ type:ty $ lit:literal) => { const VALUE: $ type = $ lit;};
         }
 "#,
-    );
-    assert_expansion(MacroKind::Items, &rules, r#"foo!(u8 0);"#, r#"const VALUE : u8 = 0 ;"#);
+    )
+    .assert_expand_items(r#"foo!(u8 0);"#, r#"const VALUE : u8 = 0 ;"#);
 }
 
 #[test]
 fn test_vis() {
-    let rules = create_rules(
+    parse_macro(
         r#"
         macro_rules! foo {
               ($ vis:vis $ name:ident) => { $ vis fn $ name() {}};
         }
 "#,
-    );
-    assert_expansion(MacroKind::Items, &rules, r#"foo!(pub foo);"#, r#"pub fn foo () {}"#);
-
-    // test optional casse
-    assert_expansion(MacroKind::Items, &rules, r#"foo!(foo);"#, r#"fn foo () {}"#);
+    )
+    .assert_expand_items(r#"foo!(pub foo);"#, r#"pub fn foo () {}"#)
+    // test optional cases
+    .assert_expand_items(r#"foo!(foo);"#, r#"fn foo () {}"#);
 }
 
 #[test]
 fn test_inner_macro_rules() {
-    let rules = create_rules(
+    parse_macro(
         r#"
 macro_rules! foo {
     ($a:ident, $b:ident, $c:tt) => {
@@ -945,10 +880,8 @@ fn $b() -> u8 {$c}
     }
 }
 "#,
-    );
-    assert_expansion(
-        MacroKind::Items,
-        &rules,
+    ).
+    assert_expand_items(
         r#"foo!(x,y, 1);"#,
         r#"macro_rules ! bar {($ bi : ident) => {fn $ bi () -> u8 {1}}} bar ! (x) ; fn y () -> u8 {1}"#,
     );
@@ -957,7 +890,7 @@ fn $b() -> u8 {$c}
 // The following tests are based on real world situations
 #[test]
 fn test_vec() {
-    let rules = create_rules(
+    let fixture = parse_macro(
         r#"
          macro_rules! vec {
             ($($item:expr),*) => {
@@ -972,16 +905,14 @@ macro_rules! vec {
 }
 "#,
     );
-    assert_expansion(MacroKind::Items, &rules, r#"vec!();"#, r#"{let mut v = Vec :: new () ; v}"#);
-    assert_expansion(
-        MacroKind::Items,
-        &rules,
-        r#"vec![1u32,2];"#,
-        r#"{let mut v = Vec :: new () ; v . push (1u32) ; v . push (2) ; v}"#,
-    );
+    fixture
+        .assert_expand_items(r#"vec!();"#, r#"{let mut v = Vec :: new () ; v}"#)
+        .assert_expand_items(
+            r#"vec![1u32,2];"#,
+            r#"{let mut v = Vec :: new () ; v . push (1u32) ; v . push (2) ; v}"#,
+        );
 
-    let expansion = expand(&rules, r#"vec![1u32,2];"#);
-    let tree = token_tree_to_syntax_node(&expansion, FragmentKind::Expr).unwrap().0.syntax_node();
+    let tree = fixture.expand_expr(r#"vec![1u32,2];"#);
 
     assert_eq!(
         format!("{:#?}", tree).trim(),
@@ -1055,7 +986,7 @@ macro_rules! vec {
 fn test_winapi_struct() {
     // from https://github.com/retep998/winapi-rs/blob/a7ef2bca086aae76cf6c4ce4c2552988ed9798ad/src/macros.rs#L366
 
-    let rules = create_rules(
+    parse_macro(
         r#"
 macro_rules! STRUCT {
     ($(#[$attrs:meta])* struct $name:ident {
@@ -1077,17 +1008,19 @@ fn default() -> $name { unsafe { $crate::_core::mem::zeroed() } }
     );
 }
 "#,
-    );
+    ).
     // from https://github.com/retep998/winapi-rs/blob/a7ef2bca086aae76cf6c4ce4c2552988ed9798ad/src/shared/d3d9caps.rs
-    assert_expansion(MacroKind::Items, &rules, r#"STRUCT!{struct D3DVSHADERCAPS2_0 {Caps: u8,}}"#,
-        "# [repr (C)] # [derive (Copy)] pub struct D3DVSHADERCAPS2_0 {pub Caps : u8 ,} impl Clone for D3DVSHADERCAPS2_0 {# [inline] fn clone (& self) -> D3DVSHADERCAPS2_0 {* self}} # [cfg (feature = \"impl-default\")] impl Default for D3DVSHADERCAPS2_0 {# [inline] fn default () -> D3DVSHADERCAPS2_0 {unsafe {$crate :: _core :: mem :: zeroed ()}}}");
-    assert_expansion(MacroKind::Items, &rules, r#"STRUCT!{#[cfg_attr(target_arch = "x86", repr(packed))] struct D3DCONTENTPROTECTIONCAPS {Caps : u8 ,}}"#,
-        "# [repr (C)] # [derive (Copy)] # [cfg_attr (target_arch = \"x86\" , repr (packed))] pub struct D3DCONTENTPROTECTIONCAPS {pub Caps : u8 ,} impl Clone for D3DCONTENTPROTECTIONCAPS {# [inline] fn clone (& self) -> D3DCONTENTPROTECTIONCAPS {* self}} # [cfg (feature = \"impl-default\")] impl Default for D3DCONTENTPROTECTIONCAPS {# [inline] fn default () -> D3DCONTENTPROTECTIONCAPS {unsafe {$crate :: _core :: mem :: zeroed ()}}}");
+    assert_expand_items(r#"STRUCT!{struct D3DVSHADERCAPS2_0 {Caps: u8,}}"#,
+        "# [repr (C)] # [derive (Copy)] pub struct D3DVSHADERCAPS2_0 {pub Caps : u8 ,} impl Clone for D3DVSHADERCAPS2_0 {# [inline] fn clone (& self) -> D3DVSHADERCAPS2_0 {* self}} # [cfg (feature = \"impl-default\")] impl Default for D3DVSHADERCAPS2_0 {# [inline] fn default () -> D3DVSHADERCAPS2_0 {unsafe {$crate :: _core :: mem :: zeroed ()}}}"
+    )
+    .assert_expand_items(r#"STRUCT!{#[cfg_attr(target_arch = "x86", repr(packed))] struct D3DCONTENTPROTECTIONCAPS {Caps : u8 ,}}"#,
+        "# [repr (C)] # [derive (Copy)] # [cfg_attr (target_arch = \"x86\" , repr (packed))] pub struct D3DCONTENTPROTECTIONCAPS {pub Caps : u8 ,} impl Clone for D3DCONTENTPROTECTIONCAPS {# [inline] fn clone (& self) -> D3DCONTENTPROTECTIONCAPS {* self}} # [cfg (feature = \"impl-default\")] impl Default for D3DCONTENTPROTECTIONCAPS {# [inline] fn default () -> D3DCONTENTPROTECTIONCAPS {unsafe {$crate :: _core :: mem :: zeroed ()}}}"
+    );
 }
 
 #[test]
 fn test_int_base() {
-    let rules = create_rules(
+    parse_macro(
         r#"
 macro_rules! int_base {
     ($Trait:ident for $T:ident as $U:ident -> $Radix:ident) => {
@@ -1100,17 +1033,15 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
     }
 }
 "#,
-    );
-
-    assert_expansion(MacroKind::Items, &rules, r#" int_base!{Binary for isize as usize -> Binary}"#,
+    ).assert_expand_items(r#" int_base!{Binary for isize as usize -> Binary}"#,
         "# [stable (feature = \"rust1\" , since = \"1.0.0\")] impl fmt ::Binary for isize {fn fmt (& self , f : & mut fmt :: Formatter < \'_ >) -> fmt :: Result {Binary . fmt_int (* self as usize , f)}}"
-        );
+    );
 }
 
 #[test]
 fn test_generate_pattern_iterators() {
     // from https://github.com/rust-lang/rust/blob/316a391dcb7d66dc25f1f9a4ec9d368ef7615005/src/libcore/str/mod.rs
-    let rules = create_rules(
+    parse_macro(
         r#"
 macro_rules! generate_pattern_iterators {
         { double ended; with $(#[$common_stability_attribute:meta])*,
@@ -1121,11 +1052,7 @@ fn foo(){}
         }
 }
 "#,
-    );
-
-    assert_expansion(
-        MacroKind::Items,
-        &rules,
+    ).assert_expand_items(
         r#"generate_pattern_iterators ! ( double ended ; with # [ stable ( feature = "rust1" , since = "1.0.0" ) ] , Split , RSplit , & 'a str );"#,
         "fn foo () {}",
     );
@@ -1134,7 +1061,7 @@ fn foo(){}
 #[test]
 fn test_impl_fn_for_zst() {
     // from https://github.com/rust-lang/rust/blob/5d20ff4d2718c820632b38c1e49d4de648a9810b/src/libcore/internal_macros.rs
-    let rules = create_rules(
+    parse_macro(
         r#"
 macro_rules! impl_fn_for_zst  {
         {  $( $( #[$attr: meta] )*
@@ -1175,9 +1102,7 @@ extern "rust-call" fn call_once(self, ($( $arg, )*): ($( $ArgTy, )*)) -> $Return
 }
         }
 "#,
-    );
-
-    assert_expansion(MacroKind::Items, &rules, r#"
+    ).assert_expand_items(r#"
 impl_fn_for_zst !   {
      # [ derive ( Clone ) ]
      struct   CharEscapeDebugContinue   impl   Fn   =   | c :   char |   ->   char :: EscapeDebug   {
@@ -1194,13 +1119,14 @@ struct   CharEscapeDefault   impl   Fn   =   | c :   char |   ->   char :: Escap
      } ;
  }
 "#,
-        "# [derive (Clone)] struct CharEscapeDebugContinue ; impl Fn < (char ,) > for CharEscapeDebugContinue {# [inline] extern \"rust-call\" fn call (& self , (c ,) : (char ,)) -> char :: EscapeDebug {{c . escape_debug_ext (false)}}} impl FnMut < (char ,) > for CharEscapeDebugContinue {# [inline] extern \"rust-call\" fn call_mut (& mut self , (c ,) : (char ,)) -> char :: EscapeDebug {Fn :: call (&* self , (c ,))}} impl FnOnce < (char ,) > for CharEscapeDebugContinue {type Output = char :: EscapeDebug ; # [inline] extern \"rust-call\" fn call_once (self , (c ,) : (char ,)) -> char :: EscapeDebug {Fn :: call (& self , (c ,))}} # [derive (Clone)] struct CharEscapeUnicode ; impl Fn < (char ,) > for CharEscapeUnicode {# [inline] extern \"rust-call\" fn call (& self , (c ,) : (char ,)) -> char :: EscapeUnicode {{c . escape_unicode ()}}} impl FnMut < (char ,) > for CharEscapeUnicode {# [inline] extern \"rust-call\" fn call_mut (& mut self , (c ,) : (char ,)) -> char :: EscapeUnicode {Fn :: call (&* self , (c ,))}} impl FnOnce < (char ,) > for CharEscapeUnicode {type Output = char :: EscapeUnicode ; # [inline] extern \"rust-call\" fn call_once (self , (c ,) : (char ,)) -> char :: EscapeUnicode {Fn :: call (& self , (c ,))}} # [derive (Clone)] struct CharEscapeDefault ; impl Fn < (char ,) > for CharEscapeDefault {# [inline] extern \"rust-call\" fn call (& self , (c ,) : (char ,)) -> char :: EscapeDefault {{c . escape_default ()}}} impl FnMut < (char ,) > for CharEscapeDefault {# [inline] extern \"rust-call\" fn call_mut (& mut self , (c ,) : (char ,)) -> char :: EscapeDefault {Fn :: call (&* self , (c ,))}} impl FnOnce < (char ,) > for CharEscapeDefault {type Output = char :: EscapeDefault ; # [inline] extern \"rust-call\" fn call_once (self , (c ,) : (char ,)) -> char :: EscapeDefault {Fn :: call (& self , (c ,))}}");
+        "# [derive (Clone)] struct CharEscapeDebugContinue ; impl Fn < (char ,) > for CharEscapeDebugContinue {# [inline] extern \"rust-call\" fn call (& self , (c ,) : (char ,)) -> char :: EscapeDebug {{c . escape_debug_ext (false)}}} impl FnMut < (char ,) > for CharEscapeDebugContinue {# [inline] extern \"rust-call\" fn call_mut (& mut self , (c ,) : (char ,)) -> char :: EscapeDebug {Fn :: call (&* self , (c ,))}} impl FnOnce < (char ,) > for CharEscapeDebugContinue {type Output = char :: EscapeDebug ; # [inline] extern \"rust-call\" fn call_once (self , (c ,) : (char ,)) -> char :: EscapeDebug {Fn :: call (& self , (c ,))}} # [derive (Clone)] struct CharEscapeUnicode ; impl Fn < (char ,) > for CharEscapeUnicode {# [inline] extern \"rust-call\" fn call (& self , (c ,) : (char ,)) -> char :: EscapeUnicode {{c . escape_unicode ()}}} impl FnMut < (char ,) > for CharEscapeUnicode {# [inline] extern \"rust-call\" fn call_mut (& mut self , (c ,) : (char ,)) -> char :: EscapeUnicode {Fn :: call (&* self , (c ,))}} impl FnOnce < (char ,) > for CharEscapeUnicode {type Output = char :: EscapeUnicode ; # [inline] extern \"rust-call\" fn call_once (self , (c ,) : (char ,)) -> char :: EscapeUnicode {Fn :: call (& self , (c ,))}} # [derive (Clone)] struct CharEscapeDefault ; impl Fn < (char ,) > for CharEscapeDefault {# [inline] extern \"rust-call\" fn call (& self , (c ,) : (char ,)) -> char :: EscapeDefault {{c . escape_default ()}}} impl FnMut < (char ,) > for CharEscapeDefault {# [inline] extern \"rust-call\" fn call_mut (& mut self , (c ,) : (char ,)) -> char :: EscapeDefault {Fn :: call (&* self , (c ,))}} impl FnOnce < (char ,) > for CharEscapeDefault {type Output = char :: EscapeDefault ; # [inline] extern \"rust-call\" fn call_once (self , (c ,) : (char ,)) -> char :: EscapeDefault {Fn :: call (& self , (c ,))}}"
+    );
 }
 
 #[test]
 fn test_impl_nonzero_fmt() {
     // from https://github.com/rust-lang/rust/blob/316a391dcb7d66dc25f1f9a4ec9d368ef7615005/src/libcore/num/mod.rs#L12
-    let rules = create_rules(
+    parse_macro(
         r#"
         macro_rules! impl_nonzero_fmt {
             ( #[$stability: meta] ( $( $Trait: ident ),+ ) for $Ty: ident ) => {
@@ -1208,11 +1134,7 @@ fn foo () {}
             }
         }
 "#,
-    );
-
-    assert_expansion(
-        MacroKind::Items,
-        &rules,
+    ).assert_expand_items(
         r#"impl_nonzero_fmt! { # [stable(feature= "nonzero",since="1.28.0")] (Debug,Display,Binary,Octal,LowerHex,UpperHex) for NonZeroU8}"#,
         "fn foo () {}",
     );
@@ -1221,7 +1143,7 @@ fn foo () {}
 #[test]
 fn test_cfg_if_items() {
     // from https://github.com/rust-lang/rust/blob/33fe1131cadba69d317156847be9a402b89f11bb/src/libstd/macros.rs#L986
-    let rules = create_rules(
+    parse_macro(
         r#"
         macro_rules! __cfg_if_items {
             (($($not:meta,)*) ; ) => {};
@@ -1230,11 +1152,7 @@ macro_rules! __cfg_if_items {
             }
         }
 "#,
-    );
-
-    assert_expansion(
-        MacroKind::Items,
-        &rules,
+    ).assert_expand_items(
         r#"__cfg_if_items ! { ( rustdoc , ) ; ( ( ) ( # [ cfg ( any ( target_os = "redox" , unix ) ) ] # [ stable ( feature = "rust1" , since = "1.0.0" ) ] pub use sys :: ext as unix ; # [ cfg ( windows ) ] # [ stable ( feature = "rust1" , since = "1.0.0" ) ] pub use sys :: ext as windows ; # [ cfg ( any ( target_os = "linux" , target_os = "l4re" ) ) ] pub mod linux ; ) ) , }"#,
         "__cfg_if_items ! {(rustdoc ,) ;}",
     );
@@ -1243,7 +1161,7 @@ macro_rules! __cfg_if_items {
 #[test]
 fn test_cfg_if_main() {
     // from https://github.com/rust-lang/rust/blob/3d211248393686e0f73851fc7548f6605220fbe1/src/libpanic_unwind/macros.rs#L9
-    let rules = create_rules(
+    parse_macro(
         r#"
         macro_rules! cfg_if {
             ($(
@@ -1264,9 +1182,7 @@ macro_rules! cfg_if {
             };
         }
 "#,
-    );
-
-    assert_expansion(MacroKind::Items, &rules, r#"
+    ).assert_expand_items(r#"
 cfg_if !   {
      if   # [ cfg ( target_env   =   "msvc" ) ]   {
          // no extra unwinder support needed
@@ -1278,11 +1194,8 @@ macro_rules! cfg_if {
      }
  }
 "#,
-        "__cfg_if_items ! {() ; ((target_env = \"msvc\") ()) , ((all (target_arch = \"wasm32\" , not (target_os = \"emscripten\"))) ()) , (() (mod libunwind ; pub use libunwind :: * ;)) ,}");
-
-    assert_expansion(
-        MacroKind::Items,
-        &rules,
+        "__cfg_if_items ! {() ; ((target_env = \"msvc\") ()) , ((all (target_arch = \"wasm32\" , not (target_os = \"emscripten\"))) ()) , (() (mod libunwind ; pub use libunwind :: * ;)) ,}"
+    ).assert_expand_items(
         r#"
 cfg_if ! { @ __apply cfg ( all ( not ( any ( not ( any ( target_os = "solaris" , target_os = "illumos" ) ) ) ) ) ) , }
 "#,
@@ -1293,7 +1206,7 @@ macro_rules! cfg_if {
 #[test]
 fn test_proptest_arbitrary() {
     // from https://github.com/AltSysrq/proptest/blob/d1c4b049337d2f75dd6f49a095115f7c532e5129/proptest/src/arbitrary/macros.rs#L16
-    let rules = create_rules(
+    parse_macro(
         r#"
 macro_rules! arbitrary {
     ([$($bounds : tt)*] $typ: ty, $strat: ty, $params: ty;
@@ -1308,22 +1221,21 @@ fn arbitrary_with($args: Self::Parameters) -> Self::Strategy {
     };
 
 }"#,
-    );
-
-    assert_expansion(MacroKind::Items, &rules, r#"arbitrary !   ( [ A : Arbitrary ]
+    ).assert_expand_items(r#"arbitrary !   ( [ A : Arbitrary ]
         Vec < A > ,
         VecStrategy < A :: Strategy > ,
         RangedParams1 < A :: Parameters > ;
         args =>   { let product_unpack !   [ range , a ] = args ; vec ( any_with :: < A >   ( a ) , range ) }
     ) ;"#,
-    "impl <A : Arbitrary > $crate :: arbitrary :: Arbitrary for Vec < A > {type Parameters = RangedParams1 < A :: Parameters > ; type Strategy = VecStrategy < A :: Strategy > ; fn arbitrary_with (args : Self :: Parameters) -> Self :: Strategy {{let product_unpack ! [range , a] = args ; vec (any_with :: < A > (a) , range)}}}");
+    "impl <A : Arbitrary > $crate :: arbitrary :: Arbitrary for Vec < A > {type Parameters = RangedParams1 < A :: Parameters > ; type Strategy = VecStrategy < A :: Strategy > ; fn arbitrary_with (args : Self :: Parameters) -> Self :: Strategy {{let product_unpack ! [range , a] = args ; vec (any_with :: < A > (a) , range)}}}"
+    );
 }
 
 #[test]
 fn test_old_ridl() {
     // This is from winapi 2.8, which do not have a link from github
     //
-    let rules = create_rules(
+    let expanded = parse_macro(
         r#"
 #[macro_export]
 macro_rules! RIDL {
@@ -1339,21 +1251,17 @@ impl $interface {
         }
     };
 }"#,
-    );
+    ).expand_tt(r#"
+    RIDL!{interface ID3D11Asynchronous(ID3D11AsynchronousVtbl): ID3D11DeviceChild(ID3D11DeviceChildVtbl) {
+        fn GetDataSize(&mut self) -> UINT
+    }}"#);
 
-    let expanded = expand(
-        &rules,
-        r#"
-RIDL!{interface ID3D11Asynchronous(ID3D11AsynchronousVtbl): ID3D11DeviceChild(ID3D11DeviceChildVtbl) {
-    fn GetDataSize(&mut self) -> UINT
-}}"#,
-    );
     assert_eq!(expanded.to_string(), "impl ID3D11Asynchronous {pub unsafe fn GetDataSize (& mut self) -> UINT {((* self . lpVtbl) .GetDataSize) (self)}}");
 }
 
 #[test]
 fn test_quick_error() {
-    let rules = create_rules(
+    let expanded = parse_macro(
         r#"
 macro_rules! quick_error {
 
@@ -1376,10 +1284,8 @@ macro_rules! quick_error {
 
 }
 "#,
-    );
-
-    let expanded = expand(
-        &rules,
+    )
+    .expand_tt(
         r#"
 quick_error ! (SORT [enum Wrapped # [derive (Debug)]] items [
         => One : UNIT [] {}
@@ -1393,7 +1299,7 @@ macro_rules! quick_error {
 
 #[test]
 fn test_empty_repeat_vars_in_empty_repeat_vars() {
-    let rules = create_rules(
+    parse_macro(
         r#"
 macro_rules! delegate_impl {
     ([$self_type:ident, $self_wrap:ty, $self_map:ident]
@@ -1440,120 +1346,117 @@ fn $method_name(self $(: $self_selftype)* $(,$marg: $marg_ty)*) -> $mret {
     }
 }
 "#,
-    );
-
-    assert_expansion(
-        MacroKind::Items,
-        &rules,
+    ).assert_expand_items(
         r#"delegate_impl ! {[G , & 'a mut G , deref] pub trait Data : GraphBase {@ section type type NodeWeight ;}}"#,
         "impl <> Data for & \'a mut G where G : Data {}",
     );
 }
 
-pub(crate) fn create_rules(macro_definition: &str) -> MacroRules {
-    let source_file = ast::SourceFile::parse(macro_definition).ok().unwrap();
-    let macro_definition =
-        source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap();
+#[test]
+fn expr_interpolation() {
+    let expanded = parse_macro(
+        r#"
+        macro_rules! id {
+            ($expr:expr) => {
+                map($expr)
+            }
+        }
+        "#,
+    )
+    .expand_expr("id!(x + foo);");
 
-    let (definition_tt, _) = ast_to_token_tree(&macro_definition.token_tree().unwrap()).unwrap();
-    crate::MacroRules::parse(&definition_tt).unwrap()
+    assert_eq!(expanded.to_string(), "map(x+foo)");
 }
 
-pub(crate) fn expand(rules: &MacroRules, invocation: &str) -> tt::Subtree {
-    let source_file = ast::SourceFile::parse(invocation).ok().unwrap();
-    let macro_invocation =
-        source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap();
+pub(crate) struct MacroFixture {
+    rules: MacroRules,
+}
 
-    let (invocation_tt, _) = ast_to_token_tree(&macro_invocation.token_tree().unwrap()).unwrap();
+impl MacroFixture {
+    pub(crate) fn expand_tt(&self, invocation: &str) -> tt::Subtree {
+        let source_file = ast::SourceFile::parse(invocation).ok().unwrap();
+        let macro_invocation =
+            source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap();
 
-    rules.expand(&invocation_tt).unwrap()
-}
+        let (invocation_tt, _) =
+            ast_to_token_tree(&macro_invocation.token_tree().unwrap()).unwrap();
 
-pub(crate) fn expand_and_map(
-    rules: &MacroRules,
-    invocation: &str,
-) -> (tt::Subtree, (TokenMap, String)) {
-    let source_file = ast::SourceFile::parse(invocation).ok().unwrap();
-    let macro_invocation =
-        source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap();
+        self.rules.expand(&invocation_tt).unwrap()
+    }
 
-    let (invocation_tt, _) = ast_to_token_tree(&macro_invocation.token_tree().unwrap()).unwrap();
-    let expanded = rules.expand(&invocation_tt).unwrap();
+    fn expand_items(&self, invocation: &str) -> SyntaxNode {
+        let expanded = self.expand_tt(invocation);
+        token_tree_to_syntax_node(&expanded, FragmentKind::Items).unwrap().0.syntax_node()
+    }
 
-    let (node, expanded_token_tree) =
-        token_tree_to_syntax_node(&expanded, FragmentKind::Items).unwrap();
+    fn expand_statements(&self, invocation: &str) -> SyntaxNode {
+        let expanded = self.expand_tt(invocation);
+        token_tree_to_syntax_node(&expanded, FragmentKind::Statements).unwrap().0.syntax_node()
+    }
 
-    (expanded, (expanded_token_tree, node.syntax_node().to_string()))
-}
+    fn expand_expr(&self, invocation: &str) -> SyntaxNode {
+        let expanded = self.expand_tt(invocation);
+        token_tree_to_syntax_node(&expanded, FragmentKind::Expr).unwrap().0.syntax_node()
+    }
 
-pub(crate) enum MacroKind {
-    Items,
-    Stmts,
-}
+    fn assert_expand_tt(&self, invocation: &str, expected: &str) {
+        let expansion = self.expand_tt(invocation);
+        assert_eq!(expansion.to_string(), expected);
+    }
 
-pub(crate) fn assert_expansion(
-    kind: MacroKind,
-    rules: &MacroRules,
-    invocation: &str,
-    expected: &str,
-) -> tt::Subtree {
-    let expanded = expand(rules, invocation);
-    assert_eq!(expanded.to_string(), expected);
+    fn assert_expand_items(&self, invocation: &str, expected: &str) -> &MacroFixture {
+        self.assert_expansion(FragmentKind::Items, invocation, expected);
+        self
+    }
 
-    let expected = expected.replace("$crate", "C_C__C");
+    fn assert_expand_statements(&self, invocation: &str, expected: &str) -> &MacroFixture {
+        self.assert_expansion(FragmentKind::Statements, invocation, expected);
+        self
+    }
 
-    // wrap the given text to a macro call
-    let expected = {
-        let wrapped = format!("wrap_macro!( {} )", expected);
-        let wrapped = ast::SourceFile::parse(&wrapped);
-        let wrapped = wrapped.tree().syntax().descendants().find_map(ast::TokenTree::cast).unwrap();
-        let mut wrapped = ast_to_token_tree(&wrapped).unwrap().0;
-        wrapped.delimiter = None;
-        wrapped
-    };
-    let (expanded_tree, expected_tree) = match kind {
-        MacroKind::Items => {
-            let expanded_tree =
-                token_tree_to_syntax_node(&expanded, FragmentKind::Items).unwrap().0.syntax_node();
-            let expected_tree =
-                token_tree_to_syntax_node(&expected, FragmentKind::Items).unwrap().0.syntax_node();
-
-            (
-                debug_dump_ignore_spaces(&expanded_tree).trim().to_string(),
-                debug_dump_ignore_spaces(&expected_tree).trim().to_string(),
-            )
-        }
+    fn assert_expansion(&self, kind: FragmentKind, invocation: &str, expected: &str) {
+        let expanded = self.expand_tt(invocation);
+        assert_eq!(expanded.to_string(), expected);
+
+        let expected = expected.replace("$crate", "C_C__C");
+
+        // wrap the given text to a macro call
+        let expected = {
+            let wrapped = format!("wrap_macro!( {} )", expected);
+            let wrapped = ast::SourceFile::parse(&wrapped);
+            let wrapped =
+                wrapped.tree().syntax().descendants().find_map(ast::TokenTree::cast).unwrap();
+            let mut wrapped = ast_to_token_tree(&wrapped).unwrap().0;
+            wrapped.delimiter = None;
+            wrapped
+        };
 
-        MacroKind::Stmts => {
-            let expanded_tree = token_tree_to_syntax_node(&expanded, FragmentKind::Statements)
-                .unwrap()
-                .0
-                .syntax_node();
-            let expected_tree = token_tree_to_syntax_node(&expected, FragmentKind::Statements)
-                .unwrap()
-                .0
-                .syntax_node();
-
-            (
-                debug_dump_ignore_spaces(&expanded_tree).trim().to_string(),
-                debug_dump_ignore_spaces(&expected_tree).trim().to_string(),
-            )
-        }
-    };
+        let expanded_tree = token_tree_to_syntax_node(&expanded, kind).unwrap().0.syntax_node();
+        let expanded_tree = debug_dump_ignore_spaces(&expanded_tree).trim().to_string();
 
-    let expected_tree = expected_tree.replace("C_C__C", "$crate");
-    assert_eq!(
-        expanded_tree, expected_tree,
-        "\nleft:\n{}\nright:\n{}",
-        expanded_tree, expected_tree,
-    );
+        let expected_tree = token_tree_to_syntax_node(&expected, kind).unwrap().0.syntax_node();
+        let expected_tree = debug_dump_ignore_spaces(&expected_tree).trim().to_string();
 
-    expanded
+        let expected_tree = expected_tree.replace("C_C__C", "$crate");
+        assert_eq!(
+            expanded_tree, expected_tree,
+            "\nleft:\n{}\nright:\n{}",
+            expanded_tree, expected_tree,
+        );
+    }
 }
 
-pub fn debug_dump_ignore_spaces(node: &ra_syntax::SyntaxNode) -> String {
-    use std::fmt::Write;
+pub(crate) fn parse_macro(macro_definition: &str) -> MacroFixture {
+    let source_file = ast::SourceFile::parse(macro_definition).ok().unwrap();
+    let macro_definition =
+        source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap();
+
+    let (definition_tt, _) = ast_to_token_tree(&macro_definition.token_tree().unwrap()).unwrap();
+    let rules = MacroRules::parse(&definition_tt).unwrap();
+    MacroFixture { rules }
+}
 
+fn debug_dump_ignore_spaces(node: &ra_syntax::SyntaxNode) -> String {
     let mut level = 0;
     let mut buf = String::new();
     macro_rules! indent {
index 09f0a2d98a1e0ef537da297b16003c8ac90134a7..6f5545a83a6ac0521183778927abb064131043a7 100644 (file)
@@ -43,6 +43,7 @@ pub(crate) fn literal(p: &mut Parser) -> Option<CompletedMarker> {
         T!['('],
         T!['{'],
         T!['['],
+        L_DOLLAR,
         T![|],
         T![move],
         T![box],
index 45241e56645a6fa0e1f0b522a625c37e7100067c..65134277e128af487e358ca5af9248958dc6c2f5 100644 (file)
@@ -83,6 +83,7 @@ pub fn parse(token_source: &mut dyn TokenSource, tree_sink: &mut dyn TreeSink) {
     parse_from_tokens(token_source, tree_sink, grammar::root);
 }
 
+#[derive(Clone, Copy)]
 pub enum FragmentKind {
     Path,
     Expr,