Unnamed repository; edit this file 'description' to name the repository.
Merge #10497
10497: internal: move tests r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
bors[bot] 2021-10-09
parent b2b703b · parent de136a5 · commit 6005ea5
-rw-r--r--crates/hir_def/src/macro_expansion_tests/mbe.rs232
-rw-r--r--crates/mbe/src/tests/expand.rs217
2 files changed, 232 insertions, 217 deletions
diff --git a/crates/hir_def/src/macro_expansion_tests/mbe.rs b/crates/hir_def/src/macro_expansion_tests/mbe.rs
index 384c70028d..7279ff7e33 100644
--- a/crates/hir_def/src/macro_expansion_tests/mbe.rs
+++ b/crates/hir_def/src/macro_expansion_tests/mbe.rs
@@ -383,3 +383,235 @@ fn baz() -> bool {
"#]],
);
}
+
+#[test]
+fn test_match_group_zero_match() {
+ check(
+ r#"
+macro_rules! m { ( $($i:ident)* ) => (); }
+m!();
+"#,
+ expect![[r#"
+macro_rules! m { ( $($i:ident)* ) => (); }
+
+"#]],
+ );
+}
+
+#[test]
+fn test_match_group_in_group() {
+ check(
+ r#"
+macro_rules! m {
+ [ $( ( $($i:ident)* ) )* ] => [ x![$( ( $($i)* ) )*]; ]
+}
+m! ( (a b) );
+"#,
+ expect![[r#"
+macro_rules! m {
+ [ $( ( $($i:ident)* ) )* ] => [ x![$( ( $($i)* ) )*]; ]
+}
+x![(a b)];
+"#]],
+ )
+}
+
+#[test]
+fn test_expand_to_item_list() {
+ check(
+ r#"
+macro_rules! structs {
+ ($($i:ident),*) => { $(struct $i { field: u32 } )* }
+}
+
+// +tree
+structs!(Foo, Bar);
+ "#,
+ expect![[r#"
+macro_rules! structs {
+ ($($i:ident),*) => { $(struct $i { field: u32 } )* }
+}
+
+struct Foo {
+ field:u32
+}
+struct Bar {
+ field:u32
+}
+// [email protected] "struct"
+// [email protected] "field"
+// [email protected] "struct"
+// [email protected] "field"
+
+ "#]],
+ );
+}
+
+#[test]
+fn test_two_idents() {
+ check(
+ r#"
+macro_rules! m {
+ ($i:ident, $j:ident) => { fn foo() { let a = $i; let b = $j; } }
+}
+m! { foo, bar }
+"#,
+ expect![[r#"
+macro_rules! m {
+ ($i:ident, $j:ident) => { fn foo() { let a = $i; let b = $j; } }
+}
+fn foo() {
+ let a = foo;
+ let b = bar;
+}
+"#]],
+ );
+}
+
+#[test]
+fn test_tt_to_stmts() {
+ check(
+ r#"
+macro_rules! m {
+ () => {
+ let a = 0;
+ a = 10 + 1;
+ a
+ }
+}
+
+fn f() -> i32 {
+ // +tree
+ m!{}
+}
+"#,
+ expect![[r#"
+macro_rules! m {
+ () => {
+ let a = 0;
+ a = 10 + 1;
+ a
+ }
+}
+
+fn f() -> i32 {
+ let a = 0;
+ a = 10+1;
+ a
+
+}
+"#]],
+ );
+}
+
+#[test]
+fn test_match_literal() {
+ check(
+ r#"
+macro_rules! m {
+ ('(') => { fn l_paren() {} }
+}
+m!['('];
+"#,
+ expect![[r#"
+macro_rules! m {
+ ('(') => { fn l_paren() {} }
+}
+fn l_paren() {}
+"#]],
+ );
+}
+
+#[test]
+fn test_parse_macro_def_simple() {
+ cov_mark::check!(parse_macro_def_simple);
+ check(
+ r#"
+macro m($id:ident) { fn $id() {} }
+m!(bar);
+"#,
+ expect![[r#"
+macro m($id:ident) { fn $id() {} }
+fn bar() {}
+"#]],
+ );
+}
+
+#[test]
+fn test_parse_macro_def_rules() {
+ cov_mark::check!(parse_macro_def_rules);
+
+ check(
+ r#"
+macro m {
+ ($id:ident) => { fn $id() {} }
+}
+m!(bar);
+"#,
+ expect![[r#"
+macro m {
+ ($id:ident) => { fn $id() {} }
+}
+fn bar() {}
+"#]],
+ );
+}
diff --git a/crates/mbe/src/tests/expand.rs b/crates/mbe/src/tests/expand.rs
index c08788cda1..393c2041d9 100644
--- a/crates/mbe/src/tests/expand.rs
+++ b/crates/mbe/src/tests/expand.rs
@@ -71,92 +71,12 @@ macro_rules! foobar {
assert_eq!(get_text(tt::TokenId(13), T!['{']), "{");
}
-#[test]
-fn test_match_group_zero_match() {
- parse_macro(
- r#"
- macro_rules! foo {
- ( $($i:ident)* ) => ();
- }"#,
- )
- .assert_expand_items("foo! ();", "");
-}
-
-#[test]
-fn test_match_group_in_group() {
- parse_macro(
- r#"
- macro_rules! foo {
- { $( ( $($i:ident)* ) )* } => ( $( ( $($i)* ) )* );
- }"#,
- )
- .assert_expand_items("foo! ( (a b) );", "(a b)");
-}
-
-#[test]
-fn test_expand_to_item_list() {
- let tree = parse_macro(
- "
- macro_rules! structs {
- ($($i:ident),*) => {
- $(struct $i { field: u32 } )*
- }
- }
- ",
- )
- .expand_items("structs!(Foo, Bar);");
- assert_eq!(
- format!("{:#?}", tree).trim(),
- r#"
- .trim()
- );
-}
-
fn to_subtree(tt: &tt::TokenTree) -> &tt::Subtree {
if let tt::TokenTree::Subtree(subtree) = tt {
return subtree;
}
unreachable!("It is not a subtree");
}
-fn to_literal(tt: &tt::TokenTree) -> &tt::Literal {
- if let tt::TokenTree::Leaf(tt::Leaf::Literal(lit)) = tt {
- return lit;
- }
- unreachable!("It is not a literal");
-}
fn to_punct(tt: &tt::TokenTree) -> &tt::Punct {
if let tt::TokenTree::Leaf(tt::Leaf::Punct(lit)) = tt {
@@ -166,35 +86,6 @@ fn to_punct(tt: &tt::TokenTree) -> &tt::Punct {
}
#[test]
-fn test_expand_literals_to_token_tree() {
- let expansion = parse_macro(
- r#"
- macro_rules! literals {
- ($i:ident) => {
- {
- let a = 'c';
- let c = 1000;
- let f = 12E+99_f64;
- let s = "rust1";
- }
- }
- }
- "#,
- )
- .expand_tt("literals!(foo);");
- let stm_tokens = &to_subtree(&expansion.token_trees[0]).token_trees;
-
- // [let] [a] [=] ['c'] [;]
- assert_eq!(to_literal(&stm_tokens[3]).text, "'c'");
- // [let] [c] [=] [1000] [;]
- assert_eq!(to_literal(&stm_tokens[5 + 3]).text, "1000");
- // [let] [f] [=] [12E+99_f64] [;]
- assert_eq!(to_literal(&stm_tokens[10 + 3]).text, "12E+99_f64");
- // [let] [s] [=] ["rust1"] [;]
- assert_eq!(to_literal(&stm_tokens[15 + 3]).text, "\"rust1\"");
-}
-
-#[test]
fn test_attr_to_token_tree() {
let expansion = parse_to_token_tree_by_syntax(
r#"
@@ -211,114 +102,6 @@ fn test_attr_to_token_tree() {
}
#[test]
-fn test_two_idents() {
- parse_macro(
- r#"
- macro_rules! foo {
- ($ i:ident, $ j:ident) => {
- fn foo() { let a = $ i; let b = $j; }
- }
- }
-"#,
- )
- .assert_expand_items("foo! { foo, bar }", "fn foo () {let a = foo ; let b = bar ;}");
-}
-
-#[test]
-fn test_tt_to_stmts() {
- let stmts = parse_macro(
- r#"
- macro_rules! foo {
- () => {
- let a = 0;
- a = 10 + 1;
- a
- }
- }
-"#,
- )
- .expand_statements("foo!{}");
-
- assert_eq!(
- format!("{:#?}", stmts).trim(),
- );
-}
-
-#[test]
-fn test_match_literal() {
- parse_macro(
- r#"
- macro_rules! foo {
- ('(') => {
- fn foo() {}
- }
- }
-"#,
- )
- .assert_expand_items("foo! ['('];", "fn foo () {}");
-}
-
-#[test]
-fn test_parse_macro_def_simple() {
- cov_mark::check!(parse_macro_def_simple);
-
- parse_macro2(
- r#"
-macro foo($id:ident) {
- fn $id() {}
-}
-"#,
- )
- .assert_expand_items("foo!(bar);", "fn bar () {}");
-}
-
-#[test]
-fn test_parse_macro_def_rules() {
- cov_mark::check!(parse_macro_def_rules);
-
- parse_macro2(
- r#"
-macro foo {
- ($id:ident) => {
- fn $id() {}
- }
-}
-"#,
- )
- .assert_expand_items("foo!(bar);", "fn bar () {}");
-}
-
-#[test]
fn test_macro_2_0_panic_2015() {
parse_macro2(
r#"