summaryrefslogtreecommitdiff
path: root/serde_dhall/tests/traits.rs
blob: 55be63b3c653b976fc1e2d994cb10bb4064d78c3 (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
#![feature(proc_macro_hygiene)]
use serde_dhall::{from_str, StaticType, Value};

#[test]
fn test_static_type() {
    fn parse(s: &str) -> Value {
        from_str(s).unwrap()
    }

    assert_eq!(bool::static_type(), parse("Bool"));
    assert_eq!(String::static_type(), parse("Text"));
    assert_eq!(<Option<bool>>::static_type(), parse("Optional Bool"));
    assert_eq!(
        <(bool, Vec<String>)>::static_type(),
        parse("{ _1: Bool, _2: List Text }")
    );

    #[derive(serde_dhall::StaticType)]
    #[allow(dead_code)]
    struct A {
        field1: bool,
        field2: Option<bool>,
    }
    assert_eq!(
        <A as serde_dhall::StaticType>::static_type(),
        parse("{ field1: Bool, field2: Optional Bool }")
    );

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

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

    #[derive(StaticType)]
    #[allow(dead_code)]
    struct D();
    assert_eq!(
        <C<D>>::static_type(),
        parse("{ _1: {}, _2: Optional Text }")
    );

    #[derive(StaticType)]
    #[allow(dead_code)]
    enum E<T> {
        A(T),
        B(String),
    };
    assert_eq!(<E<bool>>::static_type(), parse("< A: Bool | B: Text >"));

    #[derive(StaticType)]
    #[allow(dead_code)]
    enum F {
        A,
        B(bool),
    };
    assert_eq!(F::static_type(), parse("< A | B: Bool >"));
}