diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2019-07-19 12:15:55 +0100 |
---|---|---|
committer | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2019-07-19 12:15:55 +0100 |
commit | f209843e31af7f0e0212aa28ffec2efad2a70c6f (patch) | |
tree | 548227da78a3bea644f57714d075410c0bdf7469 /crates/ra_mbe/src/tests.rs | |
parent | 58d4983ba5745975446d60f2886d96f8d2adf0f2 (diff) | |
parent | d4a66166c002f0a49e41d856a49cb5685ac93202 (diff) |
Merge #1545
1545: migrate ra_syntax to the new rowan API r=matklad a=matklad
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_mbe/src/tests.rs')
-rw-r--r-- | crates/ra_mbe/src/tests.rs | 43 |
1 files changed, 17 insertions, 26 deletions
diff --git a/crates/ra_mbe/src/tests.rs b/crates/ra_mbe/src/tests.rs index 419b2c099..38a31109d 100644 --- a/crates/ra_mbe/src/tests.rs +++ b/crates/ra_mbe/src/tests.rs | |||
@@ -37,8 +37,8 @@ impl_froms!(TokenTree: Leaf, Subtree); | |||
37 | let macro_invocation = | 37 | let macro_invocation = |
38 | source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap(); | 38 | source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap(); |
39 | 39 | ||
40 | let (definition_tt, _) = ast_to_token_tree(macro_definition.token_tree().unwrap()).unwrap(); | 40 | let (definition_tt, _) = ast_to_token_tree(¯o_definition.token_tree().unwrap()).unwrap(); |
41 | let (invocation_tt, _) = ast_to_token_tree(macro_invocation.token_tree().unwrap()).unwrap(); | 41 | let (invocation_tt, _) = ast_to_token_tree(¯o_invocation.token_tree().unwrap()).unwrap(); |
42 | let rules = crate::MacroRules::parse(&definition_tt).unwrap(); | 42 | let rules = crate::MacroRules::parse(&definition_tt).unwrap(); |
43 | let expansion = rules.expand(&invocation_tt).unwrap(); | 43 | let expansion = rules.expand(&invocation_tt).unwrap(); |
44 | assert_eq!( | 44 | assert_eq!( |
@@ -53,7 +53,7 @@ pub(crate) fn create_rules(macro_definition: &str) -> MacroRules { | |||
53 | let macro_definition = | 53 | let macro_definition = |
54 | source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap(); | 54 | source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap(); |
55 | 55 | ||
56 | let (definition_tt, _) = ast_to_token_tree(macro_definition.token_tree().unwrap()).unwrap(); | 56 | let (definition_tt, _) = ast_to_token_tree(¯o_definition.token_tree().unwrap()).unwrap(); |
57 | crate::MacroRules::parse(&definition_tt).unwrap() | 57 | crate::MacroRules::parse(&definition_tt).unwrap() |
58 | } | 58 | } |
59 | 59 | ||
@@ -62,34 +62,25 @@ pub(crate) fn expand(rules: &MacroRules, invocation: &str) -> tt::Subtree { | |||
62 | let macro_invocation = | 62 | let macro_invocation = |
63 | source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap(); | 63 | source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap(); |
64 | 64 | ||
65 | let (invocation_tt, _) = ast_to_token_tree(macro_invocation.token_tree().unwrap()).unwrap(); | 65 | let (invocation_tt, _) = ast_to_token_tree(¯o_invocation.token_tree().unwrap()).unwrap(); |
66 | 66 | ||
67 | rules.expand(&invocation_tt).unwrap() | 67 | rules.expand(&invocation_tt).unwrap() |
68 | } | 68 | } |
69 | 69 | ||
70 | pub(crate) fn expand_to_items( | 70 | pub(crate) fn expand_to_items(rules: &MacroRules, invocation: &str) -> ast::MacroItems { |
71 | rules: &MacroRules, | ||
72 | invocation: &str, | ||
73 | ) -> ra_syntax::TreeArc<ast::MacroItems> { | ||
74 | let expanded = expand(rules, invocation); | 71 | let expanded = expand(rules, invocation); |
75 | token_tree_to_macro_items(&expanded).unwrap().tree().to_owned() | 72 | token_tree_to_macro_items(&expanded).unwrap().tree() |
76 | } | 73 | } |
77 | 74 | ||
78 | #[allow(unused)] | 75 | #[allow(unused)] |
79 | pub(crate) fn expand_to_stmts( | 76 | pub(crate) fn expand_to_stmts(rules: &MacroRules, invocation: &str) -> ast::MacroStmts { |
80 | rules: &MacroRules, | ||
81 | invocation: &str, | ||
82 | ) -> ra_syntax::TreeArc<ast::MacroStmts> { | ||
83 | let expanded = expand(rules, invocation); | 77 | let expanded = expand(rules, invocation); |
84 | token_tree_to_macro_stmts(&expanded).unwrap().tree().to_owned() | 78 | token_tree_to_macro_stmts(&expanded).unwrap().tree() |
85 | } | 79 | } |
86 | 80 | ||
87 | pub(crate) fn expand_to_expr( | 81 | pub(crate) fn expand_to_expr(rules: &MacroRules, invocation: &str) -> ast::Expr { |
88 | rules: &MacroRules, | ||
89 | invocation: &str, | ||
90 | ) -> ra_syntax::TreeArc<ast::Expr> { | ||
91 | let expanded = expand(rules, invocation); | 82 | let expanded = expand(rules, invocation); |
92 | token_tree_to_expr(&expanded).unwrap().tree().to_owned() | 83 | token_tree_to_expr(&expanded).unwrap().tree() |
93 | } | 84 | } |
94 | 85 | ||
95 | pub(crate) fn text_to_tokentree(text: &str) -> tt::Subtree { | 86 | pub(crate) fn text_to_tokentree(text: &str) -> tt::Subtree { |
@@ -97,7 +88,7 @@ pub(crate) fn text_to_tokentree(text: &str) -> tt::Subtree { | |||
97 | let wrapped = format!("wrap_macro!( {} )", text); | 88 | let wrapped = format!("wrap_macro!( {} )", text); |
98 | let wrapped = ast::SourceFile::parse(&wrapped); | 89 | let wrapped = ast::SourceFile::parse(&wrapped); |
99 | let wrapped = wrapped.tree().syntax().descendants().find_map(ast::TokenTree::cast).unwrap(); | 90 | let wrapped = wrapped.tree().syntax().descendants().find_map(ast::TokenTree::cast).unwrap(); |
100 | let mut wrapped = ast_to_token_tree(wrapped).unwrap().0; | 91 | let mut wrapped = ast_to_token_tree(&wrapped).unwrap().0; |
101 | wrapped.delimiter = tt::Delimiter::None; | 92 | wrapped.delimiter = tt::Delimiter::None; |
102 | 93 | ||
103 | wrapped | 94 | wrapped |
@@ -164,8 +155,8 @@ pub(crate) fn assert_expansion( | |||
164 | 155 | ||
165 | let (expanded_tree, expected_tree) = match kind { | 156 | let (expanded_tree, expected_tree) = match kind { |
166 | MacroKind::Items => { | 157 | MacroKind::Items => { |
167 | let expanded_tree = token_tree_to_macro_items(&expanded).unwrap().tree().to_owned(); | 158 | let expanded_tree = token_tree_to_macro_items(&expanded).unwrap().tree(); |
168 | let expected_tree = token_tree_to_macro_items(&expected).unwrap().tree().to_owned(); | 159 | let expected_tree = token_tree_to_macro_items(&expected).unwrap().tree(); |
169 | 160 | ||
170 | ( | 161 | ( |
171 | debug_dump_ignore_spaces(expanded_tree.syntax()).trim().to_string(), | 162 | debug_dump_ignore_spaces(expanded_tree.syntax()).trim().to_string(), |
@@ -174,8 +165,8 @@ pub(crate) fn assert_expansion( | |||
174 | } | 165 | } |
175 | 166 | ||
176 | MacroKind::Stmts => { | 167 | MacroKind::Stmts => { |
177 | let expanded_tree = token_tree_to_macro_stmts(&expanded).unwrap().tree().to_owned(); | 168 | let expanded_tree = token_tree_to_macro_stmts(&expanded).unwrap().tree(); |
178 | let expected_tree = token_tree_to_macro_stmts(&expected).unwrap().tree().to_owned(); | 169 | let expected_tree = token_tree_to_macro_stmts(&expected).unwrap().tree(); |
179 | 170 | ||
180 | ( | 171 | ( |
181 | debug_dump_ignore_spaces(expanded_tree.syntax()).trim().to_string(), | 172 | debug_dump_ignore_spaces(expanded_tree.syntax()).trim().to_string(), |
@@ -419,7 +410,7 @@ fn test_expand_to_item_list() { | |||
419 | ", | 410 | ", |
420 | ); | 411 | ); |
421 | let expansion = expand(&rules, "structs!(Foo, Bar);"); | 412 | let expansion = expand(&rules, "structs!(Foo, Bar);"); |
422 | let tree = token_tree_to_macro_items(&expansion).unwrap().tree().to_owned(); | 413 | let tree = token_tree_to_macro_items(&expansion).unwrap().tree(); |
423 | assert_eq!( | 414 | assert_eq!( |
424 | tree.syntax().debug_dump().trim(), | 415 | tree.syntax().debug_dump().trim(), |
425 | r#" | 416 | r#" |
@@ -537,7 +528,7 @@ fn test_tt_to_stmts() { | |||
537 | ); | 528 | ); |
538 | 529 | ||
539 | let expanded = expand(&rules, "foo!{}"); | 530 | let expanded = expand(&rules, "foo!{}"); |
540 | let stmts = token_tree_to_macro_stmts(&expanded).unwrap().tree().to_owned(); | 531 | let stmts = token_tree_to_macro_stmts(&expanded).unwrap().tree(); |
541 | 532 | ||
542 | assert_eq!( | 533 | assert_eq!( |
543 | stmts.syntax().debug_dump().trim(), | 534 | stmts.syntax().debug_dump().trim(), |