lc3_toolchain/
test_utils.rs1use anyhow::Result;
4use std::collections::HashMap;
5use std::fs;
6use std::path::PathBuf;
7
8pub fn get_test_files(path: &PathBuf) -> Result<HashMap<String, String>> {
11 if path.is_dir() {
12 let mut res: HashMap<String, String> = HashMap::new();
13 for dir in fs::read_dir(path)? {
14 let entry = dir?;
15 let entry = entry.path();
16 if entry.is_dir() {
17 unreachable!("Do not support recursive path!")
18 }
19 res.insert(
20 entry.file_name().unwrap().to_string_lossy().into(),
21 fs::read_to_string(&entry)?,
22 );
23 }
24 return Ok(res);
25 }
26 let mut map = HashMap::new();
27 map.insert(
28 path.file_name().unwrap().to_string_lossy().into(),
29 fs::read_to_string(path)?,
30 );
31 Ok(map)
32}
33
34pub fn compare_files(expected: &HashMap<String, String>, found: &HashMap<String, String>) {
35 for (filename, expected_content) in expected {
36 let found_content = found.get(filename);
37 assert!(found_content.is_some());
38 assert_eq!(
39 found_content.unwrap().as_str(),
40 expected_content.as_str(),
41 "The found file does not match the expected file!"
42 );
43 }
44}