diff options
author | Aleksey Kladov <[email protected]> | 2018-11-28 13:19:01 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2018-11-28 13:19:01 +0000 |
commit | e89700f9678d3797c09f0a397b7b67fe9c6f5e9f (patch) | |
tree | aedb0bd160c98dff996ad9cdd55d7f96db294b78 /crates/ra_hir/src/module/nameres.rs | |
parent | 95c0c8f3986c8b3bcf0052d34d3ace09ebb9fa1b (diff) |
Move hir tests to hit
Diffstat (limited to 'crates/ra_hir/src/module/nameres.rs')
-rw-r--r-- | crates/ra_hir/src/module/nameres.rs | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/crates/ra_hir/src/module/nameres.rs b/crates/ra_hir/src/module/nameres.rs index 837a8d5ae..de13835b2 100644 --- a/crates/ra_hir/src/module/nameres.rs +++ b/crates/ra_hir/src/module/nameres.rs | |||
@@ -336,3 +336,100 @@ where | |||
336 | f(module_items) | 336 | f(module_items) |
337 | } | 337 | } |
338 | } | 338 | } |
339 | |||
340 | //TODO: move to hir | ||
341 | #[cfg(test)] | ||
342 | mod tests { | ||
343 | use std::sync::Arc; | ||
344 | |||
345 | use salsa::Database; | ||
346 | use ra_db::FilesDatabase; | ||
347 | use ra_syntax::SmolStr; | ||
348 | |||
349 | use crate::{ | ||
350 | self as hir, | ||
351 | db::HirDatabase, | ||
352 | mock::MockDatabase, | ||
353 | }; | ||
354 | |||
355 | fn item_map(fixture: &str) -> (Arc<hir::ItemMap>, hir::ModuleId) { | ||
356 | let (db, pos) = MockDatabase::with_position(fixture); | ||
357 | let source_root = db.file_source_root(pos.file_id); | ||
358 | let module = hir::Module::guess_from_position(&db, pos).unwrap().unwrap(); | ||
359 | let module_id = module.module_id; | ||
360 | (db.item_map(source_root).unwrap(), module_id) | ||
361 | } | ||
362 | |||
363 | #[test] | ||
364 | fn test_item_map() { | ||
365 | let (item_map, module_id) = item_map( | ||
366 | " | ||
367 | //- /lib.rs | ||
368 | mod foo; | ||
369 | |||
370 | use crate::foo::bar::Baz; | ||
371 | <|> | ||
372 | |||
373 | //- /foo/mod.rs | ||
374 | pub mod bar; | ||
375 | |||
376 | //- /foo/bar.rs | ||
377 | pub struct Baz; | ||
378 | ", | ||
379 | ); | ||
380 | let name = SmolStr::from("Baz"); | ||
381 | let resolution = &item_map.per_module[&module_id].items[&name]; | ||
382 | assert!(resolution.def_id.is_some()); | ||
383 | } | ||
384 | |||
385 | #[test] | ||
386 | fn typing_inside_a_function_should_not_invalidate_item_map() { | ||
387 | let (mut db, pos) = MockDatabase::with_position( | ||
388 | " | ||
389 | //- /lib.rs | ||
390 | mod foo;<|> | ||
391 | |||
392 | use crate::foo::bar::Baz; | ||
393 | |||
394 | fn foo() -> i32 { | ||
395 | 1 + 1 | ||
396 | } | ||
397 | //- /foo/mod.rs | ||
398 | pub mod bar; | ||
399 | |||
400 | //- /foo/bar.rs | ||
401 | pub struct Baz; | ||
402 | ", | ||
403 | ); | ||
404 | let source_root = db.file_source_root(pos.file_id); | ||
405 | { | ||
406 | let events = db.log_executed(|| { | ||
407 | db.item_map(source_root).unwrap(); | ||
408 | }); | ||
409 | assert!(format!("{:?}", events).contains("item_map")) | ||
410 | } | ||
411 | |||
412 | let new_text = " | ||
413 | mod foo; | ||
414 | |||
415 | use crate::foo::bar::Baz; | ||
416 | |||
417 | fn foo() -> i32 { 92 } | ||
418 | " | ||
419 | .to_string(); | ||
420 | |||
421 | db.query_mut(ra_db::FileTextQuery) | ||
422 | .set(pos.file_id, Arc::new(new_text)); | ||
423 | |||
424 | { | ||
425 | let events = db.log_executed(|| { | ||
426 | db.item_map(source_root).unwrap(); | ||
427 | }); | ||
428 | assert!( | ||
429 | !format!("{:?}", events).contains("_item_map"), | ||
430 | "{:#?}", | ||
431 | events | ||
432 | ) | ||
433 | } | ||
434 | } | ||
435 | } | ||