|
1 | 1 | var proxyquire = require('proxyquire')
|
2 | 2 | var five = require('../../stubs/five')
|
3 | 3 | var expect = require('chai').expect
|
| 4 | +var async = require('async') |
4 | 5 |
|
5 | 6 | var exercise = require('workshopper-exercise')()
|
6 | 7 | var filecheck = require('workshopper-exercise/filecheck')
|
@@ -134,129 +135,131 @@ function testAlarmResets (analogReadListener, io, cb) {
|
134 | 135 |
|
135 | 136 | analogReadListener(random(tempToVoltage(50.1), tempToVoltage(100)))
|
136 | 137 |
|
137 |
| - // Within 2 seconds, the piezo should have sounded and the LED turned on |
138 |
| - setTimeout(function () { |
139 |
| - try { |
140 |
| - expect(io.digitalWrite.calledWith(pins.piezo, io.HIGH), 'Piezo was not turned on when fire started').to.be.true |
141 |
| - expect(io.digitalWrite.calledWith(pins.led, io.HIGH), 'LED was not turned on when fire started').to.be.true |
142 |
| - } catch (er) { |
143 |
| - return cb(er) |
144 |
| - } |
| 138 | + var lastLedLowCall, lastLedHighCall, digitalReadListener |
145 | 139 |
|
146 |
| - // Get the digital read listener for the button |
147 |
| - var digitalReadListener = null |
| 140 | + var tests = [ |
| 141 | + function(callback) { |
| 142 | + try { |
| 143 | + expect(io.digitalWrite.calledWith(pins.piezo, io.HIGH), 'Piezo was not turned on when fire started').to.be.true |
| 144 | + expect(io.digitalWrite.calledWith(pins.led, io.HIGH), 'LED was not turned on when fire started').to.be.true |
| 145 | + } catch (er) { |
| 146 | + return callback(er) |
| 147 | + } |
148 | 148 |
|
149 |
| - for (var i = 0; i < io.digitalRead.callCount; i++) { |
150 |
| - var call = io.digitalRead.getCall(i) |
151 |
| - if (call.args[0] === pins.btn) { |
152 |
| - digitalReadListener = call.args[1] |
153 |
| - break |
| 149 | + // Get the digital read listener for the button |
| 150 | + for (var i = 0; i < io.digitalRead.callCount; i++) { |
| 151 | + var call = io.digitalRead.getCall(i) |
| 152 | + if (call.args[0] === pins.btn) { |
| 153 | + digitalReadListener = call.args[1] |
| 154 | + break |
| 155 | + } |
154 | 156 | }
|
155 |
| - } |
156 | 157 |
|
157 |
| - // Push the button |
158 |
| - digitalReadListener(1) |
| 158 | + // Push the button |
| 159 | + digitalReadListener(1) |
159 | 160 |
|
160 |
| - // Buttons have a 7ms debounce |
161 |
| - setTimeout(function () { |
| 161 | + // Buttons have a 7ms debounce |
| 162 | + setTimeout(callback, 100) |
| 163 | + }, |
| 164 | + function(callback) { |
| 165 | + // release the button |
162 | 166 | digitalReadListener(0)
|
163 | 167 |
|
164 |
| - // Within 2 seconds the last call to digitalWrite should have been with io.LOW |
165 |
| - setTimeout(function () { |
166 |
| - try { |
167 |
| - expect( |
168 |
| - io.digitalWrite.calledWith(pins.piezo, io.LOW), |
169 |
| - 'Piezo was not turned off when reset button pressed' |
170 |
| - ).to.be.true |
| 168 | + setTimeout(callback, 100) |
| 169 | + }, |
| 170 | + function(callback) { |
| 171 | + try { |
| 172 | + expect( |
| 173 | + io.digitalWrite.calledWith(pins.piezo, io.LOW), |
| 174 | + 'Piezo was not turned off when reset button pressed' |
| 175 | + ).to.be.true |
| 176 | + |
| 177 | + expect( |
| 178 | + io.digitalWrite.calledWith(pins.led, io.LOW), |
| 179 | + 'LED was not turned off when reset button pressed' |
| 180 | + ).to.be.true |
| 181 | + } catch (er) { |
| 182 | + return callback(er) |
| 183 | + } |
171 | 184 |
|
172 |
| - expect( |
173 |
| - io.digitalWrite.calledWith(pins.led, io.LOW), |
174 |
| - 'LED was not turned off when reset button pressed' |
175 |
| - ).to.be.true |
176 |
| - } catch (er) { |
177 |
| - return cb(er) |
178 |
| - } |
| 185 | + //var lastPiezoLowCall = lastCallWith(io.digitalWrite, pins.piezo, io.LOW) |
| 186 | + lastLedLowCall = lastCallWith(io.digitalWrite, pins.led, io.LOW) |
| 187 | + |
| 188 | + // After 2 more seconds the last piezo/led low call should still have been before the last piezo/led high call |
| 189 | + setTimeout(callback, 2000) |
| 190 | + }, |
| 191 | + function(callback) { |
| 192 | + //var lastPiezoHighCall = lastCallWith(io.digitalWrite, pins.piezo, io.HIGH) |
| 193 | + lastLedHighCall = lastCallWith(io.digitalWrite, pins.led, io.HIGH) |
179 | 194 |
|
180 |
| - //var lastPiezoLowCall = lastCallWith(io.digitalWrite, pins.piezo, io.LOW) |
181 |
| - var lastLedLowCall = lastCallWith(io.digitalWrite, pins.led, io.LOW) |
182 |
| - |
183 |
| - // After 2 more seconds the last piezo/led low call should still have been before the last piezo/led high call |
184 |
| - setTimeout(function () { |
185 |
| - //var lastPiezoHighCall = lastCallWith(io.digitalWrite, pins.piezo, io.HIGH) |
186 |
| - var lastLedHighCall = lastCallWith(io.digitalWrite, pins.led, io.HIGH) |
187 |
| - |
188 |
| - try { |
189 |
| - // TODO: We can't currently test this as there is no way to stop a piezo from playing a tune |
190 |
| - /*expect( |
191 |
| - lastPiezoLowCall.calledAfter(lastPiezoHighCall), |
192 |
| - 'Piezo was not turned off after it was turned on and reset button pressed' |
193 |
| - ).to.be.true*/ |
194 |
| - |
195 |
| - expect( |
196 |
| - lastLedLowCall.calledAfter(lastLedHighCall), |
197 |
| - 'LED was not turned off after it was turned on and reset button pressed' |
198 |
| - ).to.be.true |
199 |
| - } catch (er) { |
200 |
| - return cb(er) |
201 |
| - } |
202 |
| - |
203 |
| - io.digitalWrite.reset() |
204 |
| - |
205 |
| - // When a new fire temperature is received, the alarm shouldn't turn on |
206 |
| - analogReadListener(random(tempToVoltage(50.1), tempToVoltage(100))) |
207 |
| - |
208 |
| - setTimeout(function () { |
209 |
| - try { |
210 |
| - expect( |
211 |
| - io.digitalWrite.calledWith(pins.piezo, io.HIGH), |
212 |
| - 'Piezo turned back on after reset button pressed before temperature dropped below 50' |
213 |
| - ).to.be.false |
214 |
| - |
215 |
| - expect(io.digitalWrite.calledWith(pins.led, io.HIGH), |
216 |
| - 'LED turned back on after reset button pressed before temperature dropped below 50' |
217 |
| - ).to.be.false |
218 |
| - } catch (er) { |
219 |
| - return cb(er) |
220 |
| - } |
221 |
| - |
222 |
| - // When the temp drops below 50 and then above 50, the alarm should turn on |
223 |
| - analogReadListener(random(tempToVoltage(0), tempToVoltage(50))) |
224 |
| - |
225 |
| - setTimeout(function () { |
226 |
| - analogReadListener(random(tempToVoltage(50.1), tempToVoltage(100))) |
227 |
| - |
228 |
| - setTimeout(function () { |
229 |
| - try { |
230 |
| - expect( |
231 |
| - io.digitalWrite.calledWith(pins.piezo, io.HIGH), |
232 |
| - 'Piezo was not turned on when fire started after reset button was pressed' |
233 |
| - ).to.be.true |
234 |
| - |
235 |
| - expect( |
236 |
| - io.digitalWrite.calledWith(pins.led, io.HIGH), |
237 |
| - 'LED was not turned on when fire started after reset button was pressed' |
238 |
| - ).to.be.true |
239 |
| - |
240 |
| - // Reset |
241 |
| - analogReadListener(random(tempToVoltage(0), tempToVoltage(50))) |
242 |
| - setTimeout(cb, 2000) |
243 |
| - |
244 |
| - } catch (er) { |
245 |
| - return cb(er) |
246 |
| - } |
247 |
| - }, 2000) |
248 |
| - |
249 |
| - }, 2000) |
250 |
| - |
251 |
| - }, 2000) |
252 |
| - |
253 |
| - }, 2000) |
| 195 | + try { |
| 196 | + // TODO: We can't currently test this as there is no way to stop a piezo from playing a tune |
| 197 | + /*expect( |
| 198 | + lastPiezoLowCall.calledAfter(lastPiezoHighCall), |
| 199 | + 'Piezo was not turned off after it was turned on and reset button pressed' |
| 200 | + ).to.be.true*/ |
| 201 | + |
| 202 | + expect( |
| 203 | + lastLedLowCall.calledAfter(lastLedHighCall), |
| 204 | + 'LED was not turned off after it was turned on and reset button pressed' |
| 205 | + ).to.be.true |
| 206 | + } catch (er) { |
| 207 | + return callback(er) |
| 208 | + } |
254 | 209 |
|
255 |
| - }, 2000) |
| 210 | + io.digitalWrite.reset() |
256 | 211 |
|
257 |
| - }, 10) |
| 212 | + // When a new fire temperature is received, the alarm shouldn't turn on |
| 213 | + analogReadListener(random(tempToVoltage(50.1), tempToVoltage(100))) |
258 | 214 |
|
259 |
| - }, 2000) |
| 215 | + setTimeout(callback, 2000) |
| 216 | + }, |
| 217 | + function(callback) { |
| 218 | + try { |
| 219 | + expect( |
| 220 | + io.digitalWrite.calledWith(pins.piezo, io.HIGH), |
| 221 | + 'Piezo turned back on after reset button pressed before temperature dropped below 50' |
| 222 | + ).to.be.false |
| 223 | + |
| 224 | + expect(io.digitalWrite.calledWith(pins.led, io.HIGH), |
| 225 | + 'LED turned back on after reset button pressed before temperature dropped below 50' |
| 226 | + ).to.be.false |
| 227 | + } catch (er) { |
| 228 | + return callback(er) |
| 229 | + } |
| 230 | + |
| 231 | + // When the temp drops below 50 and then above 50, the alarm should turn on |
| 232 | + analogReadListener(random(tempToVoltage(0), tempToVoltage(50))) |
| 233 | + |
| 234 | + setTimeout(callback, 2000) |
| 235 | + }, |
| 236 | + function(callback) { |
| 237 | + analogReadListener(random(tempToVoltage(50.1), tempToVoltage(100))) |
| 238 | + |
| 239 | + setTimeout(callback, 2000) |
| 240 | + }, |
| 241 | + function(callback) { |
| 242 | + try { |
| 243 | + expect( |
| 244 | + io.digitalWrite.calledWith(pins.piezo, io.HIGH), |
| 245 | + 'Piezo was not turned on when fire started after reset button was pressed' |
| 246 | + ).to.be.true |
| 247 | + |
| 248 | + expect( |
| 249 | + io.digitalWrite.calledWith(pins.led, io.HIGH), |
| 250 | + 'LED was not turned on when fire started after reset button was pressed' |
| 251 | + ).to.be.true |
| 252 | + |
| 253 | + // Reset |
| 254 | + analogReadListener(random(tempToVoltage(0), tempToVoltage(50))) |
| 255 | + } catch (er) { |
| 256 | + return callback(er) |
| 257 | + } |
| 258 | + |
| 259 | + setTimeout(callback, 2000) |
| 260 | + }] |
| 261 | + |
| 262 | + setTimeout(async.series.bind(null, tests, cb), 2000) |
260 | 263 | }
|
261 | 264 |
|
262 | 265 | function lastCallWith (spy/*, arg0, arg1...*/) {
|
|
0 commit comments