dont leak Name details in testing

This commit is contained in:
Aleksey Kladov 2018-12-27 21:02:08 +03:00
parent a9f55029b9
commit 63f54d234f
2 changed files with 62 additions and 19 deletions

View File

@ -2,14 +2,13 @@ use std::sync::Arc;
use salsa::Database;
use ra_db::{FilesDatabase, CrateGraph};
use ra_syntax::SmolStr;
use relative_path::RelativePath;
use test_utils::assert_eq_text;
use crate::{
self as hir,
db::HirDatabase,
mock::MockDatabase,
Name,
};
fn item_map(fixture: &str) -> (Arc<hir::ItemMap>, hir::ModuleId) {
@ -22,6 +21,35 @@ fn item_map(fixture: &str) -> (Arc<hir::ItemMap>, hir::ModuleId) {
(db.item_map(source_root).unwrap(), module_id)
}
fn check_module_item_map(map: &hir::ItemMap, module_id: hir::ModuleId, expected: &str) {
let mut lines = map.per_module[&module_id]
.items
.iter()
.map(|(name, res)| format!("{}: {}", name, dump_resolution(res)))
.collect::<Vec<_>>();
lines.sort();
let actual = lines.join("\n");
let expected = expected
.trim()
.lines()
.map(|it| it.trim())
.collect::<Vec<_>>()
.join("\n");
assert_eq_text!(&actual, &expected);
fn dump_resolution(resolution: &hir::Resolution) -> &'static str {
match (
resolution.def_id.types.is_some(),
resolution.def_id.values.is_some(),
) {
(true, true) => "t v",
(true, false) => "t",
(false, true) => "v",
(false, false) => "_",
}
}
}
#[test]
fn item_map_smoke_test() {
let (item_map, module_id) = item_map(
@ -39,13 +67,18 @@ fn item_map_smoke_test() {
pub struct Baz;
",
);
let name = Name::new(SmolStr::from("Baz"));
let resolution = &item_map.per_module[&module_id].items[&name];
assert!(resolution.def_id.take_types().is_some());
check_module_item_map(
&item_map,
module_id,
"
Baz: t v
foo: t
",
);
}
#[test]
fn test_self() {
fn item_map_using_self() {
let (item_map, module_id) = item_map(
"
//- /lib.rs
@ -58,9 +91,14 @@ fn test_self() {
pub struct Baz;
",
);
let name = Name::new(SmolStr::from("Baz"));
let resolution = &item_map.per_module[&module_id].items[&name];
assert!(resolution.def_id.take_types().is_some());
check_module_item_map(
&item_map,
module_id,
"
Baz: t v
foo: t
",
);
}
#[test]
@ -91,9 +129,14 @@ fn item_map_across_crates() {
let module_id = module.module_id;
let item_map = db.item_map(source_root).unwrap();
let name = Name::new(SmolStr::from("Baz"));
let resolution = &item_map.per_module[&module_id].items[&name];
assert!(resolution.def_id.take_types().is_some());
check_module_item_map(
&item_map,
module_id,
"
Baz: t v
test_crate: t
",
);
}
#[test]

View File

@ -5,7 +5,7 @@ use ra_syntax::{ast, SmolStr};
/// `Name` is a wrapper around string, which is used in hir for both references
/// and declarations. In theory, names should also carry hygene info, but we are
/// not there yet!
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct Name {
text: SmolStr,
}
@ -16,6 +16,12 @@ impl fmt::Display for Name {
}
}
impl fmt::Debug for Name {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Debug::fmt(&self.text, f)
}
}
impl Name {
pub(crate) fn as_known_name(&self) -> Option<KnownName> {
let name = match self.text.as_str() {
@ -38,15 +44,9 @@ impl Name {
Some(name)
}
#[cfg(not(test))]
fn new(text: SmolStr) -> Name {
Name { text }
}
#[cfg(test)]
pub(crate) fn new(text: SmolStr) -> Name {
Name { text }
}
}
pub(crate) trait AsName {