diff options
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ra_analysis/tests/tests.rs | 93 |
1 files changed, 92 insertions, 1 deletions
diff --git a/crates/ra_analysis/tests/tests.rs b/crates/ra_analysis/tests/tests.rs index 210fa2a13..a314fbc40 100644 --- a/crates/ra_analysis/tests/tests.rs +++ b/crates/ra_analysis/tests/tests.rs | |||
@@ -1,5 +1,5 @@ | |||
1 | use ra_syntax::TextRange; | 1 | use ra_syntax::TextRange; |
2 | use test_utils::assert_eq_dbg; | 2 | use test_utils::{assert_eq_dbg, assert_eq_text}; |
3 | 3 | ||
4 | use ra_analysis::{ | 4 | use ra_analysis::{ |
5 | mock_analysis::{analysis_and_position, single_file, single_file_with_position, MockAnalysis}, | 5 | mock_analysis::{analysis_and_position, single_file, single_file_with_position, MockAnalysis}, |
@@ -453,3 +453,94 @@ fn test_find_all_refs_for_fn_param() { | |||
453 | let refs = get_all_refs(code); | 453 | let refs = get_all_refs(code); |
454 | assert_eq!(refs.len(), 2); | 454 | assert_eq!(refs.len(), 2); |
455 | } | 455 | } |
456 | #[test] | ||
457 | fn test_rename_for_local() { | ||
458 | test_rename( | ||
459 | r#" | ||
460 | fn main() { | ||
461 | let mut i = 1; | ||
462 | let j = 1; | ||
463 | i = i<|> + j; | ||
464 | |||
465 | { | ||
466 | i = 0; | ||
467 | } | ||
468 | |||
469 | i = 5; | ||
470 | }"#, | ||
471 | "k", | ||
472 | r#" | ||
473 | fn main() { | ||
474 | let mut k = 1; | ||
475 | let j = 1; | ||
476 | k = k + j; | ||
477 | |||
478 | { | ||
479 | k = 0; | ||
480 | } | ||
481 | |||
482 | k = 5; | ||
483 | }"#, | ||
484 | ); | ||
485 | } | ||
486 | |||
487 | #[test] | ||
488 | fn test_rename_for_param_inside() { | ||
489 | test_rename( | ||
490 | r#" | ||
491 | fn foo(i : u32) -> u32 { | ||
492 | i<|> | ||
493 | }"#, | ||
494 | "j", | ||
495 | r#" | ||
496 | fn foo(j : u32) -> u32 { | ||
497 | j | ||
498 | }"#, | ||
499 | ); | ||
500 | } | ||
501 | |||
502 | #[test] | ||
503 | fn test_rename_refs_for_fn_param() { | ||
504 | test_rename( | ||
505 | r#" | ||
506 | fn foo(i<|> : u32) -> u32 { | ||
507 | i | ||
508 | }"#, | ||
509 | "new_name", | ||
510 | r#" | ||
511 | fn foo(new_name : u32) -> u32 { | ||
512 | new_name | ||
513 | }"#, | ||
514 | ); | ||
515 | } | ||
516 | |||
517 | #[test] | ||
518 | fn test_rename_for_mut_param() { | ||
519 | test_rename( | ||
520 | r#" | ||
521 | fn foo(mut i<|> : u32) -> u32 { | ||
522 | i | ||
523 | }"#, | ||
524 | "new_name", | ||
525 | r#" | ||
526 | fn foo(mut new_name : u32) -> u32 { | ||
527 | new_name | ||
528 | }"#, | ||
529 | ); | ||
530 | } | ||
531 | fn test_rename(text: &str, new_name: &str, expected: &str) { | ||
532 | let (analysis, position) = single_file_with_position(text); | ||
533 | let edits = analysis.rename(position, new_name).unwrap(); | ||
534 | let mut text_edit_bulder = ra_text_edit::TextEditBuilder::new(); | ||
535 | let mut file_id: Option<FileId> = None; | ||
536 | for edit in edits { | ||
537 | file_id = Some(edit.file_id); | ||
538 | for atom in edit.edit.as_atoms() { | ||
539 | text_edit_bulder.replace(atom.delete, atom.insert.clone()); | ||
540 | } | ||
541 | } | ||
542 | let result = text_edit_bulder | ||
543 | .finish() | ||
544 | .apply(&*analysis.file_text(file_id.unwrap())); | ||
545 | assert_eq_text!(expected, &*result); | ||
546 | } | ||