From 63f54d234f0d622d043dca8176f0715889a6ed48 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 27 Dec 2018 21:02:08 +0300 Subject: dont leak Name details in testing --- crates/ra_hir/src/module/nameres/tests.rs | 67 +++++++++++++++++++++++++------ crates/ra_hir/src/name.rs | 14 +++---- 2 files changed, 62 insertions(+), 19 deletions(-) diff --git a/crates/ra_hir/src/module/nameres/tests.rs b/crates/ra_hir/src/module/nameres/tests.rs index 165ac81c8..ca20f064f 100644 --- a/crates/ra_hir/src/module/nameres/tests.rs +++ b/crates/ra_hir/src/module/nameres/tests.rs @@ -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::ModuleId) { @@ -22,6 +21,35 @@ fn item_map(fixture: &str) -> (Arc, 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::>(); + lines.sort(); + let actual = lines.join("\n"); + let expected = expected + .trim() + .lines() + .map(|it| it.trim()) + .collect::>() + .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] diff --git a/crates/ra_hir/src/name.rs b/crates/ra_hir/src/name.rs index cdad31be7..e4fc141a6 100644 --- a/crates/ra_hir/src/name.rs +++ b/crates/ra_hir/src/name.rs @@ -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 { 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 { -- cgit v1.2.3