aboutsummaryrefslogtreecommitdiff
path: root/crates/completion/src/render
diff options
context:
space:
mode:
authorIgor Aleksanov <[email protected]>2020-11-01 10:36:30 +0000
committerIgor Aleksanov <[email protected]>2020-11-03 07:16:35 +0000
commit97a504805d4b0cf8b48bc5052453b2b2f3298449 (patch)
treedd5da407ca9332898107fd51199ea49f396f7590 /crates/completion/src/render
parent15b16917fcd55068d9aba3d4b5d87763ec5deb69 (diff)
Move rendering tests to the render module
Diffstat (limited to 'crates/completion/src/render')
-rw-r--r--crates/completion/src/render/enum_variant.rs74
-rw-r--r--crates/completion/src/render/function.rs210
-rw-r--r--crates/completion/src/render/macro_.rs90
3 files changed, 374 insertions, 0 deletions
diff --git a/crates/completion/src/render/enum_variant.rs b/crates/completion/src/render/enum_variant.rs
index 26cfdfeea..bc96cc303 100644
--- a/crates/completion/src/render/enum_variant.rs
+++ b/crates/completion/src/render/enum_variant.rs
@@ -93,3 +93,77 @@ impl<'a> EnumVariantRender<'a> {
93 } 93 }
94 } 94 }
95} 95}
96
97#[cfg(test)]
98mod tests {
99 use test_utils::mark;
100
101 use crate::test_utils::check_edit;
102
103 #[test]
104 fn inserts_parens_for_tuple_enums() {
105 mark::check!(inserts_parens_for_tuple_enums);
106 check_edit(
107 "Some",
108 r#"
109enum Option<T> { Some(T), None }
110use Option::*;
111fn main() -> Option<i32> {
112 Som<|>
113}
114"#,
115 r#"
116enum Option<T> { Some(T), None }
117use Option::*;
118fn main() -> Option<i32> {
119 Some($0)
120}
121"#,
122 );
123 check_edit(
124 "Some",
125 r#"
126enum Option<T> { Some(T), None }
127use Option::*;
128fn main(value: Option<i32>) {
129 match value {
130 Som<|>
131 }
132}
133"#,
134 r#"
135enum Option<T> { Some(T), None }
136use Option::*;
137fn main(value: Option<i32>) {
138 match value {
139 Some($0)
140 }
141}
142"#,
143 );
144 }
145
146 #[test]
147 fn dont_duplicate_pattern_parens() {
148 mark::check!(dont_duplicate_pattern_parens);
149 check_edit(
150 "Var",
151 r#"
152enum E { Var(i32) }
153fn main() {
154 match E::Var(92) {
155 E::<|>(92) => (),
156 }
157}
158"#,
159 r#"
160enum E { Var(i32) }
161fn main() {
162 match E::Var(92) {
163 E::Var(92) => (),
164 }
165}
166"#,
167 );
168 }
169}
diff --git a/crates/completion/src/render/function.rs b/crates/completion/src/render/function.rs
index d22081236..cf3852bf6 100644
--- a/crates/completion/src/render/function.rs
+++ b/crates/completion/src/render/function.rs
@@ -81,3 +81,213 @@ impl<'a> FunctionRender<'a> {
81 } 81 }
82 } 82 }
83} 83}
84
85#[cfg(test)]
86mod tests {
87 use test_utils::mark;
88
89 use crate::{
90 test_utils::{check_edit, check_edit_with_config},
91 CompletionConfig,
92 };
93
94 #[test]
95 fn inserts_parens_for_function_calls() {
96 mark::check!(inserts_parens_for_function_calls);
97 check_edit(
98 "no_args",
99 r#"
100fn no_args() {}
101fn main() { no_<|> }
102"#,
103 r#"
104fn no_args() {}
105fn main() { no_args()$0 }
106"#,
107 );
108
109 check_edit(
110 "with_args",
111 r#"
112fn with_args(x: i32, y: String) {}
113fn main() { with_<|> }
114"#,
115 r#"
116fn with_args(x: i32, y: String) {}
117fn main() { with_args(${1:x}, ${2:y})$0 }
118"#,
119 );
120
121 check_edit(
122 "foo",
123 r#"
124struct S;
125impl S {
126 fn foo(&self) {}
127}
128fn bar(s: &S) { s.f<|> }
129"#,
130 r#"
131struct S;
132impl S {
133 fn foo(&self) {}
134}
135fn bar(s: &S) { s.foo()$0 }
136"#,
137 );
138
139 check_edit(
140 "foo",
141 r#"
142struct S {}
143impl S {
144 fn foo(&self, x: i32) {}
145}
146fn bar(s: &S) {
147 s.f<|>
148}
149"#,
150 r#"
151struct S {}
152impl S {
153 fn foo(&self, x: i32) {}
154}
155fn bar(s: &S) {
156 s.foo(${1:x})$0
157}
158"#,
159 );
160 }
161
162 #[test]
163 fn suppress_arg_snippets() {
164 mark::check!(suppress_arg_snippets);
165 check_edit_with_config(
166 CompletionConfig { add_call_argument_snippets: false, ..CompletionConfig::default() },
167 "with_args",
168 r#"
169fn with_args(x: i32, y: String) {}
170fn main() { with_<|> }
171"#,
172 r#"
173fn with_args(x: i32, y: String) {}
174fn main() { with_args($0) }
175"#,
176 );
177 }
178
179 #[test]
180 fn strips_underscores_from_args() {
181 check_edit(
182 "foo",
183 r#"
184fn foo(_foo: i32, ___bar: bool, ho_ge_: String) {}
185fn main() { f<|> }
186"#,
187 r#"
188fn foo(_foo: i32, ___bar: bool, ho_ge_: String) {}
189fn main() { foo(${1:foo}, ${2:bar}, ${3:ho_ge_})$0 }
190"#,
191 );
192 }
193
194 #[test]
195 fn insert_ref_when_matching_local_in_scope() {
196 check_edit(
197 "ref_arg",
198 r#"
199struct Foo {}
200fn ref_arg(x: &Foo) {}
201fn main() {
202 let x = Foo {};
203 ref_ar<|>
204}
205"#,
206 r#"
207struct Foo {}
208fn ref_arg(x: &Foo) {}
209fn main() {
210 let x = Foo {};
211 ref_arg(${1:&x})$0
212}
213"#,
214 );
215 }
216
217 #[test]
218 fn insert_mut_ref_when_matching_local_in_scope() {
219 check_edit(
220 "ref_arg",
221 r#"
222struct Foo {}
223fn ref_arg(x: &mut Foo) {}
224fn main() {
225 let x = Foo {};
226 ref_ar<|>
227}
228"#,
229 r#"
230struct Foo {}
231fn ref_arg(x: &mut Foo) {}
232fn main() {
233 let x = Foo {};
234 ref_arg(${1:&mut x})$0
235}
236"#,
237 );
238 }
239
240 #[test]
241 fn insert_ref_when_matching_local_in_scope_for_method() {
242 check_edit(
243 "apply_foo",
244 r#"
245struct Foo {}
246struct Bar {}
247impl Bar {
248 fn apply_foo(&self, x: &Foo) {}
249}
250
251fn main() {
252 let x = Foo {};
253 let y = Bar {};
254 y.<|>
255}
256"#,
257 r#"
258struct Foo {}
259struct Bar {}
260impl Bar {
261 fn apply_foo(&self, x: &Foo) {}
262}
263
264fn main() {
265 let x = Foo {};
266 let y = Bar {};
267 y.apply_foo(${1:&x})$0
268}
269"#,
270 );
271 }
272
273 #[test]
274 fn trim_mut_keyword_in_func_completion() {
275 check_edit(
276 "take_mutably",
277 r#"
278fn take_mutably(mut x: &i32) {}
279
280fn main() {
281 take_m<|>
282}
283"#,
284 r#"
285fn take_mutably(mut x: &i32) {}
286
287fn main() {
288 take_mutably(${1:x})$0
289}
290"#,
291 );
292 }
293}
diff --git a/crates/completion/src/render/macro_.rs b/crates/completion/src/render/macro_.rs
index 0ad8c03b3..6df121c66 100644
--- a/crates/completion/src/render/macro_.rs
+++ b/crates/completion/src/render/macro_.rs
@@ -114,3 +114,93 @@ fn guess_macro_braces(macro_name: &str, docs: &str) -> (&'static str, &'static s
114 .unwrap(); 114 .unwrap();
115 (*bra, *ket) 115 (*bra, *ket)
116} 116}
117
118#[cfg(test)]
119mod tests {
120 use test_utils::mark;
121
122 use crate::test_utils::check_edit;
123
124 #[test]
125 fn dont_insert_macro_call_parens_unncessary() {
126 mark::check!(dont_insert_macro_call_parens_unncessary);
127 check_edit(
128 "frobnicate!",
129 r#"
130//- /main.rs crate:main deps:foo
131use foo::<|>;
132//- /foo/lib.rs crate:foo
133#[macro_export]
134macro_rules frobnicate { () => () }
135"#,
136 r#"
137use foo::frobnicate;
138"#,
139 );
140
141 check_edit(
142 "frobnicate!",
143 r#"
144macro_rules frobnicate { () => () }
145fn main() { frob<|>!(); }
146"#,
147 r#"
148macro_rules frobnicate { () => () }
149fn main() { frobnicate!(); }
150"#,
151 );
152 }
153
154 #[test]
155 fn guesses_macro_braces() {
156 check_edit(
157 "vec!",
158 r#"
159/// Creates a [`Vec`] containing the arguments.
160///
161/// ```
162/// let v = vec![1, 2, 3];
163/// assert_eq!(v[0], 1);
164/// assert_eq!(v[1], 2);
165/// assert_eq!(v[2], 3);
166/// ```
167macro_rules! vec { () => {} }
168
169fn fn main() { v<|> }
170"#,
171 r#"
172/// Creates a [`Vec`] containing the arguments.
173///
174/// ```
175/// let v = vec![1, 2, 3];
176/// assert_eq!(v[0], 1);
177/// assert_eq!(v[1], 2);
178/// assert_eq!(v[2], 3);
179/// ```
180macro_rules! vec { () => {} }
181
182fn fn main() { vec![$0] }
183"#,
184 );
185
186 check_edit(
187 "foo!",
188 r#"
189/// Foo
190///
191/// Don't call `fooo!()` `fooo!()`, or `_foo![]` `_foo![]`,
192/// call as `let _=foo! { hello world };`
193macro_rules! foo { () => {} }
194fn main() { <|> }
195"#,
196 r#"
197/// Foo
198///
199/// Don't call `fooo!()` `fooo!()`, or `_foo![]` `_foo![]`,
200/// call as `let _=foo! { hello world };`
201macro_rules! foo { () => {} }
202fn main() { foo! {$0} }
203"#,
204 )
205 }
206}