diff options
Diffstat (limited to 'crates/ra_syntax/src/tests.rs')
-rw-r--r-- | crates/ra_syntax/src/tests.rs | 100 |
1 files changed, 99 insertions, 1 deletions
diff --git a/crates/ra_syntax/src/tests.rs b/crates/ra_syntax/src/tests.rs index 959967b79..f14f23628 100644 --- a/crates/ra_syntax/src/tests.rs +++ b/crates/ra_syntax/src/tests.rs | |||
@@ -1,9 +1,11 @@ | |||
1 | use std::{ | 1 | use std::{ |
2 | env, | ||
2 | fmt::Write, | 3 | fmt::Write, |
4 | fs, | ||
3 | path::{Component, Path, PathBuf}, | 5 | path::{Component, Path, PathBuf}, |
4 | }; | 6 | }; |
5 | 7 | ||
6 | use test_utils::{collect_rust_files, dir_tests, project_dir, read_text}; | 8 | use test_utils::{assert_eq_text, project_dir}; |
7 | 9 | ||
8 | use crate::{fuzz, tokenize, SourceFile, SyntaxError, TextRange, TextSize, Token}; | 10 | use crate::{fuzz, tokenize, SourceFile, SyntaxError, TextRange, TextSize, Token}; |
9 | 11 | ||
@@ -200,3 +202,99 @@ where | |||
200 | } | 202 | } |
201 | }); | 203 | }); |
202 | } | 204 | } |
205 | |||
206 | /// Calls callback `f` with input code and file paths for each `.rs` file in `test_data_dir` | ||
207 | /// subdirectories defined by `paths`. | ||
208 | /// | ||
209 | /// If the content of the matching output file differs from the output of `f()` | ||
210 | /// the test will fail. | ||
211 | /// | ||
212 | /// If there is no matching output file it will be created and filled with the | ||
213 | /// output of `f()`, but the test will fail. | ||
214 | fn dir_tests<F>(test_data_dir: &Path, paths: &[&str], outfile_extension: &str, f: F) | ||
215 | where | ||
216 | F: Fn(&str, &Path) -> String, | ||
217 | { | ||
218 | for (path, input_code) in collect_rust_files(test_data_dir, paths) { | ||
219 | let actual = f(&input_code, &path); | ||
220 | let path = path.with_extension(outfile_extension); | ||
221 | if !path.exists() { | ||
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 | } | ||
231 | } | ||
232 | |||
233 | /// Collects all `.rs` files from `dir` subdirectories defined by `paths`. | ||
234 | fn collect_rust_files(root_dir: &Path, paths: &[&str]) -> Vec<(PathBuf, String)> { | ||
235 | paths | ||
236 | .iter() | ||
237 | .flat_map(|path| { | ||
238 | let path = root_dir.to_owned().join(path); | ||
239 | rust_files_in_dir(&path).into_iter() | ||
240 | }) | ||
241 | .map(|path| { | ||
242 | let text = read_text(&path); | ||
243 | (path, text) | ||
244 | }) | ||
245 | .collect() | ||
246 | } | ||
247 | |||
248 | /// Collects paths to all `.rs` files from `dir` in a sorted `Vec<PathBuf>`. | ||
249 | fn rust_files_in_dir(dir: &Path) -> Vec<PathBuf> { | ||
250 | let mut acc = Vec::new(); | ||
251 | for file in fs::read_dir(&dir).unwrap() { | ||
252 | let file = file.unwrap(); | ||
253 | let path = file.path(); | ||
254 | if path.extension().unwrap_or_default() == "rs" { | ||
255 | acc.push(path); | ||
256 | } | ||
257 | } | ||
258 | acc.sort(); | ||
259 | acc | ||
260 | } | ||
261 | |||
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`. | ||
265 | fn 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_EXPECTATIONS").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. | ||
286 | /// | ||
287 | /// `rustc` seems to always normalize `\r\n` newlines to `\n`: | ||
288 | /// | ||
289 | /// ``` | ||
290 | /// let s = " | ||
291 | /// "; | ||
292 | /// assert_eq!(s.as_bytes(), &[10]); | ||
293 | /// ``` | ||
294 | /// | ||
295 | /// so this should always be correct. | ||
296 | fn read_text(path: &Path) -> String { | ||
297 | fs::read_to_string(path) | ||
298 | .unwrap_or_else(|_| panic!("File at {:?} should be valid", path)) | ||
299 | .replace("\r\n", "\n") | ||
300 | } | ||