diff options
Diffstat (limited to 'crates/ide_assists/src/tests.rs')
-rw-r--r-- | crates/ide_assists/src/tests.rs | 262 |
1 files changed, 253 insertions, 9 deletions
diff --git a/crates/ide_assists/src/tests.rs b/crates/ide_assists/src/tests.rs index 6f4f97361..9c2847998 100644 --- a/crates/ide_assists/src/tests.rs +++ b/crates/ide_assists/src/tests.rs | |||
@@ -12,7 +12,10 @@ use stdx::{format_to, trim_indent}; | |||
12 | use syntax::TextRange; | 12 | use syntax::TextRange; |
13 | use test_utils::{assert_eq_text, extract_offset}; | 13 | use test_utils::{assert_eq_text, extract_offset}; |
14 | 14 | ||
15 | use crate::{handlers::Handler, Assist, AssistConfig, AssistContext, AssistKind, Assists}; | 15 | use crate::{ |
16 | handlers::Handler, Assist, AssistConfig, AssistContext, AssistKind, AssistResolveStrategy, | ||
17 | Assists, SingleResolve, | ||
18 | }; | ||
16 | 19 | ||
17 | pub(crate) const TEST_CONFIG: AssistConfig = AssistConfig { | 20 | pub(crate) const TEST_CONFIG: AssistConfig = AssistConfig { |
18 | snippet_cap: SnippetCap::new(true), | 21 | snippet_cap: SnippetCap::new(true), |
@@ -65,14 +68,14 @@ fn check_doc_test(assist_id: &str, before: &str, after: &str) { | |||
65 | let before = db.file_text(file_id).to_string(); | 68 | let before = db.file_text(file_id).to_string(); |
66 | let frange = FileRange { file_id, range: selection.into() }; | 69 | let frange = FileRange { file_id, range: selection.into() }; |
67 | 70 | ||
68 | let assist = Assist::get(&db, &TEST_CONFIG, true, frange) | 71 | let assist = Assist::get(&db, &TEST_CONFIG, AssistResolveStrategy::All, frange) |
69 | .into_iter() | 72 | .into_iter() |
70 | .find(|assist| assist.id.0 == assist_id) | 73 | .find(|assist| assist.id.0 == assist_id) |
71 | .unwrap_or_else(|| { | 74 | .unwrap_or_else(|| { |
72 | panic!( | 75 | panic!( |
73 | "\n\nAssist is not applicable: {}\nAvailable assists: {}", | 76 | "\n\nAssist is not applicable: {}\nAvailable assists: {}", |
74 | assist_id, | 77 | assist_id, |
75 | Assist::get(&db, &TEST_CONFIG, false, frange) | 78 | Assist::get(&db, &TEST_CONFIG, AssistResolveStrategy::None, frange) |
76 | .into_iter() | 79 | .into_iter() |
77 | .map(|assist| assist.id.0) | 80 | .map(|assist| assist.id.0) |
78 | .collect::<Vec<_>>() | 81 | .collect::<Vec<_>>() |
@@ -108,7 +111,7 @@ fn check(handler: Handler, before: &str, expected: ExpectedResult, assist_label: | |||
108 | let sema = Semantics::new(&db); | 111 | let sema = Semantics::new(&db); |
109 | let config = TEST_CONFIG; | 112 | let config = TEST_CONFIG; |
110 | let ctx = AssistContext::new(sema, &config, frange); | 113 | let ctx = AssistContext::new(sema, &config, frange); |
111 | let mut acc = Assists::new(&ctx, true); | 114 | let mut acc = Assists::new(&ctx, AssistResolveStrategy::All); |
112 | handler(&mut acc, &ctx); | 115 | handler(&mut acc, &ctx); |
113 | let mut res = acc.finish(); | 116 | let mut res = acc.finish(); |
114 | 117 | ||
@@ -186,7 +189,7 @@ fn assist_order_field_struct() { | |||
186 | let (before_cursor_pos, before) = extract_offset(before); | 189 | let (before_cursor_pos, before) = extract_offset(before); |
187 | let (db, file_id) = with_single_file(&before); | 190 | let (db, file_id) = with_single_file(&before); |
188 | let frange = FileRange { file_id, range: TextRange::empty(before_cursor_pos) }; | 191 | let frange = FileRange { file_id, range: TextRange::empty(before_cursor_pos) }; |
189 | let assists = Assist::get(&db, &TEST_CONFIG, false, frange); | 192 | let assists = Assist::get(&db, &TEST_CONFIG, AssistResolveStrategy::None, frange); |
190 | let mut assists = assists.iter(); | 193 | let mut assists = assists.iter(); |
191 | 194 | ||
192 | assert_eq!(assists.next().expect("expected assist").label, "Change visibility to pub(crate)"); | 195 | assert_eq!(assists.next().expect("expected assist").label, "Change visibility to pub(crate)"); |
@@ -211,7 +214,7 @@ pub fn test_some_range(a: int) -> bool { | |||
211 | "#, | 214 | "#, |
212 | ); | 215 | ); |
213 | 216 | ||
214 | let assists = Assist::get(&db, &TEST_CONFIG, false, frange); | 217 | let assists = Assist::get(&db, &TEST_CONFIG, AssistResolveStrategy::None, frange); |
215 | let expected = labels(&assists); | 218 | let expected = labels(&assists); |
216 | 219 | ||
217 | expect![[r#" | 220 | expect![[r#" |
@@ -240,7 +243,7 @@ pub fn test_some_range(a: int) -> bool { | |||
240 | let mut cfg = TEST_CONFIG; | 243 | let mut cfg = TEST_CONFIG; |
241 | cfg.allowed = Some(vec![AssistKind::Refactor]); | 244 | cfg.allowed = Some(vec![AssistKind::Refactor]); |
242 | 245 | ||
243 | let assists = Assist::get(&db, &cfg, false, frange); | 246 | let assists = Assist::get(&db, &cfg, AssistResolveStrategy::None, frange); |
244 | let expected = labels(&assists); | 247 | let expected = labels(&assists); |
245 | 248 | ||
246 | expect![[r#" | 249 | expect![[r#" |
@@ -255,7 +258,7 @@ pub fn test_some_range(a: int) -> bool { | |||
255 | { | 258 | { |
256 | let mut cfg = TEST_CONFIG; | 259 | let mut cfg = TEST_CONFIG; |
257 | cfg.allowed = Some(vec![AssistKind::RefactorExtract]); | 260 | cfg.allowed = Some(vec![AssistKind::RefactorExtract]); |
258 | let assists = Assist::get(&db, &cfg, false, frange); | 261 | let assists = Assist::get(&db, &cfg, AssistResolveStrategy::None, frange); |
259 | let expected = labels(&assists); | 262 | let expected = labels(&assists); |
260 | 263 | ||
261 | expect![[r#" | 264 | expect![[r#" |
@@ -268,9 +271,250 @@ pub fn test_some_range(a: int) -> bool { | |||
268 | { | 271 | { |
269 | let mut cfg = TEST_CONFIG; | 272 | let mut cfg = TEST_CONFIG; |
270 | cfg.allowed = Some(vec![AssistKind::QuickFix]); | 273 | cfg.allowed = Some(vec![AssistKind::QuickFix]); |
271 | let assists = Assist::get(&db, &cfg, false, frange); | 274 | let assists = Assist::get(&db, &cfg, AssistResolveStrategy::None, frange); |
272 | let expected = labels(&assists); | 275 | let expected = labels(&assists); |
273 | 276 | ||
274 | expect![[r#""#]].assert_eq(&expected); | 277 | expect![[r#""#]].assert_eq(&expected); |
275 | } | 278 | } |
276 | } | 279 | } |
280 | |||
281 | #[test] | ||
282 | fn various_resolve_strategies() { | ||
283 | let (db, frange) = RootDatabase::with_range( | ||
284 | r#" | ||
285 | pub fn test_some_range(a: int) -> bool { | ||
286 | if let 2..6 = $05$0 { | ||
287 | true | ||
288 | } else { | ||
289 | false | ||
290 | } | ||
291 | } | ||
292 | "#, | ||
293 | ); | ||
294 | |||
295 | let mut cfg = TEST_CONFIG; | ||
296 | cfg.allowed = Some(vec![AssistKind::RefactorExtract]); | ||
297 | |||
298 | { | ||
299 | let assists = Assist::get(&db, &cfg, AssistResolveStrategy::None, frange); | ||
300 | assert_eq!(2, assists.len()); | ||
301 | let mut assists = assists.into_iter(); | ||
302 | |||
303 | let extract_into_variable_assist = assists.next().unwrap(); | ||
304 | expect![[r#" | ||
305 | Assist { | ||
306 | id: AssistId( | ||
307 | "extract_variable", | ||
308 | RefactorExtract, | ||
309 | ), | ||
310 | label: "Extract into variable", | ||
311 | group: None, | ||
312 | target: 59..60, | ||
313 | source_change: None, | ||
314 | } | ||
315 | "#]] | ||
316 | .assert_debug_eq(&extract_into_variable_assist); | ||
317 | |||
318 | let extract_into_function_assist = assists.next().unwrap(); | ||
319 | expect![[r#" | ||
320 | Assist { | ||
321 | id: AssistId( | ||
322 | "extract_function", | ||
323 | RefactorExtract, | ||
324 | ), | ||
325 | label: "Extract into function", | ||
326 | group: None, | ||
327 | target: 59..60, | ||
328 | source_change: None, | ||
329 | } | ||
330 | "#]] | ||
331 | .assert_debug_eq(&extract_into_function_assist); | ||
332 | } | ||
333 | |||
334 | { | ||
335 | let assists = Assist::get( | ||
336 | &db, | ||
337 | &cfg, | ||
338 | AssistResolveStrategy::Single(SingleResolve { | ||
339 | assist_id: "SOMETHING_MISMATCHING".to_string(), | ||
340 | assist_kind: AssistKind::RefactorExtract, | ||
341 | }), | ||
342 | frange, | ||
343 | ); | ||
344 | assert_eq!(2, assists.len()); | ||
345 | let mut assists = assists.into_iter(); | ||
346 | |||
347 | let extract_into_variable_assist = assists.next().unwrap(); | ||
348 | expect![[r#" | ||
349 | Assist { | ||
350 | id: AssistId( | ||
351 | "extract_variable", | ||
352 | RefactorExtract, | ||
353 | ), | ||
354 | label: "Extract into variable", | ||
355 | group: None, | ||
356 | target: 59..60, | ||
357 | source_change: None, | ||
358 | } | ||
359 | "#]] | ||
360 | .assert_debug_eq(&extract_into_variable_assist); | ||
361 | |||
362 | let extract_into_function_assist = assists.next().unwrap(); | ||
363 | expect![[r#" | ||
364 | Assist { | ||
365 | id: AssistId( | ||
366 | "extract_function", | ||
367 | RefactorExtract, | ||
368 | ), | ||
369 | label: "Extract into function", | ||
370 | group: None, | ||
371 | target: 59..60, | ||
372 | source_change: None, | ||
373 | } | ||
374 | "#]] | ||
375 | .assert_debug_eq(&extract_into_function_assist); | ||
376 | } | ||
377 | |||
378 | { | ||
379 | let assists = Assist::get( | ||
380 | &db, | ||
381 | &cfg, | ||
382 | AssistResolveStrategy::Single(SingleResolve { | ||
383 | assist_id: "extract_variable".to_string(), | ||
384 | assist_kind: AssistKind::RefactorExtract, | ||
385 | }), | ||
386 | frange, | ||
387 | ); | ||
388 | assert_eq!(2, assists.len()); | ||
389 | let mut assists = assists.into_iter(); | ||
390 | |||
391 | let extract_into_variable_assist = assists.next().unwrap(); | ||
392 | expect![[r#" | ||
393 | Assist { | ||
394 | id: AssistId( | ||
395 | "extract_variable", | ||
396 | RefactorExtract, | ||
397 | ), | ||
398 | label: "Extract into variable", | ||
399 | group: None, | ||
400 | target: 59..60, | ||
401 | source_change: Some( | ||
402 | SourceChange { | ||
403 | source_file_edits: { | ||
404 | FileId( | ||
405 | 0, | ||
406 | ): TextEdit { | ||
407 | indels: [ | ||
408 | Indel { | ||
409 | insert: "let $0var_name = 5;\n ", | ||
410 | delete: 45..45, | ||
411 | }, | ||
412 | Indel { | ||
413 | insert: "var_name", | ||
414 | delete: 59..60, | ||
415 | }, | ||
416 | ], | ||
417 | }, | ||
418 | }, | ||
419 | file_system_edits: [], | ||
420 | is_snippet: true, | ||
421 | }, | ||
422 | ), | ||
423 | } | ||
424 | "#]] | ||
425 | .assert_debug_eq(&extract_into_variable_assist); | ||
426 | |||
427 | let extract_into_function_assist = assists.next().unwrap(); | ||
428 | expect![[r#" | ||
429 | Assist { | ||
430 | id: AssistId( | ||
431 | "extract_function", | ||
432 | RefactorExtract, | ||
433 | ), | ||
434 | label: "Extract into function", | ||
435 | group: None, | ||
436 | target: 59..60, | ||
437 | source_change: None, | ||
438 | } | ||
439 | "#]] | ||
440 | .assert_debug_eq(&extract_into_function_assist); | ||
441 | } | ||
442 | |||
443 | { | ||
444 | let assists = Assist::get(&db, &cfg, AssistResolveStrategy::All, frange); | ||
445 | assert_eq!(2, assists.len()); | ||
446 | let mut assists = assists.into_iter(); | ||
447 | |||
448 | let extract_into_variable_assist = assists.next().unwrap(); | ||
449 | expect![[r#" | ||
450 | Assist { | ||
451 | id: AssistId( | ||
452 | "extract_variable", | ||
453 | RefactorExtract, | ||
454 | ), | ||
455 | label: "Extract into variable", | ||
456 | group: None, | ||
457 | target: 59..60, | ||
458 | source_change: Some( | ||
459 | SourceChange { | ||
460 | source_file_edits: { | ||
461 | FileId( | ||
462 | 0, | ||
463 | ): TextEdit { | ||
464 | indels: [ | ||
465 | Indel { | ||
466 | insert: "let $0var_name = 5;\n ", | ||
467 | delete: 45..45, | ||
468 | }, | ||
469 | Indel { | ||
470 | insert: "var_name", | ||
471 | delete: 59..60, | ||
472 | }, | ||
473 | ], | ||
474 | }, | ||
475 | }, | ||
476 | file_system_edits: [], | ||
477 | is_snippet: true, | ||
478 | }, | ||
479 | ), | ||
480 | } | ||
481 | "#]] | ||
482 | .assert_debug_eq(&extract_into_variable_assist); | ||
483 | |||
484 | let extract_into_function_assist = assists.next().unwrap(); | ||
485 | expect![[r#" | ||
486 | Assist { | ||
487 | id: AssistId( | ||
488 | "extract_function", | ||
489 | RefactorExtract, | ||
490 | ), | ||
491 | label: "Extract into function", | ||
492 | group: None, | ||
493 | target: 59..60, | ||
494 | source_change: Some( | ||
495 | SourceChange { | ||
496 | source_file_edits: { | ||
497 | FileId( | ||
498 | 0, | ||
499 | ): TextEdit { | ||
500 | indels: [ | ||
501 | Indel { | ||
502 | insert: "fun_name()", | ||
503 | delete: 59..60, | ||
504 | }, | ||
505 | Indel { | ||
506 | insert: "\n\nfn $0fun_name() -> i32 {\n 5\n}", | ||
507 | delete: 110..110, | ||
508 | }, | ||
509 | ], | ||
510 | }, | ||
511 | }, | ||
512 | file_system_edits: [], | ||
513 | is_snippet: true, | ||
514 | }, | ||
515 | ), | ||
516 | } | ||
517 | "#]] | ||
518 | .assert_debug_eq(&extract_into_function_assist); | ||
519 | } | ||
520 | } | ||