diff options
author | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-03-26 13:58:12 +0000 |
---|---|---|
committer | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-03-26 13:58:12 +0000 |
commit | 96acf4f7ec4257af0d5fd0415b31ab6757b475ad (patch) | |
tree | 9682647d3c44d372e0bd84552b24b697f24e44be /crates | |
parent | 619a4c05ba098220ab0fb65d42b7f415dfcc1b6a (diff) | |
parent | e03189c1109573bec9600eb20efd9291d69f1d5c (diff) |
Merge #1051
1051: Move ra_ide_api unit tests r=matklad a=detrumi
Closes #1048
Co-authored-by: Wilco Kusee <[email protected]>
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ra_ide_api/src/parent_module.rs | 30 | ||||
-rw-r--r-- | crates/ra_ide_api/src/references.rs | 48 | ||||
-rw-r--r-- | crates/ra_ide_api/src/symbol_index.rs | 58 | ||||
-rw-r--r-- | crates/ra_ide_api/src/syntax_tree.rs | 257 | ||||
-rw-r--r-- | crates/ra_ide_api/tests/test/main.rs | 376 |
5 files changed, 391 insertions, 378 deletions
diff --git a/crates/ra_ide_api/src/parent_module.rs b/crates/ra_ide_api/src/parent_module.rs index 603c3db6a..27788c984 100644 --- a/crates/ra_ide_api/src/parent_module.rs +++ b/crates/ra_ide_api/src/parent_module.rs | |||
@@ -28,7 +28,11 @@ pub(crate) fn crate_for(db: &RootDatabase, file_id: FileId) -> Vec<CrateId> { | |||
28 | 28 | ||
29 | #[cfg(test)] | 29 | #[cfg(test)] |
30 | mod tests { | 30 | mod tests { |
31 | use crate::mock_analysis::analysis_and_position; | 31 | use crate::{ |
32 | AnalysisChange, CrateGraph, | ||
33 | mock_analysis::{analysis_and_position, MockAnalysis}, | ||
34 | Edition::Edition2018, | ||
35 | }; | ||
32 | 36 | ||
33 | #[test] | 37 | #[test] |
34 | fn test_resolve_parent_module() { | 38 | fn test_resolve_parent_module() { |
@@ -59,4 +63,28 @@ mod tests { | |||
59 | let nav = analysis.parent_module(pos).unwrap().pop().unwrap(); | 63 | let nav = analysis.parent_module(pos).unwrap().pop().unwrap(); |
60 | nav.assert_match("baz MODULE FileId(1) [32; 44)"); | 64 | nav.assert_match("baz MODULE FileId(1) [32; 44)"); |
61 | } | 65 | } |
66 | |||
67 | #[test] | ||
68 | fn test_resolve_crate_root() { | ||
69 | let mock = MockAnalysis::with_files( | ||
70 | " | ||
71 | //- /bar.rs | ||
72 | mod foo; | ||
73 | //- /foo.rs | ||
74 | // empty <|> | ||
75 | ", | ||
76 | ); | ||
77 | let root_file = mock.id_of("/bar.rs"); | ||
78 | let mod_file = mock.id_of("/foo.rs"); | ||
79 | let mut host = mock.analysis_host(); | ||
80 | assert!(host.analysis().crate_for(mod_file).unwrap().is_empty()); | ||
81 | |||
82 | let mut crate_graph = CrateGraph::default(); | ||
83 | let crate_id = crate_graph.add_crate_root(root_file, Edition2018); | ||
84 | let mut change = AnalysisChange::new(); | ||
85 | change.set_crate_graph(crate_graph); | ||
86 | host.apply_change(change); | ||
87 | |||
88 | assert_eq!(host.analysis().crate_for(mod_file).unwrap(), vec![crate_id]); | ||
89 | } | ||
62 | } | 90 | } |
diff --git a/crates/ra_ide_api/src/references.rs b/crates/ra_ide_api/src/references.rs index 22741445a..20bbf11a3 100644 --- a/crates/ra_ide_api/src/references.rs +++ b/crates/ra_ide_api/src/references.rs | |||
@@ -216,10 +216,56 @@ mod tests { | |||
216 | use crate::{ | 216 | use crate::{ |
217 | mock_analysis::single_file_with_position, | 217 | mock_analysis::single_file_with_position, |
218 | mock_analysis::analysis_and_position, | 218 | mock_analysis::analysis_and_position, |
219 | FileId | 219 | FileId, ReferenceSearchResult |
220 | }; | 220 | }; |
221 | 221 | ||
222 | #[test] | 222 | #[test] |
223 | fn test_find_all_refs_for_local() { | ||
224 | let code = r#" | ||
225 | fn main() { | ||
226 | let mut i = 1; | ||
227 | let j = 1; | ||
228 | i = i<|> + j; | ||
229 | |||
230 | { | ||
231 | i = 0; | ||
232 | } | ||
233 | |||
234 | i = 5; | ||
235 | }"#; | ||
236 | |||
237 | let refs = get_all_refs(code); | ||
238 | assert_eq!(refs.len(), 5); | ||
239 | } | ||
240 | |||
241 | #[test] | ||
242 | fn test_find_all_refs_for_param_inside() { | ||
243 | let code = r#" | ||
244 | fn foo(i : u32) -> u32 { | ||
245 | i<|> | ||
246 | }"#; | ||
247 | |||
248 | let refs = get_all_refs(code); | ||
249 | assert_eq!(refs.len(), 2); | ||
250 | } | ||
251 | |||
252 | #[test] | ||
253 | fn test_find_all_refs_for_fn_param() { | ||
254 | let code = r#" | ||
255 | fn foo(i<|> : u32) -> u32 { | ||
256 | i | ||
257 | }"#; | ||
258 | |||
259 | let refs = get_all_refs(code); | ||
260 | assert_eq!(refs.len(), 2); | ||
261 | } | ||
262 | |||
263 | fn get_all_refs(text: &str) -> ReferenceSearchResult { | ||
264 | let (analysis, position) = single_file_with_position(text); | ||
265 | analysis.find_all_refs(position).unwrap().unwrap() | ||
266 | } | ||
267 | |||
268 | #[test] | ||
223 | fn test_rename_for_local() { | 269 | fn test_rename_for_local() { |
224 | test_rename( | 270 | test_rename( |
225 | r#" | 271 | r#" |
diff --git a/crates/ra_ide_api/src/symbol_index.rs b/crates/ra_ide_api/src/symbol_index.rs index 0978d164a..0eadc4e71 100644 --- a/crates/ra_ide_api/src/symbol_index.rs +++ b/crates/ra_ide_api/src/symbol_index.rs | |||
@@ -270,3 +270,61 @@ fn to_file_symbol(node: &SyntaxNode, file_id: FileId) -> Option<FileSymbol> { | |||
270 | container_name: None, | 270 | container_name: None, |
271 | }) | 271 | }) |
272 | } | 272 | } |
273 | |||
274 | #[cfg(test)] | ||
275 | mod tests { | ||
276 | use ra_syntax::SmolStr; | ||
277 | use crate::{ | ||
278 | navigation_target::NavigationTarget, | ||
279 | mock_analysis::single_file, | ||
280 | Query, | ||
281 | }; | ||
282 | |||
283 | #[test] | ||
284 | fn test_world_symbols_with_no_container() { | ||
285 | let code = r#" | ||
286 | enum FooInner { } | ||
287 | "#; | ||
288 | |||
289 | let mut symbols = get_symbols_matching(code, "FooInner"); | ||
290 | |||
291 | let s = symbols.pop().unwrap(); | ||
292 | |||
293 | assert_eq!(s.name(), "FooInner"); | ||
294 | assert!(s.container_name().is_none()); | ||
295 | } | ||
296 | |||
297 | #[test] | ||
298 | fn test_world_symbols_include_container_name() { | ||
299 | let code = r#" | ||
300 | fn foo() { | ||
301 | enum FooInner { } | ||
302 | } | ||
303 | "#; | ||
304 | |||
305 | let mut symbols = get_symbols_matching(code, "FooInner"); | ||
306 | |||
307 | let s = symbols.pop().unwrap(); | ||
308 | |||
309 | assert_eq!(s.name(), "FooInner"); | ||
310 | assert_eq!(s.container_name(), Some(&SmolStr::new("foo"))); | ||
311 | |||
312 | let code = r#" | ||
313 | mod foo { | ||
314 | struct FooInner; | ||
315 | } | ||
316 | "#; | ||
317 | |||
318 | let mut symbols = get_symbols_matching(code, "FooInner"); | ||
319 | |||
320 | let s = symbols.pop().unwrap(); | ||
321 | |||
322 | assert_eq!(s.name(), "FooInner"); | ||
323 | assert_eq!(s.container_name(), Some(&SmolStr::new("foo"))); | ||
324 | } | ||
325 | |||
326 | fn get_symbols_matching(text: &str, query: &str) -> Vec<NavigationTarget> { | ||
327 | let (analysis, _) = single_file(text); | ||
328 | analysis.symbol_search(Query::new(query.into())).unwrap() | ||
329 | } | ||
330 | } | ||
diff --git a/crates/ra_ide_api/src/syntax_tree.rs b/crates/ra_ide_api/src/syntax_tree.rs index bbe9222b4..276f8a8c8 100644 --- a/crates/ra_ide_api/src/syntax_tree.rs +++ b/crates/ra_ide_api/src/syntax_tree.rs | |||
@@ -85,3 +85,260 @@ fn syntax_tree_for_token<T: AstToken>(node: &T, text_range: TextRange) -> Option | |||
85 | 85 | ||
86 | None | 86 | None |
87 | } | 87 | } |
88 | |||
89 | #[cfg(test)] | ||
90 | mod tests { | ||
91 | use crate::mock_analysis::{single_file, single_file_with_range}; | ||
92 | |||
93 | #[test] | ||
94 | fn test_syntax_tree_without_range() { | ||
95 | // Basic syntax | ||
96 | let (analysis, file_id) = single_file(r#"fn foo() {}"#); | ||
97 | let syn = analysis.syntax_tree(file_id, None); | ||
98 | |||
99 | assert_eq!( | ||
100 | syn.trim(), | ||
101 | r#" | ||
102 | SOURCE_FILE@[0; 11) | ||
103 | FN_DEF@[0; 11) | ||
104 | FN_KW@[0; 2) | ||
105 | WHITESPACE@[2; 3) | ||
106 | NAME@[3; 6) | ||
107 | IDENT@[3; 6) "foo" | ||
108 | PARAM_LIST@[6; 8) | ||
109 | L_PAREN@[6; 7) | ||
110 | R_PAREN@[7; 8) | ||
111 | WHITESPACE@[8; 9) | ||
112 | BLOCK@[9; 11) | ||
113 | L_CURLY@[9; 10) | ||
114 | R_CURLY@[10; 11) | ||
115 | "# | ||
116 | .trim() | ||
117 | ); | ||
118 | |||
119 | let (analysis, file_id) = single_file( | ||
120 | r#" | ||
121 | fn test() { | ||
122 | assert!(" | ||
123 | fn foo() { | ||
124 | } | ||
125 | ", ""); | ||
126 | }"# | ||
127 | .trim(), | ||
128 | ); | ||
129 | let syn = analysis.syntax_tree(file_id, None); | ||
130 | |||
131 | assert_eq!( | ||
132 | syn.trim(), | ||
133 | r#" | ||
134 | SOURCE_FILE@[0; 60) | ||
135 | FN_DEF@[0; 60) | ||
136 | FN_KW@[0; 2) | ||
137 | WHITESPACE@[2; 3) | ||
138 | NAME@[3; 7) | ||
139 | IDENT@[3; 7) "test" | ||
140 | PARAM_LIST@[7; 9) | ||
141 | L_PAREN@[7; 8) | ||
142 | R_PAREN@[8; 9) | ||
143 | WHITESPACE@[9; 10) | ||
144 | BLOCK@[10; 60) | ||
145 | L_CURLY@[10; 11) | ||
146 | WHITESPACE@[11; 16) | ||
147 | EXPR_STMT@[16; 58) | ||
148 | MACRO_CALL@[16; 57) | ||
149 | PATH@[16; 22) | ||
150 | PATH_SEGMENT@[16; 22) | ||
151 | NAME_REF@[16; 22) | ||
152 | IDENT@[16; 22) "assert" | ||
153 | EXCL@[22; 23) | ||
154 | TOKEN_TREE@[23; 57) | ||
155 | L_PAREN@[23; 24) | ||
156 | STRING@[24; 52) | ||
157 | COMMA@[52; 53) | ||
158 | WHITESPACE@[53; 54) | ||
159 | STRING@[54; 56) | ||
160 | R_PAREN@[56; 57) | ||
161 | SEMI@[57; 58) | ||
162 | WHITESPACE@[58; 59) | ||
163 | R_CURLY@[59; 60) | ||
164 | "# | ||
165 | .trim() | ||
166 | ); | ||
167 | } | ||
168 | |||
169 | #[test] | ||
170 | fn test_syntax_tree_with_range() { | ||
171 | let (analysis, range) = single_file_with_range(r#"<|>fn foo() {}<|>"#.trim()); | ||
172 | let syn = analysis.syntax_tree(range.file_id, Some(range.range)); | ||
173 | |||
174 | assert_eq!( | ||
175 | syn.trim(), | ||
176 | r#" | ||
177 | FN_DEF@[0; 11) | ||
178 | FN_KW@[0; 2) | ||
179 | WHITESPACE@[2; 3) | ||
180 | NAME@[3; 6) | ||
181 | IDENT@[3; 6) "foo" | ||
182 | PARAM_LIST@[6; 8) | ||
183 | L_PAREN@[6; 7) | ||
184 | R_PAREN@[7; 8) | ||
185 | WHITESPACE@[8; 9) | ||
186 | BLOCK@[9; 11) | ||
187 | L_CURLY@[9; 10) | ||
188 | R_CURLY@[10; 11) | ||
189 | "# | ||
190 | .trim() | ||
191 | ); | ||
192 | |||
193 | let (analysis, range) = single_file_with_range( | ||
194 | r#"fn test() { | ||
195 | <|>assert!(" | ||
196 | fn foo() { | ||
197 | } | ||
198 | ", "");<|> | ||
199 | }"# | ||
200 | .trim(), | ||
201 | ); | ||
202 | let syn = analysis.syntax_tree(range.file_id, Some(range.range)); | ||
203 | |||
204 | assert_eq!( | ||
205 | syn.trim(), | ||
206 | r#" | ||
207 | EXPR_STMT@[16; 58) | ||
208 | MACRO_CALL@[16; 57) | ||
209 | PATH@[16; 22) | ||
210 | PATH_SEGMENT@[16; 22) | ||
211 | NAME_REF@[16; 22) | ||
212 | IDENT@[16; 22) "assert" | ||
213 | EXCL@[22; 23) | ||
214 | TOKEN_TREE@[23; 57) | ||
215 | L_PAREN@[23; 24) | ||
216 | STRING@[24; 52) | ||
217 | COMMA@[52; 53) | ||
218 | WHITESPACE@[53; 54) | ||
219 | STRING@[54; 56) | ||
220 | R_PAREN@[56; 57) | ||
221 | SEMI@[57; 58) | ||
222 | "# | ||
223 | .trim() | ||
224 | ); | ||
225 | } | ||
226 | |||
227 | #[test] | ||
228 | fn test_syntax_tree_inside_string() { | ||
229 | let (analysis, range) = single_file_with_range( | ||
230 | r#"fn test() { | ||
231 | assert!(" | ||
232 | <|>fn foo() { | ||
233 | }<|> | ||
234 | fn bar() { | ||
235 | } | ||
236 | ", ""); | ||
237 | }"# | ||
238 | .trim(), | ||
239 | ); | ||
240 | let syn = analysis.syntax_tree(range.file_id, Some(range.range)); | ||
241 | assert_eq!( | ||
242 | syn.trim(), | ||
243 | r#" | ||
244 | SOURCE_FILE@[0; 12) | ||
245 | FN_DEF@[0; 12) | ||
246 | FN_KW@[0; 2) | ||
247 | WHITESPACE@[2; 3) | ||
248 | NAME@[3; 6) | ||
249 | IDENT@[3; 6) "foo" | ||
250 | PARAM_LIST@[6; 8) | ||
251 | L_PAREN@[6; 7) | ||
252 | R_PAREN@[7; 8) | ||
253 | WHITESPACE@[8; 9) | ||
254 | BLOCK@[9; 12) | ||
255 | L_CURLY@[9; 10) | ||
256 | WHITESPACE@[10; 11) | ||
257 | R_CURLY@[11; 12) | ||
258 | "# | ||
259 | .trim() | ||
260 | ); | ||
261 | |||
262 | // With a raw string | ||
263 | let (analysis, range) = single_file_with_range( | ||
264 | r###"fn test() { | ||
265 | assert!(r#" | ||
266 | <|>fn foo() { | ||
267 | }<|> | ||
268 | fn bar() { | ||
269 | } | ||
270 | "#, ""); | ||
271 | }"### | ||
272 | .trim(), | ||
273 | ); | ||
274 | let syn = analysis.syntax_tree(range.file_id, Some(range.range)); | ||
275 | assert_eq!( | ||
276 | syn.trim(), | ||
277 | r#" | ||
278 | SOURCE_FILE@[0; 12) | ||
279 | FN_DEF@[0; 12) | ||
280 | FN_KW@[0; 2) | ||
281 | WHITESPACE@[2; 3) | ||
282 | NAME@[3; 6) | ||
283 | IDENT@[3; 6) "foo" | ||
284 | PARAM_LIST@[6; 8) | ||
285 | L_PAREN@[6; 7) | ||
286 | R_PAREN@[7; 8) | ||
287 | WHITESPACE@[8; 9) | ||
288 | BLOCK@[9; 12) | ||
289 | L_CURLY@[9; 10) | ||
290 | WHITESPACE@[10; 11) | ||
291 | R_CURLY@[11; 12) | ||
292 | "# | ||
293 | .trim() | ||
294 | ); | ||
295 | |||
296 | // With a raw string | ||
297 | let (analysis, range) = single_file_with_range( | ||
298 | r###"fn test() { | ||
299 | assert!(r<|>#" | ||
300 | fn foo() { | ||
301 | } | ||
302 | fn bar() { | ||
303 | }"<|>#, ""); | ||
304 | }"### | ||
305 | .trim(), | ||
306 | ); | ||
307 | let syn = analysis.syntax_tree(range.file_id, Some(range.range)); | ||
308 | assert_eq!( | ||
309 | syn.trim(), | ||
310 | r#" | ||
311 | SOURCE_FILE@[0; 25) | ||
312 | FN_DEF@[0; 12) | ||
313 | FN_KW@[0; 2) | ||
314 | WHITESPACE@[2; 3) | ||
315 | NAME@[3; 6) | ||
316 | IDENT@[3; 6) "foo" | ||
317 | PARAM_LIST@[6; 8) | ||
318 | L_PAREN@[6; 7) | ||
319 | R_PAREN@[7; 8) | ||
320 | WHITESPACE@[8; 9) | ||
321 | BLOCK@[9; 12) | ||
322 | L_CURLY@[9; 10) | ||
323 | WHITESPACE@[10; 11) | ||
324 | R_CURLY@[11; 12) | ||
325 | WHITESPACE@[12; 13) | ||
326 | FN_DEF@[13; 25) | ||
327 | FN_KW@[13; 15) | ||
328 | WHITESPACE@[15; 16) | ||
329 | NAME@[16; 19) | ||
330 | IDENT@[16; 19) "bar" | ||
331 | PARAM_LIST@[19; 21) | ||
332 | L_PAREN@[19; 20) | ||
333 | R_PAREN@[20; 21) | ||
334 | WHITESPACE@[21; 22) | ||
335 | BLOCK@[22; 25) | ||
336 | L_CURLY@[22; 23) | ||
337 | WHITESPACE@[23; 24) | ||
338 | R_CURLY@[24; 25) | ||
339 | |||
340 | "# | ||
341 | .trim() | ||
342 | ); | ||
343 | } | ||
344 | } | ||
diff --git a/crates/ra_ide_api/tests/test/main.rs b/crates/ra_ide_api/tests/test/main.rs deleted file mode 100644 index d4ff21c09..000000000 --- a/crates/ra_ide_api/tests/test/main.rs +++ /dev/null | |||
@@ -1,376 +0,0 @@ | |||
1 | use ra_ide_api::{ | ||
2 | mock_analysis::{single_file, single_file_with_position, single_file_with_range, MockAnalysis}, | ||
3 | AnalysisChange, CrateGraph, Edition::Edition2018, Query, NavigationTarget, | ||
4 | ReferenceSearchResult, | ||
5 | }; | ||
6 | use ra_syntax::SmolStr; | ||
7 | |||
8 | #[test] | ||
9 | fn test_resolve_crate_root() { | ||
10 | let mock = MockAnalysis::with_files( | ||
11 | " | ||
12 | //- /bar.rs | ||
13 | mod foo; | ||
14 | //- /foo.rs | ||
15 | // empty <|> | ||
16 | ", | ||
17 | ); | ||
18 | let root_file = mock.id_of("/bar.rs"); | ||
19 | let mod_file = mock.id_of("/foo.rs"); | ||
20 | let mut host = mock.analysis_host(); | ||
21 | assert!(host.analysis().crate_for(mod_file).unwrap().is_empty()); | ||
22 | |||
23 | let mut crate_graph = CrateGraph::default(); | ||
24 | let crate_id = crate_graph.add_crate_root(root_file, Edition2018); | ||
25 | let mut change = AnalysisChange::new(); | ||
26 | change.set_crate_graph(crate_graph); | ||
27 | host.apply_change(change); | ||
28 | |||
29 | assert_eq!(host.analysis().crate_for(mod_file).unwrap(), vec![crate_id]); | ||
30 | } | ||
31 | |||
32 | fn get_all_refs(text: &str) -> ReferenceSearchResult { | ||
33 | let (analysis, position) = single_file_with_position(text); | ||
34 | analysis.find_all_refs(position).unwrap().unwrap() | ||
35 | } | ||
36 | |||
37 | fn get_symbols_matching(text: &str, query: &str) -> Vec<NavigationTarget> { | ||
38 | let (analysis, _) = single_file(text); | ||
39 | analysis.symbol_search(Query::new(query.into())).unwrap() | ||
40 | } | ||
41 | |||
42 | #[test] | ||
43 | fn test_find_all_refs_for_local() { | ||
44 | let code = r#" | ||
45 | fn main() { | ||
46 | let mut i = 1; | ||
47 | let j = 1; | ||
48 | i = i<|> + j; | ||
49 | |||
50 | { | ||
51 | i = 0; | ||
52 | } | ||
53 | |||
54 | i = 5; | ||
55 | }"#; | ||
56 | |||
57 | let refs = get_all_refs(code); | ||
58 | assert_eq!(refs.len(), 5); | ||
59 | } | ||
60 | |||
61 | #[test] | ||
62 | fn test_find_all_refs_for_param_inside() { | ||
63 | let code = r#" | ||
64 | fn foo(i : u32) -> u32 { | ||
65 | i<|> | ||
66 | }"#; | ||
67 | |||
68 | let refs = get_all_refs(code); | ||
69 | assert_eq!(refs.len(), 2); | ||
70 | } | ||
71 | |||
72 | #[test] | ||
73 | fn test_find_all_refs_for_fn_param() { | ||
74 | let code = r#" | ||
75 | fn foo(i<|> : u32) -> u32 { | ||
76 | i | ||
77 | }"#; | ||
78 | |||
79 | let refs = get_all_refs(code); | ||
80 | assert_eq!(refs.len(), 2); | ||
81 | } | ||
82 | |||
83 | #[test] | ||
84 | fn test_world_symbols_with_no_container() { | ||
85 | let code = r#" | ||
86 | enum FooInner { } | ||
87 | "#; | ||
88 | |||
89 | let mut symbols = get_symbols_matching(code, "FooInner"); | ||
90 | |||
91 | let s = symbols.pop().unwrap(); | ||
92 | |||
93 | assert_eq!(s.name(), "FooInner"); | ||
94 | assert!(s.container_name().is_none()); | ||
95 | } | ||
96 | |||
97 | #[test] | ||
98 | fn test_world_symbols_include_container_name() { | ||
99 | let code = r#" | ||
100 | fn foo() { | ||
101 | enum FooInner { } | ||
102 | } | ||
103 | "#; | ||
104 | |||
105 | let mut symbols = get_symbols_matching(code, "FooInner"); | ||
106 | |||
107 | let s = symbols.pop().unwrap(); | ||
108 | |||
109 | assert_eq!(s.name(), "FooInner"); | ||
110 | assert_eq!(s.container_name(), Some(&SmolStr::new("foo"))); | ||
111 | |||
112 | let code = r#" | ||
113 | mod foo { | ||
114 | struct FooInner; | ||
115 | } | ||
116 | "#; | ||
117 | |||
118 | let mut symbols = get_symbols_matching(code, "FooInner"); | ||
119 | |||
120 | let s = symbols.pop().unwrap(); | ||
121 | |||
122 | assert_eq!(s.name(), "FooInner"); | ||
123 | assert_eq!(s.container_name(), Some(&SmolStr::new("foo"))); | ||
124 | } | ||
125 | |||
126 | #[test] | ||
127 | fn test_syntax_tree_without_range() { | ||
128 | // Basic syntax | ||
129 | let (analysis, file_id) = single_file(r#"fn foo() {}"#); | ||
130 | let syn = analysis.syntax_tree(file_id, None); | ||
131 | |||
132 | assert_eq!( | ||
133 | syn.trim(), | ||
134 | r#" | ||
135 | SOURCE_FILE@[0; 11) | ||
136 | FN_DEF@[0; 11) | ||
137 | FN_KW@[0; 2) | ||
138 | WHITESPACE@[2; 3) | ||
139 | NAME@[3; 6) | ||
140 | IDENT@[3; 6) "foo" | ||
141 | PARAM_LIST@[6; 8) | ||
142 | L_PAREN@[6; 7) | ||
143 | R_PAREN@[7; 8) | ||
144 | WHITESPACE@[8; 9) | ||
145 | BLOCK@[9; 11) | ||
146 | L_CURLY@[9; 10) | ||
147 | R_CURLY@[10; 11) | ||
148 | "# | ||
149 | .trim() | ||
150 | ); | ||
151 | |||
152 | let (analysis, file_id) = single_file( | ||
153 | r#" | ||
154 | fn test() { | ||
155 | assert!(" | ||
156 | fn foo() { | ||
157 | } | ||
158 | ", ""); | ||
159 | }"# | ||
160 | .trim(), | ||
161 | ); | ||
162 | let syn = analysis.syntax_tree(file_id, None); | ||
163 | |||
164 | assert_eq!( | ||
165 | syn.trim(), | ||
166 | r#" | ||
167 | SOURCE_FILE@[0; 60) | ||
168 | FN_DEF@[0; 60) | ||
169 | FN_KW@[0; 2) | ||
170 | WHITESPACE@[2; 3) | ||
171 | NAME@[3; 7) | ||
172 | IDENT@[3; 7) "test" | ||
173 | PARAM_LIST@[7; 9) | ||
174 | L_PAREN@[7; 8) | ||
175 | R_PAREN@[8; 9) | ||
176 | WHITESPACE@[9; 10) | ||
177 | BLOCK@[10; 60) | ||
178 | L_CURLY@[10; 11) | ||
179 | WHITESPACE@[11; 16) | ||
180 | EXPR_STMT@[16; 58) | ||
181 | MACRO_CALL@[16; 57) | ||
182 | PATH@[16; 22) | ||
183 | PATH_SEGMENT@[16; 22) | ||
184 | NAME_REF@[16; 22) | ||
185 | IDENT@[16; 22) "assert" | ||
186 | EXCL@[22; 23) | ||
187 | TOKEN_TREE@[23; 57) | ||
188 | L_PAREN@[23; 24) | ||
189 | STRING@[24; 52) | ||
190 | COMMA@[52; 53) | ||
191 | WHITESPACE@[53; 54) | ||
192 | STRING@[54; 56) | ||
193 | R_PAREN@[56; 57) | ||
194 | SEMI@[57; 58) | ||
195 | WHITESPACE@[58; 59) | ||
196 | R_CURLY@[59; 60) | ||
197 | "# | ||
198 | .trim() | ||
199 | ); | ||
200 | } | ||
201 | |||
202 | #[test] | ||
203 | fn test_syntax_tree_with_range() { | ||
204 | let (analysis, range) = single_file_with_range(r#"<|>fn foo() {}<|>"#.trim()); | ||
205 | let syn = analysis.syntax_tree(range.file_id, Some(range.range)); | ||
206 | |||
207 | assert_eq!( | ||
208 | syn.trim(), | ||
209 | r#" | ||
210 | FN_DEF@[0; 11) | ||
211 | FN_KW@[0; 2) | ||
212 | WHITESPACE@[2; 3) | ||
213 | NAME@[3; 6) | ||
214 | IDENT@[3; 6) "foo" | ||
215 | PARAM_LIST@[6; 8) | ||
216 | L_PAREN@[6; 7) | ||
217 | R_PAREN@[7; 8) | ||
218 | WHITESPACE@[8; 9) | ||
219 | BLOCK@[9; 11) | ||
220 | L_CURLY@[9; 10) | ||
221 | R_CURLY@[10; 11) | ||
222 | "# | ||
223 | .trim() | ||
224 | ); | ||
225 | |||
226 | let (analysis, range) = single_file_with_range( | ||
227 | r#"fn test() { | ||
228 | <|>assert!(" | ||
229 | fn foo() { | ||
230 | } | ||
231 | ", "");<|> | ||
232 | }"# | ||
233 | .trim(), | ||
234 | ); | ||
235 | let syn = analysis.syntax_tree(range.file_id, Some(range.range)); | ||
236 | |||
237 | assert_eq!( | ||
238 | syn.trim(), | ||
239 | r#" | ||
240 | EXPR_STMT@[16; 58) | ||
241 | MACRO_CALL@[16; 57) | ||
242 | PATH@[16; 22) | ||
243 | PATH_SEGMENT@[16; 22) | ||
244 | NAME_REF@[16; 22) | ||
245 | IDENT@[16; 22) "assert" | ||
246 | EXCL@[22; 23) | ||
247 | TOKEN_TREE@[23; 57) | ||
248 | L_PAREN@[23; 24) | ||
249 | STRING@[24; 52) | ||
250 | COMMA@[52; 53) | ||
251 | WHITESPACE@[53; 54) | ||
252 | STRING@[54; 56) | ||
253 | R_PAREN@[56; 57) | ||
254 | SEMI@[57; 58) | ||
255 | "# | ||
256 | .trim() | ||
257 | ); | ||
258 | } | ||
259 | |||
260 | #[test] | ||
261 | fn test_syntax_tree_inside_string() { | ||
262 | let (analysis, range) = single_file_with_range( | ||
263 | r#"fn test() { | ||
264 | assert!(" | ||
265 | <|>fn foo() { | ||
266 | }<|> | ||
267 | fn bar() { | ||
268 | } | ||
269 | ", ""); | ||
270 | }"# | ||
271 | .trim(), | ||
272 | ); | ||
273 | let syn = analysis.syntax_tree(range.file_id, Some(range.range)); | ||
274 | assert_eq!( | ||
275 | syn.trim(), | ||
276 | r#" | ||
277 | SOURCE_FILE@[0; 12) | ||
278 | FN_DEF@[0; 12) | ||
279 | FN_KW@[0; 2) | ||
280 | WHITESPACE@[2; 3) | ||
281 | NAME@[3; 6) | ||
282 | IDENT@[3; 6) "foo" | ||
283 | PARAM_LIST@[6; 8) | ||
284 | L_PAREN@[6; 7) | ||
285 | R_PAREN@[7; 8) | ||
286 | WHITESPACE@[8; 9) | ||
287 | BLOCK@[9; 12) | ||
288 | L_CURLY@[9; 10) | ||
289 | WHITESPACE@[10; 11) | ||
290 | R_CURLY@[11; 12) | ||
291 | "# | ||
292 | .trim() | ||
293 | ); | ||
294 | |||
295 | // With a raw string | ||
296 | let (analysis, range) = single_file_with_range( | ||
297 | r###"fn test() { | ||
298 | assert!(r#" | ||
299 | <|>fn foo() { | ||
300 | }<|> | ||
301 | fn bar() { | ||
302 | } | ||
303 | "#, ""); | ||
304 | }"### | ||
305 | .trim(), | ||
306 | ); | ||
307 | let syn = analysis.syntax_tree(range.file_id, Some(range.range)); | ||
308 | assert_eq!( | ||
309 | syn.trim(), | ||
310 | r#" | ||
311 | SOURCE_FILE@[0; 12) | ||
312 | FN_DEF@[0; 12) | ||
313 | FN_KW@[0; 2) | ||
314 | WHITESPACE@[2; 3) | ||
315 | NAME@[3; 6) | ||
316 | IDENT@[3; 6) "foo" | ||
317 | PARAM_LIST@[6; 8) | ||
318 | L_PAREN@[6; 7) | ||
319 | R_PAREN@[7; 8) | ||
320 | WHITESPACE@[8; 9) | ||
321 | BLOCK@[9; 12) | ||
322 | L_CURLY@[9; 10) | ||
323 | WHITESPACE@[10; 11) | ||
324 | R_CURLY@[11; 12) | ||
325 | "# | ||
326 | .trim() | ||
327 | ); | ||
328 | |||
329 | // With a raw string | ||
330 | let (analysis, range) = single_file_with_range( | ||
331 | r###"fn test() { | ||
332 | assert!(r<|>#" | ||
333 | fn foo() { | ||
334 | } | ||
335 | fn bar() { | ||
336 | }"<|>#, ""); | ||
337 | }"### | ||
338 | .trim(), | ||
339 | ); | ||
340 | let syn = analysis.syntax_tree(range.file_id, Some(range.range)); | ||
341 | assert_eq!( | ||
342 | syn.trim(), | ||
343 | r#" | ||
344 | SOURCE_FILE@[0; 25) | ||
345 | FN_DEF@[0; 12) | ||
346 | FN_KW@[0; 2) | ||
347 | WHITESPACE@[2; 3) | ||
348 | NAME@[3; 6) | ||
349 | IDENT@[3; 6) "foo" | ||
350 | PARAM_LIST@[6; 8) | ||
351 | L_PAREN@[6; 7) | ||
352 | R_PAREN@[7; 8) | ||
353 | WHITESPACE@[8; 9) | ||
354 | BLOCK@[9; 12) | ||
355 | L_CURLY@[9; 10) | ||
356 | WHITESPACE@[10; 11) | ||
357 | R_CURLY@[11; 12) | ||
358 | WHITESPACE@[12; 13) | ||
359 | FN_DEF@[13; 25) | ||
360 | FN_KW@[13; 15) | ||
361 | WHITESPACE@[15; 16) | ||
362 | NAME@[16; 19) | ||
363 | IDENT@[16; 19) "bar" | ||
364 | PARAM_LIST@[19; 21) | ||
365 | L_PAREN@[19; 20) | ||
366 | R_PAREN@[20; 21) | ||
367 | WHITESPACE@[21; 22) | ||
368 | BLOCK@[22; 25) | ||
369 | L_CURLY@[22; 23) | ||
370 | WHITESPACE@[23; 24) | ||
371 | R_CURLY@[24; 25) | ||
372 | |||
373 | "# | ||
374 | .trim() | ||
375 | ); | ||
376 | } | ||