aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax/src/tests.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_syntax/src/tests.rs')
-rw-r--r--crates/ra_syntax/src/tests.rs36
1 files changed, 2 insertions, 34 deletions
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`: