about summary refs log tree commit diff
diff options
context:
space:
mode:
authorhamidreza kalbasi <hamidrezakalbasi@protonmail.com>2021-09-26 12:47:57 +0330
committerhamidreza kalbasi <hamidrezakalbasi@protonmail.com>2021-09-26 12:48:53 +0330
commit7377120fee84743ad980ed87458bb08dd97f3101 (patch)
treeac792b0c5e561aa2374f6453c5c15b0aa1ea008a
parent13d36e96c2e6ff0bb2b45b470f15fc96fcc67bbb (diff)
downloadrust-7377120fee84743ad980ed87458bb08dd97f3101.tar.gz
rust-7377120fee84743ad980ed87458bb08dd97f3101.zip
add some tests
-rw-r--r--crates/ide/src/fixture.rs19
-rw-r--r--crates/ide/src/static_index.rs80
2 files changed, 99 insertions, 0 deletions
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<RootDatabase>, 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);
+     //^^^^^ ^^^
+"#,
+        );
+    }
+}