diff options
author | Aleksey Kladov <[email protected]> | 2018-08-28 16:22:52 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2018-08-28 16:22:59 +0100 |
commit | d34588bf83898870d7f9b4b49ac2a5f71c77dabb (patch) | |
tree | a0b6eb7b41cb586d1c10e408a4791edf7c341a1f /crates/libanalysis/tests | |
parent | 748a4cacd24d9ecdca995e66117a10a1562e7d5d (diff) |
create module smartly
Diffstat (limited to 'crates/libanalysis/tests')
-rw-r--r-- | crates/libanalysis/tests/tests.rs | 72 |
1 files changed, 44 insertions, 28 deletions
diff --git a/crates/libanalysis/tests/tests.rs b/crates/libanalysis/tests/tests.rs index 7c2950ccc..e378ab986 100644 --- a/crates/libanalysis/tests/tests.rs +++ b/crates/libanalysis/tests/tests.rs | |||
@@ -1,11 +1,39 @@ | |||
1 | extern crate libanalysis; | 1 | extern crate libanalysis; |
2 | extern crate relative_path; | ||
2 | extern crate test_utils; | 3 | extern crate test_utils; |
3 | 4 | ||
4 | use std::path::PathBuf; | 5 | use std::path::{Path}; |
5 | 6 | ||
6 | use libanalysis::{WorldState, FileId}; | 7 | use relative_path::RelativePath; |
8 | use libanalysis::{WorldState, FileId, FileResolver}; | ||
7 | use test_utils::assert_eq_dbg; | 9 | use test_utils::assert_eq_dbg; |
8 | 10 | ||
11 | struct FileMap(&'static [(u32, &'static str)]); | ||
12 | |||
13 | impl FileMap { | ||
14 | fn path(&self, id: FileId) -> &'static Path { | ||
15 | let s = self.0.iter() | ||
16 | .find(|it| it.0 == id.0) | ||
17 | .unwrap() | ||
18 | .1; | ||
19 | Path::new(s) | ||
20 | } | ||
21 | } | ||
22 | |||
23 | impl FileResolver for FileMap { | ||
24 | fn file_stem(&self, id: FileId) -> String { | ||
25 | self.path(id).file_stem().unwrap().to_str().unwrap().to_string() | ||
26 | } | ||
27 | fn resolve(&self, id: FileId, rel: &RelativePath) -> Option<FileId> { | ||
28 | let path = rel.to_path(self.path(id)); | ||
29 | let path = path.to_str().unwrap(); | ||
30 | let path = RelativePath::new(&path[1..]).normalize(); | ||
31 | let &(id, _) = self.0.iter() | ||
32 | .find(|it| path == RelativePath::new(&it.1[1..]).normalize())?; | ||
33 | Some(FileId(id)) | ||
34 | } | ||
35 | } | ||
36 | |||
9 | 37 | ||
10 | #[test] | 38 | #[test] |
11 | fn test_resolve_module() { | 39 | fn test_resolve_module() { |
@@ -13,14 +41,10 @@ fn test_resolve_module() { | |||
13 | world.change_file(FileId(1), Some("mod foo;".to_string())); | 41 | world.change_file(FileId(1), Some("mod foo;".to_string())); |
14 | world.change_file(FileId(2), Some("".to_string())); | 42 | world.change_file(FileId(2), Some("".to_string())); |
15 | 43 | ||
16 | let snap = world.snapshot(|id, path| { | 44 | let snap = world.snapshot(FileMap(&[ |
17 | assert_eq!(id, FileId(1)); | 45 | (1, "/lib.rs"), |
18 | if path == PathBuf::from("../foo/mod.rs") { | 46 | (2, "/foo.rs"), |
19 | return None; | 47 | ])); |
20 | } | ||
21 | assert_eq!(path, PathBuf::from("../foo.rs")); | ||
22 | Some(FileId(2)) | ||
23 | }); | ||
24 | let symbols = snap.approximately_resolve_symbol(FileId(1), 4.into()) | 48 | let symbols = snap.approximately_resolve_symbol(FileId(1), 4.into()) |
25 | .unwrap(); | 49 | .unwrap(); |
26 | assert_eq_dbg( | 50 | assert_eq_dbg( |
@@ -28,14 +52,10 @@ fn test_resolve_module() { | |||
28 | &symbols, | 52 | &symbols, |
29 | ); | 53 | ); |
30 | 54 | ||
31 | let snap = world.snapshot(|id, path| { | 55 | let snap = world.snapshot(FileMap(&[ |
32 | assert_eq!(id, FileId(1)); | 56 | (1, "/lib.rs"), |
33 | if path == PathBuf::from("../foo.rs") { | 57 | (2, "/foo/mod.rs") |
34 | return None; | 58 | ])); |
35 | } | ||
36 | assert_eq!(path, PathBuf::from("../foo/mod.rs")); | ||
37 | Some(FileId(2)) | ||
38 | }); | ||
39 | let symbols = snap.approximately_resolve_symbol(FileId(1), 4.into()) | 59 | let symbols = snap.approximately_resolve_symbol(FileId(1), 4.into()) |
40 | .unwrap(); | 60 | .unwrap(); |
41 | assert_eq_dbg( | 61 | assert_eq_dbg( |
@@ -49,11 +69,11 @@ fn test_unresolved_module_diagnostic() { | |||
49 | let mut world = WorldState::new(); | 69 | let mut world = WorldState::new(); |
50 | world.change_file(FileId(1), Some("mod foo;".to_string())); | 70 | world.change_file(FileId(1), Some("mod foo;".to_string())); |
51 | 71 | ||
52 | let snap = world.snapshot(|_id, _path| None); | 72 | let snap = world.snapshot(FileMap(&[(1, "/lib.rs")])); |
53 | let diagnostics = snap.diagnostics(FileId(1)).unwrap(); | 73 | let diagnostics = snap.diagnostics(FileId(1)).unwrap(); |
54 | assert_eq_dbg( | 74 | assert_eq_dbg( |
55 | r#"[(Diagnostic { range: [4; 7), msg: "unresolved module" }, | 75 | r#"[(Diagnostic { range: [4; 7), msg: "unresolved module" }, |
56 | Some(CreateFile("../foo.rs")))]"#, | 76 | Some(QuickFix { fs_ops: [CreateFile { anchor: FileId(1), path: "../foo.rs" }] }))]"#, |
57 | &diagnostics, | 77 | &diagnostics, |
58 | ); | 78 | ); |
59 | } | 79 | } |
@@ -64,14 +84,10 @@ fn test_resolve_parent_module() { | |||
64 | world.change_file(FileId(1), Some("mod foo;".to_string())); | 84 | world.change_file(FileId(1), Some("mod foo;".to_string())); |
65 | world.change_file(FileId(2), Some("".to_string())); | 85 | world.change_file(FileId(2), Some("".to_string())); |
66 | 86 | ||
67 | let snap = world.snapshot(|id, path| { | 87 | let snap = world.snapshot(FileMap(&[ |
68 | assert_eq!(id, FileId(1)); | 88 | (1, "/lib.rs"), |
69 | if path == PathBuf::from("../foo/mod.rs") { | 89 | (2, "/foo.rs"), |
70 | return None; | 90 | ])); |
71 | } | ||
72 | assert_eq!(path, PathBuf::from("../foo.rs")); | ||
73 | Some(FileId(2)) | ||
74 | }); | ||
75 | let symbols = snap.parent_module(FileId(2)); | 91 | let symbols = snap.parent_module(FileId(2)); |
76 | assert_eq_dbg( | 92 | assert_eq_dbg( |
77 | r#"[(FileId(1), FileSymbol { name: "foo", node_range: [0; 8), kind: MODULE })]"#, | 93 | r#"[(FileId(1), FileSymbol { name: "foo", node_range: [0; 8), kind: MODULE })]"#, |