]> git.lizzy.rs Git - rust.git/commitdiff
Change inference tests to have one per file
authorFlorian Diebold <flodiebold@gmail.com>
Mon, 24 Dec 2018 14:36:54 +0000 (15:36 +0100)
committerFlorian Diebold <flodiebold@gmail.com>
Mon, 24 Dec 2018 14:36:54 +0000 (15:36 +0100)
crates/ra_hir/src/ty/tests.rs
crates/ra_hir/src/ty/tests/data/0001_basics.rs [deleted file]
crates/ra_hir/src/ty/tests/data/0002_let.rs [deleted file]
crates/ra_hir/src/ty/tests/data/0002_let.txt
crates/ra_hir/src/ty/tests/data/0003_paths.rs [deleted file]
crates/ra_hir/src/ty/tests/data/0003_paths.txt

index 0212277492b7a6436164218b9ed6a4d48da3fadc..b6c02cd80c2bbed09c6f33578e92fc65fdf18cde 100644 (file)
@@ -1,19 +1,74 @@
 use std::fmt::Write;
-use std::path::{PathBuf};
-use std::sync::Once;
-
-use flexi_logger::Logger;
+use std::path::{PathBuf, Path};
+use std::fs;
 
 use ra_db::{SyntaxDatabase};
 use ra_syntax::ast::{self, AstNode};
-use test_utils::{project_dir, dir_tests};
+use test_utils::{project_dir, assert_eq_text, read_text};
 
 use crate::{
     source_binder,
     mock::MockDatabase,
 };
 
-fn infer_file(content: &str) -> String {
+// These tests compare the inference results for all expressions in a file
+// against snapshots of the current results. If you change something and these
+// tests fail expectedly, you can update the comparison files by deleting them
+// and running the tests again. Similarly, to add a new test, just write the
+// test here in the same pattern and it will automatically write the snapshot.
+
+#[test]
+fn infer_basics() {
+    check_inference(
+        r#"
+fn test(a: u32, b: isize, c: !, d: &str) {
+    a;
+    b;
+    c;
+    d;
+    1usize;
+    1isize;
+    "test";
+    1.0f32;
+}"#,
+        "0001_basics.txt",
+    );
+}
+
+#[test]
+fn infer_let() {
+    check_inference(
+        r#"
+fn test() {
+    let a = 1isize;
+    let b: usize = 1;
+    let c = b;
+}
+}"#,
+        "0002_let.txt",
+    );
+}
+
+#[test]
+fn infer_paths() {
+    check_inference(
+        r#"
+fn a() -> u32 { 1 }
+
+mod b {
+    fn c() -> u32 { 1 }
+}
+
+fn test() {
+    a();
+    b::c();
+}
+}"#,
+        "0003_paths.txt",
+    );
+}
+
+fn infer(content: &str) -> String {
     let (db, _, file_id) = MockDatabase::with_single_file(content);
     let source_file = db.source_file(file_id);
     let mut acc = String::new();
@@ -41,6 +96,21 @@ fn infer_file(content: &str) -> String {
     acc
 }
 
+fn check_inference(content: &str, data_file: impl AsRef<Path>) {
+    let data_file_path = test_data_dir().join(data_file);
+    let result = infer(content);
+
+    if !data_file_path.exists() {
+        println!("File with expected result doesn't exist, creating...\n");
+        println!("{}\n{}", content, result);
+        fs::write(&data_file_path, &result).unwrap();
+        panic!("File {:?} with expected result was created", data_file_path);
+    }
+
+    let expected = read_text(&data_file_path);
+    assert_eq_text!(&expected, &result);
+}
+
 fn ellipsize(mut text: String, max_len: usize) -> String {
     if text.len() <= max_len {
         return text;
@@ -59,13 +129,6 @@ fn ellipsize(mut text: String, max_len: usize) -> String {
     text
 }
 
-#[test]
-pub fn infer_tests() {
-    static INIT: Once = Once::new();
-    INIT.call_once(|| Logger::with_env().start().unwrap());
-    dir_tests(&test_data_dir(), &["."], |text, _path| infer_file(text));
-}
-
 fn test_data_dir() -> PathBuf {
     project_dir().join("crates/ra_hir/src/ty/tests/data")
 }
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 (file)
index 59a60d0..0000000
+++ /dev/null
@@ -1,11 +0,0 @@
-
-fn test(a: u32, b: isize, c: !, d: &str) {
-    a;
-    b;
-    c;
-    d;
-    1usize;
-    1isize;
-    "test";
-    1.0f32;
-}
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 (file)
index 5641da7..0000000
+++ /dev/null
@@ -1,5 +0,0 @@
-fn test() {
-    let a = 1isize;
-    let b: usize = 1;
-    let c = b;
-}
index 5f515ee594c95e2ec1b8fb2ed8da2dcd4e9361ad..2d0d1f57b3fad925721bd7a7eed828a84171ce08 100644 (file)
@@ -1,7 +1,7 @@
-[51; 52) '1': [unknown]
-[10; 70) '{     ...= b; }': ()
-[24; 30) '1isize': [unknown]
-[20; 21) 'a': [unknown]
-[62; 63) 'c': usize
-[66; 67) 'b': usize
-[40; 41) 'b': usize
+[21; 22) 'a': [unknown]
+[52; 53) '1': [unknown]
+[11; 71) '{     ...= b; }': ()
+[63; 64) 'c': usize
+[25; 31) '1isize': [unknown]
+[41; 42) 'b': usize
+[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 (file)
index e8b1119..0000000
+++ /dev/null
@@ -1,10 +0,0 @@
-fn a() -> u32 { 1 }
-
-mod b {
-    fn c() -> u32 { 1 }
-}
-
-fn test() {
-    a();
-    b::c();
-}
index acde9b7adf31465da832bdd2fa5a5a532f8196a9..dcb5456ae36d2b6254a35504c01fadc01e5381e8 100644 (file)
@@ -1,9 +1,9 @@
-[16; 17) '1': [unknown]
-[14; 19) '{ 1 }': [unknown]
-[47; 52) '{ 1 }': [unknown]
-[49; 50) '1': [unknown]
-[81; 87) 'b::c()': u32
-[66; 90) '{     ...c(); }': ()
-[72; 73) 'a': fn() -> u32
-[72; 75) 'a()': u32
-[81; 85) 'b::c': fn() -> u32
+[15; 20) '{ 1 }': [unknown]
+[17; 18) '1': [unknown]
+[50; 51) '1': [unknown]
+[48; 53) '{ 1 }': [unknown]
+[82; 88) 'b::c()': u32
+[67; 91) '{     ...c(); }': ()
+[73; 74) 'a': fn() -> u32
+[73; 76) 'a()': u32
+[82; 86) 'b::c': fn() -> u32