aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-07-09 09:58:56 +0100
committerAleksey Kladov <[email protected]>2020-07-09 09:58:56 +0100
commitb660681a6becdcd33d4ed8cbb167c2a3dc170990 (patch)
treef249a5b4b21420dd833bc52e3705cd34a8b3c4f9 /crates
parentb9aab22d569c4ffe4d4f544a778bf07441ccf118 (diff)
Unify tests
Diffstat (limited to 'crates')
-rw-r--r--crates/expect/src/lib.rs27
-rw-r--r--crates/ra_syntax/Cargo.toml1
-rw-r--r--crates/ra_syntax/src/tests.rs36
3 files changed, 25 insertions, 39 deletions
diff --git a/crates/expect/src/lib.rs b/crates/expect/src/lib.rs
index 7579a5027..408448eed 100644
--- a/crates/expect/src/lib.rs
+++ b/crates/expect/src/lib.rs
@@ -42,7 +42,9 @@ macro_rules! expect {
42/// expect_file!["/crates/foo/test_data/bar.html"] 42/// expect_file!["/crates/foo/test_data/bar.html"]
43#[macro_export] 43#[macro_export]
44macro_rules! expect_file { 44macro_rules! expect_file {
45 [$path:literal] => {$crate::ExpectFile { path: $path }}; 45 [$path:literal] => {$crate::ExpectFile {
46 path: $crate::ExpectFilePath::Static($path)
47 }};
46} 48}
47 49
48#[derive(Debug)] 50#[derive(Debug)]
@@ -53,7 +55,13 @@ pub struct Expect {
53 55
54#[derive(Debug)] 56#[derive(Debug)]
55pub struct ExpectFile { 57pub struct ExpectFile {
56 pub path: &'static str, 58 pub path: ExpectFilePath,
59}
60
61#[derive(Debug)]
62pub enum ExpectFilePath {
63 Static(&'static str),
64 Dynamic(PathBuf),
57} 65}
58 66
59#[derive(Debug)] 67#[derive(Debug)]
@@ -112,6 +120,9 @@ impl Expect {
112} 120}
113 121
114impl ExpectFile { 122impl ExpectFile {
123 pub fn new(path: PathBuf) -> ExpectFile {
124 ExpectFile { path: ExpectFilePath::Dynamic(path) }
125 }
115 pub fn assert_eq(&self, actual: &str) { 126 pub fn assert_eq(&self, actual: &str) {
116 let expected = self.read(); 127 let expected = self.read();
117 if actual == expected { 128 if actual == expected {
@@ -125,8 +136,14 @@ impl ExpectFile {
125 fn write(&self, contents: &str) { 136 fn write(&self, contents: &str) {
126 fs::write(self.abs_path(), contents).unwrap() 137 fs::write(self.abs_path(), contents).unwrap()
127 } 138 }
139 fn path(&self) -> &Path {
140 match &self.path {
141 ExpectFilePath::Static(it) => it.as_ref(),
142 ExpectFilePath::Dynamic(it) => it.as_path(),
143 }
144 }
128 fn abs_path(&self) -> PathBuf { 145 fn abs_path(&self) -> PathBuf {
129 workspace_root().join(self.path) 146 workspace_root().join(self.path())
130 } 147 }
131} 148}
132 149
@@ -154,11 +171,11 @@ impl Runtime {
154 fn fail_file(expect: &ExpectFile, expected: &str, actual: &str) { 171 fn fail_file(expect: &ExpectFile, expected: &str, actual: &str) {
155 let mut rt = RT.lock().unwrap_or_else(|poisoned| poisoned.into_inner()); 172 let mut rt = RT.lock().unwrap_or_else(|poisoned| poisoned.into_inner());
156 if update_expect() { 173 if update_expect() {
157 println!("\x1b[1m\x1b[92mupdating\x1b[0m: {}", expect.path); 174 println!("\x1b[1m\x1b[92mupdating\x1b[0m: {}", expect.path().display());
158 expect.write(actual); 175 expect.write(actual);
159 return; 176 return;
160 } 177 }
161 rt.panic(expect.path.to_string(), expected, actual); 178 rt.panic(expect.path().display().to_string(), expected, actual);
162 } 179 }
163 180
164 fn panic(&mut self, position: String, expected: &str, actual: &str) { 181 fn panic(&mut self, position: String, expected: &str, actual: &str) {
diff --git a/crates/ra_syntax/Cargo.toml b/crates/ra_syntax/Cargo.toml
index 49696ce75..cb21b8053 100644
--- a/crates/ra_syntax/Cargo.toml
+++ b/crates/ra_syntax/Cargo.toml
@@ -31,4 +31,5 @@ serde = { version = "1.0.106", features = ["derive"] }
31 31
32[dev-dependencies] 32[dev-dependencies]
33test_utils = { path = "../test_utils" } 33test_utils = { path = "../test_utils" }
34expect = { path = "../expect" }
34walkdir = "2.3.1" 35walkdir = "2.3.1"
diff --git a/crates/ra_syntax/src/tests.rs b/crates/ra_syntax/src/tests.rs
index 7b4232497..a5b6e972e 100644
--- a/crates/ra_syntax/src/tests.rs
+++ b/crates/ra_syntax/src/tests.rs
@@ -1,11 +1,10 @@
1use std::{ 1use std::{
2 env,
3 fmt::Write, 2 fmt::Write,
4 fs, 3 fs,
5 path::{Component, Path, PathBuf}, 4 path::{Component, Path, PathBuf},
6}; 5};
7 6
8use test_utils::{assert_eq_text, project_dir}; 7use test_utils::project_dir;
9 8
10use crate::{fuzz, tokenize, SourceFile, SyntaxError, TextRange, TextSize, Token}; 9use crate::{fuzz, tokenize, SourceFile, SyntaxError, TextRange, TextSize, Token};
11 10
@@ -218,15 +217,7 @@ where
218 for (path, input_code) in collect_rust_files(test_data_dir, paths) { 217 for (path, input_code) in collect_rust_files(test_data_dir, paths) {
219 let actual = f(&input_code, &path); 218 let actual = f(&input_code, &path);
220 let path = path.with_extension(outfile_extension); 219 let path = path.with_extension(outfile_extension);
221 if !path.exists() { 220 expect::ExpectFile::new(path).assert_eq(&actual)
222 println!("\nfile: {}", path.display());
223 println!("No .txt file with expected result, creating...\n");
224 println!("{}\n{}", input_code, actual);
225 fs::write(&path, &actual).unwrap();
226 panic!("No expected result");
227 }
228 let expected = read_text(&path);
229 assert_equal_text(&expected, &actual, &path);
230 } 221 }
231} 222}
232 223
@@ -259,29 +250,6 @@ fn rust_files_in_dir(dir: &Path) -> Vec<PathBuf> {
259 acc 250 acc
260} 251}
261 252
262/// Asserts that `expected` and `actual` strings are equal. If they differ only
263/// in trailing or leading whitespace the test won't fail and
264/// the contents of `actual` will be written to the file located at `path`.
265fn assert_equal_text(expected: &str, actual: &str, path: &Path) {
266 if expected == actual {
267 return;
268 }
269 let dir = project_dir();
270 let pretty_path = path.strip_prefix(&dir).unwrap_or_else(|_| path);
271 if expected.trim() == actual.trim() {
272 println!("whitespace difference, rewriting");
273 println!("file: {}\n", pretty_path.display());
274 fs::write(path, actual).unwrap();
275 return;
276 }
277 if env::var("UPDATE_EXPECT").is_ok() {
278 println!("rewriting {}", pretty_path.display());
279 fs::write(path, actual).unwrap();
280 return;
281 }
282 assert_eq_text!(expected, actual, "file: {}", pretty_path.display());
283}
284
285/// Read file and normalize newlines. 253/// Read file and normalize newlines.
286/// 254///
287/// `rustc` seems to always normalize `\r\n` newlines to `\n`: 255/// `rustc` seems to always normalize `\r\n` newlines to `\n`: