aboutsummaryrefslogtreecommitdiff
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
parentdcdcc9e4c8e24f8b643a0cc5c70e8e4dd624289f (diff)
Remove more duplication in test fixtures
-rw-r--r--crates/ra_assists/src/doc_tests.rs4
-rw-r--r--crates/ra_assists/src/lib.rs20
-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
5 files changed, 18 insertions, 62 deletions
diff --git a/crates/ra_assists/src/doc_tests.rs b/crates/ra_assists/src/doc_tests.rs
index 6e1e3de84..43194037e 100644
--- a/crates/ra_assists/src/doc_tests.rs
+++ b/crates/ra_assists/src/doc_tests.rs
@@ -6,12 +6,12 @@
6mod generated; 6mod generated;
7 7
8use hir::mock::MockDatabase; 8use hir::mock::MockDatabase;
9use ra_db::FileRange; 9use ra_db::{fixture::WithFixture, FileRange};
10use test_utils::{assert_eq_text, extract_range_or_offset}; 10use test_utils::{assert_eq_text, extract_range_or_offset};
11 11
12fn check(assist_id: &str, before: &str, after: &str) { 12fn check(assist_id: &str, before: &str, after: &str) {
13 let (selection, before) = extract_range_or_offset(before); 13 let (selection, before) = extract_range_or_offset(before);
14 let (db, _source_root, file_id) = MockDatabase::with_single_file(&before); 14 let (db, file_id) = MockDatabase::with_single_file(&before);
15 let frange = FileRange { file_id, range: selection.into() }; 15 let frange = FileRange { file_id, range: selection.into() };
16 16
17 let (_assist_id, action) = crate::assists(&db, frange) 17 let (_assist_id, action) = crate::assists(&db, frange)
diff --git a/crates/ra_assists/src/lib.rs b/crates/ra_assists/src/lib.rs
index 38599d4f1..13db37d29 100644
--- a/crates/ra_assists/src/lib.rs
+++ b/crates/ra_assists/src/lib.rs
@@ -147,7 +147,7 @@ mod assists {
147#[cfg(test)] 147#[cfg(test)]
148mod helpers { 148mod helpers {
149 use hir::mock::MockDatabase; 149 use hir::mock::MockDatabase;
150 use ra_db::FileRange; 150 use ra_db::{fixture::WithFixture, FileRange};
151 use ra_syntax::TextRange; 151 use ra_syntax::TextRange;
152 use test_utils::{add_cursor, assert_eq_text, extract_offset, extract_range}; 152 use test_utils::{add_cursor, assert_eq_text, extract_offset, extract_range};
153 153
@@ -159,7 +159,7 @@ mod helpers {
159 after: &str, 159 after: &str,
160 ) { 160 ) {
161 let (before_cursor_pos, before) = extract_offset(before); 161 let (before_cursor_pos, before) = extract_offset(before);
162 let (db, _source_root, file_id) = MockDatabase::with_single_file(&before); 162 let (db, file_id) = MockDatabase::with_single_file(&before);
163 let frange = 163 let frange =
164 FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) }; 164 FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) };
165 let assist = 165 let assist =
@@ -187,7 +187,7 @@ mod helpers {
187 after: &str, 187 after: &str,
188 ) { 188 ) {
189 let (range, before) = extract_range(before); 189 let (range, before) = extract_range(before);
190 let (db, _source_root, file_id) = MockDatabase::with_single_file(&before); 190 let (db, file_id) = MockDatabase::with_single_file(&before);
191 let frange = FileRange { file_id, range }; 191 let frange = FileRange { file_id, range };
192 let assist = 192 let assist =
193 AssistCtx::with_ctx(&db, frange, true, assist).expect("code action is not applicable"); 193 AssistCtx::with_ctx(&db, frange, true, assist).expect("code action is not applicable");
@@ -209,7 +209,7 @@ mod helpers {
209 target: &str, 209 target: &str,
210 ) { 210 ) {
211 let (before_cursor_pos, before) = extract_offset(before); 211 let (before_cursor_pos, before) = extract_offset(before);
212 let (db, _source_root, file_id) = MockDatabase::with_single_file(&before); 212 let (db, file_id) = MockDatabase::with_single_file(&before);
213 let frange = 213 let frange =
214 FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) }; 214 FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) };
215 let assist = 215 let assist =
@@ -229,7 +229,7 @@ mod helpers {
229 target: &str, 229 target: &str,
230 ) { 230 ) {
231 let (range, before) = extract_range(before); 231 let (range, before) = extract_range(before);
232 let (db, _source_root, file_id) = MockDatabase::with_single_file(&before); 232 let (db, file_id) = MockDatabase::with_single_file(&before);
233 let frange = FileRange { file_id, range }; 233 let frange = FileRange { file_id, range };
234 let assist = 234 let assist =
235 AssistCtx::with_ctx(&db, frange, true, assist).expect("code action is not applicable"); 235 AssistCtx::with_ctx(&db, frange, true, assist).expect("code action is not applicable");
@@ -247,7 +247,7 @@ mod helpers {
247 before: &str, 247 before: &str,
248 ) { 248 ) {
249 let (before_cursor_pos, before) = extract_offset(before); 249 let (before_cursor_pos, before) = extract_offset(before);
250 let (db, _source_root, file_id) = MockDatabase::with_single_file(&before); 250 let (db, file_id) = MockDatabase::with_single_file(&before);
251 let frange = 251 let frange =
252 FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) }; 252 FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) };
253 let assist = AssistCtx::with_ctx(&db, frange, true, assist); 253 let assist = AssistCtx::with_ctx(&db, frange, true, assist);
@@ -259,7 +259,7 @@ mod helpers {
259 before: &str, 259 before: &str,
260 ) { 260 ) {
261 let (range, before) = extract_range(before); 261 let (range, before) = extract_range(before);
262 let (db, _source_root, file_id) = MockDatabase::with_single_file(&before); 262 let (db, file_id) = MockDatabase::with_single_file(&before);
263 let frange = FileRange { file_id, range }; 263 let frange = FileRange { file_id, range };
264 let assist = AssistCtx::with_ctx(&db, frange, true, assist); 264 let assist = AssistCtx::with_ctx(&db, frange, true, assist);
265 assert!(assist.is_none()); 265 assert!(assist.is_none());
@@ -269,7 +269,7 @@ mod helpers {
269#[cfg(test)] 269#[cfg(test)]
270mod tests { 270mod tests {
271 use hir::mock::MockDatabase; 271 use hir::mock::MockDatabase;
272 use ra_db::FileRange; 272 use ra_db::{fixture::WithFixture, FileRange};
273 use ra_syntax::TextRange; 273 use ra_syntax::TextRange;
274 use test_utils::{extract_offset, extract_range}; 274 use test_utils::{extract_offset, extract_range};
275 275
@@ -277,7 +277,7 @@ mod tests {
277 fn assist_order_field_struct() { 277 fn assist_order_field_struct() {
278 let before = "struct Foo { <|>bar: u32 }"; 278 let before = "struct Foo { <|>bar: u32 }";
279 let (before_cursor_pos, before) = extract_offset(before); 279 let (before_cursor_pos, before) = extract_offset(before);
280 let (db, _source_root, file_id) = MockDatabase::with_single_file(&before); 280 let (db, file_id) = MockDatabase::with_single_file(&before);
281 let frange = 281 let frange =
282 FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) }; 282 FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) };
283 let assists = super::assists(&db, frange); 283 let assists = super::assists(&db, frange);
@@ -298,7 +298,7 @@ mod tests {
298 } 298 }
299 }"; 299 }";
300 let (range, before) = extract_range(before); 300 let (range, before) = extract_range(before);
301 let (db, _source_root, file_id) = MockDatabase::with_single_file(&before); 301 let (db, file_id) = MockDatabase::with_single_file(&before);
302 let frange = FileRange { file_id, range }; 302 let frange = FileRange { file_id, range };
303 let assists = super::assists(&db, frange); 303 let assists = super::assists(&db, frange);
304 let mut assists = assists.iter(); 304 let mut assists = assists.iter();
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();