-
Notifications
You must be signed in to change notification settings - Fork 1
/
mutable_struct.rs
48 lines (42 loc) · 1.15 KB
/
mutable_struct.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
#[macro_use]
extern crate table_test;
#[derive(Debug, PartialEq)]
struct Person {
name: String,
surname: String,
}
impl Person {
fn new(name: &str, surname: &str) -> Person {
Person {
name: name.to_string(),
surname: surname.to_string(),
}
}
fn change_name(&mut self, name: &str) {
self.surname = name.to_string(); // Oups should be self.name
}
}
fn main() {
let table = vec![
(
(Person::new("John", "Smith"), "Johny"),
Person::new("Johny", "Smith"),
),
(
(Person::new("Ian", "Taylor"), "Yan"),
Person::new("Yan", "Taylor"),
),
(
(Person::new("Justin", "Williams"), "Justine"),
Person::new("Justine", "Williams"),
),
];
for (test_case, (mut person, new_name), expected) in table_test!(table) {
person.change_name(new_name);
test_case
.given("a person")
.when(&format!("{}{}", "change name to ", new_name))
.then(&format!("{}{}", "new name is ", expected.name))
.assert_eq(expected, person);
}
}