rust-analyzer/crates/hir_def/src/nameres/tests/incremental.rs

102 lines
2.5 KiB
Rust
Raw Normal View History

2019-03-14 08:53:40 +00:00
use std::sync::Arc;
2020-08-13 14:25:38 +00:00
use base_db::SourceDatabaseExt;
2019-03-14 08:53:40 +00:00
2019-10-31 15:45:10 +00:00
use super::*;
2020-03-26 14:44:31 +00:00
fn check_def_map_is_not_recomputed(ra_fixture_initial: &str, ra_fixture_change: &str) {
let (mut db, pos) = TestDB::with_position(ra_fixture_initial);
2019-11-15 10:16:16 +00:00
let krate = db.test_crate();
2019-03-14 08:53:40 +00:00
{
let events = db.log_executed(|| {
db.crate_def_map(krate);
});
assert!(format!("{:?}", events).contains("crate_def_map"), "{:#?}", events)
}
2020-03-26 14:44:31 +00:00
db.set_file_text(pos.file_id, Arc::new(ra_fixture_change.to_string()));
2019-03-14 08:53:40 +00:00
{
let events = db.log_executed(|| {
db.crate_def_map(krate);
});
assert!(!format!("{:?}", events).contains("crate_def_map"), "{:#?}", events)
}
}
#[test]
fn typing_inside_a_function_should_not_invalidate_def_map() {
check_def_map_is_not_recomputed(
2020-03-26 14:44:31 +00:00
r"
2019-03-14 08:53:40 +00:00
//- /lib.rs
mod foo;<|>
use crate::foo::bar::Baz;
2020-04-11 15:54:46 +00:00
enum E { A, B }
use E::*;
2019-03-14 08:53:40 +00:00
fn foo() -> i32 {
1 + 1
}
//- /foo/mod.rs
pub mod bar;
//- /foo/bar.rs
pub struct Baz;
",
2020-03-26 14:44:31 +00:00
r"
2019-03-14 08:53:40 +00:00
mod foo;
use crate::foo::bar::Baz;
2020-04-11 15:54:46 +00:00
enum E { A, B }
use E::*;
2019-03-14 08:53:40 +00:00
fn foo() -> i32 { 92 }
",
);
}
#[test]
fn typing_inside_a_macro_should_not_invalidate_def_map() {
2019-11-03 20:35:48 +00:00
let (mut db, pos) = TestDB::with_position(
2020-03-26 14:44:31 +00:00
r"
2019-03-14 08:53:40 +00:00
//- /lib.rs
macro_rules! m {
($ident:ident) => {
2019-03-26 16:54:52 +00:00
fn f() {
$ident + $ident;
};
}
}
2019-03-14 08:53:40 +00:00
mod foo;
//- /foo/mod.rs
pub mod bar;
//- /foo/bar.rs
<|>
m!(X);
2019-03-14 08:53:40 +00:00
",
);
2019-11-15 10:16:16 +00:00
let krate = db.test_crate();
2019-03-26 16:54:52 +00:00
{
let events = db.log_executed(|| {
2019-11-03 20:35:48 +00:00
let crate_def_map = db.crate_def_map(krate);
let (_, module_data) = crate_def_map.modules.iter().last().unwrap();
assert_eq!(module_data.scope.resolutions().count(), 1);
2019-03-26 16:54:52 +00:00
});
assert!(format!("{:?}", events).contains("crate_def_map"), "{:#?}", events)
}
db.set_file_text(pos.file_id, Arc::new("m!(Y);".to_string()));
{
let events = db.log_executed(|| {
2019-11-03 20:35:48 +00:00
let crate_def_map = db.crate_def_map(krate);
let (_, module_data) = crate_def_map.modules.iter().last().unwrap();
assert_eq!(module_data.scope.resolutions().count(), 1);
2019-03-26 16:54:52 +00:00
});
assert!(!format!("{:?}", events).contains("crate_def_map"), "{:#?}", events)
}
2019-03-14 08:53:40 +00:00
}