diff options
author | Florian Diebold <[email protected]> | 2018-12-24 14:36:54 +0000 |
---|---|---|
committer | Florian Diebold <[email protected]> | 2018-12-24 14:36:54 +0000 |
commit | 4befde1eee5b1e2b7ddc9bf764b77f82b792c318 (patch) | |
tree | 1183f4812a9f98b3df5a936b49e09f639ea309b3 /crates/ra_hir/src/ty/tests.rs | |
parent | 655f5bc26190b94e237dcc485e405de0d192e6ab (diff) |
Change inference tests to have one per file
Diffstat (limited to 'crates/ra_hir/src/ty/tests.rs')
-rw-r--r-- | crates/ra_hir/src/ty/tests.rs | 89 |
1 files changed, 76 insertions, 13 deletions
diff --git a/crates/ra_hir/src/ty/tests.rs b/crates/ra_hir/src/ty/tests.rs index 021227749..b6c02cd80 100644 --- a/crates/ra_hir/src/ty/tests.rs +++ b/crates/ra_hir/src/ty/tests.rs | |||
@@ -1,19 +1,74 @@ | |||
1 | use std::fmt::Write; | 1 | use std::fmt::Write; |
2 | use std::path::{PathBuf}; | 2 | use std::path::{PathBuf, Path}; |
3 | use std::sync::Once; | 3 | use std::fs; |
4 | |||
5 | use flexi_logger::Logger; | ||
6 | 4 | ||
7 | use ra_db::{SyntaxDatabase}; | 5 | use ra_db::{SyntaxDatabase}; |
8 | use ra_syntax::ast::{self, AstNode}; | 6 | use ra_syntax::ast::{self, AstNode}; |
9 | use test_utils::{project_dir, dir_tests}; | 7 | use test_utils::{project_dir, assert_eq_text, read_text}; |
10 | 8 | ||
11 | use crate::{ | 9 | use crate::{ |
12 | source_binder, | 10 | source_binder, |
13 | mock::MockDatabase, | 11 | mock::MockDatabase, |
14 | }; | 12 | }; |
15 | 13 | ||
16 | fn infer_file(content: &str) -> String { | 14 | // These tests compare the inference results for all expressions in a file |
15 | // against snapshots of the current results. If you change something and these | ||
16 | // tests fail expectedly, you can update the comparison files by deleting them | ||
17 | // and running the tests again. Similarly, to add a new test, just write the | ||
18 | // test here in the same pattern and it will automatically write the snapshot. | ||
19 | |||
20 | #[test] | ||
21 | fn infer_basics() { | ||
22 | check_inference( | ||
23 | r#" | ||
24 | fn test(a: u32, b: isize, c: !, d: &str) { | ||
25 | a; | ||
26 | b; | ||
27 | c; | ||
28 | d; | ||
29 | 1usize; | ||
30 | 1isize; | ||
31 | "test"; | ||
32 | 1.0f32; | ||
33 | }"#, | ||
34 | "0001_basics.txt", | ||
35 | ); | ||
36 | } | ||
37 | |||
38 | #[test] | ||
39 | fn infer_let() { | ||
40 | check_inference( | ||
41 | r#" | ||
42 | fn test() { | ||
43 | let a = 1isize; | ||
44 | let b: usize = 1; | ||
45 | let c = b; | ||
46 | } | ||
47 | }"#, | ||
48 | "0002_let.txt", | ||
49 | ); | ||
50 | } | ||
51 | |||
52 | #[test] | ||
53 | fn infer_paths() { | ||
54 | check_inference( | ||
55 | r#" | ||
56 | fn a() -> u32 { 1 } | ||
57 | |||
58 | mod b { | ||
59 | fn c() -> u32 { 1 } | ||
60 | } | ||
61 | |||
62 | fn test() { | ||
63 | a(); | ||
64 | b::c(); | ||
65 | } | ||
66 | }"#, | ||
67 | "0003_paths.txt", | ||
68 | ); | ||
69 | } | ||
70 | |||
71 | fn infer(content: &str) -> String { | ||
17 | let (db, _, file_id) = MockDatabase::with_single_file(content); | 72 | let (db, _, file_id) = MockDatabase::with_single_file(content); |
18 | let source_file = db.source_file(file_id); | 73 | let source_file = db.source_file(file_id); |
19 | let mut acc = String::new(); | 74 | let mut acc = String::new(); |
@@ -41,6 +96,21 @@ fn infer_file(content: &str) -> String { | |||
41 | acc | 96 | acc |
42 | } | 97 | } |
43 | 98 | ||
99 | fn check_inference(content: &str, data_file: impl AsRef<Path>) { | ||
100 | let data_file_path = test_data_dir().join(data_file); | ||
101 | let result = infer(content); | ||
102 | |||
103 | if !data_file_path.exists() { | ||
104 | println!("File with expected result doesn't exist, creating...\n"); | ||
105 | println!("{}\n{}", content, result); | ||
106 | fs::write(&data_file_path, &result).unwrap(); | ||
107 | panic!("File {:?} with expected result was created", data_file_path); | ||
108 | } | ||
109 | |||
110 | let expected = read_text(&data_file_path); | ||
111 | assert_eq_text!(&expected, &result); | ||
112 | } | ||
113 | |||
44 | fn ellipsize(mut text: String, max_len: usize) -> String { | 114 | fn ellipsize(mut text: String, max_len: usize) -> String { |
45 | if text.len() <= max_len { | 115 | if text.len() <= max_len { |
46 | return text; | 116 | return text; |
@@ -59,13 +129,6 @@ fn ellipsize(mut text: String, max_len: usize) -> String { | |||
59 | text | 129 | text |
60 | } | 130 | } |
61 | 131 | ||
62 | #[test] | ||
63 | pub fn infer_tests() { | ||
64 | static INIT: Once = Once::new(); | ||
65 | INIT.call_once(|| Logger::with_env().start().unwrap()); | ||
66 | dir_tests(&test_data_dir(), &["."], |text, _path| infer_file(text)); | ||
67 | } | ||
68 | |||
69 | fn test_data_dir() -> PathBuf { | 132 | fn test_data_dir() -> PathBuf { |
70 | project_dir().join("crates/ra_hir/src/ty/tests/data") | 133 | project_dir().join("crates/ra_hir/src/ty/tests/data") |
71 | } | 134 | } |