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_hir/src/expr/scope.rs | 6 +++--- crates/ra_hir/src/mock.rs | 48 ++--------------------------------------- crates/ra_hir/src/ty/tests.rs | 2 +- 3 files changed, 6 insertions(+), 50 deletions(-) (limited to 'crates/ra_hir/src') 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