From be265ece02f8925457b328abeeba952645867fbd Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 27 Jun 2020 18:21:26 +0200 Subject: Add example expect test for goto definition --- crates/ra_ide/Cargo.toml | 1 + crates/ra_ide/src/goto_definition.rs | 41 +++++++++++++++++++++++++++++------- 2 files changed, 34 insertions(+), 8 deletions(-) (limited to 'crates/ra_ide') diff --git a/crates/ra_ide/Cargo.toml b/crates/ra_ide/Cargo.toml index bbc6a5c9b..8e8892309 100644 --- a/crates/ra_ide/Cargo.toml +++ b/crates/ra_ide/Cargo.toml @@ -28,6 +28,7 @@ ra_cfg = { path = "../ra_cfg" } ra_fmt = { path = "../ra_fmt" } ra_prof = { path = "../ra_prof" } test_utils = { path = "../test_utils" } +expect = { path = "../expect" } ra_assists = { path = "../ra_assists" } ra_ssr = { path = "../ra_ssr" } diff --git a/crates/ra_ide/src/goto_definition.rs b/crates/ra_ide/src/goto_definition.rs index bea7fbfa7..969d5e0ff 100644 --- a/crates/ra_ide/src/goto_definition.rs +++ b/crates/ra_ide/src/goto_definition.rs @@ -103,6 +103,7 @@ pub(crate) fn reference_definition( #[cfg(test)] mod tests { + use expect::{expect, Expect}; use test_utils::assert_eq_text; use crate::mock_analysis::analysis_and_position; @@ -142,16 +143,40 @@ mod tests { nav.assert_match(expected); } + fn check(ra_fixture: &str, expect: Expect) { + let (analysis, pos) = analysis_and_position(ra_fixture); + + let mut navs = analysis.goto_definition(pos).unwrap().unwrap().info; + if navs.len() == 0 { + panic!("unresolved reference") + } + assert_eq!(navs.len(), 1); + + let nav = navs.pop().unwrap(); + let file_text = analysis.file_text(nav.file_id()).unwrap(); + + let mut actual = nav.debug_render(); + actual += "\n"; + actual += &file_text[nav.full_range()].to_string(); + if let Some(focus) = nav.focus_range() { + actual += "|"; + actual += &file_text[focus]; + actual += "\n"; + } + expect.assert_eq(&actual); + } + #[test] fn goto_def_in_items() { - check_goto( - " - //- /lib.rs - struct Foo; - enum E { X(Foo<|>) } - ", - "Foo STRUCT_DEF FileId(1) 0..11 7..10", - "struct Foo;|Foo", + check( + r#" +struct Foo; +enum E { X(Foo<|>) } +"#, + expect![[r#" + Foo STRUCT_DEF FileId(1) 0..11 7..10 + struct Foo;|Foo + "#]], ); } -- cgit v1.2.3