aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_analysis/tests
diff options
context:
space:
mode:
authorbors[bot] <bors[bot]@users.noreply.github.com>2018-10-31 18:05:12 +0000
committerbors[bot] <bors[bot]@users.noreply.github.com>2018-10-31 18:05:12 +0000
commit1dc5608d0bb6bf2eee5a1b9190fcb2f8cdfa2ef3 (patch)
treeb3c8a97880c625a81a814f4afa4e461ce5a58b82 /crates/ra_analysis/tests
parente60ef6260f49b2b0438f8649ca71034fbafef631 (diff)
parentc09e14a4ff02f774460a70472e1aeb3c598e01dc (diff)
Merge #176
176: Move completio to ra_analysis r=matklad a=matklad While we should handle completion for isolated file, it's better achieved by using empty Analysis, rather than working only with &File: we need memoization for type inference even inside a single file. Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_analysis/tests')
-rw-r--r--crates/ra_analysis/tests/tests.rs56
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;
5extern crate rustc_hash; 5extern crate rustc_hash;
6extern crate test_utils; 6extern crate test_utils;
7 7
8use std::{
9 sync::Arc,
10};
11
12use ra_syntax::TextRange; 8use ra_syntax::TextRange;
13use relative_path::{RelativePath, RelativePathBuf};
14use test_utils::{assert_eq_dbg, extract_offset}; 9use test_utils::{assert_eq_dbg, extract_offset};
15 10
16use ra_analysis::{ 11use 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)]
21struct FileMap(Vec<(FileId, RelativePathBuf)>);
22
23impl 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
35impl 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
46fn 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
62fn analysis(files: &[(&str, &str)]) -> Analysis { 16fn analysis(files: &[(&str, &str)]) -> Analysis {
63 analysis_host(files).analysis() 17 MockAnalysis::with_files(files).analysis()
64} 18}
65 19
66fn get_signature(text: &str) -> (FnDescriptor, Option<usize>) { 20fn get_signature(text: &str) -> (FnDescriptor, Option<usize>) {
@@ -125,7 +79,9 @@ fn test_resolve_parent_module() {
125 79
126#[test] 80#[test]
127fn test_resolve_crate_root() { 81fn 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