summaryrefslogtreecommitdiff
path: root/dhall/build.rs
blob: 46bb3a4b7fa41da059264b6b94ec31e3f0227d1e (plain)
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
use std::env;
use std::ffi::OsString;
use std::fs::File;
use std::io::Write;
use std::path::Path;
use walkdir::WalkDir;

fn dhall_files_in_dir<'a>(
    dir: &'a Path,
    take_a_suffix: bool,
) -> impl Iterator<Item = (String, String)> + 'a {
    WalkDir::new(dir)
        .into_iter()
        .filter_map(|e| e.ok())
        .filter_map(move |path| {
            let path = path.path();
            let path = path.strip_prefix(dir).unwrap();
            if path.extension() != Some(&OsString::from("dhall")) {
                return None;
            }
            let path = path.to_string_lossy();
            let path = &path[..path.len() - 6];
            let path = if take_a_suffix {
                if &path[path.len() - 1..] != "A" {
                    return None;
                } else {
                    path[..path.len() - 1].to_owned()
                }
            } else {
                path.to_owned()
            };
            let name = path.replace("/", "_").replace("-", "_");
            Some((name, path))
        })
}

fn make_test_module(
    w: &mut impl Write,
    mod_name: &str,
    dir: &Path,
    feature: &str,
    mut exclude: impl FnMut(&str) -> bool,
) -> std::io::Result<()> {
    writeln!(w, "mod {} {{", mod_name)?;
    for (name, path) in dhall_files_in_dir(&dir.join("success/"), true) {
        if exclude(&("success/".to_owned() + &path)) {
            continue;
        }
        writeln!(
            w,
            r#"make_spec_test!({}, Success, success_{}, "success/{}");"#,
            feature, name, path
        )?;
    }
    for (name, path) in dhall_files_in_dir(&dir.join("failure/"), false) {
        if exclude(&("failure/".to_owned() + &path)) {
            continue;
        }
        writeln!(
            w,
            r#"make_spec_test!({}, Failure, failure_{}, "failure/{}");"#,
            feature, name, path
        )?;
    }
    writeln!(w, "}}")?;
    Ok(())
}

fn main() -> std::io::Result<()> {
    println!("cargo:rerun-if-changed=../dhall-lang/.git");
    println!(
        "cargo:rerun-if-changed=../.git/modules/dhall-lang/refs/heads/master"
    );
    let out_dir = env::var("OUT_DIR").unwrap();
    let tests_dir = Path::new("../dhall-lang/tests/");

    let parser_tests_path = Path::new(&out_dir).join("spec_tests.rs");
    let mut file = File::create(parser_tests_path)?;

    make_test_module(
        &mut file,
        "parse",
        &tests_dir.join("parser/"),
        "Parser",
        |path| {
            // Too slow in debug mode
            path == "success/largeExpression"
        },
    )?;

    make_test_module(
        &mut file,
        "printer",
        &tests_dir.join("parser/"),
        "Printer",
        |path| {
            // Failure tests are only for the parser
            path.starts_with("failure/")
            // Too slow in debug mode
            || path == "success/largeExpression"
        },
    )?;

    make_test_module(
        &mut file,
        "binary_encoding",
        &tests_dir.join("parser/"),
        "BinaryEncoding",
        |path| {
            // Failure tests are only for the parser
            path.starts_with("failure/")
            // Too slow in debug mode
            || path == "success/largeExpression"
            // Too much of a pain to implement; shouldn't make a difference
            // since lets disappear on normalization.
            || path == "success/multilet"
            // See https://github.com/pyfisch/cbor/issues/109
            || path == "success/double"
        },
    )?;

    make_test_module(
        &mut file,
        "beta_normalize",
        &tests_dir.join("normalization/"),
        "Normalization",
        |path| {
            // We don't support bignums
            path == "success/simple/integerToDouble"
            // Too slow
            || path == "success/remoteSystems"
        },
    )?;

    make_test_module(
        &mut file,
        "alpha_normalize",
        &tests_dir.join("alpha-normalization/"),
        "AlphaNormalization",
        |_| false,
    )?;

    Ok(())
}