summaryrefslogtreecommitdiff
path: root/dhall/src/tests.rs
blob: 14c2a5fa88cf28da62a2798fec454d95ea9f2394 (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
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::*;
            // Many tests stack overflow in debug mode
            std::thread::Builder::new()
                .stack_size(4 * 1024 * 1024)
                .spawn(|| {
                    run_test($path, Feature::$type, Status::$status)
                        .map_err(|e| println!("{}", e))
                        .unwrap();
                })
                .unwrap()
                .join()
                .unwrap();
        }
    };
}

use crate::error::{Error, Result};
use crate::expr::Parsed;
use crate::DynamicType;
use std::path::PathBuf;

#[derive(Copy, Clone)]
pub enum Feature {
    Parser,
    Normalization,
    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))
}

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

pub fn run_test(
    base_path: &str,
    feature: Feature,
    status: Status,
) -> Result<()> {
    use self::Feature::*;
    use self::Status::*;
    let feature_prefix = match feature {
        Parser => "parser/",
        Normalization => "normalization/",
        Typecheck => "typecheck/",
        TypeInference => "type-inference/",
    };
    let status_prefix = match status {
        Success => "success/",
        Failure => "failure/",
    };
    let base_path = "../dhall-lang/tests/".to_owned()
        + feature_prefix
        + status_prefix
        + base_path;
    match status {
        Success => {
            let expr_file_path = base_path.clone() + "A.dhall";
            let expr = parse_file_str(&expr_file_path)?;

            if let Parser = feature {
                let expected_file_path = base_path + "B.dhallb";
                let expected = parse_binary_file_str(&expected_file_path)?;

                assert_eq_pretty!(expr, expected);

                // Round-trip pretty-printer
                let expr_string = expr.to_string();
                let expr: Parsed = crate::from_str(&expr_string, None)?;
                assert_eq!(expr, expected);

                return Ok(());
            }

            let expr = expr.resolve()?;

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

            match feature {
                Parser => unreachable!(),
                Typecheck => {
                    expr.typecheck_with(&expected.into_type())?;
                }
                TypeInference => {
                    let expr = expr.typecheck()?;
                    let ty = expr.get_type()?;
                    assert_eq_display!(ty.as_normalized()?, &expected);
                }
                Normalization => {
                    let expr = expr.skip_typecheck().normalize();
                    assert_eq_display!(expr, expected);
                }
            }
        }
        Failure => {
            let file_path = base_path + ".dhall";
            match feature {
                Parser => {
                    let err = parse_file_str(&file_path).unwrap_err();
                    match err {
                        Error::Parse(_) => {}
                        e => panic!("Expected parse error, got: {:?}", e),
                    }
                }
                Normalization => unreachable!(),
                Typecheck | TypeInference => {
                    parse_file_str(&file_path)?
                        .skip_resolve()?
                        .typecheck()
                        .unwrap_err();
                }
            }
        }
    }
    Ok(())
}