-
Notifications
You must be signed in to change notification settings - Fork 1
/
create_vec.rs
82 lines (64 loc) · 1.41 KB
/
create_vec.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
77
78
79
80
81
82
#[macro_use]
extern crate factori_imp;
pub struct Vehicle {
number_wheels: u8,
electric: bool,
}
factori!(Vehicle, {
default {
number_wheels: u8 = 4,
electric: bool = false,
}
transient {
double_wheels: bool = false
}
mixin bike {
number_wheels = 2,
}
mixin trike {
number_wheels = 3,
}
mixin electric {
electric = true,
}
builder {
let number_wheels = if double_wheels {
number_wheels * 2
} else { number_wheels };
Vehicle { number_wheels, electric }
}
});
#[test]
fn can_create_many() {
let vehicles = create_vec!(Vehicle, 5);
assert_eq!(vehicles.len(), 5);
}
#[test]
fn works_with_mixins() {
let vehicles = create_vec!(Vehicle, 3, :bike);
vehicles.iter().for_each(|vehicle| {
assert_eq!(vehicle.number_wheels, 2);
});
}
#[test]
fn works_with_overriding_fields() {
let vehicles = create_vec!(Vehicle, 3, electric: true);
vehicles.iter().for_each(|vehicle| {
assert!(vehicle.electric);
});
}
#[test]
fn works_with_transient_fields() {
let vehicles = create_vec!(Vehicle, 3, double_wheels: true);
vehicles.iter().for_each(|vehicle| {
assert_eq!(vehicle.number_wheels, 8);
});
}
#[test]
fn works_with_all_of_them() {
let vehicles = create_vec!(Vehicle, 3, :bike, electric: true, double_wheels: true);
vehicles.iter().for_each(|vehicle| {
assert_eq!(vehicle.number_wheels, 4);
assert!(vehicle.electric);
});
}