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

pub mod foo {
    #[derive(Debug, PartialEq, Default, Builder, Clone)]
    #[builder(private, setter(into))]
    pub struct Lorem {
        pub private: String,
        #[builder(public)]
        pub public: String,
    }

    #[derive(Debug, PartialEq, Default, Builder, Clone)]
    #[builder(public, setter(into))]
    pub struct Ipsum {
        #[builder(private)]
        pub private: String,
        pub public: String,
    }

    #[test]
    fn setters_same_module() {
        let x = LoremBuilder::default()
            .public("Hello")
            .private("world!")
            .build()
            .unwrap();

        assert_eq!(
            x,
            Lorem {
                public: "Hello".into(),
                private: "world!".into(),
            }
        );

        let y = IpsumBuilder::default()
            .public("Hello")
            .private("world!")
            .build()
            .unwrap();

        assert_eq!(
            y,
            Ipsum {
                public: "Hello".into(),
                private: "world!".into(),
            }
        );
    }
}

#[test]
fn public_setters_foreign_module() {
    let error = foo::IpsumBuilder::default()
        .public("Hello")
        .build()
        .unwrap_err();

    assert_eq!(&error.to_string(), "`private` must be initialized");
}