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

#[derive(Debug, PartialEq, Eq, Builder)]
#[builder(custom_constructor, build_fn(private, name = "fallible_build"))]
struct Request {
    url: &'static str,
    username: &'static str,
    #[builder(default, setter(into))]
    password: Option<&'static str>,
}

impl RequestBuilder {
    pub fn new(url: &'static str, username: &'static str) -> Self {
        Self {
            url: Some(url),
            username: Some(username),
            ..Self::create_empty()
        }
    }

    pub fn build(&self) -> Request {
        self.fallible_build()
            .expect("All required fields set upfront")
    }
}

#[test]
fn new_then_build_succeeds() {
    assert_eq!(
        RequestBuilder::new("...", "!!!").build(),
        Request {
            url: "...",
            username: "!!!",
            password: None
        }
    );
}

#[test]
fn new_then_set_succeeds() {
    assert_eq!(
        RequestBuilder::new("...", "!!!").password("test").build(),
        Request {
            url: "...",
            username: "!!!",
            password: Some("test")
        }
    );
}