Skip to content

Commit bebbbb6

Browse files
committed
Update expand macro tests
1 parent 14bf5bb commit bebbbb6

File tree

1 file changed

+115
-126
lines changed

1 file changed

+115
-126
lines changed

crates/ra_ide/src/expand_macro.rs

Lines changed: 115 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ use hir::Semantics;
22
use ra_ide_db::RootDatabase;
33
use ra_syntax::{
44
algo::{find_node_at_offset, SyntaxRewriter},
5-
ast, AstNode, NodeOrToken, SyntaxKind, SyntaxNode, WalkEvent, T,
5+
ast, AstNode, NodeOrToken, SyntaxKind,
6+
SyntaxKind::*,
7+
SyntaxNode, WalkEvent, T,
68
};
79

810
use crate::FilePosition;
@@ -65,8 +67,6 @@ fn expand_macro_recur(
6567
// FIXME: It would also be cool to share logic here and in the mbe tests,
6668
// which are pretty unreadable at the moment.
6769
fn insert_whitespaces(syn: SyntaxNode) -> String {
68-
use SyntaxKind::*;
69-
7070
let mut res = String::new();
7171
let mut token_iter = syn
7272
.preorder_with_tokens()
@@ -120,175 +120,164 @@ fn insert_whitespaces(syn: SyntaxNode) -> String {
120120

121121
#[cfg(test)]
122122
mod tests {
123-
use insta::assert_snapshot;
123+
use expect::{expect, Expect};
124124

125125
use crate::mock_analysis::analysis_and_position;
126126

127-
use super::*;
128-
129-
fn check_expand_macro(fixture: &str) -> ExpandedMacro {
130-
let (analysis, pos) = analysis_and_position(fixture);
131-
analysis.expand_macro(pos).unwrap().unwrap()
127+
fn check(ra_fixture: &str, expect: Expect) {
128+
let (analysis, pos) = analysis_and_position(ra_fixture);
129+
let expansion = analysis.expand_macro(pos).unwrap().unwrap();
130+
let actual = format!("{}\n{}", expansion.name, expansion.expansion);
131+
expect.assert_eq(&actual);
132132
}
133133

134134
#[test]
135135
fn macro_expand_recursive_expansion() {
136-
let res = check_expand_macro(
136+
check(
137137
r#"
138-
//- /lib.rs
139-
macro_rules! bar {
140-
() => { fn b() {} }
141-
}
142-
macro_rules! foo {
143-
() => { bar!(); }
144-
}
145-
macro_rules! baz {
146-
() => { foo!(); }
147-
}
148-
f<|>oo!();
149-
"#,
138+
macro_rules! bar {
139+
() => { fn b() {} }
140+
}
141+
macro_rules! foo {
142+
() => { bar!(); }
143+
}
144+
macro_rules! baz {
145+
() => { foo!(); }
146+
}
147+
f<|>oo!();
148+
"#,
149+
expect![[r#"
150+
foo
151+
fn b(){}
152+
"#]],
150153
);
151-
152-
assert_eq!(res.name, "foo");
153-
assert_snapshot!(res.expansion, @r###"
154-
fn b(){}
155-
"###);
156154
}
157155

158156
#[test]
159157
fn macro_expand_multiple_lines() {
160-
let res = check_expand_macro(
158+
check(
161159
r#"
162-
//- /lib.rs
163-
macro_rules! foo {
164-
() => {
165-
fn some_thing() -> u32 {
166-
let a = 0;
167-
a + 10
168-
}
169-
}
160+
macro_rules! foo {
161+
() => {
162+
fn some_thing() -> u32 {
163+
let a = 0;
164+
a + 10
170165
}
171-
f<|>oo!();
166+
}
167+
}
168+
f<|>oo!();
172169
"#,
170+
expect![[r#"
171+
foo
172+
fn some_thing() -> u32 {
173+
let a = 0;
174+
a+10
175+
}"#]],
173176
);
174-
175-
assert_eq!(res.name, "foo");
176-
assert_snapshot!(res.expansion, @r###"
177-
fn some_thing() -> u32 {
178-
let a = 0;
179-
a+10
180-
}
181-
"###);
182177
}
183178

184179
#[test]
185180
fn macro_expand_match_ast() {
186-
let res = check_expand_macro(
181+
check(
187182
r#"
188-
//- /lib.rs
189-
macro_rules! match_ast {
190-
(match $node:ident { $($tt:tt)* }) => { match_ast!(match ($node) { $($tt)* }) };
183+
macro_rules! match_ast {
184+
(match $node:ident { $($tt:tt)* }) => { match_ast!(match ($node) { $($tt)* }) };
185+
(match ($node:expr) {
186+
$( ast::$ast:ident($it:ident) => $res:block, )*
187+
_ => $catch_all:expr $(,)?
188+
}) => {{
189+
$( if let Some($it) = ast::$ast::cast($node.clone()) $res else )*
190+
{ $catch_all }
191+
}};
192+
}
191193
192-
(match ($node:expr) {
193-
$( ast::$ast:ident($it:ident) => $res:block, )*
194-
_ => $catch_all:expr $(,)?
195-
}) => {{
196-
$( if let Some($it) = ast::$ast::cast($node.clone()) $res else )*
197-
{ $catch_all }
198-
}};
194+
fn main() {
195+
mat<|>ch_ast! {
196+
match container {
197+
ast::TraitDef(it) => {},
198+
ast::ImplDef(it) => {},
199+
_ => { continue },
199200
}
200-
201-
fn main() {
202-
mat<|>ch_ast! {
203-
match container {
204-
ast::TraitDef(it) => {},
205-
ast::ImplDef(it) => {},
206-
_ => { continue },
207-
}
208-
}
209-
}
210-
"#,
211-
);
212-
213-
assert_eq!(res.name, "match_ast");
214-
assert_snapshot!(res.expansion, @r###"
215-
{
216-
if let Some(it) = ast::TraitDef::cast(container.clone()){}
217-
else if let Some(it) = ast::ImplDef::cast(container.clone()){}
218-
else {
219-
{
220-
continue
221201
}
222-
}
223202
}
224-
"###);
203+
"#,
204+
expect![[r#"
205+
match_ast
206+
{
207+
if let Some(it) = ast::TraitDef::cast(container.clone()){}
208+
else if let Some(it) = ast::ImplDef::cast(container.clone()){}
209+
else {
210+
{
211+
continue
212+
}
213+
}
214+
}"#]],
215+
);
225216
}
226217

227218
#[test]
228219
fn macro_expand_match_ast_inside_let_statement() {
229-
let res = check_expand_macro(
220+
check(
230221
r#"
231-
//- /lib.rs
232-
macro_rules! match_ast {
233-
(match $node:ident { $($tt:tt)* }) => { match_ast!(match ($node) { $($tt)* }) };
234-
(match ($node:expr) {}) => {{}};
235-
}
222+
macro_rules! match_ast {
223+
(match $node:ident { $($tt:tt)* }) => { match_ast!(match ($node) { $($tt)* }) };
224+
(match ($node:expr) {}) => {{}};
225+
}
236226
237-
fn main() {
238-
let p = f(|it| {
239-
let res = mat<|>ch_ast! { match c {}};
240-
Some(res)
241-
})?;
242-
}
243-
"#,
227+
fn main() {
228+
let p = f(|it| {
229+
let res = mat<|>ch_ast! { match c {}};
230+
Some(res)
231+
})?;
232+
}
233+
"#,
234+
expect![[r#"
235+
match_ast
236+
{}
237+
"#]],
244238
);
245-
246-
assert_eq!(res.name, "match_ast");
247-
assert_snapshot!(res.expansion, @r###"{}"###);
248239
}
249240

250241
#[test]
251242
fn macro_expand_inner_macro_fail_to_expand() {
252-
let res = check_expand_macro(
243+
check(
253244
r#"
254-
//- /lib.rs
255-
macro_rules! bar {
256-
(BAD) => {};
257-
}
258-
macro_rules! foo {
259-
() => {bar!()};
260-
}
245+
macro_rules! bar {
246+
(BAD) => {};
247+
}
248+
macro_rules! foo {
249+
() => {bar!()};
250+
}
261251
262-
fn main() {
263-
let res = fo<|>o!();
264-
}
265-
"#,
252+
fn main() {
253+
let res = fo<|>o!();
254+
}
255+
"#,
256+
expect![[r#"
257+
foo
258+
"#]],
266259
);
267-
268-
assert_eq!(res.name, "foo");
269-
assert_snapshot!(res.expansion, @r###""###);
270260
}
271261

272262
#[test]
273263
fn macro_expand_with_dollar_crate() {
274-
let res = check_expand_macro(
264+
check(
275265
r#"
276-
//- /lib.rs
277-
#[macro_export]
278-
macro_rules! bar {
279-
() => {0};
280-
}
281-
macro_rules! foo {
282-
() => {$crate::bar!()};
283-
}
266+
#[macro_export]
267+
macro_rules! bar {
268+
() => {0};
269+
}
270+
macro_rules! foo {
271+
() => {$crate::bar!()};
272+
}
284273
285-
fn main() {
286-
let res = fo<|>o!();
287-
}
288-
"#,
274+
fn main() {
275+
let res = fo<|>o!();
276+
}
277+
"#,
278+
expect![[r#"
279+
foo
280+
0 "#]],
289281
);
290-
291-
assert_eq!(res.name, "foo");
292-
assert_snapshot!(res.expansion, @r###"0"###);
293282
}
294283
}

0 commit comments

Comments
 (0)