aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/ty/tests.rs
diff options
context:
space:
mode:
authorFlorian Diebold <[email protected]>2018-12-24 14:36:54 +0000
committerFlorian Diebold <[email protected]>2018-12-24 14:36:54 +0000
commit4befde1eee5b1e2b7ddc9bf764b77f82b792c318 (patch)
tree1183f4812a9f98b3df5a936b49e09f639ea309b3 /crates/ra_hir/src/ty/tests.rs
parent655f5bc26190b94e237dcc485e405de0d192e6ab (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.rs89
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 @@
1use std::fmt::Write; 1use std::fmt::Write;
2use std::path::{PathBuf}; 2use std::path::{PathBuf, Path};
3use std::sync::Once; 3use std::fs;
4
5use flexi_logger::Logger;
6 4
7use ra_db::{SyntaxDatabase}; 5use ra_db::{SyntaxDatabase};
8use ra_syntax::ast::{self, AstNode}; 6use ra_syntax::ast::{self, AstNode};
9use test_utils::{project_dir, dir_tests}; 7use test_utils::{project_dir, assert_eq_text, read_text};
10 8
11use crate::{ 9use crate::{
12 source_binder, 10 source_binder,
13 mock::MockDatabase, 11 mock::MockDatabase,
14}; 12};
15 13
16fn 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]
21fn infer_basics() {
22 check_inference(
23 r#"
24fn 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]
39fn infer_let() {
40 check_inference(
41 r#"
42fn 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]
53fn infer_paths() {
54 check_inference(
55 r#"
56fn a() -> u32 { 1 }
57
58mod b {
59 fn c() -> u32 { 1 }
60}
61
62fn test() {
63 a();
64 b::c();
65}
66}"#,
67 "0003_paths.txt",
68 );
69}
70
71fn 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
99fn 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
44fn ellipsize(mut text: String, max_len: usize) -> String { 114fn 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]
63pub 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
69fn test_data_dir() -> PathBuf { 132fn 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}