Use expect for rename tests

This commit is contained in:
Aleksey Kladov 2020-07-03 18:35:43 +02:00
parent ee7ab0104d
commit 2656c415c8

View File

@ -271,7 +271,7 @@ fn rename_reference(
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use insta::assert_debug_snapshot; use expect::{expect, Expect};
use ra_text_edit::TextEditBuilder; use ra_text_edit::TextEditBuilder;
use stdx::trim_indent; use stdx::trim_indent;
use test_utils::{assert_eq_text, mark}; use test_utils::{assert_eq_text, mark};
@ -297,6 +297,12 @@ mod tests {
assert_eq_text!(ra_fixture_after, &*result); assert_eq_text!(ra_fixture_after, &*result);
} }
fn check_expect(new_name: &str, ra_fixture: &str, expect: Expect) {
let (analysis, position) = analysis_and_position(ra_fixture);
let source_change = analysis.rename(position, new_name).unwrap().unwrap();
expect.assert_debug_eq(&source_change)
}
#[test] #[test]
fn test_rename_to_underscore() { fn test_rename_to_underscore() {
check( check(
@ -650,7 +656,8 @@ fn baz(j: i32) -> Self {
#[test] #[test]
fn test_rename_mod() { fn test_rename_mod() {
let (analysis, position) = analysis_and_position( check_expect(
"foo2",
r#" r#"
//- /lib.rs //- /lib.rs
mod bar; mod bar;
@ -659,53 +666,49 @@ mod bar;
mod foo<|>; mod foo<|>;
//- /bar/foo.rs //- /bar/foo.rs
// emtpy // empty
"#, "#,
); expect![[r#"
let new_name = "foo2"; RangeInfo {
let source_change = analysis.rename(position, new_name).unwrap(); range: 4..7,
assert_debug_snapshot!(&source_change, info: SourceChange {
@r###" source_file_edits: [
Some( SourceFileEdit {
RangeInfo { file_id: FileId(
range: 4..7, 2,
info: SourceChange { ),
source_file_edits: [ edit: TextEdit {
SourceFileEdit { indels: [
file_id: FileId( Indel {
2, insert: "foo2",
), delete: 4..7,
edit: TextEdit { },
indels: [ ],
Indel { },
insert: "foo2",
delete: 4..7,
},
],
}, },
}, ],
], file_system_edits: [
file_system_edits: [ MoveFile {
MoveFile { src: FileId(
src: FileId( 3,
3, ),
), anchor: FileId(
anchor: FileId( 2,
2, ),
), dst: "foo2.rs",
dst: "foo2.rs", },
}, ],
], is_snippet: false,
is_snippet: false, },
}, }
}, "#]],
) );
"###);
} }
#[test] #[test]
fn test_rename_mod_in_use_tree() { fn test_rename_mod_in_use_tree() {
let (analysis, position) = analysis_and_position( check_expect(
"quux",
r#" r#"
//- /main.rs //- /main.rs
pub mod foo; pub mod foo;
@ -717,112 +720,23 @@ pub struct FooContent;
//- /bar.rs //- /bar.rs
use crate::foo<|>::FooContent; use crate::foo<|>::FooContent;
"#, "#,
expect![[]],
); );
let new_name = "qux";
let source_change = analysis.rename(position, new_name).unwrap();
assert_debug_snapshot!(&source_change,
@r###"
Some(
RangeInfo {
range: 11..14,
info: SourceChange {
source_file_edits: [
SourceFileEdit {
file_id: FileId(
1,
),
edit: TextEdit {
indels: [
Indel {
insert: "qux",
delete: 8..11,
},
],
},
},
SourceFileEdit {
file_id: FileId(
3,
),
edit: TextEdit {
indels: [
Indel {
insert: "qux",
delete: 11..14,
},
],
},
},
],
file_system_edits: [
MoveFile {
src: FileId(
2,
),
anchor: FileId(
3,
),
dst: "qux.rs",
},
],
is_snippet: false,
},
},
)
"###);
} }
#[test] #[test]
fn test_rename_mod_in_dir() { fn test_rename_mod_in_dir() {
let (analysis, position) = analysis_and_position( check_expect(
"foo2",
r#" r#"
//- /lib.rs //- /lib.rs
mod fo<|>o; mod fo<|>o;
//- /foo/mod.rs //- /foo/mod.rs
// emtpy // emtpy
"#, "#,
expect![[]],
); );
let new_name = "foo2";
let source_change = analysis.rename(position, new_name).unwrap();
assert_debug_snapshot!(&source_change,
@r###"
Some(
RangeInfo {
range: 4..7,
info: SourceChange {
source_file_edits: [
SourceFileEdit {
file_id: FileId(
1,
),
edit: TextEdit {
indels: [
Indel {
insert: "foo2",
delete: 4..7,
},
],
},
},
],
file_system_edits: [
MoveFile {
src: FileId(
2,
),
anchor: FileId(
1,
),
dst: "../foo2/mod.rs",
},
],
is_snippet: false,
},
},
)
"###
);
} }
#[test] #[test]
@ -852,7 +766,8 @@ fn main() {
#[test] #[test]
fn test_rename_mod_filename_and_path() { fn test_rename_mod_filename_and_path() {
let (analysis, position) = analysis_and_position( check_expect(
"foo2",
r#" r#"
//- /lib.rs //- /lib.rs
mod bar; mod bar;
@ -865,60 +780,9 @@ pub mod foo<|>;
//- /bar/foo.rs //- /bar/foo.rs
// pub fn fun() {} // pub fn fun() {}
"#, "#,
expect![[]],
); );
let new_name = "foo2";
let source_change = analysis.rename(position, new_name).unwrap();
assert_debug_snapshot!(&source_change,
@r###"
Some(
RangeInfo {
range: 8..11,
info: SourceChange {
source_file_edits: [
SourceFileEdit {
file_id: FileId(
2,
),
edit: TextEdit {
indels: [
Indel {
insert: "foo2",
delete: 8..11,
},
],
},
},
SourceFileEdit {
file_id: FileId(
1,
),
edit: TextEdit {
indels: [
Indel {
insert: "foo2",
delete: 27..30,
},
],
},
},
],
file_system_edits: [
MoveFile {
src: FileId(
3,
),
anchor: FileId(
2,
),
dst: "foo2.rs",
},
],
is_snippet: false,
},
},
)
"###);
} }
#[test] #[test]