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}; 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] #[test]
fn test_rename_to_underscore() { fn test_rename_to_underscore() {
test_rename( check(
r#"
fn main() {
let i<|> = 1;
}"#,
"_", "_",
r#" r#"
fn main() { fn main() {
let _ = 1; let i<|> = 1;
}"#, }
"#,
r#"
fn main() {
let _ = 1;
}
"#,
); );
} }
#[test] #[test]
fn test_rename_to_raw_identifier() { fn test_rename_to_raw_identifier() {
test_rename( check(
r#"
fn main() {
let i<|> = 1;
}"#,
"r#fn", "r#fn",
r#" r#"
fn main() { fn main() {
let r#fn = 1; let i<|> = 1;
}"#, }
"#,
r#"
fn main() {
let r#fn = 1;
}
"#,
); );
} }
#[test] #[test]
fn test_rename_to_invalid_identifier() { fn test_rename_to_invalid_identifier() {
let (analysis, position) = analysis_and_position( let (analysis, position) = analysis_and_position(
" r#"
fn main() { fn main() {
let i<|> = 1; let i<|> = 1;
}", }
"#,
); );
let new_name = "invalid!"; let new_name = "invalid!";
let source_change = analysis.rename(position, new_name).unwrap(); let source_change = analysis.rename(position, new_name).unwrap();
@ -323,312 +347,304 @@ mod tests {
#[test] #[test]
fn test_rename_for_local() { fn test_rename_for_local() {
test_rename( check(
r#"
fn main() {
let mut i = 1;
let j = 1;
i = i<|> + j;
{
i = 0;
}
i = 5;
}"#,
"k", "k",
r#" r#"
fn main() { fn main() {
let mut k = 1; let mut i = 1;
let j = 1; let j = 1;
k = k + j; i = i<|> + j;
{ {
k = 0; i = 0;
} }
k = 5; i = 5;
}"#, }
"#,
r#"
fn main() {
let mut k = 1;
let j = 1;
k = k + j;
{
k = 0;
}
k = 5;
}
"#,
); );
} }
#[test] #[test]
fn test_rename_for_macro_args() { fn test_rename_for_macro_args() {
test_rename( check(
r#"
macro_rules! foo {($i:ident) => {$i} }
fn main() {
let a<|> = "test";
foo!(a);
}"#,
"b", "b",
r#" r#"
macro_rules! foo {($i:ident) => {$i} } macro_rules! foo {($i:ident) => {$i} }
fn main() { fn main() {
let b = "test"; let a<|> = "test";
foo!(b); foo!(a);
}"#, }
"#,
r#"
macro_rules! foo {($i:ident) => {$i} }
fn main() {
let b = "test";
foo!(b);
}
"#,
); );
} }
#[test] #[test]
fn test_rename_for_macro_args_rev() { fn test_rename_for_macro_args_rev() {
test_rename( check(
r#"
macro_rules! foo {($i:ident) => {$i} }
fn main() {
let a = "test";
foo!(a<|>);
}"#,
"b", "b",
r#" r#"
macro_rules! foo {($i:ident) => {$i} } macro_rules! foo {($i:ident) => {$i} }
fn main() { fn main() {
let b = "test"; let a = "test";
foo!(b); foo!(a<|>);
}"#, }
"#,
r#"
macro_rules! foo {($i:ident) => {$i} }
fn main() {
let b = "test";
foo!(b);
}
"#,
); );
} }
#[test] #[test]
fn test_rename_for_macro_define_fn() { fn test_rename_for_macro_define_fn() {
test_rename( check(
r#"
macro_rules! define_fn {($id:ident) => { fn $id{} }}
define_fn!(foo);
fn main() {
fo<|>o();
}"#,
"bar", "bar",
r#" r#"
macro_rules! define_fn {($id:ident) => { fn $id{} }} macro_rules! define_fn {($id:ident) => { fn $id{} }}
define_fn!(bar); define_fn!(foo);
fn main() { fn main() {
bar(); fo<|>o();
}"#, }
"#,
r#"
macro_rules! define_fn {($id:ident) => { fn $id{} }}
define_fn!(bar);
fn main() {
bar();
}
"#,
); );
} }
#[test] #[test]
fn test_rename_for_macro_define_fn_rev() { fn test_rename_for_macro_define_fn_rev() {
test_rename( check(
r#"
macro_rules! define_fn {($id:ident) => { fn $id{} }}
define_fn!(fo<|>o);
fn main() {
foo();
}"#,
"bar", "bar",
r#" r#"
macro_rules! define_fn {($id:ident) => { fn $id{} }} macro_rules! define_fn {($id:ident) => { fn $id{} }}
define_fn!(bar); define_fn!(fo<|>o);
fn main() { fn main() {
bar(); foo();
}"#, }
"#,
r#"
macro_rules! define_fn {($id:ident) => { fn $id{} }}
define_fn!(bar);
fn main() {
bar();
}
"#,
); );
} }
#[test] #[test]
fn test_rename_for_param_inside() { fn test_rename_for_param_inside() {
test_rename( check(
r#"
fn foo(i : u32) -> u32 {
i<|>
}"#,
"j", "j",
r#" r#"
fn foo(j : u32) -> u32 { fn foo(i : u32) -> u32 {
j i<|>
}"#, }
"#,
r#"
fn foo(j : u32) -> u32 {
j
}
"#,
); );
} }
#[test] #[test]
fn test_rename_refs_for_fn_param() { fn test_rename_refs_for_fn_param() {
test_rename( check(
r#"
fn foo(i<|> : u32) -> u32 {
i
}"#,
"new_name", "new_name",
r#" r#"
fn foo(new_name : u32) -> u32 { fn foo(i<|> : u32) -> u32 {
new_name i
}"#, }
"#,
r#"
fn foo(new_name : u32) -> u32 {
new_name
}
"#,
); );
} }
#[test] #[test]
fn test_rename_for_mut_param() { fn test_rename_for_mut_param() {
test_rename( check(
r#"
fn foo(mut i<|> : u32) -> u32 {
i
}"#,
"new_name", "new_name",
r#" r#"
fn foo(mut new_name : u32) -> u32 { fn foo(mut i<|> : u32) -> u32 {
new_name i
}"#, }
"#,
r#"
fn foo(mut new_name : u32) -> u32 {
new_name
}
"#,
); );
} }
#[test] #[test]
fn test_rename_struct_field() { fn test_rename_struct_field() {
test_rename( check(
r#"
struct Foo {
i<|>: i32,
}
impl Foo {
fn new(i: i32) -> Self {
Self { i: i }
}
}
"#,
"j", "j",
r#" r#"
struct Foo { struct Foo { i<|>: i32 }
j: i32,
}
impl Foo { impl Foo {
fn new(i: i32) -> Self { fn new(i: i32) -> Self {
Self { j: i } Self { i: i }
}
} }
"#, }
"#,
r#"
struct Foo { j: i32 }
impl Foo {
fn new(i: i32) -> Self {
Self { j: i }
}
}
"#,
); );
} }
#[test] #[test]
fn test_rename_struct_field_for_shorthand() { fn test_rename_struct_field_for_shorthand() {
mark::check!(test_rename_struct_field_for_shorthand); mark::check!(test_rename_struct_field_for_shorthand);
test_rename( check(
r#"
struct Foo {
i<|>: i32,
}
impl Foo {
fn new(i: i32) -> Self {
Self { i }
}
}
"#,
"j", "j",
r#" r#"
struct Foo { struct Foo { i<|>: i32 }
j: i32,
}
impl Foo { impl Foo {
fn new(i: i32) -> Self { fn new(i: i32) -> Self {
Self { j: i } Self { i }
}
} }
"#, }
"#,
r#"
struct Foo { j: i32 }
impl Foo {
fn new(i: i32) -> Self {
Self { j: i }
}
}
"#,
); );
} }
#[test] #[test]
fn test_rename_local_for_field_shorthand() { fn test_rename_local_for_field_shorthand() {
mark::check!(test_rename_local_for_field_shorthand); mark::check!(test_rename_local_for_field_shorthand);
test_rename( check(
r#"
struct Foo {
i: i32,
}
impl Foo {
fn new(i<|>: i32) -> Self {
Self { i }
}
}
"#,
"j", "j",
r#" r#"
struct Foo { struct Foo { i: i32 }
i: i32,
}
impl Foo { impl Foo {
fn new(j: i32) -> Self { fn new(i<|>: i32) -> Self {
Self { i: j } Self { i }
}
} }
"#, }
"#,
r#"
struct Foo { i: i32 }
impl Foo {
fn new(j: i32) -> Self {
Self { i: j }
}
}
"#,
); );
} }
#[test] #[test]
fn test_field_shorthand_correct_struct() { fn test_field_shorthand_correct_struct() {
test_rename( check(
r#"
struct Foo {
i<|>: i32,
}
struct Bar {
i: i32,
}
impl Bar {
fn new(i: i32) -> Self {
Self { i }
}
}
"#,
"j", "j",
r#" r#"
struct Foo { struct Foo { i<|>: i32 }
j: i32,
}
struct Bar { struct Bar { i: i32 }
i: i32,
}
impl Bar { impl Bar {
fn new(i: i32) -> Self { fn new(i: i32) -> Self {
Self { i } Self { i }
}
} }
"#, }
"#,
r#"
struct Foo { j: i32 }
struct Bar { i: i32 }
impl Bar {
fn new(i: i32) -> Self {
Self { i }
}
}
"#,
); );
} }
#[test] #[test]
fn test_shadow_local_for_struct_shorthand() { fn test_shadow_local_for_struct_shorthand() {
test_rename( check(
r#"
struct Foo {
i: i32,
}
fn baz(i<|>: i32) -> Self {
let x = Foo { i };
{
let i = 0;
Foo { i }
}
}
"#,
"j", "j",
r#" r#"
struct Foo { struct Foo { i: i32 }
i: i32,
}
fn baz(j: i32) -> Self { fn baz(i<|>: i32) -> Self {
let x = Foo { i: j }; let x = Foo { i };
{ {
let i = 0; let i = 0;
Foo { i } Foo { i }
}
} }
"#, }
"#,
r#"
struct Foo { i: i32 }
fn baz(j: i32) -> Self {
let x = Foo { i: j };
{
let i = 0;
Foo { i }
}
}
"#,
); );
} }
@ -811,24 +827,26 @@ mod fo<|>o;
#[test] #[test]
fn test_module_rename_in_path() { fn test_module_rename_in_path() {
test_rename( check(
r#"
mod <|>foo {
pub fn bar() {}
}
fn main() {
foo::bar();
}"#,
"baz", "baz",
r#" r#"
mod baz { mod <|>foo {
pub fn bar() {} pub fn bar() {}
} }
fn main() { fn main() {
baz::bar(); foo::bar();
}"#, }
"#,
r#"
mod baz {
pub fn bar() {}
}
fn main() {
baz::bar();
}
"#,
); );
} }
@ -905,171 +923,136 @@ pub mod foo<|>;
#[test] #[test]
fn test_enum_variant_from_module_1() { fn test_enum_variant_from_module_1() {
test_rename( check(
r#"
mod foo {
pub enum Foo {
Bar<|>,
}
}
fn func(f: foo::Foo) {
match f {
foo::Foo::Bar => {}
}
}
"#,
"Baz", "Baz",
r#" r#"
mod foo { mod foo {
pub enum Foo { pub enum Foo {
Baz, Bar<|>,
}
} }
}
fn func(f: foo::Foo) { fn func(f: foo::Foo) {
match f { match f {
foo::Foo::Baz => {} foo::Foo::Bar => {}
}
} }
"#, }
"#,
r#"
mod foo {
pub enum Foo {
Baz,
}
}
fn func(f: foo::Foo) {
match f {
foo::Foo::Baz => {}
}
}
"#,
); );
} }
#[test] #[test]
fn test_enum_variant_from_module_2() { fn test_enum_variant_from_module_2() {
test_rename( check(
r#"
mod foo {
pub struct Foo {
pub bar<|>: uint,
}
}
fn foo(f: foo::Foo) {
let _ = f.bar;
}
"#,
"baz", "baz",
r#" r#"
mod foo { mod foo {
pub struct Foo { pub struct Foo { pub bar<|>: uint }
pub baz: uint, }
}
}
fn foo(f: foo::Foo) { fn foo(f: foo::Foo) {
let _ = f.baz; let _ = f.bar;
} }
"#, "#,
r#"
mod foo {
pub struct Foo { pub baz: uint }
}
fn foo(f: foo::Foo) {
let _ = f.baz;
}
"#,
); );
} }
#[test] #[test]
fn test_parameter_to_self() { fn test_parameter_to_self() {
test_rename( check(
r#"
struct Foo {
i: i32,
}
impl Foo {
fn f(foo<|>: &mut Foo) -> i32 {
foo.i
}
}
"#,
"self", "self",
r#" r#"
struct Foo { struct Foo { i: i32 }
i: i32,
}
impl Foo { impl Foo {
fn f(&mut self) -> i32 { fn f(foo<|>: &mut Foo) -> i32 {
self.i foo.i
}
} }
"#, }
"#,
r#"
struct Foo { i: i32 }
impl Foo {
fn f(&mut self) -> i32 {
self.i
}
}
"#,
); );
} }
#[test] #[test]
fn test_self_to_parameter() { fn test_self_to_parameter() {
test_rename( check(
r#"
struct Foo {
i: i32,
}
impl Foo {
fn f(&mut <|>self) -> i32 {
self.i
}
}
"#,
"foo", "foo",
r#" r#"
struct Foo { struct Foo { i: i32 }
i: i32,
}
impl Foo { impl Foo {
fn f(foo: &mut Foo) -> i32 { fn f(&mut <|>self) -> i32 {
foo.i self.i
}
} }
"#, }
"#,
r#"
struct Foo { i: i32 }
impl Foo {
fn f(foo: &mut Foo) -> i32 {
foo.i
}
}
"#,
); );
} }
#[test] #[test]
fn test_self_in_path_to_parameter() { fn test_self_in_path_to_parameter() {
test_rename( check(
r#"
struct Foo {
i: i32,
}
impl Foo {
fn f(&self) -> i32 {
let self_var = 1;
self<|>.i
}
}
"#,
"foo", "foo",
r#" r#"
struct Foo { struct Foo { i: i32 }
i: i32,
}
impl Foo { impl Foo {
fn f(foo: &Foo) -> i32 { fn f(&self) -> i32 {
let self_var = 1; let self_var = 1;
foo.i self<|>.i
} }
} }
"#, "#,
); r#"
} struct Foo { i: i32 }
fn test_rename(ra_fixture_before: &str, new_name: &str, ra_fixture_after: &str) { impl Foo {
let ra_fixture_after = &trim_indent(ra_fixture_after); fn f(foo: &Foo) -> i32 {
let (analysis, position) = analysis_and_position(ra_fixture_before); let self_var = 1;
let source_change = analysis.rename(position, new_name).unwrap(); foo.i
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);
} }
} }