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 ++++++++-------- crates/ra_hir/src/expr/scope.rs | 6 ++--- crates/ra_hir/src/mock.rs | 48 ++------------------------------------ crates/ra_hir/src/ty/tests.rs | 2 +- 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 @@ 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(); 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 #[cfg(test)] mod tests { - use ra_db::SourceDatabase; + use ra_db::{fixture::WithFixture, SourceDatabase}; use ra_syntax::{algo::find_node_at_offset, ast, AstNode}; use test_utils::{assert_eq_text, extract_offset}; @@ -191,7 +191,7 @@ mod tests { buf }; - let (db, _source_root, file_id) = MockDatabase::with_single_file(&code); + let (db, file_id) = MockDatabase::with_single_file(&code); let file = db.parse(file_id).ok().unwrap(); let marker: ast::PathExpr = find_node_at_offset(file.syntax(), off).unwrap(); let analyzer = SourceAnalyzer::new(&db, file_id, marker.syntax(), None); @@ -288,7 +288,7 @@ mod tests { fn do_check_local_name(code: &str, expected_offset: u32) { let (off, code) = extract_offset(code); - let (db, _source_root, file_id) = MockDatabase::with_single_file(&code); + let (db, file_id) = MockDatabase::with_single_file(&code); let file = db.parse(file_id).ok().unwrap(); let expected_name = find_node_at_offset::(file.syntax(), expected_offset.into()) .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}; use hir_def::{db::DefDatabase2, ModuleId}; use hir_expand::diagnostics::DiagnosticSink; use parking_lot::Mutex; -use ra_cfg::CfgOptions; use ra_db::{ - salsa, CrateGraph, CrateId, Edition, FileId, FileLoader, FileLoaderDelegate, RelativePath, - RelativePathBuf, SourceDatabase, SourceDatabaseExt, SourceRoot, SourceRootId, + salsa, CrateId, FileId, FileLoader, FileLoaderDelegate, RelativePath, SourceDatabase, + SourceRootId, }; use rustc_hash::FxHashMap; @@ -63,14 +62,6 @@ impl HirDebugHelper for MockDatabase { } impl MockDatabase { - pub fn with_single_file(text: &str) -> (MockDatabase, SourceRoot, FileId) { - let mut db = MockDatabase::default(); - let mut source_root = SourceRoot::default(); - let file_id = db.add_file(WORKSPACE, "/", &mut source_root, "/main.rs", text); - db.set_source_root(WORKSPACE, Arc::new(source_root.clone())); - (db, source_root, file_id) - } - pub fn file_id_of(&self, path: &str) -> FileId { match self.files.get(path) { Some(it) => *it, @@ -96,41 +87,6 @@ impl MockDatabase { } buf } - - fn add_file( - &mut self, - source_root_id: SourceRootId, - source_root_prefix: &str, - source_root: &mut SourceRoot, - path: &str, - text: &str, - ) -> FileId { - assert!(source_root_prefix.starts_with('/')); - assert!(source_root_prefix.ends_with('/')); - assert!(path.starts_with(source_root_prefix)); - let rel_path = RelativePathBuf::from_path(&path[source_root_prefix.len()..]).unwrap(); - - let is_crate_root = rel_path == "lib.rs" || rel_path == "/main.rs"; - - let file_id = FileId(self.files.len() as u32); - - let prev = self.files.insert(path.to_string(), file_id); - assert!(prev.is_none(), "duplicate files in the text fixture"); - Arc::make_mut(&mut self.file_paths).insert(file_id, path.to_string()); - - let text = Arc::new(text.to_string()); - self.set_file_text(file_id, text); - self.set_file_relative_path(file_id, rel_path.clone()); - self.set_file_source_root(file_id, source_root_id); - source_root.insert_file(rel_path, file_id); - - if is_crate_root { - let mut crate_graph = CrateGraph::default(); - crate_graph.add_crate_root(file_id, Edition::Edition2018, CfgOptions::default()); - self.set_crate_graph(Arc::new(crate_graph)); - } - file_id - } } impl 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 { } fn infer(content: &str) -> String { - let (db, _, file_id) = MockDatabase::with_single_file(content); + let (db, file_id) = MockDatabase::with_single_file(content); let source_file = db.parse(file_id).ok().unwrap(); let mut acc = String::new(); -- cgit v1.2.3