Skip to content

Commit 7377120

Browse files
committed
add some tests
1 parent 13d36e9 commit 7377120

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed

crates/ide/src/fixture.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,22 @@ pub(crate) fn annotations(ra_fixture: &str) -> (Analysis, FilePosition, Vec<(Fil
6666
.collect();
6767
(host.analysis(), FilePosition { file_id, offset }, annotations)
6868
}
69+
70+
/// Creates analysis from a multi-file fixture with annonations without $0
71+
pub(crate) fn annotations_without_marker(ra_fixture: &str) -> (Analysis, Vec<(FileRange, String)>) {
72+
let mut host = AnalysisHost::default();
73+
let change_fixture = ChangeFixture::parse(ra_fixture);
74+
host.db.set_enable_proc_attr_macros(true);
75+
host.db.apply_change(change_fixture.change);
76+
77+
let annotations = change_fixture
78+
.files
79+
.iter()
80+
.flat_map(|&file_id| {
81+
let file_text = host.analysis().file_text(file_id).unwrap();
82+
let annotations = extract_annotations(&file_text);
83+
annotations.into_iter().map(move |(range, data)| (FileRange { file_id, range }, data))
84+
})
85+
.collect();
86+
(host.analysis(), annotations)
87+
}

crates/ide/src/static_index.rs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,3 +179,83 @@ fn get_definition(sema: &Semantics<RootDatabase>, token: SyntaxToken) -> Option<
179179
}
180180
None
181181
}
182+
183+
#[cfg(test)]
184+
mod tests {
185+
use crate::{fixture, StaticIndex};
186+
use ide_db::base_db::FileRange;
187+
use std::collections::HashSet;
188+
189+
fn check_all_ranges(ra_fixture: &str) {
190+
let (analysis, ranges) = fixture::annotations_without_marker(ra_fixture);
191+
let s = StaticIndex::compute(&*analysis.db, &analysis).unwrap();
192+
let mut range_set: HashSet<_> = ranges.iter().map(|x| x.0).collect();
193+
for f in s.files {
194+
for (range, _) in f.tokens {
195+
let x = FileRange { file_id: f.file_id, range };
196+
if !range_set.contains(&x) {
197+
panic!("additional range {:?}", x);
198+
}
199+
range_set.remove(&x);
200+
}
201+
}
202+
if !range_set.is_empty() {
203+
panic!("unfound ranges {:?}", range_set);
204+
}
205+
}
206+
207+
fn check_definitions(ra_fixture: &str) {
208+
let (analysis, ranges) = fixture::annotations_without_marker(ra_fixture);
209+
let s = StaticIndex::compute(&*analysis.db, &analysis).unwrap();
210+
let mut range_set: HashSet<_> = ranges.iter().map(|x| x.0).collect();
211+
for (_, t) in s.tokens.iter() {
212+
if let Some(x) = t.definition {
213+
if !range_set.contains(&x) {
214+
panic!("additional definition {:?}", x);
215+
}
216+
range_set.remove(&x);
217+
}
218+
}
219+
if !range_set.is_empty() {
220+
panic!("unfound definitions {:?}", range_set);
221+
}
222+
}
223+
224+
#[test]
225+
fn struct_and_enum() {
226+
check_all_ranges(
227+
r#"
228+
struct Foo;
229+
//^^^
230+
enum E { X(Foo) }
231+
//^ ^ ^^^
232+
"#,
233+
);
234+
check_definitions(
235+
r#"
236+
struct Foo;
237+
//^^^
238+
enum E { X(Foo) }
239+
//^ ^
240+
"#,
241+
);
242+
}
243+
244+
#[test]
245+
fn derives() {
246+
check_all_ranges(
247+
r#"
248+
#[rustc_builtin_macro]
249+
pub macro Copy {}
250+
//^^^^
251+
#[rustc_builtin_macro]
252+
pub macro derive {}
253+
//^^^^^^
254+
#[derive(Copy)]
255+
//^^^^^^ ^^^^
256+
struct Hello(i32);
257+
//^^^^^ ^^^
258+
"#,
259+
);
260+
}
261+
}

0 commit comments

Comments
 (0)