mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-29 13:25:09 +00:00
dont leak Name details in testing
This commit is contained in:
parent
a9f55029b9
commit
63f54d234f
2 changed files with 62 additions and 19 deletions
|
@ -2,14 +2,13 @@ use std::sync::Arc;
|
||||||
|
|
||||||
use salsa::Database;
|
use salsa::Database;
|
||||||
use ra_db::{FilesDatabase, CrateGraph};
|
use ra_db::{FilesDatabase, CrateGraph};
|
||||||
use ra_syntax::SmolStr;
|
|
||||||
use relative_path::RelativePath;
|
use relative_path::RelativePath;
|
||||||
|
use test_utils::assert_eq_text;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
self as hir,
|
self as hir,
|
||||||
db::HirDatabase,
|
db::HirDatabase,
|
||||||
mock::MockDatabase,
|
mock::MockDatabase,
|
||||||
Name,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
fn item_map(fixture: &str) -> (Arc<hir::ItemMap>, hir::ModuleId) {
|
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)
|
(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]
|
#[test]
|
||||||
fn item_map_smoke_test() {
|
fn item_map_smoke_test() {
|
||||||
let (item_map, module_id) = item_map(
|
let (item_map, module_id) = item_map(
|
||||||
|
@ -39,13 +67,18 @@ fn item_map_smoke_test() {
|
||||||
pub struct Baz;
|
pub struct Baz;
|
||||||
",
|
",
|
||||||
);
|
);
|
||||||
let name = Name::new(SmolStr::from("Baz"));
|
check_module_item_map(
|
||||||
let resolution = &item_map.per_module[&module_id].items[&name];
|
&item_map,
|
||||||
assert!(resolution.def_id.take_types().is_some());
|
module_id,
|
||||||
|
"
|
||||||
|
Baz: t v
|
||||||
|
foo: t
|
||||||
|
",
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_self() {
|
fn item_map_using_self() {
|
||||||
let (item_map, module_id) = item_map(
|
let (item_map, module_id) = item_map(
|
||||||
"
|
"
|
||||||
//- /lib.rs
|
//- /lib.rs
|
||||||
|
@ -58,9 +91,14 @@ fn test_self() {
|
||||||
pub struct Baz;
|
pub struct Baz;
|
||||||
",
|
",
|
||||||
);
|
);
|
||||||
let name = Name::new(SmolStr::from("Baz"));
|
check_module_item_map(
|
||||||
let resolution = &item_map.per_module[&module_id].items[&name];
|
&item_map,
|
||||||
assert!(resolution.def_id.take_types().is_some());
|
module_id,
|
||||||
|
"
|
||||||
|
Baz: t v
|
||||||
|
foo: t
|
||||||
|
",
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -91,9 +129,14 @@ fn item_map_across_crates() {
|
||||||
let module_id = module.module_id;
|
let module_id = module.module_id;
|
||||||
let item_map = db.item_map(source_root).unwrap();
|
let item_map = db.item_map(source_root).unwrap();
|
||||||
|
|
||||||
let name = Name::new(SmolStr::from("Baz"));
|
check_module_item_map(
|
||||||
let resolution = &item_map.per_module[&module_id].items[&name];
|
&item_map,
|
||||||
assert!(resolution.def_id.take_types().is_some());
|
module_id,
|
||||||
|
"
|
||||||
|
Baz: t v
|
||||||
|
test_crate: t
|
||||||
|
",
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
|
@ -5,7 +5,7 @@ use ra_syntax::{ast, SmolStr};
|
||||||
/// `Name` is a wrapper around string, which is used in hir for both references
|
/// `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
|
/// and declarations. In theory, names should also carry hygene info, but we are
|
||||||
/// not there yet!
|
/// not there yet!
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
#[derive(Clone, PartialEq, Eq, Hash)]
|
||||||
pub struct Name {
|
pub struct Name {
|
||||||
text: SmolStr,
|
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 {
|
impl Name {
|
||||||
pub(crate) fn as_known_name(&self) -> Option<KnownName> {
|
pub(crate) fn as_known_name(&self) -> Option<KnownName> {
|
||||||
let name = match self.text.as_str() {
|
let name = match self.text.as_str() {
|
||||||
|
@ -38,15 +44,9 @@ impl Name {
|
||||||
Some(name)
|
Some(name)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(test))]
|
|
||||||
fn new(text: SmolStr) -> Name {
|
fn new(text: SmolStr) -> Name {
|
||||||
Name { text }
|
Name { text }
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
pub(crate) fn new(text: SmolStr) -> Name {
|
|
||||||
Name { text }
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) trait AsName {
|
pub(crate) trait AsName {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue