diff options
Diffstat (limited to 'crates/ra_analysis/tests/tests.rs')
-rw-r--r-- | crates/ra_analysis/tests/tests.rs | 56 |
1 files changed, 6 insertions, 50 deletions
diff --git a/crates/ra_analysis/tests/tests.rs b/crates/ra_analysis/tests/tests.rs index 806e1fb34..f5683aec5 100644 --- a/crates/ra_analysis/tests/tests.rs +++ b/crates/ra_analysis/tests/tests.rs | |||
@@ -5,62 +5,16 @@ extern crate relative_path; | |||
5 | extern crate rustc_hash; | 5 | extern crate rustc_hash; |
6 | extern crate test_utils; | 6 | extern crate test_utils; |
7 | 7 | ||
8 | use std::{ | ||
9 | sync::Arc, | ||
10 | }; | ||
11 | |||
12 | use ra_syntax::TextRange; | 8 | use ra_syntax::TextRange; |
13 | use relative_path::{RelativePath, RelativePathBuf}; | ||
14 | use test_utils::{assert_eq_dbg, extract_offset}; | 9 | use test_utils::{assert_eq_dbg, extract_offset}; |
15 | 10 | ||
16 | use ra_analysis::{ | 11 | use ra_analysis::{ |
17 | AnalysisChange, Analysis, AnalysisHost, CrateGraph, CrateId, FileId, FileResolver, FnDescriptor, | 12 | MockAnalysis, |
13 | AnalysisChange, Analysis, CrateGraph, CrateId, FileId, FnDescriptor, | ||
18 | }; | 14 | }; |
19 | 15 | ||
20 | #[derive(Debug)] | ||
21 | struct FileMap(Vec<(FileId, RelativePathBuf)>); | ||
22 | |||
23 | impl FileMap { | ||
24 | fn iter<'a>(&'a self) -> impl Iterator<Item = (FileId, &'a RelativePath)> + 'a { | ||
25 | self.0 | ||
26 | .iter() | ||
27 | .map(|(id, path)| (*id, path.as_relative_path())) | ||
28 | } | ||
29 | |||
30 | fn path(&self, id: FileId) -> &RelativePath { | ||
31 | self.iter().find(|&(it, _)| it == id).unwrap().1 | ||
32 | } | ||
33 | } | ||
34 | |||
35 | impl FileResolver for FileMap { | ||
36 | fn file_stem(&self, id: FileId) -> String { | ||
37 | self.path(id).file_stem().unwrap().to_string() | ||
38 | } | ||
39 | fn resolve(&self, id: FileId, rel: &RelativePath) -> Option<FileId> { | ||
40 | let path = self.path(id).join(rel).normalize(); | ||
41 | let id = self.iter().find(|&(_, p)| path == p)?.0; | ||
42 | Some(id) | ||
43 | } | ||
44 | } | ||
45 | |||
46 | fn analysis_host(files: &[(&str, &str)]) -> AnalysisHost { | ||
47 | let mut host = AnalysisHost::new(); | ||
48 | let mut file_map = Vec::new(); | ||
49 | let mut change = AnalysisChange::new(); | ||
50 | for (id, &(path, contents)) in files.iter().enumerate() { | ||
51 | let file_id = FileId((id + 1) as u32); | ||
52 | assert!(path.starts_with('/')); | ||
53 | let path = RelativePathBuf::from_path(&path[1..]).unwrap(); | ||
54 | change.add_file(file_id, contents.to_string()); | ||
55 | file_map.push((file_id, path)); | ||
56 | } | ||
57 | change.set_file_resolver(Arc::new(FileMap(file_map))); | ||
58 | host.apply_change(change); | ||
59 | host | ||
60 | } | ||
61 | |||
62 | fn analysis(files: &[(&str, &str)]) -> Analysis { | 16 | fn analysis(files: &[(&str, &str)]) -> Analysis { |
63 | analysis_host(files).analysis() | 17 | MockAnalysis::with_files(files).analysis() |
64 | } | 18 | } |
65 | 19 | ||
66 | fn get_signature(text: &str) -> (FnDescriptor, Option<usize>) { | 20 | fn get_signature(text: &str) -> (FnDescriptor, Option<usize>) { |
@@ -125,7 +79,9 @@ fn test_resolve_parent_module() { | |||
125 | 79 | ||
126 | #[test] | 80 | #[test] |
127 | fn test_resolve_crate_root() { | 81 | fn test_resolve_crate_root() { |
128 | let mut host = analysis_host(&[("/lib.rs", "mod foo;"), ("/foo.rs", "")]); | 82 | let mut host = MockAnalysis::with_files( |
83 | &[("/lib.rs", "mod foo;"), ("/foo.rs", "")] | ||
84 | ).analysis_host(); | ||
129 | let snap = host.analysis(); | 85 | let snap = host.analysis(); |
130 | assert!(snap.crate_for(FileId(2)).unwrap().is_empty()); | 86 | assert!(snap.crate_for(FileId(2)).unwrap().is_empty()); |
131 | 87 | ||