From be265ece02f8925457b328abeeba952645867fbd Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 27 Jun 2020 18:21:26 +0200 Subject: [PATCH] Add example expect test for goto definition --- Cargo.lock | 1 + crates/ra_ide/Cargo.toml | 1 + crates/ra_ide/src/goto_definition.rs | 41 ++++++++++++++++++++++------ 3 files changed, 35 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a3cfe5dc4b3..de7337be136 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1130,6 +1130,7 @@ name = "ra_ide" version = "0.1.0" dependencies = [ "either", + "expect", "indexmap", "insta", "itertools", diff --git a/crates/ra_ide/Cargo.toml b/crates/ra_ide/Cargo.toml index bbc6a5c9b8a..8e889230939 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 bea7fbfa77f..969d5e0ffc7 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 + "#]], ); }