diff options
Diffstat (limited to 'crates/ra_hir/src/ty/tests.rs')
-rw-r--r-- | crates/ra_hir/src/ty/tests.rs | 76 |
1 files changed, 29 insertions, 47 deletions
diff --git a/crates/ra_hir/src/ty/tests.rs b/crates/ra_hir/src/ty/tests.rs index dc9b626dc..9bf1fd3b2 100644 --- a/crates/ra_hir/src/ty/tests.rs +++ b/crates/ra_hir/src/ty/tests.rs | |||
@@ -1,11 +1,8 @@ | |||
1 | use std::sync::Arc; | 1 | use std::sync::Arc; |
2 | use std::fmt::Write; | 2 | use std::fmt::Write; |
3 | use std::path::{PathBuf, Path}; | ||
4 | use std::fs; | ||
5 | 3 | ||
6 | use ra_db::{SyntaxDatabase, salsa::Database}; | 4 | use ra_db::{SyntaxDatabase, salsa::Database}; |
7 | use ra_syntax::ast::{self, AstNode}; | 5 | use ra_syntax::ast::{self, AstNode}; |
8 | use test_utils::{project_dir, assert_eq_text, read_text}; | ||
9 | 6 | ||
10 | use crate::{ | 7 | use crate::{ |
11 | source_binder, | 8 | source_binder, |
@@ -13,14 +10,13 @@ use crate::{ | |||
13 | }; | 10 | }; |
14 | 11 | ||
15 | // These tests compare the inference results for all expressions in a file | 12 | // These tests compare the inference results for all expressions in a file |
16 | // against snapshots of the expected results. If you change something and these | 13 | // against snapshots of the expected results using insta. Run the tests with |
17 | // tests fail expectedly, you can update the comparison files by deleting them | 14 | // INSTA_UPDATE=1 to update the snapshots. |
18 | // and running the tests again. Similarly, to add a new test, just write the | ||
19 | // test here in the same pattern and it will automatically write the snapshot. | ||
20 | 15 | ||
21 | #[test] | 16 | #[test] |
22 | fn infer_basics() { | 17 | fn infer_basics() { |
23 | check_inference( | 18 | check_inference( |
19 | "infer_basics", | ||
24 | r#" | 20 | r#" |
25 | fn test(a: u32, b: isize, c: !, d: &str) { | 21 | fn test(a: u32, b: isize, c: !, d: &str) { |
26 | a; | 22 | a; |
@@ -32,13 +28,13 @@ fn test(a: u32, b: isize, c: !, d: &str) { | |||
32 | "test"; | 28 | "test"; |
33 | 1.0f32; | 29 | 1.0f32; |
34 | }"#, | 30 | }"#, |
35 | "basics.txt", | ||
36 | ); | 31 | ); |
37 | } | 32 | } |
38 | 33 | ||
39 | #[test] | 34 | #[test] |
40 | fn infer_let() { | 35 | fn infer_let() { |
41 | check_inference( | 36 | check_inference( |
37 | "infer_let", | ||
42 | r#" | 38 | r#" |
43 | fn test() { | 39 | fn test() { |
44 | let a = 1isize; | 40 | let a = 1isize; |
@@ -46,13 +42,13 @@ fn test() { | |||
46 | let c = b; | 42 | let c = b; |
47 | } | 43 | } |
48 | }"#, | 44 | }"#, |
49 | "let.txt", | ||
50 | ); | 45 | ); |
51 | } | 46 | } |
52 | 47 | ||
53 | #[test] | 48 | #[test] |
54 | fn infer_paths() { | 49 | fn infer_paths() { |
55 | check_inference( | 50 | check_inference( |
51 | "infer_paths", | ||
56 | r#" | 52 | r#" |
57 | fn a() -> u32 { 1 } | 53 | fn a() -> u32 { 1 } |
58 | 54 | ||
@@ -65,13 +61,13 @@ fn test() { | |||
65 | b::c(); | 61 | b::c(); |
66 | } | 62 | } |
67 | }"#, | 63 | }"#, |
68 | "paths.txt", | ||
69 | ); | 64 | ); |
70 | } | 65 | } |
71 | 66 | ||
72 | #[test] | 67 | #[test] |
73 | fn infer_struct() { | 68 | fn infer_struct() { |
74 | check_inference( | 69 | check_inference( |
70 | "infer_struct", | ||
75 | r#" | 71 | r#" |
76 | struct A { | 72 | struct A { |
77 | b: B, | 73 | b: B, |
@@ -88,13 +84,13 @@ fn test() { | |||
88 | a.c; | 84 | a.c; |
89 | } | 85 | } |
90 | "#, | 86 | "#, |
91 | "struct.txt", | ||
92 | ); | 87 | ); |
93 | } | 88 | } |
94 | 89 | ||
95 | #[test] | 90 | #[test] |
96 | fn infer_enum() { | 91 | fn infer_enum() { |
97 | check_inference( | 92 | check_inference( |
93 | "infer_enum", | ||
98 | r#" | 94 | r#" |
99 | enum E { | 95 | enum E { |
100 | V1 { field: u32 }, | 96 | V1 { field: u32 }, |
@@ -104,13 +100,13 @@ fn test() { | |||
104 | E::V1 { field: 1 }; | 100 | E::V1 { field: 1 }; |
105 | E::V2; | 101 | E::V2; |
106 | }"#, | 102 | }"#, |
107 | "enum.txt", | ||
108 | ); | 103 | ); |
109 | } | 104 | } |
110 | 105 | ||
111 | #[test] | 106 | #[test] |
112 | fn infer_refs() { | 107 | fn infer_refs() { |
113 | check_inference( | 108 | check_inference( |
109 | "infer_refs", | ||
114 | r#" | 110 | r#" |
115 | fn test(a: &u32, b: &mut u32, c: *const u32, d: *mut u32) { | 111 | fn test(a: &u32, b: &mut u32, c: *const u32, d: *mut u32) { |
116 | a; | 112 | a; |
@@ -126,13 +122,13 @@ fn test(a: &u32, b: &mut u32, c: *const u32, d: *mut u32) { | |||
126 | *d; | 122 | *d; |
127 | } | 123 | } |
128 | "#, | 124 | "#, |
129 | "refs_and_ptrs.txt", | ||
130 | ); | 125 | ); |
131 | } | 126 | } |
132 | 127 | ||
133 | #[test] | 128 | #[test] |
134 | fn infer_literals() { | 129 | fn infer_literals() { |
135 | check_inference( | 130 | check_inference( |
131 | "infer_literals", | ||
136 | r##" | 132 | r##" |
137 | fn test() { | 133 | fn test() { |
138 | 5i32; | 134 | 5i32; |
@@ -152,13 +148,13 @@ fn test() { | |||
152 | br#"yolo"#; | 148 | br#"yolo"#; |
153 | } | 149 | } |
154 | "##, | 150 | "##, |
155 | "literals.txt", | ||
156 | ); | 151 | ); |
157 | } | 152 | } |
158 | 153 | ||
159 | #[test] | 154 | #[test] |
160 | fn infer_unary_op() { | 155 | fn infer_unary_op() { |
161 | check_inference( | 156 | check_inference( |
157 | "infer_unary_op", | ||
162 | r#" | 158 | r#" |
163 | enum SomeType {} | 159 | enum SomeType {} |
164 | 160 | ||
@@ -175,13 +171,13 @@ fn test(x: SomeType) { | |||
175 | -"hello"; | 171 | -"hello"; |
176 | } | 172 | } |
177 | "#, | 173 | "#, |
178 | "unary_op.txt", | ||
179 | ); | 174 | ); |
180 | } | 175 | } |
181 | 176 | ||
182 | #[test] | 177 | #[test] |
183 | fn infer_backwards() { | 178 | fn infer_backwards() { |
184 | check_inference( | 179 | check_inference( |
180 | "infer_backwards", | ||
185 | r#" | 181 | r#" |
186 | fn takes_u32(x: u32) {} | 182 | fn takes_u32(x: u32) {} |
187 | 183 | ||
@@ -196,13 +192,13 @@ fn test() -> &mut &f64 { | |||
196 | &mut &c | 192 | &mut &c |
197 | } | 193 | } |
198 | "#, | 194 | "#, |
199 | "backwards.txt", | ||
200 | ); | 195 | ); |
201 | } | 196 | } |
202 | 197 | ||
203 | #[test] | 198 | #[test] |
204 | fn infer_self() { | 199 | fn infer_self() { |
205 | check_inference( | 200 | check_inference( |
201 | "infer_self", | ||
206 | r#" | 202 | r#" |
207 | struct S; | 203 | struct S; |
208 | 204 | ||
@@ -215,13 +211,13 @@ impl S { | |||
215 | } | 211 | } |
216 | } | 212 | } |
217 | "#, | 213 | "#, |
218 | "self.txt", | ||
219 | ); | 214 | ); |
220 | } | 215 | } |
221 | 216 | ||
222 | #[test] | 217 | #[test] |
223 | fn infer_binary_op() { | 218 | fn infer_binary_op() { |
224 | check_inference( | 219 | check_inference( |
220 | "infer_binary_op", | ||
225 | r#" | 221 | r#" |
226 | fn f(x: bool) -> i32 { | 222 | fn f(x: bool) -> i32 { |
227 | 0i32 | 223 | 0i32 |
@@ -242,13 +238,13 @@ fn test() -> bool { | |||
242 | ten < 3 | 238 | ten < 3 |
243 | } | 239 | } |
244 | "#, | 240 | "#, |
245 | "binary_op.txt", | ||
246 | ); | 241 | ); |
247 | } | 242 | } |
248 | 243 | ||
249 | #[test] | 244 | #[test] |
250 | fn infer_field_autoderef() { | 245 | fn infer_field_autoderef() { |
251 | check_inference( | 246 | check_inference( |
247 | "infer_field_autoderef", | ||
252 | r#" | 248 | r#" |
253 | struct A { | 249 | struct A { |
254 | b: B, | 250 | b: B, |
@@ -273,25 +269,25 @@ fn test2(a1: *const A, a2: *mut A) { | |||
273 | a2.b; | 269 | a2.b; |
274 | } | 270 | } |
275 | "#, | 271 | "#, |
276 | "field_autoderef.txt", | ||
277 | ); | 272 | ); |
278 | } | 273 | } |
279 | 274 | ||
280 | #[test] | 275 | #[test] |
281 | fn infer_bug_484() { | 276 | fn bug_484() { |
282 | check_inference( | 277 | check_inference( |
278 | "bug_484", | ||
283 | r#" | 279 | r#" |
284 | fn test() { | 280 | fn test() { |
285 | let x = if true {}; | 281 | let x = if true {}; |
286 | } | 282 | } |
287 | "#, | 283 | "#, |
288 | "bug_484.txt", | ||
289 | ); | 284 | ); |
290 | } | 285 | } |
291 | 286 | ||
292 | #[test] | 287 | #[test] |
293 | fn infer_inherent_method() { | 288 | fn infer_inherent_method() { |
294 | check_inference( | 289 | check_inference( |
290 | "infer_inherent_method", | ||
295 | r#" | 291 | r#" |
296 | struct A; | 292 | struct A; |
297 | 293 | ||
@@ -311,13 +307,13 @@ fn test(a: A) { | |||
311 | a.bar(1); | 307 | a.bar(1); |
312 | } | 308 | } |
313 | "#, | 309 | "#, |
314 | "inherent_method.txt", | ||
315 | ); | 310 | ); |
316 | } | 311 | } |
317 | 312 | ||
318 | #[test] | 313 | #[test] |
319 | fn infer_tuple() { | 314 | fn infer_tuple() { |
320 | check_inference( | 315 | check_inference( |
316 | "infer_tuple", | ||
321 | r#" | 317 | r#" |
322 | fn test(x: &str, y: isize) { | 318 | fn test(x: &str, y: isize) { |
323 | let a: (u32, &str) = (1, "a"); | 319 | let a: (u32, &str) = (1, "a"); |
@@ -328,13 +324,13 @@ fn test(x: &str, y: isize) { | |||
328 | let f = (e, "d"); | 324 | let f = (e, "d"); |
329 | } | 325 | } |
330 | "#, | 326 | "#, |
331 | "tuple.txt", | ||
332 | ); | 327 | ); |
333 | } | 328 | } |
334 | 329 | ||
335 | #[test] | 330 | #[test] |
336 | fn infer_array() { | 331 | fn infer_array() { |
337 | check_inference( | 332 | check_inference( |
333 | "infer_array", | ||
338 | r#" | 334 | r#" |
339 | fn test(x: &str, y: isize) { | 335 | fn test(x: &str, y: isize) { |
340 | let a = [x]; | 336 | let a = [x]; |
@@ -354,13 +350,13 @@ fn test(x: &str, y: isize) { | |||
354 | let x: [u8; 0] = []; | 350 | let x: [u8; 0] = []; |
355 | } | 351 | } |
356 | "#, | 352 | "#, |
357 | "array.txt", | ||
358 | ); | 353 | ); |
359 | } | 354 | } |
360 | 355 | ||
361 | #[test] | 356 | #[test] |
362 | fn infer_pattern() { | 357 | fn infer_pattern() { |
363 | check_inference( | 358 | check_inference( |
359 | "infer_pattern", | ||
364 | r#" | 360 | r#" |
365 | fn test(x: &i32) { | 361 | fn test(x: &i32) { |
366 | let y = x; | 362 | let y = x; |
@@ -384,13 +380,13 @@ fn test(x: &i32) { | |||
384 | let k = mut_ref_to_x; | 380 | let k = mut_ref_to_x; |
385 | } | 381 | } |
386 | "#, | 382 | "#, |
387 | "pattern.txt", | ||
388 | ); | 383 | ); |
389 | } | 384 | } |
390 | 385 | ||
391 | #[test] | 386 | #[test] |
392 | fn infer_adt_pattern() { | 387 | fn infer_adt_pattern() { |
393 | check_inference( | 388 | check_inference( |
389 | "infer_adt_pattern", | ||
394 | r#" | 390 | r#" |
395 | enum E { | 391 | enum E { |
396 | A { x: usize }, | 392 | A { x: usize }, |
@@ -414,13 +410,13 @@ fn test() { | |||
414 | d; | 410 | d; |
415 | } | 411 | } |
416 | "#, | 412 | "#, |
417 | "adt_pattern.txt", | ||
418 | ); | 413 | ); |
419 | } | 414 | } |
420 | 415 | ||
421 | #[test] | 416 | #[test] |
422 | fn infer_struct_generics() { | 417 | fn infer_struct_generics() { |
423 | check_inference( | 418 | check_inference( |
419 | "infer_struct_generics", | ||
424 | r#" | 420 | r#" |
425 | struct A<T> { | 421 | struct A<T> { |
426 | x: T, | 422 | x: T, |
@@ -434,13 +430,13 @@ fn test(a1: A<u32>, i: i32) { | |||
434 | a3.x; | 430 | a3.x; |
435 | } | 431 | } |
436 | "#, | 432 | "#, |
437 | "struct_generics.txt", | ||
438 | ); | 433 | ); |
439 | } | 434 | } |
440 | 435 | ||
441 | #[test] | 436 | #[test] |
442 | fn infer_generics_in_patterns() { | 437 | fn infer_generics_in_patterns() { |
443 | check_inference( | 438 | check_inference( |
439 | "infer_generics_in_patterns", | ||
444 | r#" | 440 | r#" |
445 | struct A<T> { | 441 | struct A<T> { |
446 | x: T, | 442 | x: T, |
@@ -460,13 +456,13 @@ fn test(a1: A<u32>, o: Option<u64>) { | |||
460 | }; | 456 | }; |
461 | } | 457 | } |
462 | "#, | 458 | "#, |
463 | "generics_in_patterns.txt", | ||
464 | ); | 459 | ); |
465 | } | 460 | } |
466 | 461 | ||
467 | #[test] | 462 | #[test] |
468 | fn infer_function_generics() { | 463 | fn infer_function_generics() { |
469 | check_inference( | 464 | check_inference( |
465 | "infer_function_generics", | ||
470 | r#" | 466 | r#" |
471 | fn id<T>(t: T) -> T { t } | 467 | fn id<T>(t: T) -> T { t } |
472 | 468 | ||
@@ -476,13 +472,13 @@ fn test() { | |||
476 | let x: u64 = id(1); | 472 | let x: u64 = id(1); |
477 | } | 473 | } |
478 | "#, | 474 | "#, |
479 | "function_generics.txt", | ||
480 | ); | 475 | ); |
481 | } | 476 | } |
482 | 477 | ||
483 | #[test] | 478 | #[test] |
484 | fn infer_generic_chain() { | 479 | fn infer_generic_chain() { |
485 | check_inference( | 480 | check_inference( |
481 | "infer_generic_chain", | ||
486 | r#" | 482 | r#" |
487 | struct A<T> { | 483 | struct A<T> { |
488 | x: T, | 484 | x: T, |
@@ -503,13 +499,13 @@ fn test() -> i128 { | |||
503 | b.x() | 499 | b.x() |
504 | } | 500 | } |
505 | "#, | 501 | "#, |
506 | "generic_chain.txt", | ||
507 | ); | 502 | ); |
508 | } | 503 | } |
509 | 504 | ||
510 | #[test] | 505 | #[test] |
511 | fn no_panic_on_field_of_enum() { | 506 | fn no_panic_on_field_of_enum() { |
512 | check_inference( | 507 | check_inference( |
508 | "no_panic_on_field_of_enum", | ||
513 | r#" | 509 | r#" |
514 | enum X {} | 510 | enum X {} |
515 | 511 | ||
@@ -517,13 +513,13 @@ fn test(x: X) { | |||
517 | x.some_field; | 513 | x.some_field; |
518 | } | 514 | } |
519 | "#, | 515 | "#, |
520 | "no_panic_on_field_of_enum.txt", | ||
521 | ); | 516 | ); |
522 | } | 517 | } |
523 | 518 | ||
524 | #[test] | 519 | #[test] |
525 | fn bug_585() { | 520 | fn bug_585() { |
526 | check_inference( | 521 | check_inference( |
522 | "bug_585", | ||
527 | r#" | 523 | r#" |
528 | fn test() { | 524 | fn test() { |
529 | X {}; | 525 | X {}; |
@@ -533,7 +529,6 @@ fn test() { | |||
533 | } | 529 | } |
534 | } | 530 | } |
535 | "#, | 531 | "#, |
536 | "bug_585.txt", | ||
537 | ); | 532 | ); |
538 | } | 533 | } |
539 | 534 | ||
@@ -581,19 +576,10 @@ fn infer(content: &str) -> String { | |||
581 | acc | 576 | acc |
582 | } | 577 | } |
583 | 578 | ||
584 | fn check_inference(content: &str, data_file: impl AsRef<Path>) { | 579 | fn check_inference(name: &str, content: &str) { |
585 | let data_file_path = test_data_dir().join(data_file); | ||
586 | let result = infer(content); | 580 | let result = infer(content); |
587 | 581 | ||
588 | if !data_file_path.exists() { | 582 | insta::assert_snapshot_matches!(&name, &result); |
589 | println!("File with expected result doesn't exist, creating...\n"); | ||
590 | println!("{}\n{}", content, result); | ||
591 | fs::write(&data_file_path, &result).unwrap(); | ||
592 | panic!("File {:?} with expected result was created", data_file_path); | ||
593 | } | ||
594 | |||
595 | let expected = read_text(&data_file_path); | ||
596 | assert_eq_text!(&expected, &result); | ||
597 | } | 583 | } |
598 | 584 | ||
599 | fn ellipsize(mut text: String, max_len: usize) -> String { | 585 | fn ellipsize(mut text: String, max_len: usize) -> String { |
@@ -614,10 +600,6 @@ fn ellipsize(mut text: String, max_len: usize) -> String { | |||
614 | text | 600 | text |
615 | } | 601 | } |
616 | 602 | ||
617 | fn test_data_dir() -> PathBuf { | ||
618 | project_dir().join("crates/ra_hir/src/ty/tests/data") | ||
619 | } | ||
620 | |||
621 | #[test] | 603 | #[test] |
622 | fn typing_whitespace_inside_a_function_should_not_invalidate_types() { | 604 | fn typing_whitespace_inside_a_function_should_not_invalidate_types() { |
623 | let (mut db, pos) = MockDatabase::with_position( | 605 | let (mut db, pos) = MockDatabase::with_position( |