Skip to content

Commit 216e093

Browse files
committed
Macro tests
1 parent 03ca334 commit 216e093

File tree

2 files changed

+70
-119
lines changed

2 files changed

+70
-119
lines changed

crates/ra_ide/src/completion/complete_macro_in_item_position.rs

Lines changed: 16 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -15,130 +15,27 @@ pub(super) fn complete_macro_in_item_position(acc: &mut Completions, ctx: &Compl
1515

1616
#[cfg(test)]
1717
mod tests {
18-
use insta::assert_debug_snapshot;
18+
use expect::{expect, Expect};
1919

20-
use crate::completion::{test_utils::do_completion, CompletionItem, CompletionKind};
20+
use crate::completion::{test_utils::completion_list, CompletionKind};
2121

22-
fn do_reference_completion(code: &str) -> Vec<CompletionItem> {
23-
do_completion(code, CompletionKind::Reference)
22+
fn check(ra_fixture: &str, expect: Expect) {
23+
let actual = completion_list(ra_fixture, CompletionKind::Reference);
24+
expect.assert_eq(&actual)
2425
}
2526

2627
#[test]
2728
fn completes_macros_as_item() {
28-
assert_debug_snapshot!(
29-
do_reference_completion(
30-
"
31-
//- /main.rs
32-
macro_rules! foo {
33-
() => {}
34-
}
35-
36-
fn foo() {}
37-
38-
<|>
39-
"
40-
),
41-
@r###"
42-
[
43-
CompletionItem {
44-
label: "foo!(…)",
45-
source_range: 48..48,
46-
delete: 48..48,
47-
insert: "foo!($0)",
48-
kind: Macro,
49-
detail: "macro_rules! foo",
50-
},
51-
]
52-
"###
53-
);
54-
}
55-
56-
#[test]
57-
fn completes_vec_macros_with_square_brackets() {
58-
assert_debug_snapshot!(
59-
do_reference_completion(
60-
"
61-
//- /main.rs
62-
/// Creates a [`Vec`] containing the arguments.
63-
///
64-
/// - Create a [`Vec`] containing a given list of elements:
65-
///
66-
/// ```
67-
/// let v = vec![1, 2, 3];
68-
/// assert_eq!(v[0], 1);
69-
/// assert_eq!(v[1], 2);
70-
/// assert_eq!(v[2], 3);
71-
/// ```
72-
macro_rules! vec {
73-
() => {}
74-
}
75-
76-
fn foo() {}
77-
78-
<|>
79-
"
80-
),
81-
@r###"
82-
[
83-
CompletionItem {
84-
label: "vec![…]",
85-
source_range: 282..282,
86-
delete: 282..282,
87-
insert: "vec![$0]",
88-
kind: Macro,
89-
detail: "macro_rules! vec",
90-
documentation: Documentation(
91-
"Creates a [`Vec`] containing the arguments.\n\n- Create a [`Vec`] containing a given list of elements:\n\n```\nlet v = vec![1, 2, 3];\nassert_eq!(v[0], 1);\nassert_eq!(v[1], 2);\nassert_eq!(v[2], 3);\n```",
92-
),
93-
},
94-
]
95-
"###
96-
);
97-
}
98-
99-
#[test]
100-
fn completes_macros_braces_guessing() {
101-
assert_debug_snapshot!(
102-
do_reference_completion(
103-
"
104-
//- /main.rs
105-
/// Foo
106-
///
107-
/// Not call `fooo!()` `fooo!()`, or `_foo![]` `_foo![]`.
108-
/// Call as `let _=foo! { hello world };`
109-
macro_rules! foo {
110-
() => {}
111-
}
112-
113-
fn main() {
114-
<|>
115-
}
116-
"
117-
),
118-
@r###"
119-
[
120-
CompletionItem {
121-
label: "foo! {…}",
122-
source_range: 164..164,
123-
delete: 164..164,
124-
insert: "foo! {$0}",
125-
kind: Macro,
126-
detail: "macro_rules! foo",
127-
documentation: Documentation(
128-
"Foo\n\nNot call `fooo!()` `fooo!()`, or `_foo![]` `_foo![]`.\nCall as `let _=foo! { hello world };`",
129-
),
130-
},
131-
CompletionItem {
132-
label: "main()",
133-
source_range: 164..164,
134-
delete: 164..164,
135-
insert: "main()$0",
136-
kind: Function,
137-
lookup: "main",
138-
detail: "fn main()",
139-
},
140-
]
141-
"###
142-
);
29+
check(
30+
r#"
31+
macro_rules! foo { () => {} }
32+
fn foo() {}
33+
34+
<|>
35+
"#,
36+
expect![[r#"
37+
ma foo!(…) macro_rules! foo
38+
"#]],
39+
)
14340
}
14441
}

crates/ra_ide/src/completion/presentation.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ impl Completions {
174174
builder
175175
.insert_snippet(cap, format!("{}!{}$0{}", name, bra, ket))
176176
.label(format!("{}!{}…{}", name, bra, ket))
177+
.lookup_by(format!("{}!", name))
177178
}
178179
None if needs_bang => builder.insert_text(format!("{}!", name)),
179180
_ => {
@@ -1079,4 +1080,57 @@ fn go(world: &WorldSnapshot) { go(w<|>) }
10791080
"#]],
10801081
);
10811082
}
1083+
1084+
#[test]
1085+
fn guesses_macro_braces() {
1086+
check_edit(
1087+
"vec!",
1088+
r#"
1089+
/// Creates a [`Vec`] containing the arguments.
1090+
///
1091+
/// ```
1092+
/// let v = vec![1, 2, 3];
1093+
/// assert_eq!(v[0], 1);
1094+
/// assert_eq!(v[1], 2);
1095+
/// assert_eq!(v[2], 3);
1096+
/// ```
1097+
macro_rules! vec { () => {} }
1098+
1099+
fn fn main() { v<|> }
1100+
"#,
1101+
r#"
1102+
/// Creates a [`Vec`] containing the arguments.
1103+
///
1104+
/// ```
1105+
/// let v = vec![1, 2, 3];
1106+
/// assert_eq!(v[0], 1);
1107+
/// assert_eq!(v[1], 2);
1108+
/// assert_eq!(v[2], 3);
1109+
/// ```
1110+
macro_rules! vec { () => {} }
1111+
1112+
fn fn main() { vec![$0] }
1113+
"#,
1114+
);
1115+
1116+
check_edit(
1117+
"foo!",
1118+
r#"
1119+
/// Foo
1120+
///
1121+
/// Don't call `fooo!()` `fooo!()`, or `_foo![]` `_foo![]`,
1122+
/// call as `let _=foo! { hello world };`
1123+
macro_rules! foo { () => {} }
1124+
fn main() { <|> }
1125+
"#,
1126+
r#"
1127+
/// Foo
1128+
///
1129+
/// Don't call `fooo!()` `fooo!()`, or `_foo![]` `_foo![]`,
1130+
/// call as `let _=foo! { hello world };`
1131+
macro_rules! foo { () => {} }
1132+
fn main() { foo! {$0} }
1133+
"#,
1134+
)
1135+
}
10821136
}

0 commit comments

Comments
 (0)