rust-analyzer/crates/ra_hir/src/nameres/tests.rs

579 lines
11 KiB
Rust
Raw Normal View History

2019-03-14 08:53:40 +00:00
mod macros;
mod globs;
mod incremental;
2019-05-30 12:14:11 +00:00
mod primitives;
2019-09-05 18:43:32 +00:00
mod mod_resolution;
2019-03-14 08:53:40 +00:00
use std::sync::Arc;
2019-08-29 13:49:10 +00:00
use insta::assert_snapshot;
use ra_db::SourceDatabase;
use test_utils::covers;
2019-05-21 10:18:30 +00:00
use crate::{
mock::{CrateGraphFixture, MockDatabase},
Crate,
2019-05-21 10:18:30 +00:00
};
use super::*;
fn compute_crate_def_map(fixture: &str, graph: Option<CrateGraphFixture>) -> Arc<CrateDefMap> {
let mut db = MockDatabase::with_files(fixture);
if let Some(graph) = graph {
db.set_crate_graph_from_fixture(graph);
}
let crate_id = db.crate_graph().iter().next().unwrap();
let krate = Crate { crate_id };
2019-03-13 13:38:02 +00:00
db.crate_def_map(krate)
}
fn render_crate_def_map(map: &CrateDefMap) -> String {
let mut buf = String::new();
go(&mut buf, map, "\ncrate", map.root);
2019-05-21 10:18:30 +00:00
return buf.trim().to_string();
2019-03-16 15:57:53 +00:00
fn go(buf: &mut String, map: &CrateDefMap, path: &str, module: CrateModuleId) {
*buf += path;
*buf += "\n";
2019-05-21 10:18:30 +00:00
let mut entries = map.modules[module]
.scope
.items
.iter()
.map(|(name, res)| (name, res.def))
.collect::<Vec<_>>();
2019-05-21 10:18:30 +00:00
entries.sort_by_key(|(name, _)| *name);
2019-05-21 10:18:30 +00:00
for (name, res) in entries {
*buf += &format!("{}:", name);
if res.types.is_some() {
*buf += " t";
}
if res.values.is_some() {
*buf += " v";
}
if res.macros.is_some() {
*buf += " m";
}
if res.is_none() {
*buf += " _";
2019-05-26 12:11:18 +00:00
}
*buf += "\n";
}
for (name, child) in map.modules[module].children.iter() {
let path = path.to_string() + &format!("::{}", name);
go(buf, map, &path, *child);
}
}
}
fn def_map(fixtute: &str) -> String {
let dm = compute_crate_def_map(fixtute, None);
render_crate_def_map(&dm)
}
2019-05-21 11:51:52 +00:00
fn def_map_with_crate_graph(fixture: &str, graph: CrateGraphFixture) -> String {
let dm = compute_crate_def_map(fixture, Some(graph));
render_crate_def_map(&dm)
}
#[test]
fn crate_def_map_smoke_test() {
let map = def_map(
"
//- /lib.rs
mod foo;
struct S;
2019-03-14 08:53:40 +00:00
use crate::foo::bar::E;
use self::E::V;
//- /foo/mod.rs
pub mod bar;
fn f() {}
//- /foo/bar.rs
pub struct Baz;
enum E { V }
",
);
2019-08-29 13:49:10 +00:00
assert_snapshot!(map, @r###"
2019-05-21 10:18:30 +00:00
crate
E: t
S: t v
V: t v
foo: t
crate::foo
bar: t
f: v
crate::foo::bar
Baz: t v
E: t
"###)
}
2019-03-16 15:06:45 +00:00
#[test]
fn bogus_paths() {
covers!(bogus_paths);
let map = def_map(
"
//- /lib.rs
mod foo;
struct S;
use self;
//- /foo/mod.rs
use super;
use crate;
",
);
2019-08-29 13:49:10 +00:00
assert_snapshot!(map, @r###"
2019-05-21 10:18:30 +00:00
crate
S: t v
foo: t
crate::foo
"###
2019-03-16 15:06:45 +00:00
)
}
#[test]
2019-03-14 08:53:40 +00:00
fn use_as() {
let map = def_map(
"
//- /lib.rs
2019-03-14 08:53:40 +00:00
mod foo;
use crate::foo::Baz as Foo;
2019-03-14 08:53:40 +00:00
//- /foo/mod.rs
pub struct Baz;
",
);
2019-08-29 13:49:10 +00:00
assert_snapshot!(map,
2019-03-14 08:53:40 +00:00
@r###"
2019-05-21 10:18:30 +00:00
crate
Foo: t v
foo: t
crate::foo
Baz: t v
"###
2019-03-14 08:53:40 +00:00
);
}
#[test]
2019-03-14 08:53:40 +00:00
fn use_trees() {
let map = def_map(
"
//- /lib.rs
2019-03-14 08:53:40 +00:00
mod foo;
use crate::foo::bar::{Baz, Quux};
//- /foo/mod.rs
pub mod bar;
2019-03-14 08:53:40 +00:00
//- /foo/bar.rs
pub struct Baz;
pub enum Quux {};
",
);
2019-08-29 13:49:10 +00:00
assert_snapshot!(map, @r###"
2019-05-21 10:18:30 +00:00
crate
Baz: t v
Quux: t
foo: t
crate::foo
bar: t
crate::foo::bar
Baz: t v
Quux: t
"###);
}
#[test]
2019-03-14 08:53:40 +00:00
fn re_exports() {
let map = def_map(
"
2019-03-14 08:53:40 +00:00
//- /lib.rs
mod foo;
2019-03-14 08:53:40 +00:00
use self::foo::Baz;
2019-03-14 08:53:40 +00:00
//- /foo/mod.rs
pub mod bar;
pub use self::bar::Baz;
//- /foo/bar.rs
pub struct Baz;
",
);
2019-08-29 13:49:10 +00:00
assert_snapshot!(map, @r###"
2019-05-21 10:18:30 +00:00
crate
Baz: t v
foo: t
crate::foo
Baz: t v
bar: t
crate::foo::bar
Baz: t v
"###);
}
#[test]
fn std_prelude() {
covers!(std_prelude);
let map = def_map_with_crate_graph(
"
//- /main.rs
use Foo::*;
//- /lib.rs
mod prelude;
#[prelude_import]
use prelude::*;
//- /prelude.rs
pub enum Foo { Bar, Baz };
",
crate_graph! {
"main": ("/main.rs", ["test_crate"]),
"test_crate": ("/lib.rs", []),
},
);
2019-08-29 13:49:10 +00:00
assert_snapshot!(map, @r###"
2019-05-21 10:18:30 +00:00
crate
Bar: t v
Baz: t v
"###);
}
#[test]
2019-03-14 08:53:40 +00:00
fn can_import_enum_variant() {
covers!(can_import_enum_variant);
let map = def_map(
"
//- /lib.rs
enum E { V }
use self::E::V;
",
);
2019-08-29 13:49:10 +00:00
assert_snapshot!(map, @r###"
2019-05-21 10:18:30 +00:00
crate
E: t
V: t v
"###
2019-03-14 08:53:40 +00:00
);
}
#[test]
fn edition_2015_imports() {
let map = def_map_with_crate_graph(
"
//- /main.rs
mod foo;
mod bar;
//- /bar.rs
struct Bar;
//- /foo.rs
use bar::Bar;
use other_crate::FromLib;
//- /lib.rs
struct FromLib;
",
crate_graph! {
"main": ("/main.rs", "2015", ["other_crate"]),
"other_crate": ("/lib.rs", "2018", []),
},
);
2019-08-29 13:49:10 +00:00
assert_snapshot!(map, @r###"
2019-05-21 10:18:30 +00:00
crate
bar: t
foo: t
crate::bar
Bar: t v
crate::foo
Bar: t v
FromLib: t v
"###);
2019-03-14 08:53:40 +00:00
}
#[test]
fn item_map_using_self() {
let map = def_map(
"
//- /lib.rs
mod foo;
use crate::foo::bar::Baz::{self};
//- /foo/mod.rs
pub mod bar;
//- /foo/bar.rs
pub struct Baz;
",
);
2019-08-29 13:49:10 +00:00
assert_snapshot!(map, @r###"
2019-05-21 10:18:30 +00:00
crate
Baz: t v
foo: t
crate::foo
bar: t
crate::foo::bar
Baz: t v
"###);
2019-03-14 08:53:40 +00:00
}
#[test]
fn item_map_across_crates() {
let map = def_map_with_crate_graph(
"
//- /main.rs
2019-03-14 08:53:40 +00:00
use test_crate::Baz;
//- /lib.rs
pub struct Baz;
",
crate_graph! {
"main": ("/main.rs", ["test_crate"]),
"test_crate": ("/lib.rs", []),
},
);
2019-03-14 08:53:40 +00:00
2019-08-29 13:49:10 +00:00
assert_snapshot!(map, @r###"
2019-05-21 10:18:30 +00:00
crate
Baz: t v
"###);
}
#[test]
2019-03-14 08:53:40 +00:00
fn extern_crate_rename() {
let map = def_map_with_crate_graph(
"
2019-03-14 08:53:40 +00:00
//- /main.rs
extern crate alloc as alloc_crate;
mod alloc;
mod sync;
//- /sync.rs
use alloc_crate::Arc;
//- /lib.rs
2019-03-14 08:53:40 +00:00
struct Arc;
",
2019-03-14 08:53:40 +00:00
crate_graph! {
"main": ("/main.rs", ["alloc"]),
"alloc": ("/lib.rs", []),
},
);
2019-03-14 08:53:40 +00:00
2019-08-29 13:49:10 +00:00
assert_snapshot!(map, @r###"
2019-05-21 11:51:52 +00:00
crate
alloc_crate: t
sync: t
crate::sync
Arc: t v
2019-05-21 10:18:30 +00:00
"###);
}
#[test]
2019-03-14 08:53:40 +00:00
fn extern_crate_rename_2015_edition() {
let map = def_map_with_crate_graph(
"
//- /main.rs
extern crate alloc as alloc_crate;
mod alloc;
mod sync;
//- /sync.rs
use alloc_crate::Arc;
//- /lib.rs
struct Arc;
",
crate_graph! {
"main": ("/main.rs", "2015", ["alloc"]),
"alloc": ("/lib.rs", []),
},
);
2019-08-29 13:49:10 +00:00
assert_snapshot!(map,
2019-03-14 08:53:40 +00:00
@r###"
2019-05-21 10:18:30 +00:00
crate
2019-05-21 11:51:52 +00:00
alloc_crate: t
sync: t
crate::sync
2019-05-21 10:18:30 +00:00
Arc: t v
"###
2019-03-14 08:53:40 +00:00
);
}
#[test]
fn import_across_source_roots() {
let map = def_map_with_crate_graph(
"
//- /lib.rs
2019-03-14 08:53:40 +00:00
pub mod a {
pub mod b {
pub struct C;
}
}
2019-03-14 08:53:40 +00:00
//- root /main/
//- /main/main.rs
use test_crate::a::b::C;
",
2019-03-14 08:53:40 +00:00
crate_graph! {
"main": ("/main/main.rs", ["test_crate"]),
"test_crate": ("/lib.rs", []),
},
);
2019-03-14 08:53:40 +00:00
2019-08-29 13:49:10 +00:00
assert_snapshot!(map, @r###"
2019-05-21 10:18:30 +00:00
crate
C: t v
"###);
2019-03-14 08:53:40 +00:00
}
#[test]
fn reexport_across_crates() {
let map = def_map_with_crate_graph(
"
//- /main.rs
use test_crate::Baz;
//- /lib.rs
pub use foo::Baz;
mod foo;
//- /foo.rs
pub struct Baz;
",
crate_graph! {
"main": ("/main.rs", ["test_crate"]),
"test_crate": ("/lib.rs", []),
},
);
2019-08-29 13:49:10 +00:00
assert_snapshot!(map, @r###"
2019-05-21 10:18:30 +00:00
crate
Baz: t v
"###);
}
2019-03-14 08:53:40 +00:00
#[test]
fn values_dont_shadow_extern_crates() {
let map = def_map_with_crate_graph(
"
//- /main.rs
fn foo() {}
use foo::Bar;
//- /foo/lib.rs
pub struct Bar;
",
crate_graph! {
"main": ("/main.rs", ["foo"]),
"foo": ("/foo/lib.rs", []),
},
);
2019-08-29 13:49:10 +00:00
assert_snapshot!(map, @r###"
2019-05-21 10:18:30 +00:00
crate
Bar: t v
foo: v
"###);
2019-03-14 08:53:40 +00:00
}
#[test]
fn cfg_not_test() {
let map = def_map_with_crate_graph(
r#"
//- /main.rs
use {Foo, Bar, Baz};
//- /lib.rs
#[prelude_import]
pub use self::prelude::*;
mod prelude {
#[cfg(test)]
pub struct Foo;
#[cfg(not(test))]
pub struct Bar;
#[cfg(all(not(any()), feature = "foo", feature = "bar", opt = "42"))]
pub struct Baz;
}
"#,
crate_graph! {
"main": ("/main.rs", ["std"]),
"std": ("/lib.rs", []),
},
);
assert_snapshot!(map, @r###"
crate
Bar: t v
Baz: _
Foo: _
"###);
}
#[test]
fn cfg_test() {
let map = def_map_with_crate_graph(
r#"
//- /main.rs
use {Foo, Bar, Baz};
//- /lib.rs
#[prelude_import]
pub use self::prelude::*;
mod prelude {
#[cfg(test)]
pub struct Foo;
#[cfg(not(test))]
pub struct Bar;
#[cfg(all(not(any()), feature = "foo", feature = "bar", opt = "42"))]
pub struct Baz;
}
"#,
crate_graph! {
"main": ("/main.rs", ["std"]),
2019-10-08 11:39:44 +00:00
"std": ("/lib.rs", [], cfg = {
"test",
"feature" = "foo",
"feature" = "bar",
"opt" = "42",
}),
},
);
assert_snapshot!(map, @r###"
crate
Bar: _
Baz: t v
Foo: t v
"###);
}