From 24894aca3162d654ee051f591a920ef1525d821f Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 4 Nov 2019 22:12:49 +0300 Subject: Remove more duplication in test fixtures --- crates/ra_assists/src/doc_tests.rs | 4 ++-- crates/ra_assists/src/lib.rs | 20 ++++++++++---------- 2 files changed, 12 insertions(+), 12 deletions(-) (limited to 'crates/ra_assists') 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 @@ mod generated; use hir::mock::MockDatabase; -use ra_db::FileRange; +use ra_db::{fixture::WithFixture, FileRange}; use test_utils::{assert_eq_text, extract_range_or_offset}; fn check(assist_id: &str, before: &str, after: &str) { let (selection, before) = extract_range_or_offset(before); - let (db, _source_root, file_id) = MockDatabase::with_single_file(&before); + let (db, file_id) = MockDatabase::with_single_file(&before); let frange = FileRange { file_id, range: selection.into() }; 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 { #[cfg(test)] mod helpers { use hir::mock::MockDatabase; - use ra_db::FileRange; + use ra_db::{fixture::WithFixture, FileRange}; use ra_syntax::TextRange; use test_utils::{add_cursor, assert_eq_text, extract_offset, extract_range}; @@ -159,7 +159,7 @@ mod helpers { after: &str, ) { let (before_cursor_pos, before) = extract_offset(before); - let (db, _source_root, file_id) = MockDatabase::with_single_file(&before); + let (db, file_id) = MockDatabase::with_single_file(&before); let frange = FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) }; let assist = @@ -187,7 +187,7 @@ mod helpers { after: &str, ) { let (range, before) = extract_range(before); - let (db, _source_root, file_id) = MockDatabase::with_single_file(&before); + let (db, file_id) = MockDatabase::with_single_file(&before); let frange = FileRange { file_id, range }; let assist = AssistCtx::with_ctx(&db, frange, true, assist).expect("code action is not applicable"); @@ -209,7 +209,7 @@ mod helpers { target: &str, ) { let (before_cursor_pos, before) = extract_offset(before); - let (db, _source_root, file_id) = MockDatabase::with_single_file(&before); + let (db, file_id) = MockDatabase::with_single_file(&before); let frange = FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) }; let assist = @@ -229,7 +229,7 @@ mod helpers { target: &str, ) { let (range, before) = extract_range(before); - let (db, _source_root, file_id) = MockDatabase::with_single_file(&before); + let (db, file_id) = MockDatabase::with_single_file(&before); let frange = FileRange { file_id, range }; let assist = AssistCtx::with_ctx(&db, frange, true, assist).expect("code action is not applicable"); @@ -247,7 +247,7 @@ mod helpers { before: &str, ) { let (before_cursor_pos, before) = extract_offset(before); - let (db, _source_root, file_id) = MockDatabase::with_single_file(&before); + let (db, file_id) = MockDatabase::with_single_file(&before); let frange = FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) }; let assist = AssistCtx::with_ctx(&db, frange, true, assist); @@ -259,7 +259,7 @@ mod helpers { before: &str, ) { let (range, before) = extract_range(before); - let (db, _source_root, file_id) = MockDatabase::with_single_file(&before); + let (db, file_id) = MockDatabase::with_single_file(&before); let frange = FileRange { file_id, range }; let assist = AssistCtx::with_ctx(&db, frange, true, assist); assert!(assist.is_none()); @@ -269,7 +269,7 @@ mod helpers { #[cfg(test)] mod tests { use hir::mock::MockDatabase; - use ra_db::FileRange; + use ra_db::{fixture::WithFixture, FileRange}; use ra_syntax::TextRange; use test_utils::{extract_offset, extract_range}; @@ -277,7 +277,7 @@ mod tests { fn assist_order_field_struct() { let before = "struct Foo { <|>bar: u32 }"; let (before_cursor_pos, before) = extract_offset(before); - let (db, _source_root, file_id) = MockDatabase::with_single_file(&before); + let (db, file_id) = MockDatabase::with_single_file(&before); let frange = FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) }; let assists = super::assists(&db, frange); @@ -298,7 +298,7 @@ mod tests { } }"; let (range, before) = extract_range(before); - let (db, _source_root, file_id) = MockDatabase::with_single_file(&before); + let (db, file_id) = MockDatabase::with_single_file(&before); let frange = FileRange { file_id, range }; let assists = super::assists(&db, frange); let mut assists = assists.iter(); -- cgit v1.2.3 From 74d827bb8001952f90c85fca3d064fe3096009bd Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 4 Nov 2019 22:21:15 +0300 Subject: Rename MockDatabase -> TestDB Actually working rename is sooo useful! --- crates/ra_assists/src/doc_tests.rs | 4 ++-- crates/ra_assists/src/lib.rs | 32 ++++++++++++++++---------------- 2 files changed, 18 insertions(+), 18 deletions(-) (limited to 'crates/ra_assists') diff --git a/crates/ra_assists/src/doc_tests.rs b/crates/ra_assists/src/doc_tests.rs index 43194037e..2500a5ab7 100644 --- a/crates/ra_assists/src/doc_tests.rs +++ b/crates/ra_assists/src/doc_tests.rs @@ -5,13 +5,13 @@ mod generated; -use hir::mock::MockDatabase; +use hir::mock::TestDB; use ra_db::{fixture::WithFixture, FileRange}; use test_utils::{assert_eq_text, extract_range_or_offset}; fn check(assist_id: &str, before: &str, after: &str) { let (selection, before) = extract_range_or_offset(before); - let (db, file_id) = MockDatabase::with_single_file(&before); + let (db, file_id) = TestDB::with_single_file(&before); let frange = FileRange { file_id, range: selection.into() }; 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 13db37d29..85fb72395 100644 --- a/crates/ra_assists/src/lib.rs +++ b/crates/ra_assists/src/lib.rs @@ -146,7 +146,7 @@ mod assists { #[cfg(test)] mod helpers { - use hir::mock::MockDatabase; + use hir::mock::TestDB; use ra_db::{fixture::WithFixture, FileRange}; use ra_syntax::TextRange; use test_utils::{add_cursor, assert_eq_text, extract_offset, extract_range}; @@ -154,12 +154,12 @@ mod helpers { use crate::{Assist, AssistCtx}; pub(crate) fn check_assist( - assist: fn(AssistCtx) -> Option, + assist: fn(AssistCtx) -> Option, before: &str, after: &str, ) { let (before_cursor_pos, before) = extract_offset(before); - let (db, file_id) = MockDatabase::with_single_file(&before); + let (db, file_id) = TestDB::with_single_file(&before); let frange = FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) }; let assist = @@ -182,12 +182,12 @@ mod helpers { } pub(crate) fn check_assist_range( - assist: fn(AssistCtx) -> Option, + assist: fn(AssistCtx) -> Option, before: &str, after: &str, ) { let (range, before) = extract_range(before); - let (db, file_id) = MockDatabase::with_single_file(&before); + let (db, file_id) = TestDB::with_single_file(&before); let frange = FileRange { file_id, range }; let assist = AssistCtx::with_ctx(&db, frange, true, assist).expect("code action is not applicable"); @@ -204,12 +204,12 @@ mod helpers { } pub(crate) fn check_assist_target( - assist: fn(AssistCtx) -> Option, + assist: fn(AssistCtx) -> Option, before: &str, target: &str, ) { let (before_cursor_pos, before) = extract_offset(before); - let (db, file_id) = MockDatabase::with_single_file(&before); + let (db, file_id) = TestDB::with_single_file(&before); let frange = FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) }; let assist = @@ -224,12 +224,12 @@ mod helpers { } pub(crate) fn check_assist_range_target( - assist: fn(AssistCtx) -> Option, + assist: fn(AssistCtx) -> Option, before: &str, target: &str, ) { let (range, before) = extract_range(before); - let (db, file_id) = MockDatabase::with_single_file(&before); + let (db, file_id) = TestDB::with_single_file(&before); let frange = FileRange { file_id, range }; let assist = AssistCtx::with_ctx(&db, frange, true, assist).expect("code action is not applicable"); @@ -243,11 +243,11 @@ mod helpers { } pub(crate) fn check_assist_not_applicable( - assist: fn(AssistCtx) -> Option, + assist: fn(AssistCtx) -> Option, before: &str, ) { let (before_cursor_pos, before) = extract_offset(before); - let (db, file_id) = MockDatabase::with_single_file(&before); + let (db, file_id) = TestDB::with_single_file(&before); let frange = FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) }; let assist = AssistCtx::with_ctx(&db, frange, true, assist); @@ -255,11 +255,11 @@ mod helpers { } pub(crate) fn check_assist_range_not_applicable( - assist: fn(AssistCtx) -> Option, + assist: fn(AssistCtx) -> Option, before: &str, ) { let (range, before) = extract_range(before); - let (db, file_id) = MockDatabase::with_single_file(&before); + let (db, file_id) = TestDB::with_single_file(&before); let frange = FileRange { file_id, range }; let assist = AssistCtx::with_ctx(&db, frange, true, assist); assert!(assist.is_none()); @@ -268,7 +268,7 @@ mod helpers { #[cfg(test)] mod tests { - use hir::mock::MockDatabase; + use hir::mock::TestDB; use ra_db::{fixture::WithFixture, FileRange}; use ra_syntax::TextRange; use test_utils::{extract_offset, extract_range}; @@ -277,7 +277,7 @@ mod tests { fn assist_order_field_struct() { let before = "struct Foo { <|>bar: u32 }"; let (before_cursor_pos, before) = extract_offset(before); - let (db, file_id) = MockDatabase::with_single_file(&before); + let (db, file_id) = TestDB::with_single_file(&before); let frange = FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) }; let assists = super::assists(&db, frange); @@ -298,7 +298,7 @@ mod tests { } }"; let (range, before) = extract_range(before); - let (db, file_id) = MockDatabase::with_single_file(&before); + let (db, file_id) = TestDB::with_single_file(&before); let frange = FileRange { file_id, range }; let assists = super::assists(&db, frange); let mut assists = assists.iter(); -- cgit v1.2.3 From 42370610ce03c1bb5893d9e29c2c215482e8d8ca Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 4 Nov 2019 22:28:47 +0300 Subject: Restore assists tests --- crates/ra_assists/src/doc_tests.rs | 3 ++- crates/ra_assists/src/lib.rs | 8 ++++--- crates/ra_assists/src/test_db.rs | 43 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 4 deletions(-) create mode 100644 crates/ra_assists/src/test_db.rs (limited to 'crates/ra_assists') diff --git a/crates/ra_assists/src/doc_tests.rs b/crates/ra_assists/src/doc_tests.rs index 2500a5ab7..a8f8446cb 100644 --- a/crates/ra_assists/src/doc_tests.rs +++ b/crates/ra_assists/src/doc_tests.rs @@ -5,10 +5,11 @@ mod generated; -use hir::mock::TestDB; use ra_db::{fixture::WithFixture, FileRange}; use test_utils::{assert_eq_text, extract_range_or_offset}; +use crate::test_db::TestDB; + fn check(assist_id: &str, before: &str, after: &str) { let (selection, before) = extract_range_or_offset(before); let (db, file_id) = TestDB::with_single_file(&before); diff --git a/crates/ra_assists/src/lib.rs b/crates/ra_assists/src/lib.rs index 85fb72395..39c1c283f 100644 --- a/crates/ra_assists/src/lib.rs +++ b/crates/ra_assists/src/lib.rs @@ -9,6 +9,8 @@ mod assist_ctx; mod marks; #[cfg(test)] mod doc_tests; +#[cfg(test)] +mod test_db; use hir::db::HirDatabase; use ra_db::FileRange; @@ -146,12 +148,11 @@ mod assists { #[cfg(test)] mod helpers { - use hir::mock::TestDB; use ra_db::{fixture::WithFixture, FileRange}; use ra_syntax::TextRange; use test_utils::{add_cursor, assert_eq_text, extract_offset, extract_range}; - use crate::{Assist, AssistCtx}; + use crate::{test_db::TestDB, Assist, AssistCtx}; pub(crate) fn check_assist( assist: fn(AssistCtx) -> Option, @@ -268,11 +269,12 @@ mod helpers { #[cfg(test)] mod tests { - use hir::mock::TestDB; use ra_db::{fixture::WithFixture, FileRange}; use ra_syntax::TextRange; use test_utils::{extract_offset, extract_range}; + use crate::test_db::TestDB; + #[test] fn assist_order_field_struct() { let before = "struct Foo { <|>bar: u32 }"; diff --git a/crates/ra_assists/src/test_db.rs b/crates/ra_assists/src/test_db.rs new file mode 100644 index 000000000..7b196561a --- /dev/null +++ b/crates/ra_assists/src/test_db.rs @@ -0,0 +1,43 @@ +use std::sync::Arc; + +use ra_db::{salsa, CrateId, FileId, FileLoader, FileLoaderDelegate, RelativePath}; + +#[salsa::database( + ra_db::SourceDatabaseExtStorage, + ra_db::SourceDatabaseStorage, + hir::db::InternDatabaseStorage, + hir::db::AstDatabaseStorage, + hir::db::DefDatabaseStorage, + hir::db::DefDatabase2Storage, + hir::db::HirDatabaseStorage +)] +#[derive(Debug, Default)] +pub struct TestDB { + runtime: salsa::Runtime, +} + +impl salsa::Database for TestDB { + fn salsa_runtime(&self) -> &salsa::Runtime { + &self.runtime + } +} + +impl std::panic::RefUnwindSafe for TestDB {} + +impl FileLoader for TestDB { + fn file_text(&self, file_id: FileId) -> Arc { + FileLoaderDelegate(self).file_text(file_id) + } + fn resolve_relative_path( + &self, + anchor: FileId, + relative_path: &RelativePath, + ) -> Option { + FileLoaderDelegate(self).resolve_relative_path(anchor, relative_path) + } + fn relevant_crates(&self, file_id: FileId) -> Arc> { + FileLoaderDelegate(self).relevant_crates(file_id) + } +} + +impl hir::debug::HirDebugHelper for TestDB {} -- cgit v1.2.3 From 50364bd47815f4f9174d46ae33f44f7f56545b2d Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 4 Nov 2019 22:33:21 +0300 Subject: Appease the linter by dummy doc comments --- crates/ra_assists/src/test_db.rs | 2 ++ 1 file changed, 2 insertions(+) (limited to 'crates/ra_assists') diff --git a/crates/ra_assists/src/test_db.rs b/crates/ra_assists/src/test_db.rs index 7b196561a..5be7383ed 100644 --- a/crates/ra_assists/src/test_db.rs +++ b/crates/ra_assists/src/test_db.rs @@ -1,3 +1,5 @@ +//! Database used for testing `ra_assists`. + use std::sync::Arc; use ra_db::{salsa, CrateId, FileId, FileLoader, FileLoaderDelegate, RelativePath}; -- cgit v1.2.3