From 46a299bceece6f8633d7c1518939efbb3a57fae3 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 18 Dec 2019 15:33:36 +0100 Subject: Refactor goto tests to always specify texts --- crates/ra_ide/src/goto_definition.rs | 124 +++++++++++++++++++++-------------- crates/test_utils/src/lib.rs | 4 +- 2 files changed, 76 insertions(+), 52 deletions(-) (limited to 'crates') diff --git a/crates/ra_ide/src/goto_definition.rs b/crates/ra_ide/src/goto_definition.rs index 3b4d89e3e..48757f170 100644 --- a/crates/ra_ide/src/goto_definition.rs +++ b/crates/ra_ide/src/goto_definition.rs @@ -225,30 +225,35 @@ mod tests { use crate::mock_analysis::analysis_and_position; - fn check_goto(fixture: &str, expected: &str) { + fn check_goto(fixture: &str, expected: &str, expected_range: &str) { let (analysis, pos) = analysis_and_position(fixture); let mut navs = analysis.goto_definition(pos).unwrap().unwrap().info; assert_eq!(navs.len(), 1); - let nav = navs.pop().unwrap(); - nav.assert_match(expected); - } - - fn check_goto_with_range_content(fixture: &str, expected: &str, expected_range: &str) { - let (analysis, pos) = analysis_and_position(fixture); - let mut navs = analysis.goto_definition(pos).unwrap().unwrap().info; - assert_eq!(navs.len(), 1); let nav = navs.pop().unwrap(); - let file_text = analysis.file_text(pos.file_id).unwrap(); + let file_text = analysis.file_text(nav.file_id()).unwrap(); - let actual_full_range = &file_text[nav.full_range()]; - let actual_range = &file_text[nav.range()]; + let mut actual = file_text[nav.full_range()].to_string(); + if let Some(focus) = nav.focus_range() { + actual += "|"; + actual += &file_text[focus]; + } + + if !expected_range.contains("...") { + test_utils::assert_eq_text!(&actual, expected_range); + } else { + let mut parts = expected_range.split("..."); + let prefix = parts.next().unwrap(); + let suffix = parts.next().unwrap(); + assert!( + actual.starts_with(prefix) && actual.ends_with(suffix), + "\nExpected: {}\n Actual: {}\n", + expected_range, + actual + ); + } - test_utils::assert_eq_text!( - &format!("{}|{}", actual_full_range, actual_range), - expected_range - ); nav.assert_match(expected); } @@ -261,6 +266,7 @@ mod tests { enum E { X(Foo<|>) } ", "Foo STRUCT_DEF FileId(1) [0; 11) [7; 10)", + "struct Foo;|Foo", ); } @@ -273,6 +279,7 @@ mod tests { enum E { X(<|>Foo) } ", "Foo STRUCT_DEF FileId(1) [0; 11) [7; 10)", + "struct Foo;|Foo", ); } @@ -293,6 +300,7 @@ mod tests { struct Foo; ", "Foo STRUCT_DEF FileId(2) [0; 11) [7; 10)", + "struct Foo;|Foo", ); } @@ -307,6 +315,7 @@ mod tests { // empty ", "foo SOURCE_FILE FileId(2) [0; 10)", + "// empty\n\n", ); check_goto( @@ -318,6 +327,7 @@ mod tests { // empty ", "foo SOURCE_FILE FileId(2) [0; 10)", + "// empty\n\n", ); } @@ -327,17 +337,14 @@ mod tests { check_goto( " //- /lib.rs - macro_rules! foo { - () => { - {} - }; - } + macro_rules! foo { () => { () } } fn bar() { <|>foo!(); } ", - "foo MACRO_CALL FileId(1) [0; 50) [13; 16)", + "foo MACRO_CALL FileId(1) [0; 33) [13; 16)", + "macro_rules! foo { () => { () } }|foo", ); } @@ -354,13 +361,10 @@ mod tests { //- /foo/lib.rs #[macro_export] - macro_rules! foo { - () => { - {} - }; - } + macro_rules! foo { () => { () } } ", - "foo MACRO_CALL FileId(2) [0; 66) [29; 32)", + "foo MACRO_CALL FileId(2) [0; 49) [29; 32)", + "#[macro_export]\nmacro_rules! foo { () => { () } }|foo", ); } @@ -373,19 +377,16 @@ mod tests { //- /foo/lib.rs #[macro_export] - macro_rules! foo { - () => { - {} - }; - } + macro_rules! foo { () => { () } } ", - "foo MACRO_CALL FileId(2) [0; 66) [29; 32)", + "foo MACRO_CALL FileId(2) [0; 49) [29; 32)", + "#[macro_export]\nmacro_rules! foo { () => { () } }|foo", ); } #[test] fn goto_definition_works_for_macro_defined_fn_with_arg() { - check_goto_with_range_content( + check_goto( " //- /lib.rs macro_rules! define_fn { @@ -405,7 +406,7 @@ mod tests { #[test] fn goto_definition_works_for_macro_defined_fn_no_arg() { - check_goto_with_range_content( + check_goto( " //- /lib.rs macro_rules! define_fn { @@ -431,14 +432,15 @@ mod tests { //- /lib.rs struct Foo; impl Foo { - fn frobnicate(&self) { } + fn frobnicate(&self) { } } fn bar(foo: &Foo) { foo.frobnicate<|>(); } ", - "frobnicate FN_DEF FileId(1) [27; 52) [30; 40)", + "frobnicate FN_DEF FileId(1) [27; 51) [30; 40)", + "fn frobnicate(&self) { }|frobnicate", ); } @@ -457,6 +459,7 @@ mod tests { } ", "spam RECORD_FIELD_DEF FileId(1) [17; 26) [17; 21)", + "spam: u32|spam", ); } @@ -477,6 +480,7 @@ mod tests { } ", "spam RECORD_FIELD_DEF FileId(1) [17; 26) [17; 21)", + "spam: u32|spam", ); } @@ -493,6 +497,7 @@ mod tests { } ", "TUPLE_FIELD_DEF FileId(1) [11; 14)", + "u32", ); } @@ -503,14 +508,15 @@ mod tests { //- /lib.rs struct Foo; impl Foo { - fn frobnicate() { } + fn frobnicate() { } } fn bar(foo: &Foo) { Foo::frobnicate<|>(); } ", - "frobnicate FN_DEF FileId(1) [27; 47) [30; 40)", + "frobnicate FN_DEF FileId(1) [27; 46) [30; 40)", + "fn frobnicate() { }|frobnicate", ); } @@ -528,6 +534,7 @@ mod tests { } ", "frobnicate FN_DEF FileId(1) [16; 32) [19; 29)", + "fn frobnicate();|frobnicate", ); } @@ -547,6 +554,7 @@ mod tests { } ", "frobnicate FN_DEF FileId(1) [30; 46) [33; 43)", + "fn frobnicate();|frobnicate", ); } @@ -563,6 +571,7 @@ mod tests { } ", "impl IMPL_BLOCK FileId(1) [12; 73)", + "impl Foo {...}", ); check_goto( @@ -576,6 +585,7 @@ mod tests { } ", "impl IMPL_BLOCK FileId(1) [12; 73)", + "impl Foo {...}", ); check_goto( @@ -589,6 +599,7 @@ mod tests { } ", "impl IMPL_BLOCK FileId(1) [15; 75)", + "impl Foo {...}", ); check_goto( @@ -601,6 +612,7 @@ mod tests { } ", "impl IMPL_BLOCK FileId(1) [15; 62)", + "impl Foo {...}", ); } @@ -620,6 +632,7 @@ mod tests { } ", "impl IMPL_BLOCK FileId(1) [49; 115)", + "impl Make for Foo {...}", ); check_goto( @@ -636,6 +649,7 @@ mod tests { } ", "impl IMPL_BLOCK FileId(1) [49; 115)", + "impl Make for Foo {...}", ); } @@ -647,6 +661,7 @@ mod tests { struct Foo<|> { value: u32 } ", "Foo STRUCT_DEF FileId(1) [0; 25) [7; 10)", + "struct Foo { value: u32 }|Foo", ); check_goto( @@ -657,15 +672,16 @@ mod tests { } "#, "field RECORD_FIELD_DEF FileId(1) [17; 30) [17; 22)", + "field: string|field", ); check_goto( " //- /lib.rs - fn foo_test<|>() { - } + fn foo_test<|>() { } ", "foo_test FN_DEF FileId(1) [0; 17) [3; 11)", + "fn foo_test() { }|foo_test", ); check_goto( @@ -676,6 +692,7 @@ mod tests { } ", "Foo ENUM_DEF FileId(1) [0; 25) [5; 8)", + "enum Foo {...}|Foo", ); check_goto( @@ -688,22 +705,25 @@ mod tests { } ", "Variant2 ENUM_VARIANT FileId(1) [29; 37) [29; 37)", + "Variant2|Variant2", ); check_goto( r#" //- /lib.rs - static inner<|>: &str = ""; + static INNER<|>: &str = ""; "#, - "inner STATIC_DEF FileId(1) [0; 24) [7; 12)", + "INNER STATIC_DEF FileId(1) [0; 24) [7; 12)", + "static INNER: &str = \"\";|INNER", ); check_goto( r#" //- /lib.rs - const inner<|>: &str = ""; + const INNER<|>: &str = ""; "#, - "inner CONST_DEF FileId(1) [0; 23) [6; 11)", + "INNER CONST_DEF FileId(1) [0; 23) [6; 11)", + "const INNER: &str = \"\";|INNER", ); check_goto( @@ -712,24 +732,25 @@ mod tests { type Thing<|> = Option<()>; "#, "Thing TYPE_ALIAS_DEF FileId(1) [0; 24) [5; 10)", + "type Thing = Option<()>;|Thing", ); check_goto( r#" //- /lib.rs - trait Foo<|> { - } + trait Foo<|> { } "#, "Foo TRAIT_DEF FileId(1) [0; 13) [6; 9)", + "trait Foo { }|Foo", ); check_goto( r#" //- /lib.rs - mod bar<|> { - } + mod bar<|> { } "#, "bar MODULE FileId(1) [0; 11) [4; 7)", + "mod bar { }|bar", ); } @@ -750,6 +771,7 @@ mod tests { mod confuse_index { fn foo(); } ", "foo FN_DEF FileId(1) [52; 63) [55; 58)", + "fn foo() {}|foo", ); } @@ -778,6 +800,7 @@ mod tests { } ", "foo FN_DEF FileId(1) [398; 415) [401; 404)", + "fn foo() -> i8 {}|foo", ); } @@ -791,6 +814,7 @@ mod tests { } ", "T TYPE_PARAM FileId(1) [11; 12)", + "T", ); } } diff --git a/crates/test_utils/src/lib.rs b/crates/test_utils/src/lib.rs index 657ddf2a6..659f77b71 100644 --- a/crates/test_utils/src/lib.rs +++ b/crates/test_utils/src/lib.rs @@ -207,8 +207,8 @@ pub fn lines_match(expected: &str, actual: &str) -> bool { // Let's not deal with / vs \ (windows...) // First replace backslash-escaped backslashes with forward slashes // which can occur in, for example, JSON output - let expected = expected.replace("\\\\", "/").replace("\\", "/"); - let mut actual: &str = &actual.replace("\\\\", "/").replace("\\", "/"); + let expected = expected.replace(r"\\", "/").replace(r"\", "/"); + let mut actual: &str = &actual.replace(r"\\", "/").replace(r"\", "/"); for (i, part) in expected.split("[..]").enumerate() { match actual.find(part) { Some(j) => { -- cgit v1.2.3