diff options
Diffstat (limited to 'crates/ra_analysis/src')
-rw-r--r-- | crates/ra_analysis/src/call_info.rs | 249 |
1 files changed, 249 insertions, 0 deletions
diff --git a/crates/ra_analysis/src/call_info.rs b/crates/ra_analysis/src/call_info.rs index 8da19a648..2fcd03c9b 100644 --- a/crates/ra_analysis/src/call_info.rs +++ b/crates/ra_analysis/src/call_info.rs | |||
@@ -128,3 +128,252 @@ impl<'a> FnCallNode<'a> { | |||
128 | } | 128 | } |
129 | } | 129 | } |
130 | } | 130 | } |
131 | |||
132 | #[cfg(test)] | ||
133 | mod tests { | ||
134 | use super::*; | ||
135 | |||
136 | use crate::mock_analysis::single_file_with_position; | ||
137 | |||
138 | fn call_info(text: &str) -> CallInfo { | ||
139 | let (analysis, position) = single_file_with_position(text); | ||
140 | analysis.call_info(position).unwrap().unwrap() | ||
141 | } | ||
142 | |||
143 | #[test] | ||
144 | fn test_fn_signature_two_args_first() { | ||
145 | let info = call_info( | ||
146 | r#"fn foo(x: u32, y: u32) -> u32 {x + y} | ||
147 | fn bar() { foo(<|>3, ); }"#, | ||
148 | ); | ||
149 | |||
150 | assert_eq!(info.parameters, vec!("x".to_string(), "y".to_string())); | ||
151 | assert_eq!(info.active_parameter, Some(0)); | ||
152 | } | ||
153 | |||
154 | #[test] | ||
155 | fn test_fn_signature_two_args_second() { | ||
156 | let info = call_info( | ||
157 | r#"fn foo(x: u32, y: u32) -> u32 {x + y} | ||
158 | fn bar() { foo(3, <|>); }"#, | ||
159 | ); | ||
160 | |||
161 | assert_eq!(info.parameters, vec!("x".to_string(), "y".to_string())); | ||
162 | assert_eq!(info.active_parameter, Some(1)); | ||
163 | } | ||
164 | |||
165 | #[test] | ||
166 | fn test_fn_signature_for_impl() { | ||
167 | let info = call_info( | ||
168 | r#"struct F; impl F { pub fn new() { F{}} } | ||
169 | fn bar() {let _ : F = F::new(<|>);}"#, | ||
170 | ); | ||
171 | |||
172 | assert_eq!(info.parameters, Vec::<String>::new()); | ||
173 | assert_eq!(info.active_parameter, None); | ||
174 | } | ||
175 | |||
176 | #[test] | ||
177 | fn test_fn_signature_for_method_self() { | ||
178 | let info = call_info( | ||
179 | r#"struct F; | ||
180 | impl F { | ||
181 | pub fn new() -> F{ | ||
182 | F{} | ||
183 | } | ||
184 | |||
185 | pub fn do_it(&self) {} | ||
186 | } | ||
187 | |||
188 | fn bar() { | ||
189 | let f : F = F::new(); | ||
190 | f.do_it(<|>); | ||
191 | }"#, | ||
192 | ); | ||
193 | |||
194 | assert_eq!(info.parameters, vec!["&self".to_string()]); | ||
195 | assert_eq!(info.active_parameter, None); | ||
196 | } | ||
197 | |||
198 | #[test] | ||
199 | fn test_fn_signature_for_method_with_arg() { | ||
200 | let info = call_info( | ||
201 | r#"struct F; | ||
202 | impl F { | ||
203 | pub fn new() -> F{ | ||
204 | F{} | ||
205 | } | ||
206 | |||
207 | pub fn do_it(&self, x: i32) {} | ||
208 | } | ||
209 | |||
210 | fn bar() { | ||
211 | let f : F = F::new(); | ||
212 | f.do_it(<|>); | ||
213 | }"#, | ||
214 | ); | ||
215 | |||
216 | assert_eq!(info.parameters, vec!["&self".to_string(), "x".to_string()]); | ||
217 | assert_eq!(info.active_parameter, Some(1)); | ||
218 | } | ||
219 | |||
220 | #[test] | ||
221 | fn test_fn_signature_with_docs_simple() { | ||
222 | let info = call_info( | ||
223 | r#" | ||
224 | /// test | ||
225 | // non-doc-comment | ||
226 | fn foo(j: u32) -> u32 { | ||
227 | j | ||
228 | } | ||
229 | |||
230 | fn bar() { | ||
231 | let _ = foo(<|>); | ||
232 | } | ||
233 | "#, | ||
234 | ); | ||
235 | |||
236 | assert_eq!(info.parameters, vec!["j".to_string()]); | ||
237 | assert_eq!(info.active_parameter, Some(0)); | ||
238 | assert_eq!(info.label, "fn foo(j: u32) -> u32".to_string()); | ||
239 | assert_eq!(info.doc, Some("test".into())); | ||
240 | } | ||
241 | |||
242 | #[test] | ||
243 | fn test_fn_signature_with_docs() { | ||
244 | let info = call_info( | ||
245 | r#" | ||
246 | /// Adds one to the number given. | ||
247 | /// | ||
248 | /// # Examples | ||
249 | /// | ||
250 | /// ``` | ||
251 | /// let five = 5; | ||
252 | /// | ||
253 | /// assert_eq!(6, my_crate::add_one(5)); | ||
254 | /// ``` | ||
255 | pub fn add_one(x: i32) -> i32 { | ||
256 | x + 1 | ||
257 | } | ||
258 | |||
259 | pub fn do() { | ||
260 | add_one(<|> | ||
261 | }"#, | ||
262 | ); | ||
263 | |||
264 | assert_eq!(info.parameters, vec!["x".to_string()]); | ||
265 | assert_eq!(info.active_parameter, Some(0)); | ||
266 | assert_eq!(info.label, "pub fn add_one(x: i32) -> i32".to_string()); | ||
267 | assert_eq!( | ||
268 | info.doc, | ||
269 | Some( | ||
270 | r#"Adds one to the number given. | ||
271 | |||
272 | # Examples | ||
273 | |||
274 | ```rust | ||
275 | let five = 5; | ||
276 | |||
277 | assert_eq!(6, my_crate::add_one(5)); | ||
278 | ```"# | ||
279 | .into() | ||
280 | ) | ||
281 | ); | ||
282 | } | ||
283 | |||
284 | #[test] | ||
285 | fn test_fn_signature_with_docs_impl() { | ||
286 | let info = call_info( | ||
287 | r#" | ||
288 | struct addr; | ||
289 | impl addr { | ||
290 | /// Adds one to the number given. | ||
291 | /// | ||
292 | /// # Examples | ||
293 | /// | ||
294 | /// ``` | ||
295 | /// let five = 5; | ||
296 | /// | ||
297 | /// assert_eq!(6, my_crate::add_one(5)); | ||
298 | /// ``` | ||
299 | pub fn add_one(x: i32) -> i32 { | ||
300 | x + 1 | ||
301 | } | ||
302 | } | ||
303 | |||
304 | pub fn do_it() { | ||
305 | addr {}; | ||
306 | addr::add_one(<|>); | ||
307 | }"#, | ||
308 | ); | ||
309 | |||
310 | assert_eq!(info.parameters, vec!["x".to_string()]); | ||
311 | assert_eq!(info.active_parameter, Some(0)); | ||
312 | assert_eq!(info.label, "pub fn add_one(x: i32) -> i32".to_string()); | ||
313 | assert_eq!( | ||
314 | info.doc, | ||
315 | Some( | ||
316 | r#"Adds one to the number given. | ||
317 | |||
318 | # Examples | ||
319 | |||
320 | ```rust | ||
321 | let five = 5; | ||
322 | |||
323 | assert_eq!(6, my_crate::add_one(5)); | ||
324 | ```"# | ||
325 | .into() | ||
326 | ) | ||
327 | ); | ||
328 | } | ||
329 | |||
330 | #[test] | ||
331 | fn test_fn_signature_with_docs_from_actix() { | ||
332 | let info = call_info( | ||
333 | r#" | ||
334 | pub trait WriteHandler<E> | ||
335 | where | ||
336 | Self: Actor, | ||
337 | Self::Context: ActorContext, | ||
338 | { | ||
339 | /// Method is called when writer emits error. | ||
340 | /// | ||
341 | /// If this method returns `ErrorAction::Continue` writer processing | ||
342 | /// continues otherwise stream processing stops. | ||
343 | fn error(&mut self, err: E, ctx: &mut Self::Context) -> Running { | ||
344 | Running::Stop | ||
345 | } | ||
346 | |||
347 | /// Method is called when writer finishes. | ||
348 | /// | ||
349 | /// By default this method stops actor's `Context`. | ||
350 | fn finished(&mut self, ctx: &mut Self::Context) { | ||
351 | ctx.stop() | ||
352 | } | ||
353 | } | ||
354 | |||
355 | pub fn foo() { | ||
356 | WriteHandler r; | ||
357 | r.finished(<|>); | ||
358 | } | ||
359 | |||
360 | "#, | ||
361 | ); | ||
362 | |||
363 | assert_eq!( | ||
364 | info.parameters, | ||
365 | vec!["&mut self".to_string(), "ctx".to_string()] | ||
366 | ); | ||
367 | assert_eq!(info.active_parameter, Some(1)); | ||
368 | assert_eq!( | ||
369 | info.doc, | ||
370 | Some( | ||
371 | r#"Method is called when writer finishes. | ||
372 | |||
373 | By default this method stops actor's `Context`."# | ||
374 | .into() | ||
375 | ) | ||
376 | ); | ||
377 | } | ||
378 | |||
379 | } | ||