aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_analysis/tests
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2018-09-16 10:54:24 +0100
committerAleksey Kladov <[email protected]>2018-09-16 11:07:39 +0100
commitb5021411a84822cb3f1e3aeffad9550dd15bdeb6 (patch)
tree9dca564f8e51b298dced01c4ce669c756dce3142 /crates/ra_analysis/tests
parentba0bfeee12e19da40b5eabc8d0408639af10e96f (diff)
rename all things
Diffstat (limited to 'crates/ra_analysis/tests')
-rw-r--r--crates/ra_analysis/tests/tests.rs146
1 files changed, 146 insertions, 0 deletions
diff --git a/crates/ra_analysis/tests/tests.rs b/crates/ra_analysis/tests/tests.rs
new file mode 100644
index 000000000..334dc5e48
--- /dev/null
+++ b/crates/ra_analysis/tests/tests.rs
@@ -0,0 +1,146 @@
1extern crate relative_path;
2extern crate ra_analysis;
3extern crate test_utils;
4
5use std::{
6 sync::Arc,
7 collections::HashMap,
8};
9
10use relative_path::{RelativePath, RelativePathBuf};
11use ra_analysis::{Analysis, AnalysisHost, FileId, FileResolver, JobHandle, CrateGraph, CrateId};
12use test_utils::assert_eq_dbg;
13
14#[derive(Debug)]
15struct FileMap(Vec<(FileId, RelativePathBuf)>);
16
17impl FileMap {
18 fn iter<'a>(&'a self) -> impl Iterator<Item=(FileId, &'a RelativePath)> + 'a {
19 self.0.iter().map(|(id, path)| (*id, path.as_relative_path()))
20 }
21
22 fn path(&self, id: FileId) -> &RelativePath {
23 self.iter()
24 .find(|&(it, _)| it == id)
25 .unwrap()
26 .1
27 }
28}
29
30impl FileResolver for FileMap {
31 fn file_stem(&self, id: FileId) -> String {
32 self.path(id).file_stem().unwrap().to_string()
33 }
34 fn resolve(&self, id: FileId, rel: &RelativePath) -> Option<FileId> {
35 let path = self.path(id).join(rel).normalize();
36 let id = self.iter().find(|&(_, p)| path == p)?.0;
37 Some(id)
38 }
39}
40
41fn analysis_host(files: &'static [(&'static str, &'static str)]) -> AnalysisHost {
42 let mut host = AnalysisHost::new();
43 let mut file_map = Vec::new();
44 for (id, &(path, contents)) in files.iter().enumerate() {
45 let file_id = FileId((id + 1) as u32);
46 assert!(path.starts_with('/'));
47 let path = RelativePathBuf::from_path(&path[1..]).unwrap();
48 host.change_file(file_id, Some(contents.to_string()));
49 file_map.push((file_id, path));
50 }
51 host.set_file_resolver(Arc::new(FileMap(file_map)));
52 host
53}
54
55fn analysis(files: &'static [(&'static str, &'static str)]) -> Analysis {
56 analysis_host(files).analysis()
57}
58
59#[test]
60fn test_resolve_module() {
61 let snap = analysis(&[
62 ("/lib.rs", "mod foo;"),
63 ("/foo.rs", "")
64 ]);
65 let (_handle, token) = JobHandle::new();
66 let symbols = snap.approximately_resolve_symbol(FileId(1), 4.into(), &token);
67 assert_eq_dbg(
68 r#"[(FileId(2), FileSymbol { name: "foo", node_range: [0; 0), kind: MODULE })]"#,
69 &symbols,
70 );
71
72 let snap = analysis(&[
73 ("/lib.rs", "mod foo;"),
74 ("/foo/mod.rs", "")
75 ]);
76 let symbols = snap.approximately_resolve_symbol(FileId(1), 4.into(), &token);
77 assert_eq_dbg(
78 r#"[(FileId(2), FileSymbol { name: "foo", node_range: [0; 0), kind: MODULE })]"#,
79 &symbols,
80 );
81}
82
83#[test]
84fn test_unresolved_module_diagnostic() {
85 let snap = analysis(&[("/lib.rs", "mod foo;")]);
86 let diagnostics = snap.diagnostics(FileId(1));
87 assert_eq_dbg(
88 r#"[Diagnostic {
89 message: "unresolved module",
90 range: [4; 7),
91 fix: Some(SourceChange {
92 label: "create module",
93 source_file_edits: [],
94 file_system_edits: [CreateFile { anchor: FileId(1), path: "../foo.rs" }],
95 cursor_position: None }) }]"#,
96 &diagnostics,
97 );
98}
99
100#[test]
101fn test_unresolved_module_diagnostic_no_diag_for_inline_mode() {
102 let snap = analysis(&[("/lib.rs", "mod foo {}")]);
103 let diagnostics = snap.diagnostics(FileId(1));
104 assert_eq_dbg(
105 r#"[]"#,
106 &diagnostics,
107 );
108}
109
110#[test]
111fn test_resolve_parent_module() {
112 let snap = analysis(&[
113 ("/lib.rs", "mod foo;"),
114 ("/foo.rs", ""),
115 ]);
116 let symbols = snap.parent_module(FileId(2));
117 assert_eq_dbg(
118 r#"[(FileId(1), FileSymbol { name: "foo", node_range: [0; 8), kind: MODULE })]"#,
119 &symbols,
120 );
121}
122
123#[test]
124fn test_resolve_crate_root() {
125 let mut host = analysis_host(&[
126 ("/lib.rs", "mod foo;"),
127 ("/foo.rs", ""),
128 ]);
129 let snap = host.analysis();
130 assert!(snap.crate_for(FileId(2)).is_empty());
131
132 let crate_graph = CrateGraph {
133 crate_roots: {
134 let mut m = HashMap::new();
135 m.insert(CrateId(1), FileId(1));
136 m
137 },
138 };
139 host.set_crate_graph(crate_graph);
140 let snap = host.analysis();
141
142 assert_eq!(
143 snap.crate_for(FileId(2)),
144 vec![CrateId(1)],
145 );
146}