Skip to content

Commit be265ec

Browse files
committed
Add example expect test for goto definition
1 parent 03c5a66 commit be265ec

File tree

3 files changed

+35
-8
lines changed

3 files changed

+35
-8
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/ra_ide/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ ra_cfg = { path = "../ra_cfg" }
2828
ra_fmt = { path = "../ra_fmt" }
2929
ra_prof = { path = "../ra_prof" }
3030
test_utils = { path = "../test_utils" }
31+
expect = { path = "../expect" }
3132
ra_assists = { path = "../ra_assists" }
3233
ra_ssr = { path = "../ra_ssr" }
3334

crates/ra_ide/src/goto_definition.rs

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ pub(crate) fn reference_definition(
103103

104104
#[cfg(test)]
105105
mod tests {
106+
use expect::{expect, Expect};
106107
use test_utils::assert_eq_text;
107108

108109
use crate::mock_analysis::analysis_and_position;
@@ -142,16 +143,40 @@ mod tests {
142143
nav.assert_match(expected);
143144
}
144145

146+
fn check(ra_fixture: &str, expect: Expect) {
147+
let (analysis, pos) = analysis_and_position(ra_fixture);
148+
149+
let mut navs = analysis.goto_definition(pos).unwrap().unwrap().info;
150+
if navs.len() == 0 {
151+
panic!("unresolved reference")
152+
}
153+
assert_eq!(navs.len(), 1);
154+
155+
let nav = navs.pop().unwrap();
156+
let file_text = analysis.file_text(nav.file_id()).unwrap();
157+
158+
let mut actual = nav.debug_render();
159+
actual += "\n";
160+
actual += &file_text[nav.full_range()].to_string();
161+
if let Some(focus) = nav.focus_range() {
162+
actual += "|";
163+
actual += &file_text[focus];
164+
actual += "\n";
165+
}
166+
expect.assert_eq(&actual);
167+
}
168+
145169
#[test]
146170
fn goto_def_in_items() {
147-
check_goto(
148-
"
149-
//- /lib.rs
150-
struct Foo;
151-
enum E { X(Foo<|>) }
152-
",
153-
"Foo STRUCT_DEF FileId(1) 0..11 7..10",
154-
"struct Foo;|Foo",
171+
check(
172+
r#"
173+
struct Foo;
174+
enum E { X(Foo<|>) }
175+
"#,
176+
expect![[r#"
177+
Foo STRUCT_DEF FileId(1) 0..11 7..10
178+
struct Foo;|Foo
179+
"#]],
155180
);
156181
}
157182

0 commit comments

Comments
 (0)