Skip to content

Commit 3263c70

Browse files
bors[bot]kjeremy
andauthored
Merge #2772
2772: Actually test references r=kjeremy a=kjeremy This will be a little more work when `ReferenceSearchResults` change but I think it's easier to maintain in the end. It also follows a similar pattern to navigation targets and call hierarchy. Co-authored-by: kjeremy <kjeremy@gmail.com> Co-authored-by: Jeremy Kolb <kjeremy@gmail.com>
2 parents 0d6e5a9 + 6fc80ef commit 3263c70

File tree

2 files changed

+99
-15
lines changed

2 files changed

+99
-15
lines changed

crates/ra_ide/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ pub use crate::{
7575
inlay_hints::{InlayHint, InlayKind},
7676
line_index::{LineCol, LineIndex},
7777
line_index_utils::translate_offset_with_edit,
78-
references::{ReferenceSearchResult, SearchScope},
78+
references::{Reference, ReferenceKind, ReferenceSearchResult, SearchScope},
7979
runnables::{Runnable, RunnableKind},
8080
source_change::{FileSystemEdit, SourceChange, SourceFileEdit},
8181
syntax_highlighting::HighlightedRange,

crates/ra_ide/src/references.rs

Lines changed: 98 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ fn process_definition(
214214
mod tests {
215215
use crate::{
216216
mock_analysis::{analysis_and_position, single_file_with_position, MockAnalysis},
217-
ReferenceSearchResult, SearchScope,
217+
Reference, ReferenceKind, ReferenceSearchResult, SearchScope,
218218
};
219219

220220
#[test]
@@ -232,7 +232,12 @@ mod tests {
232232
}"#;
233233

234234
let refs = get_all_refs(code);
235-
assert_eq!(refs.len(), 2);
235+
check_result(
236+
refs,
237+
"Foo STRUCT_DEF FileId(1) [5; 39) [12; 15)",
238+
ReferenceKind::Other,
239+
&["FileId(1) [142; 145) StructLiteral"],
240+
);
236241
}
237242

238243
#[test]
@@ -251,7 +256,17 @@ mod tests {
251256
}"#;
252257

253258
let refs = get_all_refs(code);
254-
assert_eq!(refs.len(), 5);
259+
check_result(
260+
refs,
261+
"i BIND_PAT FileId(1) [33; 34)",
262+
ReferenceKind::Other,
263+
&[
264+
"FileId(1) [67; 68) Other",
265+
"FileId(1) [71; 72) Other",
266+
"FileId(1) [101; 102) Other",
267+
"FileId(1) [127; 128) Other",
268+
],
269+
);
255270
}
256271

257272
#[test]
@@ -262,7 +277,12 @@ mod tests {
262277
}"#;
263278

264279
let refs = get_all_refs(code);
265-
assert_eq!(refs.len(), 2);
280+
check_result(
281+
refs,
282+
"i BIND_PAT FileId(1) [12; 13)",
283+
ReferenceKind::Other,
284+
&["FileId(1) [38; 39) Other"],
285+
);
266286
}
267287

268288
#[test]
@@ -273,7 +293,12 @@ mod tests {
273293
}"#;
274294

275295
let refs = get_all_refs(code);
276-
assert_eq!(refs.len(), 2);
296+
check_result(
297+
refs,
298+
"i BIND_PAT FileId(1) [12; 13)",
299+
ReferenceKind::Other,
300+
&["FileId(1) [38; 39) Other"],
301+
);
277302
}
278303

279304
#[test]
@@ -290,7 +315,12 @@ mod tests {
290315
"#;
291316

292317
let refs = get_all_refs(code);
293-
assert_eq!(refs.len(), 2);
318+
check_result(
319+
refs,
320+
"spam RECORD_FIELD_DEF FileId(1) [66; 79) [70; 74)",
321+
ReferenceKind::Other,
322+
&["FileId(1) [152; 156) Other"],
323+
);
294324
}
295325

296326
#[test]
@@ -304,7 +334,7 @@ mod tests {
304334
"#;
305335

306336
let refs = get_all_refs(code);
307-
assert_eq!(refs.len(), 1);
337+
check_result(refs, "f FN_DEF FileId(1) [88; 104) [91; 92)", ReferenceKind::Other, &[]);
308338
}
309339

310340
#[test]
@@ -319,7 +349,7 @@ mod tests {
319349
"#;
320350

321351
let refs = get_all_refs(code);
322-
assert_eq!(refs.len(), 1);
352+
check_result(refs, "B ENUM_VARIANT FileId(1) [83; 84) [83; 84)", ReferenceKind::Other, &[]);
323353
}
324354

325355
#[test]
@@ -358,7 +388,12 @@ mod tests {
358388

359389
let (analysis, pos) = analysis_and_position(code);
360390
let refs = analysis.find_all_refs(pos, None).unwrap().unwrap();
361-
assert_eq!(refs.len(), 3);
391+
check_result(
392+
refs,
393+
"Foo STRUCT_DEF FileId(2) [16; 50) [27; 30)",
394+
ReferenceKind::Other,
395+
&["FileId(1) [52; 55) StructLiteral", "FileId(3) [77; 80) StructLiteral"],
396+
);
362397
}
363398

364399
// `mod foo;` is not in the results because `foo` is an `ast::Name`.
@@ -384,7 +419,12 @@ mod tests {
384419

385420
let (analysis, pos) = analysis_and_position(code);
386421
let refs = analysis.find_all_refs(pos, None).unwrap().unwrap();
387-
assert_eq!(refs.len(), 2);
422+
check_result(
423+
refs,
424+
"foo SOURCE_FILE FileId(2) [0; 35)",
425+
ReferenceKind::Other,
426+
&["FileId(1) [13; 16) Other"],
427+
);
388428
}
389429

390430
#[test]
@@ -409,7 +449,12 @@ mod tests {
409449

410450
let (analysis, pos) = analysis_and_position(code);
411451
let refs = analysis.find_all_refs(pos, None).unwrap().unwrap();
412-
assert_eq!(refs.len(), 3);
452+
check_result(
453+
refs,
454+
"Foo STRUCT_DEF FileId(3) [0; 41) [18; 21)",
455+
ReferenceKind::Other,
456+
&["FileId(2) [20; 23) Other", "FileId(2) [46; 49) StructLiteral"],
457+
);
413458
}
414459

415460
#[test]
@@ -433,11 +478,21 @@ mod tests {
433478
let analysis = mock.analysis();
434479

435480
let refs = analysis.find_all_refs(pos, None).unwrap().unwrap();
436-
assert_eq!(refs.len(), 3);
481+
check_result(
482+
refs,
483+
"quux FN_DEF FileId(1) [18; 34) [25; 29)",
484+
ReferenceKind::Other,
485+
&["FileId(2) [16; 20) Other", "FileId(3) [16; 20) Other"],
486+
);
437487

438488
let refs =
439489
analysis.find_all_refs(pos, Some(SearchScope::single_file(bar))).unwrap().unwrap();
440-
assert_eq!(refs.len(), 2);
490+
check_result(
491+
refs,
492+
"quux FN_DEF FileId(1) [18; 34) [25; 29)",
493+
ReferenceKind::Other,
494+
&["FileId(3) [16; 20) Other"],
495+
);
441496
}
442497

443498
#[test]
@@ -452,11 +507,40 @@ mod tests {
452507
}"#;
453508

454509
let refs = get_all_refs(code);
455-
assert_eq!(refs.len(), 3);
510+
check_result(
511+
refs,
512+
"m1 MACRO_CALL FileId(1) [9; 63) [46; 48)",
513+
ReferenceKind::Other,
514+
&["FileId(1) [96; 98) Other", "FileId(1) [114; 116) Other"],
515+
);
456516
}
457517

458518
fn get_all_refs(text: &str) -> ReferenceSearchResult {
459519
let (analysis, position) = single_file_with_position(text);
460520
analysis.find_all_refs(position, None).unwrap().unwrap()
461521
}
522+
523+
fn check_result(
524+
res: ReferenceSearchResult,
525+
expected_decl: &str,
526+
decl_kind: ReferenceKind,
527+
expected_refs: &[&str],
528+
) {
529+
res.declaration().assert_match(expected_decl);
530+
assert_eq!(res.declaration_kind, decl_kind);
531+
532+
assert_eq!(res.references.len(), expected_refs.len());
533+
res.references().iter().enumerate().for_each(|(i, r)| r.assert_match(expected_refs[i]));
534+
}
535+
536+
impl Reference {
537+
fn debug_render(&self) -> String {
538+
format!("{:?} {:?} {:?}", self.file_range.file_id, self.file_range.range, self.kind)
539+
}
540+
541+
fn assert_match(&self, expected: &str) {
542+
let actual = self.debug_render();
543+
test_utils::assert_eq_text!(expected.trim(), actual.trim(),);
544+
}
545+
}
462546
}

0 commit comments

Comments
 (0)