From e805e8c1d5bf26e9716fb855f97d950395129c20 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 29 Jun 2020 14:21:57 +0200 Subject: (T): make typification tests more data driven --- crates/ra_hir_ty/src/tests.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'crates/ra_hir_ty/src/tests.rs') diff --git a/crates/ra_hir_ty/src/tests.rs b/crates/ra_hir_ty/src/tests.rs index 2a85ce85d..4d0dc3011 100644 --- a/crates/ra_hir_ty/src/tests.rs +++ b/crates/ra_hir_ty/src/tests.rs @@ -28,6 +28,7 @@ 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, @@ -37,6 +38,21 @@ use crate::{ // against snapshots of the expected results using insta. Use cargo-insta to // update the snapshots. +fn check_types(ra_fixture: &str) { + 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 (offset, expected) in annotations { + let actual = type_at_pos(&db, FilePosition { file_id, offset }); + assert_eq!(expected, actual); + checked_one = true; + } + } + assert!(checked_one, "no `//^` annotations found"); +} + fn type_at_pos(db: &TestDB, pos: FilePosition) -> String { type_at_pos_displayed(db, pos, |ty, _| ty.display(db).to_string()) } -- cgit v1.2.3 From bbc4dc995612916a4c0a99396b5259a5fb1dda80 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 29 Jun 2020 17:22:47 +0200 Subject: Update the rest of the tests --- crates/ra_hir_ty/src/tests.rs | 46 ++++++++++++++++++++----------------------- 1 file changed, 21 insertions(+), 25 deletions(-) (limited to 'crates/ra_hir_ty/src/tests.rs') diff --git a/crates/ra_hir_ty/src/tests.rs b/crates/ra_hir_ty/src/tests.rs index 4d0dc3011..5424e6bb1 100644 --- a/crates/ra_hir_ty/src/tests.rs +++ b/crates/ra_hir_ty/src/tests.rs @@ -17,11 +17,11 @@ use hir_def::{ item_scope::ItemScope, keys, nameres::CrateDefMap, - AssocItemId, DefWithBodyId, LocalModuleId, Lookup, ModuleDefId, ModuleId, + AssocItemId, DefWithBodyId, LocalModuleId, Lookup, ModuleDefId, }; use hir_expand::{db::AstDatabase, InFile}; use insta::assert_snapshot; -use ra_db::{fixture::WithFixture, salsa::Database, FilePosition, SourceDatabase}; +use ra_db::{fixture::WithFixture, salsa::Database, FileRange, SourceDatabase}; use ra_syntax::{ algo, ast::{self, AstNode}, @@ -39,13 +39,27 @@ use crate::{ // update the snapshots. fn check_types(ra_fixture: &str) { + check_types_impl(ra_fixture, false) +} + +fn check_types_source_code(ra_fixture: &str) { + check_types_impl(ra_fixture, true) +} + +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 (offset, expected) in annotations { - let actual = type_at_pos(&db, FilePosition { file_id, offset }); + for (range, expected) in annotations { + let ty = type_at_range(&db, FileRange { file_id, range }); + let actual = if display_source { + let module = db.module_for_file(file_id); + ty.display_source_code(&db, module).unwrap() + } else { + ty.display(&db).to_string() + }; assert_eq!(expected, actual); checked_one = true; } @@ -53,21 +67,9 @@ fn check_types(ra_fixture: &str) { assert!(checked_one, "no `//^` annotations found"); } -fn type_at_pos(db: &TestDB, pos: FilePosition) -> String { - type_at_pos_displayed(db, pos, |ty, _| ty.display(db).to_string()) -} - -fn displayed_source_at_pos(db: &TestDB, pos: FilePosition) -> String { - type_at_pos_displayed(db, pos, |ty, module_id| ty.display_source_code(db, module_id).unwrap()) -} - -fn type_at_pos_displayed( - db: &TestDB, - pos: FilePosition, - display_fn: impl FnOnce(&Ty, ModuleId) -> String, -) -> String { +fn type_at_range(db: &TestDB, pos: FileRange) -> Ty { let file = db.parse(pos.file_id).ok().unwrap(); - let expr = algo::find_node_at_offset::(file.syntax(), pos.offset).unwrap(); + let expr = algo::find_node_at_range::(file.syntax(), pos.range).unwrap(); let fn_def = expr.syntax().ancestors().find_map(ast::FnDef::cast).unwrap(); let module = db.module_for_file(pos.file_id); let func = *module.child_by_source(db)[keys::FUNCTION] @@ -77,17 +79,11 @@ fn type_at_pos_displayed( let (_body, source_map) = db.body_with_source_map(func.into()); if let Some(expr_id) = source_map.node_expr(InFile::new(pos.file_id.into(), &expr)) { let infer = db.infer(func.into()); - let ty = &infer[expr_id]; - return display_fn(ty, module); + return infer[expr_id].clone(); } panic!("Can't find expression") } -fn type_at(ra_fixture: &str) -> String { - let (db, file_pos) = TestDB::with_position(ra_fixture); - type_at_pos(&db, file_pos) -} - fn infer(ra_fixture: &str) -> String { infer_with_mismatches(ra_fixture, false) } -- cgit v1.2.3