From 7377120fee84743ad980ed87458bb08dd97f3101 Mon Sep 17 00:00:00 2001 From: hamidreza kalbasi Date: Sun, 26 Sep 2021 12:47:57 +0330 Subject: [PATCH] add some tests --- crates/ide/src/fixture.rs | 19 ++++++++ crates/ide/src/static_index.rs | 80 ++++++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+) diff --git a/crates/ide/src/fixture.rs b/crates/ide/src/fixture.rs index 700f4dc9550..2ea6f6a9ab1 100644 --- a/crates/ide/src/fixture.rs +++ b/crates/ide/src/fixture.rs @@ -66,3 +66,22 @@ pub(crate) fn annotations(ra_fixture: &str) -> (Analysis, FilePosition, Vec<(Fil .collect(); (host.analysis(), FilePosition { file_id, offset }, annotations) } + +/// Creates analysis from a multi-file fixture with annonations without $0 +pub(crate) fn annotations_without_marker(ra_fixture: &str) -> (Analysis, Vec<(FileRange, String)>) { + let mut host = AnalysisHost::default(); + let change_fixture = ChangeFixture::parse(ra_fixture); + host.db.set_enable_proc_attr_macros(true); + host.db.apply_change(change_fixture.change); + + let annotations = change_fixture + .files + .iter() + .flat_map(|&file_id| { + let file_text = host.analysis().file_text(file_id).unwrap(); + let annotations = extract_annotations(&file_text); + annotations.into_iter().map(move |(range, data)| (FileRange { file_id, range }, data)) + }) + .collect(); + (host.analysis(), annotations) +} diff --git a/crates/ide/src/static_index.rs b/crates/ide/src/static_index.rs index 55a6710fcf4..d467e794cec 100644 --- a/crates/ide/src/static_index.rs +++ b/crates/ide/src/static_index.rs @@ -179,3 +179,83 @@ fn get_definition(sema: &Semantics, token: SyntaxToken) -> Option< } None } + +#[cfg(test)] +mod tests { + use crate::{fixture, StaticIndex}; + use ide_db::base_db::FileRange; + use std::collections::HashSet; + + fn check_all_ranges(ra_fixture: &str) { + let (analysis, ranges) = fixture::annotations_without_marker(ra_fixture); + let s = StaticIndex::compute(&*analysis.db, &analysis).unwrap(); + let mut range_set: HashSet<_> = ranges.iter().map(|x| x.0).collect(); + for f in s.files { + for (range, _) in f.tokens { + let x = FileRange { file_id: f.file_id, range }; + if !range_set.contains(&x) { + panic!("additional range {:?}", x); + } + range_set.remove(&x); + } + } + if !range_set.is_empty() { + panic!("unfound ranges {:?}", range_set); + } + } + + fn check_definitions(ra_fixture: &str) { + let (analysis, ranges) = fixture::annotations_without_marker(ra_fixture); + let s = StaticIndex::compute(&*analysis.db, &analysis).unwrap(); + let mut range_set: HashSet<_> = ranges.iter().map(|x| x.0).collect(); + for (_, t) in s.tokens.iter() { + if let Some(x) = t.definition { + if !range_set.contains(&x) { + panic!("additional definition {:?}", x); + } + range_set.remove(&x); + } + } + if !range_set.is_empty() { + panic!("unfound definitions {:?}", range_set); + } + } + + #[test] + fn struct_and_enum() { + check_all_ranges( + r#" +struct Foo; + //^^^ +enum E { X(Foo) } + //^ ^ ^^^ +"#, + ); + check_definitions( + r#" +struct Foo; + //^^^ +enum E { X(Foo) } + //^ ^ +"#, + ); + } + + #[test] + fn derives() { + check_all_ranges( + r#" +#[rustc_builtin_macro] +pub macro Copy {} + //^^^^ +#[rustc_builtin_macro] +pub macro derive {} + //^^^^^^ +#[derive(Copy)] +//^^^^^^ ^^^^ +struct Hello(i32); + //^^^^^ ^^^ +"#, + ); + } +}