rust-analyzer/crates/ra_hir_def/src/nameres/tests/globs.rs

378 lines
7.8 KiB
Rust
Raw Normal View History

2019-03-14 08:53:40 +00:00
use super::*;
#[test]
fn glob_1() {
let map = def_map(
r"
2019-03-14 08:53:40 +00:00
//- /lib.rs
mod foo;
use foo::*;
//- /foo/mod.rs
pub mod bar;
pub use self::bar::Baz;
pub struct Foo;
//- /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 v
bar: t
foo: t
crate::foo
Baz: t v
Foo: t v
bar: t
crate::foo::bar
Baz: t v
"###
2019-03-14 08:53:40 +00:00
);
}
#[test]
fn glob_2() {
let map = def_map(
"
//- /lib.rs
mod foo;
use foo::*;
//- /foo/mod.rs
pub mod bar;
pub use self::bar::*;
pub struct Foo;
//- /foo/bar.rs
pub struct Baz;
pub use super::*;
",
);
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 v
bar: t
foo: t
crate::foo
Baz: t v
Foo: t v
bar: t
crate::foo::bar
Baz: t v
Foo: t v
bar: t
"###
2019-03-14 08:53:40 +00:00
);
}
#[test]
fn glob_privacy_1() {
let map = def_map(
r"
//- /lib.rs
mod foo;
use foo::*;
//- /foo/mod.rs
pub mod bar;
pub use self::bar::*;
struct PrivateStructFoo;
//- /foo/bar.rs
pub struct Baz;
struct PrivateStructBar;
pub use super::*;
",
);
assert_snapshot!(map, @r###"
crate
Baz: t v
bar: t
foo: t
crate::foo
Baz: t v
PrivateStructFoo: t v
bar: t
crate::foo::bar
Baz: t v
PrivateStructBar: t v
PrivateStructFoo: t v
bar: t
"###
);
}
#[test]
fn glob_privacy_2() {
let map = def_map(
r"
//- /lib.rs
mod foo;
use foo::*;
use foo::bar::*;
//- /foo/mod.rs
2019-12-26 14:49:13 +00:00
mod bar;
fn Foo() {};
pub struct Foo {};
//- /foo/bar.rs
pub(super) struct PrivateBaz;
struct PrivateBar;
pub(crate) struct PubCrateStruct;
",
);
assert_snapshot!(map, @r###"
crate
Foo: t
PubCrateStruct: t v
foo: t
crate::foo
Foo: t v
bar: t
crate::foo::bar
PrivateBar: t v
PrivateBaz: t v
PubCrateStruct: t v
"###
);
}
2019-03-14 08:53:40 +00:00
#[test]
fn glob_across_crates() {
2020-05-20 10:59:20 +00:00
mark::check!(glob_across_crates);
2019-11-03 20:35:48 +00:00
let map = def_map(
r"
2019-11-03 20:35:48 +00:00
//- /main.rs crate:main deps:test_crate
2019-03-14 08:53:40 +00:00
use test_crate::*;
2019-11-03 20:35:48 +00:00
//- /lib.rs crate:test_crate
2019-03-14 08:53:40 +00:00
pub struct Baz;
",
);
2019-08-29 13:49:10 +00:00
assert_snapshot!(map, @r###"
crate
Baz: t v
2019-05-21 10:18:30 +00:00
"###
2019-03-14 08:53:40 +00:00
);
}
2019-12-26 15:31:38 +00:00
#[test]
fn glob_privacy_across_crates() {
let map = def_map(
r"
2019-12-26 15:31:38 +00:00
//- /main.rs crate:main deps:test_crate
use test_crate::*;
//- /lib.rs crate:test_crate
pub struct Baz;
struct Foo;
",
);
assert_snapshot!(map, @r###"
crate
Baz: t v
2019-12-26 15:31:38 +00:00
"###
);
}
2019-03-14 08:53:40 +00:00
#[test]
fn glob_enum() {
2020-05-20 10:59:20 +00:00
mark::check!(glob_enum);
2019-03-14 08:53:40 +00:00
let map = def_map(
"
//- /lib.rs
enum Foo {
Bar, Baz
}
use self::Foo::*;
",
);
2019-08-29 13:49:10 +00:00
assert_snapshot!(map, @r###"
crate
Bar: t v
Baz: t v
Foo: t
2019-05-21 10:18:30 +00:00
"###
2019-03-14 08:53:40 +00:00
);
}
2019-12-19 15:57:22 +00:00
#[test]
fn glob_enum_group() {
2020-05-20 10:59:20 +00:00
mark::check!(glob_enum_group);
2019-12-19 15:57:22 +00:00
let map = def_map(
r"
2019-12-19 15:57:22 +00:00
//- /lib.rs
enum Foo {
Bar, Baz
}
use self::Foo::{*};
",
);
assert_snapshot!(map, @r###"
crate
Bar: t v
Baz: t v
Foo: t
2019-12-19 15:57:22 +00:00
"###
);
}
#[test]
fn glob_shadowed_def() {
let db = TestDB::with_files(
r###"
//- /lib.rs
mod foo;
mod bar;
use foo::*;
use bar::Baz;
//- /foo.rs
pub struct Baz;
//- /bar.rs
pub struct Baz;
"###,
);
let krate = db.test_crate();
let crate_def_map = db.crate_def_map(krate);
let (_, root_module) = crate_def_map
.modules
.iter()
.find(|(_, module_data)| module_data.parent.is_none())
.expect("Root module not found");
let visible_entries = root_module.scope.entries().collect::<Vec<_>>();
insta::assert_debug_snapshot!(
visible_entries,
@r###"
[
(
Name(
Text(
"Baz",
),
),
PerNs {
types: Some(
(
AdtId(
StructId(
StructId(
1,
),
),
),
Module(
ModuleId {
krate: CrateId(
0,
),
local_id: Idx::<ModuleData>(0),
},
),
),
),
values: Some(
(
AdtId(
StructId(
StructId(
1,
),
),
),
Module(
ModuleId {
krate: CrateId(
0,
),
local_id: Idx::<ModuleData>(0),
},
),
),
),
macros: None,
},
),
(
Name(
Text(
"bar",
),
),
PerNs {
types: Some(
(
ModuleId(
ModuleId {
krate: CrateId(
0,
),
local_id: Idx::<ModuleData>(2),
},
),
Module(
ModuleId {
krate: CrateId(
0,
),
local_id: Idx::<ModuleData>(0),
},
),
),
),
values: None,
macros: None,
},
),
(
Name(
Text(
"foo",
),
),
PerNs {
types: Some(
(
ModuleId(
ModuleId {
krate: CrateId(
0,
),
local_id: Idx::<ModuleData>(1),
},
),
Module(
ModuleId {
krate: CrateId(
0,
),
local_id: Idx::<ModuleData>(0),
},
),
),
),
values: None,
macros: None,
},
),
]
"###
);
}