Adhere to code style in rename tests

This commit is contained in:
Aleksey Kladov 2020-07-03 18:29:37 +02:00
parent 0f68fed4a0
commit ee7ab0104d

View file

@ -278,43 +278,67 @@ mod tests {
use crate::{mock_analysis::analysis_and_position, FileId};
fn check(new_name: &str, ra_fixture_before: &str, ra_fixture_after: &str) {
let ra_fixture_after = &trim_indent(ra_fixture_after);
let (analysis, position) = analysis_and_position(ra_fixture_before);
let source_change = analysis.rename(position, new_name).unwrap();
let mut text_edit_builder = TextEditBuilder::default();
let mut file_id: Option<FileId> = None;
if let Some(change) = source_change {
for edit in change.info.source_file_edits {
file_id = Some(edit.file_id);
for indel in edit.edit.into_iter() {
text_edit_builder.replace(indel.delete, indel.insert);
}
}
}
let mut result = analysis.file_text(file_id.unwrap()).unwrap().to_string();
text_edit_builder.finish().apply(&mut result);
assert_eq_text!(ra_fixture_after, &*result);
}
#[test]
fn test_rename_to_underscore() {
test_rename(
r#"
fn main() {
let i<|> = 1;
}"#,
check(
"_",
r#"
fn main() {
let i<|> = 1;
}
"#,
r#"
fn main() {
let _ = 1;
}"#,
}
"#,
);
}
#[test]
fn test_rename_to_raw_identifier() {
test_rename(
r#"
fn main() {
let i<|> = 1;
}"#,
check(
"r#fn",
r#"
fn main() {
let i<|> = 1;
}
"#,
r#"
fn main() {
let r#fn = 1;
}"#,
}
"#,
);
}
#[test]
fn test_rename_to_invalid_identifier() {
let (analysis, position) = analysis_and_position(
"
r#"
fn main() {
let i<|> = 1;
}",
}
"#,
);
let new_name = "invalid!";
let source_change = analysis.rename(position, new_name).unwrap();
@ -323,7 +347,8 @@ mod tests {
#[test]
fn test_rename_for_local() {
test_rename(
check(
"k",
r#"
fn main() {
let mut i = 1;
@ -335,8 +360,8 @@ mod tests {
}
i = 5;
}"#,
"k",
}
"#,
r#"
fn main() {
let mut k = 1;
@ -348,138 +373,152 @@ mod tests {
}
k = 5;
}"#,
}
"#,
);
}
#[test]
fn test_rename_for_macro_args() {
test_rename(
check(
"b",
r#"
macro_rules! foo {($i:ident) => {$i} }
fn main() {
let a<|> = "test";
foo!(a);
}"#,
"b",
}
"#,
r#"
macro_rules! foo {($i:ident) => {$i} }
fn main() {
let b = "test";
foo!(b);
}"#,
}
"#,
);
}
#[test]
fn test_rename_for_macro_args_rev() {
test_rename(
check(
"b",
r#"
macro_rules! foo {($i:ident) => {$i} }
fn main() {
let a = "test";
foo!(a<|>);
}"#,
"b",
}
"#,
r#"
macro_rules! foo {($i:ident) => {$i} }
fn main() {
let b = "test";
foo!(b);
}"#,
}
"#,
);
}
#[test]
fn test_rename_for_macro_define_fn() {
test_rename(
check(
"bar",
r#"
macro_rules! define_fn {($id:ident) => { fn $id{} }}
define_fn!(foo);
fn main() {
fo<|>o();
}"#,
"bar",
}
"#,
r#"
macro_rules! define_fn {($id:ident) => { fn $id{} }}
define_fn!(bar);
fn main() {
bar();
}"#,
}
"#,
);
}
#[test]
fn test_rename_for_macro_define_fn_rev() {
test_rename(
check(
"bar",
r#"
macro_rules! define_fn {($id:ident) => { fn $id{} }}
define_fn!(fo<|>o);
fn main() {
foo();
}"#,
"bar",
}
"#,
r#"
macro_rules! define_fn {($id:ident) => { fn $id{} }}
define_fn!(bar);
fn main() {
bar();
}"#,
}
"#,
);
}
#[test]
fn test_rename_for_param_inside() {
test_rename(
check(
"j",
r#"
fn foo(i : u32) -> u32 {
i<|>
}"#,
"j",
}
"#,
r#"
fn foo(j : u32) -> u32 {
j
}"#,
}
"#,
);
}
#[test]
fn test_rename_refs_for_fn_param() {
test_rename(
check(
"new_name",
r#"
fn foo(i<|> : u32) -> u32 {
i
}"#,
"new_name",
}
"#,
r#"
fn foo(new_name : u32) -> u32 {
new_name
}"#,
}
"#,
);
}
#[test]
fn test_rename_for_mut_param() {
test_rename(
check(
"new_name",
r#"
fn foo(mut i<|> : u32) -> u32 {
i
}"#,
"new_name",
}
"#,
r#"
fn foo(mut new_name : u32) -> u32 {
new_name
}"#,
}
"#,
);
}
#[test]
fn test_rename_struct_field() {
test_rename(
check(
"j",
r#"
struct Foo {
i<|>: i32,
}
struct Foo { i<|>: i32 }
impl Foo {
fn new(i: i32) -> Self {
@ -487,11 +526,8 @@ mod tests {
}
}
"#,
"j",
r#"
struct Foo {
j: i32,
}
struct Foo { j: i32 }
impl Foo {
fn new(i: i32) -> Self {
@ -505,11 +541,10 @@ mod tests {
#[test]
fn test_rename_struct_field_for_shorthand() {
mark::check!(test_rename_struct_field_for_shorthand);
test_rename(
check(
"j",
r#"
struct Foo {
i<|>: i32,
}
struct Foo { i<|>: i32 }
impl Foo {
fn new(i: i32) -> Self {
@ -517,11 +552,8 @@ mod tests {
}
}
"#,
"j",
r#"
struct Foo {
j: i32,
}
struct Foo { j: i32 }
impl Foo {
fn new(i: i32) -> Self {
@ -535,11 +567,10 @@ mod tests {
#[test]
fn test_rename_local_for_field_shorthand() {
mark::check!(test_rename_local_for_field_shorthand);
test_rename(
check(
"j",
r#"
struct Foo {
i: i32,
}
struct Foo { i: i32 }
impl Foo {
fn new(i<|>: i32) -> Self {
@ -547,11 +578,8 @@ mod tests {
}
}
"#,
"j",
r#"
struct Foo {
i: i32,
}
struct Foo { i: i32 }
impl Foo {
fn new(j: i32) -> Self {
@ -564,15 +592,12 @@ mod tests {
#[test]
fn test_field_shorthand_correct_struct() {
test_rename(
check(
"j",
r#"
struct Foo {
i<|>: i32,
}
struct Foo { i<|>: i32 }
struct Bar {
i: i32,
}
struct Bar { i: i32 }
impl Bar {
fn new(i: i32) -> Self {
@ -580,15 +605,10 @@ mod tests {
}
}
"#,
"j",
r#"
struct Foo {
j: i32,
}
struct Foo { j: i32 }
struct Bar {
i: i32,
}
struct Bar { i: i32 }
impl Bar {
fn new(i: i32) -> Self {
@ -601,11 +621,10 @@ mod tests {
#[test]
fn test_shadow_local_for_struct_shorthand() {
test_rename(
check(
"j",
r#"
struct Foo {
i: i32,
}
struct Foo { i: i32 }
fn baz(i<|>: i32) -> Self {
let x = Foo { i };
@ -615,11 +634,8 @@ mod tests {
}
}
"#,
"j",
r#"
struct Foo {
i: i32,
}
struct Foo { i: i32 }
fn baz(j: i32) -> Self {
let x = Foo { i: j };
@ -811,7 +827,8 @@ mod fo<|>o;
#[test]
fn test_module_rename_in_path() {
test_rename(
check(
"baz",
r#"
mod <|>foo {
pub fn bar() {}
@ -819,8 +836,8 @@ mod fo<|>o;
fn main() {
foo::bar();
}"#,
"baz",
}
"#,
r#"
mod baz {
pub fn bar() {}
@ -828,7 +845,8 @@ mod fo<|>o;
fn main() {
baz::bar();
}"#,
}
"#,
);
}
@ -905,7 +923,8 @@ pub mod foo<|>;
#[test]
fn test_enum_variant_from_module_1() {
test_rename(
check(
"Baz",
r#"
mod foo {
pub enum Foo {
@ -919,7 +938,6 @@ pub mod foo<|>;
}
}
"#,
"Baz",
r#"
mod foo {
pub enum Foo {
@ -938,24 +956,20 @@ pub mod foo<|>;
#[test]
fn test_enum_variant_from_module_2() {
test_rename(
check(
"baz",
r#"
mod foo {
pub struct Foo {
pub bar<|>: uint,
}
pub struct Foo { pub bar<|>: uint }
}
fn foo(f: foo::Foo) {
let _ = f.bar;
}
"#,
"baz",
r#"
mod foo {
pub struct Foo {
pub baz: uint,
}
pub struct Foo { pub baz: uint }
}
fn foo(f: foo::Foo) {
@ -967,11 +981,10 @@ pub mod foo<|>;
#[test]
fn test_parameter_to_self() {
test_rename(
check(
"self",
r#"
struct Foo {
i: i32,
}
struct Foo { i: i32 }
impl Foo {
fn f(foo<|>: &mut Foo) -> i32 {
@ -979,11 +992,8 @@ pub mod foo<|>;
}
}
"#,
"self",
r#"
struct Foo {
i: i32,
}
struct Foo { i: i32 }
impl Foo {
fn f(&mut self) -> i32 {
@ -996,11 +1006,10 @@ pub mod foo<|>;
#[test]
fn test_self_to_parameter() {
test_rename(
check(
"foo",
r#"
struct Foo {
i: i32,
}
struct Foo { i: i32 }
impl Foo {
fn f(&mut <|>self) -> i32 {
@ -1008,11 +1017,8 @@ pub mod foo<|>;
}
}
"#,
"foo",
r#"
struct Foo {
i: i32,
}
struct Foo { i: i32 }
impl Foo {
fn f(foo: &mut Foo) -> i32 {
@ -1025,11 +1031,10 @@ pub mod foo<|>;
#[test]
fn test_self_in_path_to_parameter() {
test_rename(
check(
"foo",
r#"
struct Foo {
i: i32,
}
struct Foo { i: i32 }
impl Foo {
fn f(&self) -> i32 {
@ -1038,11 +1043,8 @@ pub mod foo<|>;
}
}
"#,
"foo",
r#"
struct Foo {
i: i32,
}
struct Foo { i: i32 }
impl Foo {
fn f(foo: &Foo) -> i32 {
@ -1053,23 +1055,4 @@ pub mod foo<|>;
"#,
);
}
fn test_rename(ra_fixture_before: &str, new_name: &str, ra_fixture_after: &str) {
let ra_fixture_after = &trim_indent(ra_fixture_after);
let (analysis, position) = analysis_and_position(ra_fixture_before);
let source_change = analysis.rename(position, new_name).unwrap();
let mut text_edit_builder = TextEditBuilder::default();
let mut file_id: Option<FileId> = None;
if let Some(change) = source_change {
for edit in change.info.source_file_edits {
file_id = Some(edit.file_id);
for indel in edit.edit.into_iter() {
text_edit_builder.replace(indel.delete, indel.insert);
}
}
}
let mut result = analysis.file_text(file_id.unwrap()).unwrap().to_string();
text_edit_builder.finish().apply(&mut result);
assert_eq_text!(ra_fixture_after, &*result);
}
}