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

141 lines
3.3 KiB
Rust
Raw Normal View History

2019-03-14 08:53:40 +00:00
use std::sync::Arc;
use ra_db::{SourceDatabase, SourceDatabaseExt};
2019-03-14 08:53:40 +00:00
2019-10-31 15:45:10 +00:00
use super::*;
2019-03-14 08:53:40 +00:00
fn check_def_map_is_not_recomputed(initial: &str, file_change: &str) {
let (mut db, pos) = MockDatabase::with_position(initial);
2019-10-31 15:45:10 +00:00
let krate = db.crate_graph().iter().next().unwrap();
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)
}
db.set_file_text(pos.file_id, Arc::new(file_change.to_string()));
{
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(
"
//- /lib.rs
mod foo;<|>
use crate::foo::bar::Baz;
fn foo() -> i32 {
1 + 1
}
//- /foo/mod.rs
pub mod bar;
//- /foo/bar.rs
pub struct Baz;
",
"
mod foo;
use crate::foo::bar::Baz;
fn foo() -> i32 { 92 }
",
);
}
#[test]
fn adding_inner_items_should_not_invalidate_def_map() {
check_def_map_is_not_recomputed(
"
//- /lib.rs
struct S { a: i32}
enum E { A }
trait T {
fn a() {}
}
mod foo;<|>
impl S {
fn a() {}
}
use crate::foo::bar::Baz;
//- /foo/mod.rs
pub mod bar;
//- /foo/bar.rs
pub struct Baz;
",
"
struct S { a: i32, b: () }
enum E { A, B }
trait T {
fn a() {}
fn b() {}
}
mod foo;<|>
impl S {
fn a() {}
fn b() {}
}
use crate::foo::bar::Baz;
",
);
}
#[test]
fn typing_inside_a_macro_should_not_invalidate_def_map() {
2019-03-26 16:54:52 +00:00
let (mut db, pos) = MockDatabase::with_position(
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-03-26 16:54:52 +00:00
{
let events = db.log_executed(|| {
2019-09-16 10:48:54 +00:00
let src = crate::Source {
file_id: pos.file_id.into(),
ast: crate::ModuleSource::new(&db, Some(pos.file_id), None),
};
let module = crate::Module::from_definition(&db, src).unwrap();
2019-03-26 16:54:52 +00:00
let decls = module.declarations(&db);
2019-05-30 12:03:58 +00:00
assert_eq!(decls.len(), 18);
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-09-16 10:48:54 +00:00
let src = crate::Source {
file_id: pos.file_id.into(),
ast: crate::ModuleSource::new(&db, Some(pos.file_id), None),
};
let module = crate::Module::from_definition(&db, src).unwrap();
2019-03-26 16:54:52 +00:00
let decls = module.declarations(&db);
2019-05-30 12:03:58 +00:00
assert_eq!(decls.len(), 18);
2019-03-26 16:54:52 +00:00
});
assert!(!format!("{:?}", events).contains("crate_def_map"), "{:#?}", events)
}
2019-03-14 08:53:40 +00:00
}