summaryrefslogtreecommitdiff
path: root/dhall/src/tests.rs
blob: 8f16a12d8922d174ffef12c5cec3f3db3d6529d3 (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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
use pretty_assertions::assert_eq as assert_eq_pretty;

macro_rules! assert_eq_display {
    ($left:expr, $right:expr) => {{
        match (&$left, &$right) {
            (left_val, right_val) => {
                if !(*left_val == *right_val) {
                    panic!(
                        r#"assertion failed: `(left == right)`
 left: `{}`,
right: `{}`"#,
                        left_val, right_val
                    )
                }
            }
        }
    }};
}

#[macro_export]
macro_rules! make_spec_test {
    ($type:ident, $status:ident, $name:ident, $path:expr) => {
        #[test]
        #[allow(non_snake_case)]
        fn $name() {
            use crate::tests::*;
            match run_test_stringy_error($path, Feature::$type, Status::$status)
            {
                Ok(_) => {}
                Err(s) => panic!(s),
            }
        }
    };
}

use std::fs::File;
use std::io::Read;
use std::path::PathBuf;

use crate::error::{Error, Result};
use crate::phase::Parsed;

#[derive(Copy, Clone)]
pub enum Feature {
    Parser,
    Printer,
    BinaryEncoding,
    BinaryDecoding,
    Import,
    Normalization,
    AlphaNormalization,
    Typecheck,
    TypeInference,
}

#[derive(Copy, Clone)]
pub enum Status {
    Success,
    Failure,
}

fn parse_file_str<'i>(file_path: &str) -> Result<Parsed> {
    Parsed::parse_file(&PathBuf::from(file_path))
}

pub fn run_test_stringy_error(
    base_path: &str,
    feature: Feature,
    status: Status,
) -> std::result::Result<(), String> {
    let base_path: String = base_path.to_string();
    run_test(&base_path, feature, status)
        .map_err(|e| e.to_string())
        .map(|_| ())
}

pub fn run_test(
    base_path: &str,
    feature: Feature,
    status: Status,
) -> Result<()> {
    use self::Feature::*;
    use self::Status::*;
    let base_path = base_path.to_owned();
    match status {
        Success => {
            match feature {
                BinaryDecoding => {
                    let expr_file_path = base_path.clone() + "A.dhallb";
                    let expr_file_path = PathBuf::from(&expr_file_path);
                    let mut expr_data = Vec::new();
                    {
                        File::open(&expr_file_path)?
                            .read_to_end(&mut expr_data)?;
                    }
                    let expr = Parsed::parse_binary(&expr_data)?;
                    let expected_file_path = base_path + "B.dhall";
                    let expected = parse_file_str(&expected_file_path)?;
                    assert_eq_pretty!(expr, expected);

                    return Ok(());
                }
                _ => {}
            }
            let expr_file_path = base_path.clone() + "A.dhall";
            let expr = parse_file_str(&expr_file_path)?;

            match feature {
                Parser => {
                    // This exercices both parsing and binary decoding
                    // Compare parse/decoded
                    let expected_file_path = base_path + "B.dhallb";
                    let expected_file_path = PathBuf::from(&expected_file_path);
                    let mut expected_data = Vec::new();
                    {
                        File::open(&expected_file_path)?
                            .read_to_end(&mut expected_data)?;
                    }
                    let expected = Parsed::parse_binary(&expected_data)?;
                    assert_eq_pretty!(expr, expected);

                    return Ok(());
                }
                Printer => {
                    // Round-trip pretty-printer
                    let expr_string = expr.to_string();
                    let expected = expr;
                    let expr: Parsed = Parsed::parse_str(&expr_string)?;
                    assert_eq!(expr, expected);

                    return Ok(());
                }
                BinaryEncoding => {
                    let expected_file_path = base_path + "B.dhallb";
                    let expected_file_path = PathBuf::from(&expected_file_path);
                    let mut expected_data = Vec::new();
                    {
                        File::open(&expected_file_path)?
                            .read_to_end(&mut expected_data)?;
                    }
                    let expr_data = expr.encode()?;

                    // Compare bit-by-bit
                    if expr_data != expected_data {
                        // use std::io::Write;
                        // File::create(&expected_file_path)?.write_all(&expr_data)?;
                        // Pretty-print difference
                        assert_eq_pretty!(
                            serde_cbor::de::from_slice::<
                                serde_cbor::value::Value,
                            >(&expr_data)
                            .unwrap(),
                            serde_cbor::de::from_slice::<
                                serde_cbor::value::Value,
                            >(&expected_data)
                            .unwrap()
                        );
                        // If difference was not visible in the cbor::Value
                        assert_eq!(expr_data, expected_data);
                    }

                    return Ok(());
                }
                _ => {}
            }

            let expr = expr.resolve()?;

            let expected_file_path = base_path + "B.dhall";
            let expected = parse_file_str(&expected_file_path)?
                .resolve()?
                .typecheck()?
                .normalize();

            match feature {
                Parser | Printer | BinaryEncoding | BinaryDecoding => {
                    unreachable!()
                }
                Import => {
                    let expr = expr.typecheck()?.normalize();
                    assert_eq_display!(expr, expected);
                }
                Typecheck => {
                    expr.typecheck_with(&expected.into_typed())?.get_type()?;
                }
                TypeInference => {
                    let expr = expr.typecheck()?;
                    let ty = expr.get_type()?.normalize();
                    assert_eq_display!(ty, expected);
                }
                Normalization => {
                    let expr = expr.typecheck()?.normalize();
                    assert_eq_display!(expr, expected);
                }
                AlphaNormalization => {
                    let expr = expr.typecheck()?.normalize().to_expr_alpha();
                    assert_eq_display!(expr, expected.to_expr());
                }
            }
        }
        Failure => {
            let file_path = base_path + ".dhall";
            match feature {
                Parser => {
                    let err = parse_file_str(&file_path).unwrap_err();
                    match &err {
                        Error::Parse(_) => {}
                        Error::IO(e)
                            if e.kind() == std::io::ErrorKind::InvalidData => {}
                        e => panic!("Expected parse error, got: {:?}", e),
                    }
                }
                Printer | BinaryEncoding => unreachable!(),
                BinaryDecoding => {
                    let expr_file_path = file_path + "b";
                    let mut expr_data = Vec::new();
                    {
                        File::open(&PathBuf::from(&expr_file_path))?
                            .read_to_end(&mut expr_data)?;
                    }
                    Parsed::parse_binary(&expr_data).unwrap_err();
                }
                Import => {
                    parse_file_str(&file_path)?.resolve().unwrap_err();
                }
                Normalization | AlphaNormalization => unreachable!(),
                Typecheck | TypeInference => {
                    let res =
                        parse_file_str(&file_path)?.skip_resolve()?.typecheck();
                    match res {
                        Err(_) => {}
                        // If e did typecheck, check that it doesn't have a type
                        Ok(e) => {
                            e.get_type().unwrap_err();
                        }
                    }
                }
            }
        }
    }
    Ok(())
}

mod spec {
    // See build.rs
    include!(concat!(env!("OUT_DIR"), "/spec_tests.rs"));
}