diff options
Diffstat (limited to 'crates/ra_analysis/tests/tests.rs')
-rw-r--r-- | crates/ra_analysis/tests/tests.rs | 65 |
1 files changed, 52 insertions, 13 deletions
diff --git a/crates/ra_analysis/tests/tests.rs b/crates/ra_analysis/tests/tests.rs index e0c637d65..7ae3d0eeb 100644 --- a/crates/ra_analysis/tests/tests.rs +++ b/crates/ra_analysis/tests/tests.rs | |||
@@ -7,13 +7,15 @@ extern crate test_utils; | |||
7 | 7 | ||
8 | use std::sync::Arc; | 8 | use std::sync::Arc; |
9 | 9 | ||
10 | use ra_analysis::{ | 10 | use ra_syntax::TextRange; |
11 | Analysis, AnalysisHost, CrateGraph, CrateId, FileId, FileResolver, FnDescriptor, JobHandle, | ||
12 | }; | ||
13 | use relative_path::{RelativePath, RelativePathBuf}; | 11 | use relative_path::{RelativePath, RelativePathBuf}; |
14 | use rustc_hash::FxHashMap; | 12 | use rustc_hash::FxHashMap; |
15 | use test_utils::{assert_eq_dbg, extract_offset}; | 13 | use test_utils::{assert_eq_dbg, extract_offset}; |
16 | 14 | ||
15 | use ra_analysis::{ | ||
16 | Analysis, AnalysisHost, CrateGraph, CrateId, FileId, FileResolver, FnDescriptor, | ||
17 | }; | ||
18 | |||
17 | #[derive(Debug)] | 19 | #[derive(Debug)] |
18 | struct FileMap(Vec<(FileId, RelativePathBuf)>); | 20 | struct FileMap(Vec<(FileId, RelativePathBuf)>); |
19 | 21 | ||
@@ -62,24 +64,22 @@ fn get_signature(text: &str) -> (FnDescriptor, Option<usize>) { | |||
62 | let (offset, code) = extract_offset(text); | 64 | let (offset, code) = extract_offset(text); |
63 | let code = code.as_str(); | 65 | let code = code.as_str(); |
64 | 66 | ||
65 | let (_handle, token) = JobHandle::new(); | ||
66 | let snap = analysis(&[("/lib.rs", code)]); | 67 | let snap = analysis(&[("/lib.rs", code)]); |
67 | 68 | ||
68 | snap.resolve_callable(FileId(1), offset, &token).unwrap() | 69 | snap.resolve_callable(FileId(1), offset).unwrap().unwrap() |
69 | } | 70 | } |
70 | 71 | ||
71 | #[test] | 72 | #[test] |
72 | fn test_resolve_module() { | 73 | fn test_resolve_module() { |
73 | let snap = analysis(&[("/lib.rs", "mod foo;"), ("/foo.rs", "")]); | 74 | let snap = analysis(&[("/lib.rs", "mod foo;"), ("/foo.rs", "")]); |
74 | let (_handle, token) = JobHandle::new(); | 75 | let symbols = snap.approximately_resolve_symbol(FileId(1), 4.into()).unwrap(); |
75 | let symbols = snap.approximately_resolve_symbol(FileId(1), 4.into(), &token); | ||
76 | assert_eq_dbg( | 76 | assert_eq_dbg( |
77 | r#"[(FileId(2), FileSymbol { name: "foo", node_range: [0; 0), kind: MODULE })]"#, | 77 | r#"[(FileId(2), FileSymbol { name: "foo", node_range: [0; 0), kind: MODULE })]"#, |
78 | &symbols, | 78 | &symbols, |
79 | ); | 79 | ); |
80 | 80 | ||
81 | let snap = analysis(&[("/lib.rs", "mod foo;"), ("/foo/mod.rs", "")]); | 81 | let snap = analysis(&[("/lib.rs", "mod foo;"), ("/foo/mod.rs", "")]); |
82 | let symbols = snap.approximately_resolve_symbol(FileId(1), 4.into(), &token); | 82 | let symbols = snap.approximately_resolve_symbol(FileId(1), 4.into()).unwrap(); |
83 | assert_eq_dbg( | 83 | assert_eq_dbg( |
84 | r#"[(FileId(2), FileSymbol { name: "foo", node_range: [0; 0), kind: MODULE })]"#, | 84 | r#"[(FileId(2), FileSymbol { name: "foo", node_range: [0; 0), kind: MODULE })]"#, |
85 | &symbols, | 85 | &symbols, |
@@ -89,7 +89,7 @@ fn test_resolve_module() { | |||
89 | #[test] | 89 | #[test] |
90 | fn test_unresolved_module_diagnostic() { | 90 | fn test_unresolved_module_diagnostic() { |
91 | let snap = analysis(&[("/lib.rs", "mod foo;")]); | 91 | let snap = analysis(&[("/lib.rs", "mod foo;")]); |
92 | let diagnostics = snap.diagnostics(FileId(1)); | 92 | let diagnostics = snap.diagnostics(FileId(1)).unwrap(); |
93 | assert_eq_dbg( | 93 | assert_eq_dbg( |
94 | r#"[Diagnostic { | 94 | r#"[Diagnostic { |
95 | message: "unresolved module", | 95 | message: "unresolved module", |
@@ -106,14 +106,14 @@ fn test_unresolved_module_diagnostic() { | |||
106 | #[test] | 106 | #[test] |
107 | fn test_unresolved_module_diagnostic_no_diag_for_inline_mode() { | 107 | fn test_unresolved_module_diagnostic_no_diag_for_inline_mode() { |
108 | let snap = analysis(&[("/lib.rs", "mod foo {}")]); | 108 | let snap = analysis(&[("/lib.rs", "mod foo {}")]); |
109 | let diagnostics = snap.diagnostics(FileId(1)); | 109 | let diagnostics = snap.diagnostics(FileId(1)).unwrap(); |
110 | assert_eq_dbg(r#"[]"#, &diagnostics); | 110 | assert_eq_dbg(r#"[]"#, &diagnostics); |
111 | } | 111 | } |
112 | 112 | ||
113 | #[test] | 113 | #[test] |
114 | fn test_resolve_parent_module() { | 114 | fn test_resolve_parent_module() { |
115 | let snap = analysis(&[("/lib.rs", "mod foo;"), ("/foo.rs", "")]); | 115 | let snap = analysis(&[("/lib.rs", "mod foo;"), ("/foo.rs", "")]); |
116 | let symbols = snap.parent_module(FileId(2)); | 116 | let symbols = snap.parent_module(FileId(2)).unwrap(); |
117 | assert_eq_dbg( | 117 | assert_eq_dbg( |
118 | r#"[(FileId(1), FileSymbol { name: "foo", node_range: [0; 8), kind: MODULE })]"#, | 118 | r#"[(FileId(1), FileSymbol { name: "foo", node_range: [0; 8), kind: MODULE })]"#, |
119 | &symbols, | 119 | &symbols, |
@@ -124,7 +124,7 @@ fn test_resolve_parent_module() { | |||
124 | fn test_resolve_crate_root() { | 124 | fn test_resolve_crate_root() { |
125 | let mut host = analysis_host(&[("/lib.rs", "mod foo;"), ("/foo.rs", "")]); | 125 | let mut host = analysis_host(&[("/lib.rs", "mod foo;"), ("/foo.rs", "")]); |
126 | let snap = host.analysis(); | 126 | let snap = host.analysis(); |
127 | assert!(snap.crate_for(FileId(2)).is_empty()); | 127 | assert!(snap.crate_for(FileId(2)).unwrap().is_empty()); |
128 | 128 | ||
129 | let crate_graph = CrateGraph { | 129 | let crate_graph = CrateGraph { |
130 | crate_roots: { | 130 | crate_roots: { |
@@ -136,7 +136,7 @@ fn test_resolve_crate_root() { | |||
136 | host.set_crate_graph(crate_graph); | 136 | host.set_crate_graph(crate_graph); |
137 | let snap = host.analysis(); | 137 | let snap = host.analysis(); |
138 | 138 | ||
139 | assert_eq!(snap.crate_for(FileId(2)), vec![CrateId(1)],); | 139 | assert_eq!(snap.crate_for(FileId(2)).unwrap(), vec![CrateId(1)],); |
140 | } | 140 | } |
141 | 141 | ||
142 | #[test] | 142 | #[test] |
@@ -225,3 +225,42 @@ fn bar() { | |||
225 | assert_eq!(desc.ret_type, None); | 225 | assert_eq!(desc.ret_type, None); |
226 | assert_eq!(param, Some(1)); | 226 | assert_eq!(param, Some(1)); |
227 | } | 227 | } |
228 | |||
229 | fn get_all_refs(text: &str) -> Vec<(FileId, TextRange)> { | ||
230 | let (offset, code) = extract_offset(text); | ||
231 | let code = code.as_str(); | ||
232 | |||
233 | let snap = analysis(&[("/lib.rs", code)]); | ||
234 | |||
235 | snap.find_all_refs(FileId(1), offset).unwrap() | ||
236 | } | ||
237 | |||
238 | #[test] | ||
239 | fn test_find_all_refs_for_local() { | ||
240 | let code = r#" | ||
241 | fn main() { | ||
242 | let mut i = 1; | ||
243 | let j = 1; | ||
244 | i = i<|> + j; | ||
245 | |||
246 | { | ||
247 | i = 0; | ||
248 | } | ||
249 | |||
250 | i = 5; | ||
251 | }"#; | ||
252 | |||
253 | let refs = get_all_refs(code); | ||
254 | assert_eq!(refs.len(), 5); | ||
255 | } | ||
256 | |||
257 | #[test] | ||
258 | fn test_find_all_refs_for_param_inside() { | ||
259 | let code = r#" | ||
260 | fn foo(i : u32) -> u32 { | ||
261 | i<|> | ||
262 | }"#; | ||
263 | |||
264 | let refs = get_all_refs(code); | ||
265 | assert_eq!(refs.len(), 2); | ||
266 | } | ||