diff options
author | bors[bot] <bors[bot]@users.noreply.github.com> | 2018-10-15 17:41:57 +0100 |
---|---|---|
committer | bors[bot] <bors[bot]@users.noreply.github.com> | 2018-10-15 17:41:57 +0100 |
commit | e031b65f93f73164a5729cf81ff60299708bc931 (patch) | |
tree | 1a891b75af3b436549381e8726c48ec5028a8341 /crates/ra_analysis/tests | |
parent | 8cec1d986161298dbe9cc53f5477e87bfe5d0f47 (diff) | |
parent | c9909f42ba4adf55b1e73e7118b48f1b10c80ac6 (diff) |
Merge #110
110: Signature help r=matklad a=kjeremy
@matklad Once this is in shape I would like to add tests. I think a separate PR should be done for returning documentation information and markdown.
Fixes #102
Co-authored-by: Jeremy A. Kolb <[email protected]>
Diffstat (limited to 'crates/ra_analysis/tests')
-rw-r--r-- | crates/ra_analysis/tests/tests.rs | 102 |
1 files changed, 98 insertions, 4 deletions
diff --git a/crates/ra_analysis/tests/tests.rs b/crates/ra_analysis/tests/tests.rs index a886cd0ff..755640fb4 100644 --- a/crates/ra_analysis/tests/tests.rs +++ b/crates/ra_analysis/tests/tests.rs | |||
@@ -1,6 +1,8 @@ | |||
1 | extern crate relative_path; | 1 | extern crate relative_path; |
2 | extern crate ra_analysis; | 2 | extern crate ra_analysis; |
3 | extern crate rustc_hash; | 3 | extern crate rustc_hash; |
4 | extern crate ra_editor; | ||
5 | extern crate ra_syntax; | ||
4 | extern crate test_utils; | 6 | extern crate test_utils; |
5 | 7 | ||
6 | use std::{ | 8 | use std::{ |
@@ -9,8 +11,8 @@ use std::{ | |||
9 | 11 | ||
10 | use rustc_hash::FxHashMap; | 12 | use rustc_hash::FxHashMap; |
11 | use relative_path::{RelativePath, RelativePathBuf}; | 13 | use relative_path::{RelativePath, RelativePathBuf}; |
12 | use ra_analysis::{Analysis, AnalysisHost, FileId, FileResolver, JobHandle, CrateGraph, CrateId}; | 14 | use ra_analysis::{Analysis, AnalysisHost, FileId, FileResolver, JobHandle, CrateGraph, CrateId, FnDescriptor}; |
13 | use test_utils::assert_eq_dbg; | 15 | use test_utils::{assert_eq_dbg, extract_offset}; |
14 | 16 | ||
15 | #[derive(Debug)] | 17 | #[derive(Debug)] |
16 | struct FileMap(Vec<(FileId, RelativePathBuf)>); | 18 | struct FileMap(Vec<(FileId, RelativePathBuf)>); |
@@ -39,7 +41,7 @@ impl FileResolver for FileMap { | |||
39 | } | 41 | } |
40 | } | 42 | } |
41 | 43 | ||
42 | fn analysis_host(files: &'static [(&'static str, &'static str)]) -> AnalysisHost { | 44 | fn analysis_host(files: &[(&str, &str)]) -> AnalysisHost { |
43 | let mut host = AnalysisHost::new(); | 45 | let mut host = AnalysisHost::new(); |
44 | let mut file_map = Vec::new(); | 46 | let mut file_map = Vec::new(); |
45 | for (id, &(path, contents)) in files.iter().enumerate() { | 47 | for (id, &(path, contents)) in files.iter().enumerate() { |
@@ -53,10 +55,20 @@ fn analysis_host(files: &'static [(&'static str, &'static str)]) -> AnalysisHost | |||
53 | host | 55 | host |
54 | } | 56 | } |
55 | 57 | ||
56 | fn analysis(files: &'static [(&'static str, &'static str)]) -> Analysis { | 58 | fn analysis(files: &[(&str, &str)]) -> Analysis { |
57 | analysis_host(files).analysis() | 59 | analysis_host(files).analysis() |
58 | } | 60 | } |
59 | 61 | ||
62 | fn get_signature(text: &str) -> (FnDescriptor, Option<usize>) { | ||
63 | let (offset, code) = extract_offset(text); | ||
64 | let code = code.as_str(); | ||
65 | |||
66 | let (_handle, token) = JobHandle::new(); | ||
67 | let snap = analysis(&[("/lib.rs", code)]); | ||
68 | |||
69 | snap.resolve_callable(FileId(1), offset, &token).unwrap() | ||
70 | } | ||
71 | |||
60 | #[test] | 72 | #[test] |
61 | fn test_resolve_module() { | 73 | fn test_resolve_module() { |
62 | let snap = analysis(&[ | 74 | let snap = analysis(&[ |
@@ -145,3 +157,85 @@ fn test_resolve_crate_root() { | |||
145 | vec![CrateId(1)], | 157 | vec![CrateId(1)], |
146 | ); | 158 | ); |
147 | } | 159 | } |
160 | |||
161 | #[test] | ||
162 | fn test_fn_signature_two_args_first() { | ||
163 | let (desc, param) = get_signature( | ||
164 | r#"fn foo(x: u32, y: u32) -> u32 {x + y} | ||
165 | fn bar() { foo(<|>3, ); }"#); | ||
166 | |||
167 | assert_eq!(desc.name, "foo".to_string()); | ||
168 | assert_eq!(desc.params, vec!("x".to_string(),"y".to_string())); | ||
169 | assert_eq!(desc.ret_type, Some("-> u32".into())); | ||
170 | assert_eq!(param, Some(0)); | ||
171 | } | ||
172 | |||
173 | #[test] | ||
174 | fn test_fn_signature_two_args_second() { | ||
175 | let (desc, param) = get_signature( | ||
176 | r#"fn foo(x: u32, y: u32) -> u32 {x + y} | ||
177 | fn bar() { foo(3, <|>); }"#); | ||
178 | |||
179 | assert_eq!(desc.name, "foo".to_string()); | ||
180 | assert_eq!(desc.params, vec!("x".to_string(),"y".to_string())); | ||
181 | assert_eq!(desc.ret_type, Some("-> u32".into())); | ||
182 | assert_eq!(param, Some(1)); | ||
183 | } | ||
184 | |||
185 | #[test] | ||
186 | fn test_fn_signature_for_impl() { | ||
187 | let (desc, param) = get_signature( | ||
188 | r#"struct F; impl F { pub fn new() { F{}} } | ||
189 | fn bar() {let _ : F = F::new(<|>);}"#); | ||
190 | |||
191 | assert_eq!(desc.name, "new".to_string()); | ||
192 | assert_eq!(desc.params, Vec::<String>::new()); | ||
193 | assert_eq!(desc.ret_type, None); | ||
194 | assert_eq!(param, None); | ||
195 | } | ||
196 | |||
197 | #[test] | ||
198 | fn test_fn_signature_for_method_self() { | ||
199 | let (desc, param) = get_signature( | ||
200 | r#"struct F; | ||
201 | impl F { | ||
202 | pub fn new() -> F{ | ||
203 | F{} | ||
204 | } | ||
205 | |||
206 | pub fn do_it(&self) {} | ||
207 | } | ||
208 | |||
209 | fn bar() { | ||
210 | let f : F = F::new(); | ||
211 | f.do_it(<|>); | ||
212 | }"#); | ||
213 | |||
214 | assert_eq!(desc.name, "do_it".to_string()); | ||
215 | assert_eq!(desc.params, vec!["&self".to_string()]); | ||
216 | assert_eq!(desc.ret_type, None); | ||
217 | assert_eq!(param, None); | ||
218 | } | ||
219 | |||
220 | #[test] | ||
221 | fn test_fn_signature_for_method_with_arg() { | ||
222 | let (desc, param) = get_signature( | ||
223 | r#"struct F; | ||
224 | impl F { | ||
225 | pub fn new() -> F{ | ||
226 | F{} | ||
227 | } | ||
228 | |||
229 | pub fn do_it(&self, x: i32) {} | ||
230 | } | ||
231 | |||
232 | fn bar() { | ||
233 | let f : F = F::new(); | ||
234 | f.do_it(<|>); | ||
235 | }"#); | ||
236 | |||
237 | assert_eq!(desc.name, "do_it".to_string()); | ||
238 | assert_eq!(desc.params, vec!["&self".to_string(), "x".to_string()]); | ||
239 | assert_eq!(desc.ret_type, None); | ||
240 | assert_eq!(param, Some(1)); | ||
241 | } \ No newline at end of file | ||