-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.rs
63 lines (53 loc) · 1.83 KB
/
tests.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
#[cfg(test)]
mod tests {
use crate::machine::{State, StateMachine, Feature};
#[derive(Clone, Copy, PartialEq)]
enum TrafficLight {
Red,
RedYellow,
Green,
Yellow
}
impl State for TrafficLight {
fn transition_allowed(self, new_state: &TrafficLight) -> bool {
match (self, new_state) {
(TrafficLight::Red, TrafficLight::RedYellow) => true,
(TrafficLight::RedYellow, TrafficLight::Green) => true,
(TrafficLight::Green, TrafficLight::Yellow) => true,
(TrafficLight::Yellow, TrafficLight::Red) => true,
_ => false
}
}
}
#[derive(Clone, Copy, PartialEq)]
enum TrafficFeature {
Drive
}
impl Feature<TrafficLight> for TrafficFeature {
fn allowed(self, state: &TrafficLight) -> bool {
match state {
TrafficLight::Green => true,
_ => false
}
}
}
#[test]
fn traffic_light_transitions() {
let mut state_machine = StateMachine::new(TrafficLight::Red);
assert!(state_machine.set(&TrafficLight::RedYellow).is_ok());
assert!(state_machine.set(&TrafficLight::Green).is_ok());
assert!(state_machine.set(&TrafficLight::Yellow).is_ok());
assert!(state_machine.set(&TrafficLight::RedYellow).is_err());
}
#[test]
fn traffic_light_features() {
let mut state_machine = StateMachine::new(TrafficLight::Red);
assert!(!state_machine.feature_allowed(&TrafficFeature::Drive));
assert!(state_machine.set(&TrafficLight::RedYellow).is_ok());
assert!(!state_machine.feature_allowed(&TrafficFeature::Drive));
assert!(state_machine.set(&TrafficLight::Green).is_ok());
assert!(state_machine.feature_allowed(&TrafficFeature::Drive));
assert!(state_machine.set(&TrafficLight::Yellow).is_ok());
assert!(!state_machine.feature_allowed(&TrafficFeature::Drive));
}
}