From d34e725f09994af145884464942dd205bfc7717e Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 30 Jun 2020 12:14:16 +0200 Subject: Better factoring --- crates/ra_hir_ty/src/test_db.rs | 22 +++++++++++++++++----- crates/ra_hir_ty/src/tests.rs | 5 +---- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/crates/ra_hir_ty/src/test_db.rs b/crates/ra_hir_ty/src/test_db.rs index 0481a7b12..fddf0604d 100644 --- a/crates/ra_hir_ty/src/test_db.rs +++ b/crates/ra_hir_ty/src/test_db.rs @@ -8,8 +8,10 @@ use std::{ use hir_def::{db::DefDatabase, AssocItemId, ModuleDefId, ModuleId}; use hir_expand::{db::AstDatabase, diagnostics::DiagnosticSink}; use ra_db::{salsa, CrateId, FileId, FileLoader, FileLoaderDelegate, SourceDatabase, Upcast}; -use rustc_hash::FxHashSet; +use ra_syntax::TextRange; +use rustc_hash::{FxHashMap, FxHashSet}; use stdx::format_to; +use test_utils::extract_annotations; use crate::{ db::HirDatabase, diagnostics::Diagnostic, expr::ExprValidator, @@ -155,17 +157,27 @@ impl TestDB { (buf, count) } - pub fn all_files(&self) -> Vec { - let mut res = Vec::new(); + pub fn extract_annotations(&self) -> FxHashMap> { + let mut files = Vec::new(); let crate_graph = self.crate_graph(); for krate in crate_graph.iter() { let crate_def_map = self.crate_def_map(krate); for (module_id, _) in crate_def_map.modules.iter() { let file_id = crate_def_map[module_id].origin.file_id(); - res.extend(file_id) + files.extend(file_id) } } - res + files + .into_iter() + .filter_map(|file_id| { + let text = self.file_text(file_id); + let annotations = extract_annotations(&text); + if annotations.is_empty() { + return None; + } + Some((file_id, annotations)) + }) + .collect() } } diff --git a/crates/ra_hir_ty/src/tests.rs b/crates/ra_hir_ty/src/tests.rs index 5424e6bb1..9084c3bed 100644 --- a/crates/ra_hir_ty/src/tests.rs +++ b/crates/ra_hir_ty/src/tests.rs @@ -28,7 +28,6 @@ use ra_syntax::{ SyntaxNode, }; use stdx::format_to; -use test_utils::extract_annotations; use crate::{ db::HirDatabase, display::HirDisplay, infer::TypeMismatch, test_db::TestDB, InferenceResult, Ty, @@ -49,9 +48,7 @@ fn check_types_source_code(ra_fixture: &str) { fn check_types_impl(ra_fixture: &str, display_source: bool) { let db = TestDB::with_files(ra_fixture); let mut checked_one = false; - for file_id in db.all_files() { - let text = db.parse(file_id).syntax_node().to_string(); - let annotations = extract_annotations(&text); + for (file_id, annotations) in db.extract_annotations() { for (range, expected) in annotations { let ty = type_at_range(&db, FileRange { file_id, range }); let actual = if display_source { -- cgit v1.2.3