-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.rs
76 lines (67 loc) · 2.09 KB
/
main.rs
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
use builder_macro::Builder;
fn main() {}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn should_generate_builder_for_struct_with_no_properties() {
#[derive(Builder)]
struct ExampleStructNoFields {}
let _: ExampleStructNoFields = ExampleStructNoFields::builder().build();
}
#[test]
fn should_generate_builder_for_struct_with_one_property() {
#[derive(Builder)]
struct Gleipnir {
roots_of: String,
}
let example = Gleipnir::builder()
.roots_of("mountains".to_string())
.build();
assert_eq!(example.roots_of, "mountains".to_string());
}
#[test]
fn should_generate_builder_for_struct_with_two_properties() {
#[derive(Builder)]
struct Gleipnir {
roots_of: String,
breath_of_a_fish: u8,
}
let gleipnir = Gleipnir::builder()
.roots_of("mountains".to_string())
.breath_of_a_fish(1)
.build();
assert_eq!(gleipnir.roots_of, "mountains".to_string());
assert_eq!(gleipnir.breath_of_a_fish, 1);
}
#[test]
fn should_generate_builder_for_struct_with_multiple_properties() {
#[derive(Builder)]
struct Gleipnir {
roots_of: String,
breath_of_a_fish: u8,
other_necessities: Vec<String>,
}
let gleipnir = Gleipnir::builder()
.roots_of("mountains".to_string())
.breath_of_a_fish(1)
.other_necessities(vec![
"sound of cat's footsteps".to_string(),
"beard of a woman".to_string(),
"spittle of a bird".to_string(),
])
.build();
assert_eq!(gleipnir.roots_of, "mountains".to_string());
assert_eq!(gleipnir.breath_of_a_fish, 1);
assert_eq!(gleipnir.other_necessities.len(), 3)
}
#[test]
#[should_panic]
fn should_panic_when_field_is_missing() {
#[derive(Builder)]
struct Gleipnir {
_roots_of: String,
}
Gleipnir::builder().build();
}
}