aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/ty
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src/ty')
-rw-r--r--crates/ra_hir/src/ty/tests.rs89
-rw-r--r--crates/ra_hir/src/ty/tests/data/0001_basics.rs11
-rw-r--r--crates/ra_hir/src/ty/tests/data/0002_let.rs5
-rw-r--r--crates/ra_hir/src/ty/tests/data/0002_let.txt14
-rw-r--r--crates/ra_hir/src/ty/tests/data/0003_paths.rs10
-rw-r--r--crates/ra_hir/src/ty/tests/data/0003_paths.txt18
6 files changed, 92 insertions, 55 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}
diff --git a/crates/ra_hir/src/ty/tests/data/0001_basics.rs b/crates/ra_hir/src/ty/tests/data/0001_basics.rs
deleted file mode 100644
index 59a60d031..000000000
--- a/crates/ra_hir/src/ty/tests/data/0001_basics.rs
+++ /dev/null
@@ -1,11 +0,0 @@
1
2fn test(a: u32, b: isize, c: !, d: &str) {
3 a;
4 b;
5 c;
6 d;
7 1usize;
8 1isize;
9 "test";
10 1.0f32;
11}
diff --git a/crates/ra_hir/src/ty/tests/data/0002_let.rs b/crates/ra_hir/src/ty/tests/data/0002_let.rs
deleted file mode 100644
index 5641da75b..000000000
--- a/crates/ra_hir/src/ty/tests/data/0002_let.rs
+++ /dev/null
@@ -1,5 +0,0 @@
1fn test() {
2 let a = 1isize;
3 let b: usize = 1;
4 let c = b;
5}
diff --git a/crates/ra_hir/src/ty/tests/data/0002_let.txt b/crates/ra_hir/src/ty/tests/data/0002_let.txt
index 5f515ee59..2d0d1f57b 100644
--- a/crates/ra_hir/src/ty/tests/data/0002_let.txt
+++ b/crates/ra_hir/src/ty/tests/data/0002_let.txt
@@ -1,7 +1,7 @@
1[51; 52) '1': [unknown] 1[21; 22) 'a': [unknown]
2[10; 70) '{ ...= b; }': () 2[52; 53) '1': [unknown]
3[24; 30) '1isize': [unknown] 3[11; 71) '{ ...= b; }': ()
4[20; 21) 'a': [unknown] 4[63; 64) 'c': usize
5[62; 63) 'c': usize 5[25; 31) '1isize': [unknown]
6[66; 67) 'b': usize 6[41; 42) 'b': usize
7[40; 41) 'b': usize 7[67; 68) 'b': usize
diff --git a/crates/ra_hir/src/ty/tests/data/0003_paths.rs b/crates/ra_hir/src/ty/tests/data/0003_paths.rs
deleted file mode 100644
index e8b11198b..000000000
--- a/crates/ra_hir/src/ty/tests/data/0003_paths.rs
+++ /dev/null
@@ -1,10 +0,0 @@
1fn a() -> u32 { 1 }
2
3mod b {
4 fn c() -> u32 { 1 }
5}
6
7fn test() {
8 a();
9 b::c();
10}
diff --git a/crates/ra_hir/src/ty/tests/data/0003_paths.txt b/crates/ra_hir/src/ty/tests/data/0003_paths.txt
index acde9b7ad..dcb5456ae 100644
--- a/crates/ra_hir/src/ty/tests/data/0003_paths.txt
+++ b/crates/ra_hir/src/ty/tests/data/0003_paths.txt
@@ -1,9 +1,9 @@
1[16; 17) '1': [unknown] 1[15; 20) '{ 1 }': [unknown]
2[14; 19) '{ 1 }': [unknown] 2[17; 18) '1': [unknown]
3[47; 52) '{ 1 }': [unknown] 3[50; 51) '1': [unknown]
4[49; 50) '1': [unknown] 4[48; 53) '{ 1 }': [unknown]
5[81; 87) 'b::c()': u32 5[82; 88) 'b::c()': u32
6[66; 90) '{ ...c(); }': () 6[67; 91) '{ ...c(); }': ()
7[72; 73) 'a': fn() -> u32 7[73; 74) 'a': fn() -> u32
8[72; 75) 'a()': u32 8[73; 76) 'a()': u32
9[81; 85) 'b::c': fn() -> u32 9[82; 86) 'b::c': fn() -> u32