diff options
Diffstat (limited to 'crates/ra_assists/src')
-rw-r--r-- | crates/ra_assists/src/doc_tests.rs | 7 | ||||
-rw-r--r-- | crates/ra_assists/src/lib.rs | 40 | ||||
-rw-r--r-- | crates/ra_assists/src/test_db.rs | 45 |
3 files changed, 70 insertions, 22 deletions
diff --git a/crates/ra_assists/src/doc_tests.rs b/crates/ra_assists/src/doc_tests.rs index 6e1e3de84..a8f8446cb 100644 --- a/crates/ra_assists/src/doc_tests.rs +++ b/crates/ra_assists/src/doc_tests.rs | |||
@@ -5,13 +5,14 @@ | |||
5 | 5 | ||
6 | mod generated; | 6 | mod generated; |
7 | 7 | ||
8 | use hir::mock::MockDatabase; | 8 | use ra_db::{fixture::WithFixture, FileRange}; |
9 | use ra_db::FileRange; | ||
10 | use test_utils::{assert_eq_text, extract_range_or_offset}; | 9 | use test_utils::{assert_eq_text, extract_range_or_offset}; |
11 | 10 | ||
11 | use crate::test_db::TestDB; | ||
12 | |||
12 | fn check(assist_id: &str, before: &str, after: &str) { | 13 | fn check(assist_id: &str, before: &str, after: &str) { |
13 | let (selection, before) = extract_range_or_offset(before); | 14 | let (selection, before) = extract_range_or_offset(before); |
14 | let (db, _source_root, file_id) = MockDatabase::with_single_file(&before); | 15 | let (db, file_id) = TestDB::with_single_file(&before); |
15 | let frange = FileRange { file_id, range: selection.into() }; | 16 | let frange = FileRange { file_id, range: selection.into() }; |
16 | 17 | ||
17 | let (_assist_id, action) = crate::assists(&db, frange) | 18 | 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..39c1c283f 100644 --- a/crates/ra_assists/src/lib.rs +++ b/crates/ra_assists/src/lib.rs | |||
@@ -9,6 +9,8 @@ mod assist_ctx; | |||
9 | mod marks; | 9 | mod marks; |
10 | #[cfg(test)] | 10 | #[cfg(test)] |
11 | mod doc_tests; | 11 | mod doc_tests; |
12 | #[cfg(test)] | ||
13 | mod test_db; | ||
12 | 14 | ||
13 | use hir::db::HirDatabase; | 15 | use hir::db::HirDatabase; |
14 | use ra_db::FileRange; | 16 | use ra_db::FileRange; |
@@ -146,20 +148,19 @@ mod assists { | |||
146 | 148 | ||
147 | #[cfg(test)] | 149 | #[cfg(test)] |
148 | mod helpers { | 150 | mod helpers { |
149 | use hir::mock::MockDatabase; | 151 | use ra_db::{fixture::WithFixture, FileRange}; |
150 | use ra_db::FileRange; | ||
151 | use ra_syntax::TextRange; | 152 | use ra_syntax::TextRange; |
152 | use test_utils::{add_cursor, assert_eq_text, extract_offset, extract_range}; | 153 | use test_utils::{add_cursor, assert_eq_text, extract_offset, extract_range}; |
153 | 154 | ||
154 | use crate::{Assist, AssistCtx}; | 155 | use crate::{test_db::TestDB, Assist, AssistCtx}; |
155 | 156 | ||
156 | pub(crate) fn check_assist( | 157 | pub(crate) fn check_assist( |
157 | assist: fn(AssistCtx<MockDatabase>) -> Option<Assist>, | 158 | assist: fn(AssistCtx<TestDB>) -> Option<Assist>, |
158 | before: &str, | 159 | before: &str, |
159 | after: &str, | 160 | after: &str, |
160 | ) { | 161 | ) { |
161 | let (before_cursor_pos, before) = extract_offset(before); | 162 | let (before_cursor_pos, before) = extract_offset(before); |
162 | let (db, _source_root, file_id) = MockDatabase::with_single_file(&before); | 163 | let (db, file_id) = TestDB::with_single_file(&before); |
163 | let frange = | 164 | let frange = |
164 | FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) }; | 165 | FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) }; |
165 | let assist = | 166 | let assist = |
@@ -182,12 +183,12 @@ mod helpers { | |||
182 | } | 183 | } |
183 | 184 | ||
184 | pub(crate) fn check_assist_range( | 185 | pub(crate) fn check_assist_range( |
185 | assist: fn(AssistCtx<MockDatabase>) -> Option<Assist>, | 186 | assist: fn(AssistCtx<TestDB>) -> Option<Assist>, |
186 | before: &str, | 187 | before: &str, |
187 | after: &str, | 188 | after: &str, |
188 | ) { | 189 | ) { |
189 | let (range, before) = extract_range(before); | 190 | let (range, before) = extract_range(before); |
190 | let (db, _source_root, file_id) = MockDatabase::with_single_file(&before); | 191 | let (db, file_id) = TestDB::with_single_file(&before); |
191 | let frange = FileRange { file_id, range }; | 192 | let frange = FileRange { file_id, range }; |
192 | let assist = | 193 | let assist = |
193 | AssistCtx::with_ctx(&db, frange, true, assist).expect("code action is not applicable"); | 194 | AssistCtx::with_ctx(&db, frange, true, assist).expect("code action is not applicable"); |
@@ -204,12 +205,12 @@ mod helpers { | |||
204 | } | 205 | } |
205 | 206 | ||
206 | pub(crate) fn check_assist_target( | 207 | pub(crate) fn check_assist_target( |
207 | assist: fn(AssistCtx<MockDatabase>) -> Option<Assist>, | 208 | assist: fn(AssistCtx<TestDB>) -> Option<Assist>, |
208 | before: &str, | 209 | before: &str, |
209 | target: &str, | 210 | target: &str, |
210 | ) { | 211 | ) { |
211 | let (before_cursor_pos, before) = extract_offset(before); | 212 | let (before_cursor_pos, before) = extract_offset(before); |
212 | let (db, _source_root, file_id) = MockDatabase::with_single_file(&before); | 213 | let (db, file_id) = TestDB::with_single_file(&before); |
213 | let frange = | 214 | let frange = |
214 | FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) }; | 215 | FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) }; |
215 | let assist = | 216 | let assist = |
@@ -224,12 +225,12 @@ mod helpers { | |||
224 | } | 225 | } |
225 | 226 | ||
226 | pub(crate) fn check_assist_range_target( | 227 | pub(crate) fn check_assist_range_target( |
227 | assist: fn(AssistCtx<MockDatabase>) -> Option<Assist>, | 228 | assist: fn(AssistCtx<TestDB>) -> Option<Assist>, |
228 | before: &str, | 229 | before: &str, |
229 | target: &str, | 230 | target: &str, |
230 | ) { | 231 | ) { |
231 | let (range, before) = extract_range(before); | 232 | let (range, before) = extract_range(before); |
232 | let (db, _source_root, file_id) = MockDatabase::with_single_file(&before); | 233 | let (db, file_id) = TestDB::with_single_file(&before); |
233 | let frange = FileRange { file_id, range }; | 234 | let frange = FileRange { file_id, range }; |
234 | let assist = | 235 | let assist = |
235 | AssistCtx::with_ctx(&db, frange, true, assist).expect("code action is not applicable"); | 236 | AssistCtx::with_ctx(&db, frange, true, assist).expect("code action is not applicable"); |
@@ -243,11 +244,11 @@ mod helpers { | |||
243 | } | 244 | } |
244 | 245 | ||
245 | pub(crate) fn check_assist_not_applicable( | 246 | pub(crate) fn check_assist_not_applicable( |
246 | assist: fn(AssistCtx<MockDatabase>) -> Option<Assist>, | 247 | assist: fn(AssistCtx<TestDB>) -> Option<Assist>, |
247 | before: &str, | 248 | before: &str, |
248 | ) { | 249 | ) { |
249 | let (before_cursor_pos, before) = extract_offset(before); | 250 | let (before_cursor_pos, before) = extract_offset(before); |
250 | let (db, _source_root, file_id) = MockDatabase::with_single_file(&before); | 251 | let (db, file_id) = TestDB::with_single_file(&before); |
251 | let frange = | 252 | let frange = |
252 | FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) }; | 253 | FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) }; |
253 | let assist = AssistCtx::with_ctx(&db, frange, true, assist); | 254 | let assist = AssistCtx::with_ctx(&db, frange, true, assist); |
@@ -255,11 +256,11 @@ mod helpers { | |||
255 | } | 256 | } |
256 | 257 | ||
257 | pub(crate) fn check_assist_range_not_applicable( | 258 | pub(crate) fn check_assist_range_not_applicable( |
258 | assist: fn(AssistCtx<MockDatabase>) -> Option<Assist>, | 259 | assist: fn(AssistCtx<TestDB>) -> Option<Assist>, |
259 | before: &str, | 260 | before: &str, |
260 | ) { | 261 | ) { |
261 | let (range, before) = extract_range(before); | 262 | let (range, before) = extract_range(before); |
262 | let (db, _source_root, file_id) = MockDatabase::with_single_file(&before); | 263 | let (db, file_id) = TestDB::with_single_file(&before); |
263 | let frange = FileRange { file_id, range }; | 264 | let frange = FileRange { file_id, range }; |
264 | let assist = AssistCtx::with_ctx(&db, frange, true, assist); | 265 | let assist = AssistCtx::with_ctx(&db, frange, true, assist); |
265 | assert!(assist.is_none()); | 266 | assert!(assist.is_none()); |
@@ -268,16 +269,17 @@ mod helpers { | |||
268 | 269 | ||
269 | #[cfg(test)] | 270 | #[cfg(test)] |
270 | mod tests { | 271 | mod tests { |
271 | use hir::mock::MockDatabase; | 272 | use ra_db::{fixture::WithFixture, FileRange}; |
272 | use ra_db::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 | ||
276 | use crate::test_db::TestDB; | ||
277 | |||
276 | #[test] | 278 | #[test] |
277 | fn assist_order_field_struct() { | 279 | fn assist_order_field_struct() { |
278 | let before = "struct Foo { <|>bar: u32 }"; | 280 | let before = "struct Foo { <|>bar: u32 }"; |
279 | let (before_cursor_pos, before) = extract_offset(before); | 281 | let (before_cursor_pos, before) = extract_offset(before); |
280 | let (db, _source_root, file_id) = MockDatabase::with_single_file(&before); | 282 | let (db, file_id) = TestDB::with_single_file(&before); |
281 | let frange = | 283 | let frange = |
282 | FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) }; | 284 | FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) }; |
283 | let assists = super::assists(&db, frange); | 285 | let assists = super::assists(&db, frange); |
@@ -298,7 +300,7 @@ mod tests { | |||
298 | } | 300 | } |
299 | }"; | 301 | }"; |
300 | let (range, before) = extract_range(before); | 302 | let (range, before) = extract_range(before); |
301 | let (db, _source_root, file_id) = MockDatabase::with_single_file(&before); | 303 | let (db, file_id) = TestDB::with_single_file(&before); |
302 | let frange = FileRange { file_id, range }; | 304 | let frange = FileRange { file_id, range }; |
303 | let assists = super::assists(&db, frange); | 305 | let assists = super::assists(&db, frange); |
304 | let mut assists = assists.iter(); | 306 | let mut assists = assists.iter(); |
diff --git a/crates/ra_assists/src/test_db.rs b/crates/ra_assists/src/test_db.rs new file mode 100644 index 000000000..5be7383ed --- /dev/null +++ b/crates/ra_assists/src/test_db.rs | |||
@@ -0,0 +1,45 @@ | |||
1 | //! Database used for testing `ra_assists`. | ||
2 | |||
3 | use std::sync::Arc; | ||
4 | |||
5 | use ra_db::{salsa, CrateId, FileId, FileLoader, FileLoaderDelegate, RelativePath}; | ||
6 | |||
7 | #[salsa::database( | ||
8 | ra_db::SourceDatabaseExtStorage, | ||
9 | ra_db::SourceDatabaseStorage, | ||
10 | hir::db::InternDatabaseStorage, | ||
11 | hir::db::AstDatabaseStorage, | ||
12 | hir::db::DefDatabaseStorage, | ||
13 | hir::db::DefDatabase2Storage, | ||
14 | hir::db::HirDatabaseStorage | ||
15 | )] | ||
16 | #[derive(Debug, Default)] | ||
17 | pub struct TestDB { | ||
18 | runtime: salsa::Runtime<TestDB>, | ||
19 | } | ||
20 | |||
21 | impl salsa::Database for TestDB { | ||
22 | fn salsa_runtime(&self) -> &salsa::Runtime<Self> { | ||
23 | &self.runtime | ||
24 | } | ||
25 | } | ||
26 | |||
27 | impl std::panic::RefUnwindSafe for TestDB {} | ||
28 | |||
29 | impl FileLoader for TestDB { | ||
30 | fn file_text(&self, file_id: FileId) -> Arc<String> { | ||
31 | FileLoaderDelegate(self).file_text(file_id) | ||
32 | } | ||
33 | fn resolve_relative_path( | ||
34 | &self, | ||
35 | anchor: FileId, | ||
36 | relative_path: &RelativePath, | ||
37 | ) -> Option<FileId> { | ||
38 | FileLoaderDelegate(self).resolve_relative_path(anchor, relative_path) | ||
39 | } | ||
40 | fn relevant_crates(&self, file_id: FileId) -> Arc<Vec<CrateId>> { | ||
41 | FileLoaderDelegate(self).relevant_crates(file_id) | ||
42 | } | ||
43 | } | ||
44 | |||
45 | impl hir::debug::HirDebugHelper for TestDB {} | ||