summaryrefslogtreecommitdiffstats
path: root/vendor/derive_builder/tests/custom_default.rs
blob: 8fd6d215f12b552e504171db9258cc4907faa84f (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
#[macro_use]
extern crate pretty_assertions;
#[macro_use]
extern crate derive_builder;

mod field_level {
    use derive_builder::UninitializedFieldError;
    #[derive(Debug, PartialEq, Default, Builder, Clone)]
    struct Lorem {
        required: String,
        #[builder(default)]
        explicit_default: String,
        #[builder(default = "\"foo\".to_string()")]
        escaped_default: String,
        #[builder(default = r#"format!("Hello {}!", "World")"#)]
        raw_default: String,
        #[builder(default = r#"format!("{}-{}-{}-{}",
                             Clone::clone(self.required
                                .as_ref()
                                .ok_or_else(|| UninitializedFieldError::new("required"))?),
                             match self.explicit_default { Some(ref x) => x, None => "EMPTY" },
                             self.escaped_default.as_ref().map(|x| x.as_ref()).unwrap_or("EMPTY"),
                             if let Some(ref x) = self.raw_default { x } else { "EMPTY" })"#)]
        computed_default: String,
    }

    #[test]
    fn error_if_uninitialized() {
        let error = LoremBuilder::default().build().unwrap_err();
        assert_eq!(&error.to_string(), "`required` must be initialized");
    }

    #[test]
    fn custom_default() {
        let x = LoremBuilder::default()
            .required("ipsum".to_string())
            .build()
            .unwrap();

        assert_eq!(
            x,
            Lorem {
                required: "ipsum".to_string(),
                explicit_default: "".to_string(),
                escaped_default: "foo".to_string(),
                raw_default: "Hello World!".to_string(),
                computed_default: "ipsum-EMPTY-EMPTY-EMPTY".to_string(),
            }
        );
    }

    #[test]
    fn builder_test() {
        let x = LoremBuilder::default()
            .required("ipsum".to_string())
            .explicit_default("lorem".to_string())
            .escaped_default("dolor".to_string())
            .raw_default("sit".to_string())
            .build()
            .unwrap();

        assert_eq!(
            x,
            Lorem {
                required: "ipsum".to_string(),
                explicit_default: "lorem".to_string(),
                escaped_default: "dolor".to_string(),
                raw_default: "sit".to_string(),
                computed_default: "ipsum-lorem-dolor-sit".to_string(),
            }
        );
    }
}

mod struct_level {
    #[derive(Debug, Clone, PartialEq, Eq, Builder)]
    #[builder(default = "explicit_default()")]
    struct Lorem {
        #[builder(default = "true")]
        overwritten: bool,
        not_type_default: Option<&'static str>,
    }

    fn explicit_default() -> Lorem {
        Lorem {
            overwritten: false,
            not_type_default: Some("defined on struct-level"),
        }
    }

    #[derive(Debug, Clone, PartialEq, Eq, Builder)]
    #[builder(default)]
    struct Ipsum {
        not_type_default: Option<u16>,
        also_custom: bool,
        is_type_default: String,
    }

    impl Default for Ipsum {
        fn default() -> Self {
            Ipsum {
                not_type_default: Some(20),
                also_custom: true,
                is_type_default: Default::default(),
            }
        }
    }

    #[test]
    fn explicit_defaults_are_equal() {
        let lorem = LoremBuilder::default().build().unwrap();

        assert_eq!(
            lorem,
            Lorem {
                overwritten: true,
                ..explicit_default()
            }
        );
    }

    #[test]
    fn implicit_defaults_are_equal() {
        let ipsum = IpsumBuilder::default().build().unwrap();

        assert_eq!(ipsum, Ipsum::default());
    }

    #[test]
    fn overrides_work() {
        let ipsum = IpsumBuilder::default()
            .not_type_default(None)
            .build()
            .expect("Struct-level default makes all fields optional");

        assert_eq!(ipsum.not_type_default, None);
    }
}