1 mod diff;
2 mod snippet;
3
4 use crate::snippet::SnippetDef;
5 use annotate_snippets::{display_list::DisplayList, snippet::Snippet};
6 use glob::glob;
7 use std::{error::Error, fs::File, io, io::prelude::*};
8
read_file(path: &str) -> Result<String, io::Error>9 fn read_file(path: &str) -> Result<String, io::Error> {
10 let mut f = File::open(path)?;
11 let mut s = String::new();
12 (f.read_to_string(&mut s))?;
13 Ok(s.trim_end().to_string())
14 }
15
read_fixture<'de>(src: &'de str) -> Result<Snippet<'de>, Box<dyn Error>>16 fn read_fixture<'de>(src: &'de str) -> Result<Snippet<'de>, Box<dyn Error>> {
17 Ok(toml::from_str(src).map(|a: SnippetDef| a.into())?)
18 }
19
20 #[test]
test_fixtures()21 fn test_fixtures() {
22 for entry in glob("./tests/fixtures/no-color/**/*.toml").expect("Failed to read glob pattern") {
23 let p = entry.expect("Error while getting an entry");
24
25 let path_in = p.to_str().expect("Can't print path");
26 let path_out = path_in.replace(".toml", ".txt");
27
28 let src = read_file(&path_in).expect("Failed to read file");
29 let snippet = read_fixture(&src).expect("Failed to read file");
30 let expected_out = read_file(&path_out).expect("Failed to read file");
31
32 let dl = DisplayList::from(snippet);
33 let actual_out = dl.to_string();
34 println!("{}", expected_out);
35 println!("{}", actual_out.trim_end());
36
37 assert_eq!(
38 expected_out,
39 actual_out.trim_end(),
40 "\n\n\nWhile parsing: {}\nThe diff is:\n\n\n{}\n\n\n",
41 path_in,
42 diff::get_diff(expected_out.as_str(), actual_out.as_str())
43 );
44 }
45 }
46