summaryrefslogtreecommitdiff
path: root/dhall/tests/traits.rs
blob: 1e9b3c2a9c67c9af7713e6ff951a04416f6ae0ee (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
#![feature(proc_macro_hygiene)]
use dhall::SimpleStaticType;
use dhall_core::{SubExpr, X};
use dhall_generator::dhall_expr;

#[test]
fn test_static_type() {
    fn mktype(x: SubExpr<X, X>) -> dhall::expr::SimpleType {
        x.into()
    }

    assert_eq!(bool::get_simple_static_type(), mktype(dhall_expr!(Bool)));
    assert_eq!(String::get_simple_static_type(), mktype(dhall_expr!(Text)));
    assert_eq!(
        <Option<bool>>::get_simple_static_type(),
        mktype(dhall_expr!(Optional Bool))
    );
    assert_eq!(
        <(bool, Option<String>)>::get_simple_static_type(),
        mktype(dhall_expr!({ _1: Bool, _2: Optional Text }))
    );

    #[derive(dhall::SimpleStaticType)]
    #[allow(dead_code)]
    struct A {
        field1: bool,
        field2: Option<bool>,
    }
    assert_eq!(
        <A as dhall::SimpleStaticType>::get_simple_static_type(),
        mktype(dhall_expr!({ field1: Bool, field2: Optional Bool }))
    );

    #[derive(SimpleStaticType)]
    #[allow(dead_code)]
    struct B<'a, T: 'a> {
        field1: &'a T,
        field2: Option<T>,
    }
    assert_eq!(
        <B<'static, bool>>::get_simple_static_type(),
        A::get_simple_static_type()
    );

    #[derive(SimpleStaticType)]
    #[allow(dead_code)]
    struct C<T>(T, Option<String>);
    assert_eq!(
        <C<bool>>::get_simple_static_type(),
        <(bool, Option<String>)>::get_simple_static_type()
    );

    #[derive(SimpleStaticType)]
    #[allow(dead_code)]
    struct D();
    assert_eq!(
        <C<D>>::get_simple_static_type(),
        mktype(dhall_expr!({ _1: {}, _2: Optional Text }))
    );

    #[derive(SimpleStaticType)]
    #[allow(dead_code)]
    enum E<T> {
        A(T),
        B(String),
    };
    assert_eq!(
        <E<bool>>::get_simple_static_type(),
        mktype(dhall_expr!(< A: Bool | B: Text >))
    );
}