|
2 | 2 | require 'minitest/autorun' |
3 | 3 |
|
4 | 4 | class TrafficLightTest < Minitest::Test |
5 | | - def test_direction |
6 | | - light = TrafficLight.new(direction: 'Widdershins', timer: 55, state: TrafficLight::GREEN_STATE) |
7 | | - |
8 | | - assert_equal 'Widdershins', light.direction |
9 | | - end |
10 | | - |
11 | | - def test_state |
12 | | - light = TrafficLight.new(direction: 'Widdershins', timer: 55, state: TrafficLight::GREEN_STATE) |
13 | | - |
14 | | - # reader |
15 | | - assert_equal TrafficLight::GREEN_STATE, light.state |
16 | | - |
17 | | - # writer |
18 | | - light.state = 'test_state' |
19 | | - assert_equal 'test_state', light.state |
20 | | - end |
21 | | - |
22 | | - def test_timer |
23 | | - light = TrafficLight.new(direction: 'Widdershins', timer: 55, state: TrafficLight::GREEN_STATE) |
24 | | - |
25 | | - # reader |
26 | | - assert_equal 55, light.timer |
27 | | - |
28 | | - # writer |
29 | | - light.timer = 101 |
30 | | - assert_equal 101, light.timer |
31 | | - end |
32 | | - |
33 | 5 | def test_direction_to_s |
34 | | - light = TrafficLight.new(direction: 'Widdershins', timer: 55, state: TrafficLight::GREEN_STATE) |
| 6 | + light = TrafficLight.new(direction: 'Widdershins', timer: 55, state: TrafficLightState::GREEN) |
35 | 7 | light.to_s |
36 | 8 |
|
37 | 9 | assert_includes light.to_s, 'Direction: Widdershins' |
38 | 10 | end |
39 | 11 |
|
40 | 12 | def test_time_left_to_s |
41 | | - light = TrafficLight.new(direction: 'Widdershins', timer: 55, state: TrafficLight::GREEN_STATE) |
| 13 | + light = TrafficLight.new(direction: 'Widdershins', timer: 55, state: TrafficLightState::GREEN) |
42 | 14 | light.to_s |
43 | 15 |
|
44 | 16 | assert_includes light.to_s, 'Time left: 55s' |
45 | 17 | end |
46 | 18 |
|
47 | 19 | def test_red_light_to_s |
48 | | - light = TrafficLight.new(direction: 'Widdershins', timer: 55, state: TrafficLight::RED_STATE) |
| 20 | + light = TrafficLight.new(direction: 'Widdershins', timer: 55, state: TrafficLightState::RED) |
49 | 21 |
|
50 | 22 | light.to_s |
51 | 23 |
|
52 | 24 | assert_includes light.to_s, "\e[31mred\e[0m" |
53 | 25 | end |
54 | 26 |
|
55 | 27 | def test_green_light_to_s |
56 | | - light = TrafficLight.new(direction: 'Widdershins', timer: 55, state: TrafficLight::GREEN_STATE) |
| 28 | + light = TrafficLight.new(direction: 'Widdershins', timer: 55, state: TrafficLightState::GREEN) |
57 | 29 |
|
58 | 30 | light.to_s |
59 | 31 |
|
60 | 32 | assert_includes light.to_s, "\e[32mgreen\e[0m" |
61 | 33 | end |
62 | 34 |
|
63 | 35 | def test_amber_light_to_s |
64 | | - light = TrafficLight.new(direction: 'Widdershins', timer: 55, state: TrafficLight::AMBER_STATE) |
| 36 | + light = TrafficLight.new(direction: 'Widdershins', timer: 55, state: TrafficLightState::AMBER) |
65 | 37 |
|
66 | 38 | light.to_s |
67 | 39 |
|
68 | 40 | assert_includes light.to_s, "\e[33mamber\e[0m" |
69 | 41 | end |
70 | 42 |
|
71 | 43 | def test_progress_with_incomplete_timer |
72 | | - light = TrafficLight.new(direction: 'Widdershins', timer: 55, state: TrafficLight::GREEN_STATE) |
| 44 | + light = TrafficLight.new(direction: 'Widdershins', timer: 55, state: TrafficLightState::GREEN) |
| 45 | + |
73 | 46 | light.progress! |
74 | 47 |
|
75 | | - assert_equal 54, light.timer |
76 | | - assert_equal TrafficLight::GREEN_STATE, light.state |
| 48 | + assert_includes light.to_s, 'green' |
77 | 49 | end |
78 | 50 |
|
79 | | - def test_progress_progreses_state |
80 | | - light = TrafficLight.new(direction: 'Widdershins', timer: 1, state: TrafficLight::GREEN_STATE) |
| 51 | + def test_progress_progreses_state_with_complete_timer |
| 52 | + light = TrafficLight.new(direction: 'Widdershins', timer: 1, state: TrafficLightState::GREEN) |
81 | 53 | light.progress! |
82 | 54 |
|
83 | | - assert_equal 3, light.timer |
84 | | - assert_equal TrafficLight::AMBER_STATE, light.state |
| 55 | + assert_includes light.to_s, 'amber' |
85 | 56 | end |
86 | 57 |
|
87 | | - def test_cannot_walk_pedestrian_signal_status |
88 | | - light = TrafficLight.new(direction: 'Widdershins', timer: 55, state: TrafficLight::RED_STATE) |
89 | | - |
90 | | - light.can_walk = true |
| 58 | + def test_inital_pedestrian_signal_status |
| 59 | + light = TrafficLight.new(direction: 'Widdershins', timer: 1, state: TrafficLightState::RED) |
91 | 60 |
|
92 | | - assert_includes light.pedestrian_signal_status, "Widdershins: \e[32mWALK\e[0m" |
| 61 | + # Even on red, pedestrians cannot walk initially |
| 62 | + assert_includes light.pedestrian_signal_status, "Widdershins: \e[31mDON'T WALK\e[0m" |
93 | 63 | end |
94 | 64 |
|
95 | 65 | def test_can_walk_pedestrian_signal_status |
96 | | - light = TrafficLight.new(direction: 'Widdershins', timer: 55, state: TrafficLight::RED_STATE) |
| 66 | + light = TrafficLight.new(direction: 'Widdershins', timer: 1, state: TrafficLightState::AMBER) |
97 | 67 |
|
98 | | - assert_includes light.pedestrian_signal_status, "Widdershins: \e[31mDON'T WALK\e[0m" |
99 | | - end |
100 | | - |
101 | | - def test_current_state_complete |
102 | | - light = TrafficLight.new(direction: 'Widdershins', timer: 0, state: TrafficLight::RED_STATE) |
| 68 | + # Progress to red light for traffic |
| 69 | + light.next_state! |
103 | 70 |
|
104 | | - assert_equal true, light.current_state_complete? |
| 71 | + assert_includes light.pedestrian_signal_status, "Widdershins: \e[32mWALK\e[0m" |
105 | 72 | end |
106 | 73 |
|
107 | | - def test_current_state_incomplete |
108 | | - light = TrafficLight.new(direction: 'Widdershins', timer: 1, state: TrafficLight::RED_STATE) |
| 74 | + def test_cannot_walk_pedestrian_signal_status |
| 75 | + light = TrafficLight.new(direction: 'Widdershins', timer: 55, state: TrafficLightState::GREEN) |
| 76 | + |
| 77 | + # Run through the whole cycle once |
| 78 | + 3.times do |
| 79 | + light.next_state! |
| 80 | + end |
109 | 81 |
|
110 | | - assert_equal false, light.current_state_complete? |
| 82 | + assert_includes light.pedestrian_signal_status, "Widdershins: \e[31mDON'T WALK\e[0m" |
111 | 83 | end |
112 | 84 |
|
113 | 85 | def test_red |
114 | | - light = TrafficLight.new(direction: 'Widdershins', timer: 1, state: TrafficLight::GREEN_STATE) |
| 86 | + light = TrafficLight.new(direction: 'Widdershins', timer: 1, state: TrafficLightState::GREEN) |
115 | 87 |
|
116 | 88 | light.red! |
117 | 89 |
|
118 | | - assert_equal TrafficLight::RED_STATE, light.state |
119 | | - assert_equal 10, light.timer |
120 | | - assert_equal true, light.can_walk |
121 | | - end |
122 | | - |
123 | | - def test_green |
124 | | - light = TrafficLight.new(direction: 'Widdershins', timer: 1, state: TrafficLight::RED_STATE) |
125 | | - |
126 | | - light.green! |
127 | | - |
128 | | - assert_equal TrafficLight::GREEN_STATE, light.state |
129 | | - assert_equal 8, light.timer |
130 | | - assert_equal false, light.can_walk |
131 | | - end |
132 | | - |
133 | | - def test_amber |
134 | | - light = TrafficLight.new(direction: 'Widdershins', timer: 1, state: TrafficLight::RED_STATE) |
135 | | - |
136 | | - light.amber! |
137 | | - |
138 | | - assert_equal TrafficLight::AMBER_STATE, light.state |
139 | | - assert_equal 3, light.timer |
140 | | - assert_equal false, light.can_walk |
| 90 | + assert_equal false, light.allows_traffic? |
| 91 | + assert_equal true, light.allows_pedestrians? |
141 | 92 | end |
142 | 93 |
|
143 | 94 | def test_next_state |
144 | | - light = TrafficLight.new(direction: 'Widdershins', timer: 1, state: TrafficLight::RED_STATE) |
| 95 | + light = TrafficLight.new(direction: 'Widdershins', timer: 1, state: TrafficLightState::RED) |
145 | 96 |
|
146 | 97 | light.next_state! |
147 | 98 |
|
148 | | - assert_equal TrafficLight::GREEN_STATE, light.state |
| 99 | + assert_includes light.to_s, 'green' |
149 | 100 |
|
150 | 101 | light.next_state! |
151 | 102 |
|
152 | | - assert_equal TrafficLight::AMBER_STATE, light.state |
| 103 | + assert_includes light.to_s, 'amber' |
153 | 104 |
|
154 | 105 | light.next_state! |
155 | 106 |
|
156 | | - assert_equal TrafficLight::RED_STATE, light.state |
| 107 | + assert_includes light.to_s, 'red' |
157 | 108 | end |
158 | 109 |
|
159 | 110 | def test_allows_traffic_red |
160 | | - light = TrafficLight.new(direction: 'Widdershins', timer: 1, state: TrafficLight::RED_STATE) |
| 111 | + light = TrafficLight.new(direction: 'Widdershins', timer: 1, state: TrafficLightState::RED) |
161 | 112 |
|
162 | 113 | assert_equal false, light.allows_traffic? |
163 | 114 | end |
164 | 115 |
|
165 | 116 | def test_allows_traffic_green |
166 | | - light = TrafficLight.new(direction: 'Widdershins', timer: 1, state: TrafficLight::GREEN_STATE) |
| 117 | + light = TrafficLight.new(direction: 'Widdershins', timer: 1, state: TrafficLightState::GREEN) |
167 | 118 |
|
168 | 119 | assert_equal true, light.allows_traffic? |
169 | 120 | end |
170 | 121 |
|
171 | 122 | def test_allows_traffic_amber |
172 | | - light = TrafficLight.new(direction: 'Widdershins', timer: 1, state: TrafficLight::AMBER_STATE) |
| 123 | + light = TrafficLight.new(direction: 'Widdershins', timer: 1, state: TrafficLightState::AMBER) |
173 | 124 |
|
174 | 125 | assert_equal true, light.allows_traffic? |
175 | 126 | end |
|
0 commit comments