diff options
author | Aleksey Kladov <[email protected]> | 2018-09-10 10:57:40 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2018-09-10 10:57:40 +0100 |
commit | 505895a25f98423de07c3cec4793b66a19d098c7 (patch) | |
tree | 2df3f41f33b5db0b2bba1e2d2acd08f23fffbc2a /crates/libanalysis/tests | |
parent | 4f647096665b2ca3725ba1f7415a21fbc46044bb (diff) |
store file rsovler
Diffstat (limited to 'crates/libanalysis/tests')
-rw-r--r-- | crates/libanalysis/tests/tests.rs | 93 |
1 files changed, 45 insertions, 48 deletions
diff --git a/crates/libanalysis/tests/tests.rs b/crates/libanalysis/tests/tests.rs index ebc8037b3..00efe059c 100644 --- a/crates/libanalysis/tests/tests.rs +++ b/crates/libanalysis/tests/tests.rs | |||
@@ -3,21 +3,38 @@ extern crate relative_path; | |||
3 | extern crate test_utils; | 3 | extern crate test_utils; |
4 | 4 | ||
5 | use std::{ | 5 | use std::{ |
6 | sync::Arc, | ||
6 | collections::HashMap, | 7 | collections::HashMap, |
7 | }; | 8 | }; |
8 | 9 | ||
9 | use relative_path::{RelativePath}; | 10 | use relative_path::{RelativePath, RelativePathBuf}; |
10 | use libanalysis::{AnalysisHost, FileId, FileResolver, JobHandle, CrateGraph, CrateId}; | 11 | use libanalysis::{Analysis, AnalysisHost, FileId, FileResolver, JobHandle, CrateGraph, CrateId}; |
11 | use test_utils::assert_eq_dbg; | 12 | use test_utils::assert_eq_dbg; |
12 | 13 | ||
13 | struct FileMap(&'static [(u32, &'static str)]); | 14 | #[derive(Debug)] |
15 | struct FileMap(Vec<(FileId, RelativePathBuf)>); | ||
16 | |||
17 | fn analysis_host(files: &'static [(&'static str, &'static str)]) -> AnalysisHost { | ||
18 | let mut host = AnalysisHost::new(); | ||
19 | let mut file_map = Vec::new(); | ||
20 | for (id, &(path, contents)) in files.iter().enumerate() { | ||
21 | let file_id = FileId((id + 1) as u32); | ||
22 | assert!(path.starts_with('/')); | ||
23 | let path = RelativePathBuf::from_path(&path[1..]).unwrap(); | ||
24 | host.change_file(file_id, Some(contents.to_string())); | ||
25 | file_map.push((file_id, path)); | ||
26 | } | ||
27 | host.set_file_resolver(Arc::new(FileMap(file_map))); | ||
28 | host | ||
29 | } | ||
30 | |||
31 | fn analysis(files: &'static [(&'static str, &'static str)]) -> Analysis { | ||
32 | analysis_host(files).analysis() | ||
33 | } | ||
14 | 34 | ||
15 | impl FileMap { | 35 | impl FileMap { |
16 | fn iter<'a>(&'a self) -> impl Iterator<Item=(FileId, &'a RelativePath)> + 'a { | 36 | fn iter<'a>(&'a self) -> impl Iterator<Item=(FileId, &'a RelativePath)> + 'a { |
17 | self.0.iter().map(|&(id, path)| { | 37 | self.0.iter().map(|(id, path)| (*id, path.as_relative_path())) |
18 | assert!(path.starts_with('/')); | ||
19 | (FileId(id), RelativePath::new(&path[1..])) | ||
20 | }) | ||
21 | } | 38 | } |
22 | 39 | ||
23 | fn path(&self, id: FileId) -> &RelativePath { | 40 | fn path(&self, id: FileId) -> &RelativePath { |
@@ -42,14 +59,10 @@ impl FileResolver for FileMap { | |||
42 | 59 | ||
43 | #[test] | 60 | #[test] |
44 | fn test_resolve_module() { | 61 | fn test_resolve_module() { |
45 | let mut world = AnalysisHost::new(); | 62 | let snap = analysis(&[ |
46 | world.change_file(FileId(1), Some("mod foo;".to_string())); | 63 | ("/lib.rs", "mod foo;"), |
47 | world.change_file(FileId(2), Some("".to_string())); | 64 | ("/foo.rs", "") |
48 | 65 | ]); | |
49 | let snap = world.analysis(FileMap(&[ | ||
50 | (1, "/lib.rs"), | ||
51 | (2, "/foo.rs"), | ||
52 | ])); | ||
53 | let (_handle, token) = JobHandle::new(); | 66 | let (_handle, token) = JobHandle::new(); |
54 | let symbols = snap.approximately_resolve_symbol(FileId(1), 4.into(), &token); | 67 | let symbols = snap.approximately_resolve_symbol(FileId(1), 4.into(), &token); |
55 | assert_eq_dbg( | 68 | assert_eq_dbg( |
@@ -57,10 +70,10 @@ fn test_resolve_module() { | |||
57 | &symbols, | 70 | &symbols, |
58 | ); | 71 | ); |
59 | 72 | ||
60 | let snap = world.analysis(FileMap(&[ | 73 | let snap = analysis(&[ |
61 | (1, "/lib.rs"), | 74 | ("/lib.rs", "mod foo;"), |
62 | (2, "/foo/mod.rs") | 75 | ("/foo/mod.rs", "") |
63 | ])); | 76 | ]); |
64 | let symbols = snap.approximately_resolve_symbol(FileId(1), 4.into(), &token); | 77 | let symbols = snap.approximately_resolve_symbol(FileId(1), 4.into(), &token); |
65 | assert_eq_dbg( | 78 | assert_eq_dbg( |
66 | r#"[(FileId(2), FileSymbol { name: "foo", node_range: [0; 0), kind: MODULE })]"#, | 79 | r#"[(FileId(2), FileSymbol { name: "foo", node_range: [0; 0), kind: MODULE })]"#, |
@@ -70,10 +83,7 @@ fn test_resolve_module() { | |||
70 | 83 | ||
71 | #[test] | 84 | #[test] |
72 | fn test_unresolved_module_diagnostic() { | 85 | fn test_unresolved_module_diagnostic() { |
73 | let mut world = AnalysisHost::new(); | 86 | let snap = analysis(&[("/lib.rs", "mod foo;")]); |
74 | world.change_file(FileId(1), Some("mod foo;".to_string())); | ||
75 | |||
76 | let snap = world.analysis(FileMap(&[(1, "/lib.rs")])); | ||
77 | let diagnostics = snap.diagnostics(FileId(1)); | 87 | let diagnostics = snap.diagnostics(FileId(1)); |
78 | assert_eq_dbg( | 88 | assert_eq_dbg( |
79 | r#"[Diagnostic { | 89 | r#"[Diagnostic { |
@@ -90,10 +100,7 @@ fn test_unresolved_module_diagnostic() { | |||
90 | 100 | ||
91 | #[test] | 101 | #[test] |
92 | fn test_unresolved_module_diagnostic_no_diag_for_inline_mode() { | 102 | fn test_unresolved_module_diagnostic_no_diag_for_inline_mode() { |
93 | let mut world = AnalysisHost::new(); | 103 | let snap = analysis(&[("/lib.rs", "mod foo {}")]); |
94 | world.change_file(FileId(1), Some("mod foo {}".to_string())); | ||
95 | |||
96 | let snap = world.analysis(FileMap(&[(1, "/lib.rs")])); | ||
97 | let diagnostics = snap.diagnostics(FileId(1)); | 104 | let diagnostics = snap.diagnostics(FileId(1)); |
98 | assert_eq_dbg( | 105 | assert_eq_dbg( |
99 | r#"[]"#, | 106 | r#"[]"#, |
@@ -103,14 +110,10 @@ fn test_unresolved_module_diagnostic_no_diag_for_inline_mode() { | |||
103 | 110 | ||
104 | #[test] | 111 | #[test] |
105 | fn test_resolve_parent_module() { | 112 | fn test_resolve_parent_module() { |
106 | let mut world = AnalysisHost::new(); | 113 | let snap = analysis(&[ |
107 | world.change_file(FileId(1), Some("mod foo;".to_string())); | 114 | ("/lib.rs", "mod foo;"), |
108 | world.change_file(FileId(2), Some("".to_string())); | 115 | ("/foo.rs", ""), |
109 | 116 | ]); | |
110 | let snap = world.analysis(FileMap(&[ | ||
111 | (1, "/lib.rs"), | ||
112 | (2, "/foo.rs"), | ||
113 | ])); | ||
114 | let symbols = snap.parent_module(FileId(2)); | 117 | let symbols = snap.parent_module(FileId(2)); |
115 | assert_eq_dbg( | 118 | assert_eq_dbg( |
116 | r#"[(FileId(1), FileSymbol { name: "foo", node_range: [0; 8), kind: MODULE })]"#, | 119 | r#"[(FileId(1), FileSymbol { name: "foo", node_range: [0; 8), kind: MODULE })]"#, |
@@ -120,14 +123,11 @@ fn test_resolve_parent_module() { | |||
120 | 123 | ||
121 | #[test] | 124 | #[test] |
122 | fn test_resolve_crate_root() { | 125 | fn test_resolve_crate_root() { |
123 | let mut world = AnalysisHost::new(); | 126 | let mut host = analysis_host(&[ |
124 | world.change_file(FileId(1), Some("mod foo;".to_string())); | 127 | ("/lib.rs", "mod foo;"), |
125 | world.change_file(FileId(2), Some("".to_string())); | 128 | ("/foo.rs", ""), |
126 | 129 | ]); | |
127 | let snap = world.analysis(FileMap(&[ | 130 | let snap = host.analysis(); |
128 | (1, "/lib.rs"), | ||
129 | (2, "/foo.rs"), | ||
130 | ])); | ||
131 | assert!(snap.crate_for(FileId(2)).is_empty()); | 131 | assert!(snap.crate_for(FileId(2)).is_empty()); |
132 | 132 | ||
133 | let crate_graph = CrateGraph { | 133 | let crate_graph = CrateGraph { |
@@ -137,12 +137,9 @@ fn test_resolve_crate_root() { | |||
137 | m | 137 | m |
138 | }, | 138 | }, |
139 | }; | 139 | }; |
140 | world.set_crate_graph(crate_graph); | 140 | host.set_crate_graph(crate_graph); |
141 | let snap = host.analysis(); | ||
141 | 142 | ||
142 | let snap = world.analysis(FileMap(&[ | ||
143 | (1, "/lib.rs"), | ||
144 | (2, "/foo.rs"), | ||
145 | ])); | ||
146 | assert_eq!( | 143 | assert_eq!( |
147 | snap.crate_for(FileId(2)), | 144 | snap.crate_for(FileId(2)), |
148 | vec![CrateId(1)], | 145 | vec![CrateId(1)], |