-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathbuild.rs
More file actions
43 lines (36 loc) · 1.79 KB
/
build.rs
File metadata and controls
43 lines (36 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
use std::{io::Write, iter};
fn main() {
println!("cargo::rerun-if-changed=spec.processed.md");
// Generates tests out of spec.clear.md and spec.processed.md files
let test_in = String::from_utf8(std::fs::read("spec.clear.md").unwrap()).unwrap();
let iter_in = prepare_test_md_file(&test_in);
let test_out = String::from_utf8(std::fs::read("spec.processed.md").unwrap()).unwrap();
let out_tests = prepare_test_md_file(&test_out);
let mut f = std::fs::File::create("src/tests.rs").unwrap();
writeln!(f, "//! Generated by build.rs, do not modify.").unwrap();
writeln!(f, "//! See spec.clear.md and spec.processed.md.\n").unwrap();
writeln!(f, "use crate::test::{{process, assert_process_eq}};\n").unwrap();
iter_in
.zip(out_tests.chain(iter::repeat(("none".to_owned(), ""))))
.for_each(|((name, input_data), (out_name, output_data))| {
(name == out_name).then_some(()).unwrap_or_else(|| {
panic!("expected {name} test case section spec.processed.md, got {out_name}")
});
writeln!(f, "#[test]").unwrap();
writeln!(f, "fn {name}() {{").unwrap();
writeln!(f, " assert_process_eq!(").unwrap();
writeln!(f, " {input_data:?},").unwrap();
writeln!(f, " {output_data:?}").unwrap();
writeln!(f, " );").unwrap();
writeln!(f, "}}\n").unwrap();
})
}
fn prepare_test_md_file(input: &str) -> impl Iterator<Item = (String, &str)> {
let mut iter = input.split("#### ");
iter.next(); // skipping first section
iter.map(|section| section.split_once('\n').unwrap())
.map(|(name, code)| {
let name = name.replace([' ', '-', '(', ')'], "_").to_ascii_lowercase();
(name, code)
})
}