aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax
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/ra_syntax
parentb9aab22d569c4ffe4d4f544a778bf07441ccf118 (diff)
Unify tests
Diffstat (limited to 'crates/ra_syntax')
-rw-r--r--crates/ra_syntax/Cargo.toml1
-rw-r--r--crates/ra_syntax/src/tests.rs36
2 files changed, 3 insertions, 34 deletions
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`: