Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions crates/ra_hir_ty/src/test_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,19 @@ impl TestDB {
});
(buf, count)
}

pub fn all_files(&self) -> Vec<FileId> {
let mut res = 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)
}
}
res
}
}

impl TestDB {
Expand Down
50 changes: 31 additions & 19 deletions crates/ra_hir_ty/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,18 @@ 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},
SyntaxNode,
};
use stdx::format_to;
use test_utils::extract_annotations;

use crate::{
db::HirDatabase, display::HirDisplay, infer::TypeMismatch, test_db::TestDB, InferenceResult, Ty,
Expand All @@ -37,21 +38,38 @@ use crate::{
// against snapshots of the expected results using insta. Use cargo-insta to
// update the snapshots.

fn type_at_pos(db: &TestDB, pos: FilePosition) -> String {
type_at_pos_displayed(db, pos, |ty, _| ty.display(db).to_string())
fn check_types(ra_fixture: &str) {
check_types_impl(ra_fixture, false)
}

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 check_types_source_code(ra_fixture: &str) {
check_types_impl(ra_fixture, true)
}

fn type_at_pos_displayed(
db: &TestDB,
pos: FilePosition,
display_fn: impl FnOnce(&Ty, ModuleId) -> String,
) -> String {
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 (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;
}
}
assert!(checked_one, "no `//^` annotations found");
}

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::<ast::Expr>(file.syntax(), pos.offset).unwrap();
let expr = algo::find_node_at_range::<ast::Expr>(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]
Expand All @@ -61,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)
}
Expand Down
3 changes: 2 additions & 1 deletion crates/ra_hir_ty/src/tests/coercion.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use super::infer_with_mismatches;
use insta::assert_snapshot;
use test_utils::mark;

use super::infer_with_mismatches;

// Infer with some common definitions and impls.
fn infer(source: &str) -> String {
let defs = r#"
Expand Down
49 changes: 20 additions & 29 deletions crates/ra_hir_ty/src/tests/display_source_code.rs
Original file line number Diff line number Diff line change
@@ -1,50 +1,41 @@
use super::displayed_source_at_pos;
use crate::test_db::TestDB;
use ra_db::fixture::WithFixture;
use super::check_types_source_code;

#[test]
fn qualify_path_to_submodule() {
let (db, pos) = TestDB::with_position(
check_types_source_code(
r#"
//- /main.rs

mod foo {
pub struct Foo;
}

fn bar() {
let foo: foo::Foo = foo::Foo;
foo<|>
}
foo
} //^ foo::Foo

"#,
);
assert_eq!("foo::Foo", displayed_source_at_pos(&db, pos));
}

#[test]
fn omit_default_type_parameters() {
let (db, pos) = TestDB::with_position(
r"
//- /main.rs
struct Foo<T = u8> { t: T }
fn main() {
let foo = Foo { t: 5u8 };
foo<|>;
}
",
check_types_source_code(
r#"
struct Foo<T = u8> { t: T }
fn main() {
let foo = Foo { t: 5u8 };
foo;
} //^ Foo
"#,
);
assert_eq!("Foo", displayed_source_at_pos(&db, pos));

let (db, pos) = TestDB::with_position(
r"
//- /main.rs
struct Foo<K, T = u8> { k: K, t: T }
fn main() {
let foo = Foo { k: 400, t: 5u8 };
foo<|>;
}
",
check_types_source_code(
r#"
struct Foo<K, T = u8> { k: K, t: T }
fn main() {
let foo = Foo { k: 400, t: 5u8 };
foo;
} //^ Foo<i32>
"#,
);
assert_eq!("Foo<i32>", displayed_source_at_pos(&db, pos));
}
Loading