aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-11-04 19:12:49 +0000
committerAleksey Kladov <[email protected]>2019-11-04 19:12:49 +0000
commit24894aca3162d654ee051f591a920ef1525d821f (patch)
tree900d53ca0b8cb1e99f1a8c526f13eb712808ca7e /crates/ra_hir
parentdcdcc9e4c8e24f8b643a0cc5c70e8e4dd624289f (diff)
Remove more duplication in test fixtures
Diffstat (limited to 'crates/ra_hir')
-rw-r--r--crates/ra_hir/src/expr/scope.rs6
-rw-r--r--crates/ra_hir/src/mock.rs48
-rw-r--r--crates/ra_hir/src/ty/tests.rs2
3 files changed, 6 insertions, 50 deletions
diff --git a/crates/ra_hir/src/expr/scope.rs b/crates/ra_hir/src/expr/scope.rs
index 5496822e7..f7196b740 100644
--- a/crates/ra_hir/src/expr/scope.rs
+++ b/crates/ra_hir/src/expr/scope.rs
@@ -174,7 +174,7 @@ fn compute_expr_scopes(expr: ExprId, body: &Body, scopes: &mut ExprScopes, scope
174 174
175#[cfg(test)] 175#[cfg(test)]
176mod tests { 176mod tests {
177 use ra_db::SourceDatabase; 177 use ra_db::{fixture::WithFixture, SourceDatabase};
178 use ra_syntax::{algo::find_node_at_offset, ast, AstNode}; 178 use ra_syntax::{algo::find_node_at_offset, ast, AstNode};
179 use test_utils::{assert_eq_text, extract_offset}; 179 use test_utils::{assert_eq_text, extract_offset};
180 180
@@ -191,7 +191,7 @@ mod tests {
191 buf 191 buf
192 }; 192 };
193 193
194 let (db, _source_root, file_id) = MockDatabase::with_single_file(&code); 194 let (db, file_id) = MockDatabase::with_single_file(&code);
195 let file = db.parse(file_id).ok().unwrap(); 195 let file = db.parse(file_id).ok().unwrap();
196 let marker: ast::PathExpr = find_node_at_offset(file.syntax(), off).unwrap(); 196 let marker: ast::PathExpr = find_node_at_offset(file.syntax(), off).unwrap();
197 let analyzer = SourceAnalyzer::new(&db, file_id, marker.syntax(), None); 197 let analyzer = SourceAnalyzer::new(&db, file_id, marker.syntax(), None);
@@ -288,7 +288,7 @@ mod tests {
288 fn do_check_local_name(code: &str, expected_offset: u32) { 288 fn do_check_local_name(code: &str, expected_offset: u32) {
289 let (off, code) = extract_offset(code); 289 let (off, code) = extract_offset(code);
290 290
291 let (db, _source_root, file_id) = MockDatabase::with_single_file(&code); 291 let (db, file_id) = MockDatabase::with_single_file(&code);
292 let file = db.parse(file_id).ok().unwrap(); 292 let file = db.parse(file_id).ok().unwrap();
293 let expected_name = find_node_at_offset::<ast::Name>(file.syntax(), expected_offset.into()) 293 let expected_name = find_node_at_offset::<ast::Name>(file.syntax(), expected_offset.into())
294 .expect("failed to find a name at the target offset"); 294 .expect("failed to find a name at the target offset");
diff --git a/crates/ra_hir/src/mock.rs b/crates/ra_hir/src/mock.rs
index 48723fa02..94aff93de 100644
--- a/crates/ra_hir/src/mock.rs
+++ b/crates/ra_hir/src/mock.rs
@@ -5,10 +5,9 @@ use std::{panic, sync::Arc};
5use hir_def::{db::DefDatabase2, ModuleId}; 5use hir_def::{db::DefDatabase2, ModuleId};
6use hir_expand::diagnostics::DiagnosticSink; 6use hir_expand::diagnostics::DiagnosticSink;
7use parking_lot::Mutex; 7use parking_lot::Mutex;
8use ra_cfg::CfgOptions;
9use ra_db::{ 8use ra_db::{
10 salsa, CrateGraph, CrateId, Edition, FileId, FileLoader, FileLoaderDelegate, RelativePath, 9 salsa, CrateId, FileId, FileLoader, FileLoaderDelegate, RelativePath, SourceDatabase,
11 RelativePathBuf, SourceDatabase, SourceDatabaseExt, SourceRoot, SourceRootId, 10 SourceRootId,
12}; 11};
13use rustc_hash::FxHashMap; 12use rustc_hash::FxHashMap;
14 13
@@ -63,14 +62,6 @@ impl HirDebugHelper for MockDatabase {
63} 62}
64 63
65impl MockDatabase { 64impl MockDatabase {
66 pub fn with_single_file(text: &str) -> (MockDatabase, SourceRoot, FileId) {
67 let mut db = MockDatabase::default();
68 let mut source_root = SourceRoot::default();
69 let file_id = db.add_file(WORKSPACE, "/", &mut source_root, "/main.rs", text);
70 db.set_source_root(WORKSPACE, Arc::new(source_root.clone()));
71 (db, source_root, file_id)
72 }
73
74 pub fn file_id_of(&self, path: &str) -> FileId { 65 pub fn file_id_of(&self, path: &str) -> FileId {
75 match self.files.get(path) { 66 match self.files.get(path) {
76 Some(it) => *it, 67 Some(it) => *it,
@@ -96,41 +87,6 @@ impl MockDatabase {
96 } 87 }
97 buf 88 buf
98 } 89 }
99
100 fn add_file(
101 &mut self,
102 source_root_id: SourceRootId,
103 source_root_prefix: &str,
104 source_root: &mut SourceRoot,
105 path: &str,
106 text: &str,
107 ) -> FileId {
108 assert!(source_root_prefix.starts_with('/'));
109 assert!(source_root_prefix.ends_with('/'));
110 assert!(path.starts_with(source_root_prefix));
111 let rel_path = RelativePathBuf::from_path(&path[source_root_prefix.len()..]).unwrap();
112
113 let is_crate_root = rel_path == "lib.rs" || rel_path == "/main.rs";
114
115 let file_id = FileId(self.files.len() as u32);
116
117 let prev = self.files.insert(path.to_string(), file_id);
118 assert!(prev.is_none(), "duplicate files in the text fixture");
119 Arc::make_mut(&mut self.file_paths).insert(file_id, path.to_string());
120
121 let text = Arc::new(text.to_string());
122 self.set_file_text(file_id, text);
123 self.set_file_relative_path(file_id, rel_path.clone());
124 self.set_file_source_root(file_id, source_root_id);
125 source_root.insert_file(rel_path, file_id);
126
127 if is_crate_root {
128 let mut crate_graph = CrateGraph::default();
129 crate_graph.add_crate_root(file_id, Edition::Edition2018, CfgOptions::default());
130 self.set_crate_graph(Arc::new(crate_graph));
131 }
132 file_id
133 }
134} 90}
135 91
136impl salsa::Database for MockDatabase { 92impl salsa::Database for MockDatabase {
diff --git a/crates/ra_hir/src/ty/tests.rs b/crates/ra_hir/src/ty/tests.rs
index 4b7e34878..4af2fe0b4 100644
--- a/crates/ra_hir/src/ty/tests.rs
+++ b/crates/ra_hir/src/ty/tests.rs
@@ -4665,7 +4665,7 @@ fn type_at(content: &str) -> String {
4665} 4665}
4666 4666
4667fn infer(content: &str) -> String { 4667fn infer(content: &str) -> String {
4668 let (db, _, file_id) = MockDatabase::with_single_file(content); 4668 let (db, file_id) = MockDatabase::with_single_file(content);
4669 let source_file = db.parse(file_id).ok().unwrap(); 4669 let source_file = db.parse(file_id).ok().unwrap();
4670 4670
4671 let mut acc = String::new(); 4671 let mut acc = String::new();