From c6795fb83a850dde6ac0b08decf108c0c3aa452a Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 24 Jun 2020 11:29:43 +0200 Subject: More consistent usage of fixtures --- crates/ra_ide/src/references.rs | 488 ++++++++++++++++++++-------------------- 1 file changed, 249 insertions(+), 239 deletions(-) (limited to 'crates/ra_ide/src/references.rs') diff --git a/crates/ra_ide/src/references.rs b/crates/ra_ide/src/references.rs index 4a96d6505..50929bb72 100644 --- a/crates/ra_ide/src/references.rs +++ b/crates/ra_ide/src/references.rs @@ -197,233 +197,238 @@ mod tests { #[test] fn test_struct_literal_after_space() { - let code = r#" - struct Foo <|>{ - a: i32, - } - impl Foo { - fn f() -> i32 { 42 } - } - fn main() { - let f: Foo; - f = Foo {a: Foo::f()}; - }"#; - - let refs = get_all_refs(code); + let refs = get_all_refs( + r#" +struct Foo <|>{ + a: i32, +} +impl Foo { + fn f() -> i32 { 42 } +} +fn main() { + let f: Foo; + f = Foo {a: Foo::f()}; +} +"#, + ); check_result( refs, - "Foo STRUCT_DEF FileId(1) 5..39 12..15 Other", - &["FileId(1) 138..141 StructLiteral"], + "Foo STRUCT_DEF FileId(1) 0..26 7..10 Other", + &["FileId(1) 101..104 StructLiteral"], ); } #[test] - fn test_struct_literal_befor_space() { - let code = r#" - struct Foo<|> {} - fn main() { - let f: Foo; - f = Foo {}; - }"#; - - let refs = get_all_refs(code); + fn test_struct_literal_before_space() { + let refs = get_all_refs( + r#" +struct Foo<|> {} + fn main() { + let f: Foo; + f = Foo {}; +} +"#, + ); check_result( refs, - "Foo STRUCT_DEF FileId(1) 5..18 12..15 Other", - &["FileId(1) 54..57 Other", "FileId(1) 71..74 StructLiteral"], + "Foo STRUCT_DEF FileId(1) 0..13 7..10 Other", + &["FileId(1) 41..44 Other", "FileId(1) 54..57 StructLiteral"], ); } #[test] fn test_struct_literal_with_generic_type() { - let code = r#" - struct Foo <|>{} - fn main() { - let f: Foo::; - f = Foo {}; - }"#; - - let refs = get_all_refs(code); + let refs = get_all_refs( + r#" +struct Foo <|>{} + fn main() { + let f: Foo::; + f = Foo {}; +} +"#, + ); check_result( refs, - "Foo STRUCT_DEF FileId(1) 5..21 12..15 Other", - &["FileId(1) 81..84 StructLiteral"], + "Foo STRUCT_DEF FileId(1) 0..16 7..10 Other", + &["FileId(1) 64..67 StructLiteral"], ); } #[test] fn test_struct_literal_for_tuple() { - let code = r#" - struct Foo<|>(i32); - - fn main() { - let f: Foo; - f = Foo(1); - }"#; + let refs = get_all_refs( + r#" +struct Foo<|>(i32); - let refs = get_all_refs(code); +fn main() { + let f: Foo; + f = Foo(1); +} +"#, + ); check_result( refs, - "Foo STRUCT_DEF FileId(1) 5..21 12..15 Other", - &["FileId(1) 71..74 StructLiteral"], + "Foo STRUCT_DEF FileId(1) 0..16 7..10 Other", + &["FileId(1) 54..57 StructLiteral"], ); } #[test] fn test_find_all_refs_for_local() { - let code = r#" - fn main() { - let mut i = 1; - let j = 1; - i = i<|> + j; + let refs = get_all_refs( + r#" +fn main() { + let mut i = 1; + let j = 1; + i = i<|> + j; - { - i = 0; - } - - i = 5; - }"#; + { + i = 0; + } - let refs = get_all_refs(code); + i = 5; +}"#, + ); check_result( refs, - "i BIND_PAT FileId(1) 33..34 Other Write", + "i BIND_PAT FileId(1) 24..25 Other Write", &[ - "FileId(1) 67..68 Other Write", - "FileId(1) 71..72 Other Read", - "FileId(1) 101..102 Other Write", - "FileId(1) 127..128 Other Write", + "FileId(1) 50..51 Other Write", + "FileId(1) 54..55 Other Read", + "FileId(1) 76..77 Other Write", + "FileId(1) 94..95 Other Write", ], ); } #[test] fn search_filters_by_range() { - let code = r#" - fn foo() { - let spam<|> = 92; - spam + spam - } - fn bar() { - let spam = 92; - spam + spam - } - "#; - let refs = get_all_refs(code); + let refs = get_all_refs( + r#" +fn foo() { + let spam<|> = 92; + spam + spam +} +fn bar() { + let spam = 92; + spam + spam +} +"#, + ); check_result( refs, - "spam BIND_PAT FileId(1) 44..48 Other", - &["FileId(1) 71..75 Other Read", "FileId(1) 78..82 Other Read"], + "spam BIND_PAT FileId(1) 19..23 Other", + &["FileId(1) 34..38 Other Read", "FileId(1) 41..45 Other Read"], ); } #[test] fn test_find_all_refs_for_param_inside() { - let code = r#" - fn foo(i : u32) -> u32 { - i<|> - }"#; - - let refs = get_all_refs(code); - check_result(refs, "i BIND_PAT FileId(1) 12..13 Other", &["FileId(1) 38..39 Other Read"]); + let refs = get_all_refs( + r#" +fn foo(i : u32) -> u32 { + i<|> +} +"#, + ); + check_result(refs, "i BIND_PAT FileId(1) 7..8 Other", &["FileId(1) 29..30 Other Read"]); } #[test] fn test_find_all_refs_for_fn_param() { - let code = r#" - fn foo(i<|> : u32) -> u32 { - i - }"#; - - let refs = get_all_refs(code); - check_result(refs, "i BIND_PAT FileId(1) 12..13 Other", &["FileId(1) 38..39 Other Read"]); + let refs = get_all_refs( + r#" +fn foo(i<|> : u32) -> u32 { + i +} +"#, + ); + check_result(refs, "i BIND_PAT FileId(1) 7..8 Other", &["FileId(1) 29..30 Other Read"]); } #[test] fn test_find_all_refs_field_name() { - let code = r#" - //- /lib.rs - struct Foo { - pub spam<|>: u32, - } - - fn main(s: Foo) { - let f = s.spam; - } - "#; + let refs = get_all_refs( + r#" +//- /lib.rs +struct Foo { + pub spam<|>: u32, +} - let refs = get_all_refs(code); +fn main(s: Foo) { + let f = s.spam; +} +"#, + ); check_result( refs, - "spam RECORD_FIELD_DEF FileId(1) 66..79 70..74 Other", - &["FileId(1) 152..156 Other Read"], + "spam RECORD_FIELD_DEF FileId(1) 17..30 21..25 Other", + &["FileId(1) 67..71 Other Read"], ); } #[test] fn test_find_all_refs_impl_item_name() { - let code = r#" - //- /lib.rs - struct Foo; - impl Foo { - fn f<|>(&self) { } - } - "#; - - let refs = get_all_refs(code); - check_result(refs, "f FN_DEF FileId(1) 88..104 91..92 Other", &[]); + let refs = get_all_refs( + r#" +struct Foo; +impl Foo { + fn f<|>(&self) { } +} +"#, + ); + check_result(refs, "f FN_DEF FileId(1) 27..43 30..31 Other", &[]); } #[test] fn test_find_all_refs_enum_var_name() { - let code = r#" - //- /lib.rs - enum Foo { - A, - B<|>, - C, - } - "#; - - let refs = get_all_refs(code); - check_result(refs, "B ENUM_VARIANT FileId(1) 83..84 83..84 Other", &[]); + let refs = get_all_refs( + r#" +enum Foo { + A, + B<|>, + C, +} +"#, + ); + check_result(refs, "B ENUM_VARIANT FileId(1) 22..23 22..23 Other", &[]); } #[test] fn test_find_all_refs_two_modules() { - let code = r#" - //- /lib.rs - pub mod foo; - pub mod bar; - - fn f() { - let i = foo::Foo { n: 5 }; - } - - //- /foo.rs - use crate::bar; + let (analysis, pos) = analysis_and_position( + r#" +//- /lib.rs +pub mod foo; +pub mod bar; + +fn f() { + let i = foo::Foo { n: 5 }; +} - pub struct Foo { - pub n: u32, - } +//- /foo.rs +use crate::bar; - fn f() { - let i = bar::Bar { n: 5 }; - } +pub struct Foo { + pub n: u32, +} - //- /bar.rs - use crate::foo; +fn f() { + let i = bar::Bar { n: 5 }; +} - pub struct Bar { - pub n: u32, - } +//- /bar.rs +use crate::foo; - fn f() { - let i = foo::Foo<|> { n: 5 }; - } - "#; +pub struct Bar { + pub n: u32, +} - let (analysis, pos) = analysis_and_position(code); +fn f() { + let i = foo::Foo<|> { n: 5 }; +} +"#, + ); let refs = analysis.find_all_refs(pos, None).unwrap().unwrap(); check_result( refs, @@ -437,48 +442,48 @@ mod tests { // which is the whole `foo.rs`, and the second one is in `use foo::Foo`. #[test] fn test_find_all_refs_decl_module() { - let code = r#" - //- /lib.rs - mod foo<|>; + let (analysis, pos) = analysis_and_position( + r#" +//- /lib.rs +mod foo<|>; - use foo::Foo; +use foo::Foo; - fn f() { - let i = Foo { n: 5 }; - } - - //- /foo.rs - pub struct Foo { - pub n: u32, - } - "#; +fn f() { + let i = Foo { n: 5 }; +} - let (analysis, pos) = analysis_and_position(code); +//- /foo.rs +pub struct Foo { + pub n: u32, +} +"#, + ); let refs = analysis.find_all_refs(pos, None).unwrap().unwrap(); check_result(refs, "foo SOURCE_FILE FileId(2) 0..35 Other", &["FileId(1) 14..17 Other"]); } #[test] fn test_find_all_refs_super_mod_vis() { - let code = r#" - //- /lib.rs - mod foo; - - //- /foo.rs - mod some; - use some::Foo; + let (analysis, pos) = analysis_and_position( + r#" +//- /lib.rs +mod foo; - fn f() { - let i = Foo { n: 5 }; - } +//- /foo.rs +mod some; +use some::Foo; - //- /foo/some.rs - pub(super) struct Foo<|> { - pub n: u32, - } - "#; +fn f() { + let i = Foo { n: 5 }; +} - let (analysis, pos) = analysis_and_position(code); +//- /foo/some.rs +pub(super) struct Foo<|> { + pub n: u32, +} +"#, + ); let refs = analysis.find_all_refs(pos, None).unwrap().unwrap(); check_result( refs, @@ -525,93 +530,98 @@ mod tests { #[test] fn test_find_all_refs_macro_def() { - let code = r#" - #[macro_export] - macro_rules! m1<|> { () => (()) } - - fn foo() { - m1(); - m1(); - }"#; - - let refs = get_all_refs(code); + let refs = get_all_refs( + r#" +#[macro_export] +macro_rules! m1<|> { () => (()) } + +fn foo() { + m1(); + m1(); +} +"#, + ); check_result( refs, - "m1 MACRO_CALL FileId(1) 9..63 46..48 Other", - &["FileId(1) 96..98 StructLiteral", "FileId(1) 114..116 StructLiteral"], + "m1 MACRO_CALL FileId(1) 0..46 29..31 Other", + &["FileId(1) 63..65 StructLiteral", "FileId(1) 73..75 StructLiteral"], ); } #[test] fn test_basic_highlight_read_write() { - let code = r#" - fn foo() { - let mut i<|> = 0; - i = i + 1; - }"#; - - let refs = get_all_refs(code); + let refs = get_all_refs( + r#" +fn foo() { + let mut i<|> = 0; + i = i + 1; +} +"#, + ); check_result( refs, - "i BIND_PAT FileId(1) 40..41 Other Write", - &["FileId(1) 59..60 Other Write", "FileId(1) 63..64 Other Read"], + "i BIND_PAT FileId(1) 23..24 Other Write", + &["FileId(1) 34..35 Other Write", "FileId(1) 38..39 Other Read"], ); } #[test] fn test_basic_highlight_field_read_write() { - let code = r#" - struct S { - f: u32, - } - - fn foo() { - let mut s = S{f: 0}; - s.f<|> = 0; - }"#; + let refs = get_all_refs( + r#" +struct S { + f: u32, +} - let refs = get_all_refs(code); +fn foo() { + let mut s = S{f: 0}; + s.f<|> = 0; +} +"#, + ); check_result( refs, - "f RECORD_FIELD_DEF FileId(1) 32..38 32..33 Other", - &["FileId(1) 96..97 Other Read", "FileId(1) 117..118 Other Write"], + "f RECORD_FIELD_DEF FileId(1) 15..21 15..16 Other", + &["FileId(1) 55..56 Other Read", "FileId(1) 68..69 Other Write"], ); } #[test] fn test_basic_highlight_decl_no_write() { - let code = r#" - fn foo() { - let i<|>; - i = 1; - }"#; - - let refs = get_all_refs(code); - check_result(refs, "i BIND_PAT FileId(1) 36..37 Other", &["FileId(1) 51..52 Other Write"]); + let refs = get_all_refs( + r#" +fn foo() { + let i<|>; + i = 1; +} +"#, + ); + check_result(refs, "i BIND_PAT FileId(1) 19..20 Other", &["FileId(1) 26..27 Other Write"]); } #[test] fn test_find_struct_function_refs_outside_module() { - let code = r#" - mod foo { - pub struct Foo; + let refs = get_all_refs( + r#" +mod foo { + pub struct Foo; - impl Foo { - pub fn new<|>() -> Foo { - Foo - } - } + impl Foo { + pub fn new<|>() -> Foo { + Foo } + } +} - fn main() { - let _f = foo::Foo::new(); - }"#; - - let refs = get_all_refs(code); +fn main() { + let _f = foo::Foo::new(); +} +"#, + ); check_result( refs, - "new FN_DEF FileId(1) 87..150 94..97 Other", - &["FileId(1) 227..230 StructLiteral"], + "new FN_DEF FileId(1) 54..101 61..64 Other", + &["FileId(1) 146..149 StructLiteral"], ); } @@ -642,8 +652,8 @@ mod tests { ); } - fn get_all_refs(text: &str) -> ReferenceSearchResult { - let (analysis, position) = single_file_with_position(text); + fn get_all_refs(ra_fixture: &str) -> ReferenceSearchResult { + let (analysis, position) = single_file_with_position(ra_fixture); analysis.find_all_refs(position, None).unwrap().unwrap() } -- cgit v1.2.3