aboutsummaryrefslogtreecommitdiff
path: root/crates/test_utils
diff options
context:
space:
mode:
Diffstat (limited to 'crates/test_utils')
-rw-r--r--crates/test_utils/src/lib.rs34
1 files changed, 16 insertions, 18 deletions
diff --git a/crates/test_utils/src/lib.rs b/crates/test_utils/src/lib.rs
index db03df1c4..4164bfd5e 100644
--- a/crates/test_utils/src/lib.rs
+++ b/crates/test_utils/src/lib.rs
@@ -302,42 +302,40 @@ pub fn find_mismatch<'a>(expected: &'a Value, actual: &'a Value) -> Option<(&'a
302 } 302 }
303} 303}
304 304
305/// Calls callback `f` with input code and file paths of all `.rs` files from `test_data_dir` 305/// Calls callback `f` with input code and file paths for each `.rs` file in `test_data_dir`
306/// subdirectories defined by `paths`. 306/// subdirectories defined by `paths`.
307/// 307///
308/// If the content of the matching `.txt` file differs from the output of `f()` 308/// If the content of the matching output file differs from the output of `f()`
309/// the test will fail. 309/// the test will fail.
310/// 310///
311/// If there is no matching `.txt` file it will be created and filled with the 311/// If there is no matching output file it will be created and filled with the
312/// output of `f()`, but the test will fail. 312/// output of `f()`, but the test will fail.
313pub fn dir_tests<F>(test_data_dir: &Path, paths: &[&str], f: F) 313pub fn dir_tests<F>(test_data_dir: &Path, paths: &[&str], outfile_extension: &str, f: F)
314where 314where
315 F: Fn(&str, &Path) -> String, 315 F: Fn(&str, &Path) -> String,
316{ 316{
317 for (path, input_code) in collect_tests(test_data_dir, paths) { 317 for (path, input_code) in collect_rust_files(test_data_dir, paths) {
318 let parse_tree = f(&input_code, &path); 318 let actual = f(&input_code, &path);
319 let path = path.with_extension("txt"); 319 let path = path.with_extension(outfile_extension);
320 if !path.exists() { 320 if !path.exists() {
321 println!("\nfile: {}", path.display()); 321 println!("\nfile: {}", path.display());
322 println!("No .txt file with expected result, creating...\n"); 322 println!("No .txt file with expected result, creating...\n");
323 println!("{}\n{}", input_code, parse_tree); 323 println!("{}\n{}", input_code, actual);
324 fs::write(&path, &parse_tree).unwrap(); 324 fs::write(&path, &actual).unwrap();
325 panic!("No expected result") 325 panic!("No expected result");
326 } 326 }
327 let expected = read_text(&path); 327 let expected = read_text(&path);
328 let expected = expected.as_str(); 328 assert_equal_text(&expected, &actual, &path);
329 let parse_tree = parse_tree.as_str();
330 assert_equal_text(expected, parse_tree, &path);
331 } 329 }
332} 330}
333 331
334/// Collects all `.rs` files from `test_data_dir` subdirectories defined by `paths`. 332/// Collects all `.rs` files from `dir` subdirectories defined by `paths`.
335pub fn collect_tests(test_data_dir: &Path, paths: &[&str]) -> Vec<(PathBuf, String)> { 333pub fn collect_rust_files(root_dir: &Path, paths: &[&str]) -> Vec<(PathBuf, String)> {
336 paths 334 paths
337 .iter() 335 .iter()
338 .flat_map(|path| { 336 .flat_map(|path| {
339 let path = test_data_dir.to_owned().join(path); 337 let path = root_dir.to_owned().join(path);
340 test_from_dir(&path).into_iter() 338 rust_files_in_dir(&path).into_iter()
341 }) 339 })
342 .map(|path| { 340 .map(|path| {
343 let text = read_text(&path); 341 let text = read_text(&path);
@@ -347,7 +345,7 @@ pub fn collect_tests(test_data_dir: &Path, paths: &[&str]) -> Vec<(PathBuf, Stri
347} 345}
348 346
349/// Collects paths to all `.rs` files from `dir` in a sorted `Vec<PathBuf>`. 347/// Collects paths to all `.rs` files from `dir` in a sorted `Vec<PathBuf>`.
350fn test_from_dir(dir: &Path) -> Vec<PathBuf> { 348fn rust_files_in_dir(dir: &Path) -> Vec<PathBuf> {
351 let mut acc = Vec::new(); 349 let mut acc = Vec::new();
352 for file in fs::read_dir(&dir).unwrap() { 350 for file in fs::read_dir(&dir).unwrap() {
353 let file = file.unwrap(); 351 let file = file.unwrap();