Skip to content

Commit 1a3adbd

Browse files
committed
pull all language out into i18n file
1 parent 5798e4b commit 1a3adbd

File tree

17 files changed

+748
-524
lines changed

17 files changed

+748
-524
lines changed

exercises/blink_blink/exercise.js

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
var proxyquire = require('proxyquire')
22
var five = require('../../stubs/five')
3-
var expect = require('chai').expect
43
var exercise = require('workshopper-exercise')()
54
var filecheck = require('workshopper-exercise/filecheck')
65
var path = require('path')
7-
var notifier = require('../../lib/notifier')
8-
var broadcaster = require('../../lib/broadcaster')
6+
var verifyProcessor = require('../../lib/verify-processor')
97

108
// checks that the submission file actually exists
119
exercise = filecheck(exercise)
@@ -16,7 +14,7 @@ exercise.addProcessor(function (mode, callback) {
1614
proxyquire(path.join(process.cwd(), exercise.args[0]), {'johnny-five': five.spyOn('Led')})
1715

1816
setTimeout(function() {
19-
console.log('Please wait while your solution is tested...')
17+
console.log(exercise.__('please_wait'))
2018
}, 1000)
2119

2220
// need a better way of detecting when we are done..
@@ -25,40 +23,36 @@ exercise.addProcessor(function (mode, callback) {
2523
}, 4000)
2624
})
2725

28-
// add a processor only for 'verify' calls
29-
exercise.addVerifyProcessor(function (callback) {
30-
try {
31-
var io = five.stubs.firmata.singleton
26+
var pins = {
27+
led: 13
28+
}
3229

33-
expect(io, 'no board instance created').to.exist
30+
// add a processor only for 'verify' calls
31+
exercise.addVerifyProcessor(verifyProcessor(exercise, function (test, done) {
32+
var io = five.stubs.firmata.singleton
3433

35-
var led = five.Led.instances[0]
34+
test.truthy(io, 'create_board_instance')
3635

37-
expect(led, 'no led instance created').to.exist
36+
var led = five.Led.instances[0]
3837

39-
expect(led.pin, 'led expected to be connected to pin 13').to.equal(13)
40-
41-
if (!led.strobe.called && !led.blink.called) {
42-
expect(led.strobe.called, 'led.strobe was not called').to.be.true
43-
}
38+
test.truthy(led, 'create_led_instance')
39+
test.equals(led.pin, pins.led, 'connect_led_to_pin', {pin: pins.pins})
40+
test.truthy(led.strobe.called || led.blink.called, 'led_flashing')
4441

45-
if (led.blink.called) {
46-
expect(led.blink.getCall(0).args[0], 'led.blink was not called with 1000').to.equal(1000)
47-
} else {
48-
expect(led.strobe.getCall(0).args[0], 'led.strobe was not called with 1000').to.equal(1000)
49-
}
42+
if (led.blink.called) {
43+
test.equals(led.blink.getCall(0).args[0], 1000, 'led_flashing')
44+
} else {
45+
test.equals(led.strobe.getCall(0).args[0], 1000, 'led_flashing')
46+
}
5047

51-
// should have set pin 13 into digital output mode
52-
expect(io.pinMode.calledWith(13, io.MODES.OUTPUT)).to.be.true
48+
// should have set pin 13 into digital output mode
49+
test.truthy(io.pinMode.calledWith(13, io.MODES.OUTPUT), 'pin_mode', {pin: 2, mode: 'OUTPUT'})
5350

54-
// should have turned pin 13 on and off
55-
expect(io.digitalWrite.calledWith(13, io.HIGH)).to.be.true
56-
expect(io.digitalWrite.calledWith(13, io.LOW)).to.be.true
51+
// should have turned pin 13 on and off
52+
test.truthy(io.digitalWrite.calledWith(13, io.HIGH), 'pin_turned_on')
53+
test.truthy(io.digitalWrite.calledWith(13, io.LOW), 'pin_turned_off')
5754

58-
broadcaster(exercise)(function (er) { notifier(exercise)(er, callback) })
59-
} catch(error) {
60-
broadcaster(exercise)(error, function (er) { notifier(exercise)(er, callback) })
61-
}
62-
})
55+
done()
56+
}))
6357

6458
module.exports = exercise

exercises/fire_alarm/exercise.js

Lines changed: 63 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
var proxyquire = require('proxyquire')
22
var five = require('../../stubs/five')
3-
var expect = require('chai').expect
43
var async = require('async')
54
var exercise = require('workshopper-exercise')()
65
var filecheck = require('workshopper-exercise/filecheck')
76
var path = require('path')
8-
var notifier = require('../../lib/notifier')
9-
var broadcaster = require('../../lib/broadcaster')
7+
var verifyProcessor = require('../../lib/verify-processor')
108

119
// checks that the submission file actually exists
1210
exercise = filecheck(exercise)
@@ -17,7 +15,7 @@ exercise.addProcessor(function (mode, callback) {
1715
proxyquire(path.join(process.cwd(), exercise.args[0]), {'johnny-five': five})
1816

1917
setTimeout(function() {
20-
console.log('Please wait while your solution is tested...')
18+
console.log(exercise.__('please_wait'))
2119
}, 1000)
2220

2321
// need a better way of detecting when we are done..
@@ -34,46 +32,63 @@ const pins = {
3432
}
3533

3634
// add a processor only for 'verify' calls
37-
exercise.addVerifyProcessor(function (callback) {
38-
try {
39-
var io = five.stubs.firmata.singleton
35+
exercise.addVerifyProcessor(verifyProcessor(exercise, function (test, done) {
36+
var io = five.stubs.firmata.singleton
4037

41-
expect(io, 'no board instance created').to.exist
38+
test.truthy(io, 'create_board_instance')
4239

43-
// Get the listener that is listening for reads on pin A0
44-
var analogReadListener = null
40+
/*
41+
var piezo = five.Piezo.instances[0]
42+
var btn = five.Button.instances[0]
43+
var led = five.Led.instances[0]
44+
var temp = five.Thermometer.instances[0]
4545
46-
for (var i = 0; i < io.analogRead.callCount; i++) {
47-
var call = io.analogRead.getCall(i)
48-
if (call.args[0] === pins.temp) {
49-
analogReadListener = call.args[1]
50-
break
51-
}
46+
test.truthy('create_speaker_instance', piezo)
47+
test.equals('connect_speaker_to_pin', piezo.pin, pins.piezo, {pin: pins.piezo})
48+
49+
test.truthy('create_button_instance', btn)
50+
test.equals('connect_button_to_pin', btn.pin, pins.btn, {pin: pins.btn})
51+
52+
test.truthy('create_led_instance', led)
53+
test.equals('connect_led_to_pin', led.pin, pins.led, {pin: pins.led})
54+
55+
test.truthy('create_thermometer_instance', temp)
56+
test.equals('connect_thermometer_to_pin', temp.pin, pins.temp, {pin: pins.temp})
57+
*/
58+
59+
// Get the listener that is listening for reads on pin A0
60+
var analogReadListener = null
61+
62+
for (var i = 0; i < io.analogRead.callCount; i++) {
63+
var call = io.analogRead.getCall(i)
64+
if (call.args[0] === pins.temp) {
65+
analogReadListener = call.args[1]
66+
break
5267
}
68+
}
5369

54-
expect(analogReadListener, 'No values were read from A0').to.not.be.null
55-
expect(io.digitalWrite.called, 'Fire alarm went off before a temperature was received!').to.be.false
70+
test.truthy(analogReadListener, 'read_analogue_values', {pin: 'A0'})
71+
test.falsey(io.digitalWrite.called, 'premature_fire_alarm')
5672

57-
testAlarmTurnsOff(analogReadListener, io, function (error) {
58-
if (error) return broadcaster(exercise)(error, function (er) { notifier(exercise)(er, callback) })
73+
testAlarmTurnsOff(test, analogReadListener, io, function (error) {
74+
if (error) {
75+
return done(error)
76+
}
5977

60-
testAlarmResets(analogReadListener, io, function (error) {
61-
broadcaster(exercise)(error, function (er) { notifier(exercise)(er, callback) })
62-
})
78+
testAlarmResets(test, analogReadListener, io, function (error) {
79+
return done(error)
6380
})
64-
} catch (error) {
65-
broadcaster(exercise)(error, function (er) { notifier(exercise)(er, callback) })
66-
}
67-
})
81+
})
82+
}))
6883

69-
function testAlarmTurnsOff (analogReadListener, io, cb) {
84+
function testAlarmTurnsOff (test, analogReadListener, io, cb) {
7085
analogReadListener(random(tempToVoltage(50.1), tempToVoltage(100)))
7186

7287
// Within 2 seconds, the piezo should have sounded and the LED turned on
7388
setTimeout(function () {
7489
try {
75-
expect(io.digitalWrite.calledWith(pins.piezo, io.HIGH), 'Piezo was not turned on when fire started').to.be.true
76-
expect(io.digitalWrite.calledWith(pins.led, io.HIGH), 'LED was not turned on when fire started').to.be.true
90+
test.truthy(io.digitalWrite.calledWith(pins.piezo, io.HIGH), 'speaker_turned_on')
91+
test.truthy(io.digitalWrite.calledWith(pins.led, io.HIGH), 'led_turned_on')
7792
} catch (er) {
7893
return cb(er)
7994
}
@@ -83,8 +98,8 @@ function testAlarmTurnsOff (analogReadListener, io, cb) {
8398
// Within 2 seconds the last call to digitalWrite should have been with io.LOW
8499
setTimeout(function () {
85100
try {
86-
expect(io.digitalWrite.calledWith(pins.piezo, io.LOW), 'Piezo was not turned off when fire stopped').to.be.true
87-
expect(io.digitalWrite.calledWith(pins.led, io.LOW), 'LED was not turned off when fire stopped').to.be.true
101+
test.truthy(io.digitalWrite.calledWith(pins.piezo, io.LOW), 'speaker_turned_off')
102+
test.truthy(io.digitalWrite.calledWith(pins.led, io.LOW), 'led_turned_off')
88103
} catch (er) {
89104
return cb(er)
90105
}
@@ -99,15 +114,9 @@ function testAlarmTurnsOff (analogReadListener, io, cb) {
99114

100115
try {
101116
// TODO: We can't currently test this as there is no way to stop a piezo from playing a tune
102-
/*expect(
103-
lastPiezoLowCall.calledAfter(lastPiezoHighCall),
104-
'Piezo was not turned off after it was turned on and fire stopped'
105-
).to.be.true*/
117+
//test.truthy(lastPiezoLowCall.calledAfter(lastPiezoHighCall), 'speaker_turned_off')
106118

107-
expect(
108-
lastLedLowCall.calledAfter(lastLedHighCall),
109-
'LED was not turned off after it was turned on and fire stopped'
110-
).to.be.true
119+
test.truthy(lastLedLowCall.calledAfter(lastLedHighCall), 'speaker_turned_off')
111120

112121
cb()
113122
} catch (er) {
@@ -120,7 +129,7 @@ function testAlarmTurnsOff (analogReadListener, io, cb) {
120129
}, 2000)
121130
}
122131

123-
function testAlarmResets (analogReadListener, io, cb) {
132+
function testAlarmResets (test, analogReadListener, io, cb) {
124133
io.digitalWrite.reset()
125134

126135
analogReadListener(random(tempToVoltage(50.1), tempToVoltage(100)))
@@ -130,8 +139,8 @@ function testAlarmResets (analogReadListener, io, cb) {
130139
var tests = [
131140
function(callback) {
132141
try {
133-
expect(io.digitalWrite.calledWith(pins.piezo, io.HIGH), 'Piezo was not turned on when fire started').to.be.true
134-
expect(io.digitalWrite.calledWith(pins.led, io.HIGH), 'LED was not turned on when fire started').to.be.true
142+
test.truthy(io.digitalWrite.calledWith(pins.piezo, io.HIGH), 'speaker_turned_on')
143+
test.truthy(io.digitalWrite.calledWith(pins.led, io.HIGH), 'led_turned_on')
135144
} catch (er) {
136145
return callback(er)
137146
}
@@ -159,15 +168,8 @@ function testAlarmResets (analogReadListener, io, cb) {
159168
},
160169
function(callback) {
161170
try {
162-
expect(
163-
io.digitalWrite.calledWith(pins.piezo, io.LOW),
164-
'Piezo was not turned off when reset button pressed'
165-
).to.be.true
166-
167-
expect(
168-
io.digitalWrite.calledWith(pins.led, io.LOW),
169-
'LED was not turned off when reset button pressed'
170-
).to.be.true
171+
test.truthy(io.digitalWrite.calledWith(pins.piezo, io.LOW), 'speaker_reset')
172+
test.truthy(io.digitalWrite.calledWith(pins.led, io.LOW), 'led_reset')
171173
} catch (er) {
172174
return callback(er)
173175
}
@@ -184,15 +186,9 @@ function testAlarmResets (analogReadListener, io, cb) {
184186

185187
try {
186188
// TODO: We can't currently test this as there is no way to stop a piezo from playing a tune
187-
/*expect(
188-
lastPiezoLowCall.calledAfter(lastPiezoHighCall),
189-
'Piezo was not turned off after it was turned on and reset button pressed'
190-
).to.be.true*/
191-
192-
expect(
193-
lastLedLowCall.calledAfter(lastLedHighCall),
194-
'LED was not turned off after it was turned on and reset button pressed'
195-
).to.be.true
189+
//test.truthy('speaker_turned_off_after_reset', lastPiezoLowCall.calledAfter(lastPiezoHighCall))
190+
191+
test.truthy(lastLedLowCall.calledAfter(lastLedHighCall), 'led_turned_off_after_reset')
196192
} catch (er) {
197193
return callback(er)
198194
}
@@ -206,14 +202,10 @@ function testAlarmResets (analogReadListener, io, cb) {
206202
},
207203
function(callback) {
208204
try {
209-
expect(
210-
io.digitalWrite.calledWith(pins.piezo, io.HIGH),
211-
'Piezo turned back on after reset button pressed before temperature dropped below 50'
212-
).to.be.false
213-
214-
expect(io.digitalWrite.calledWith(pins.led, io.HIGH),
215-
'LED turned back on after reset button pressed before temperature dropped below 50'
216-
).to.be.false
205+
test.falsey(io.digitalWrite.calledWith(pins.piezo, io.HIGH),
206+
'speaker_stayed_off_after_reset_before_temperature_drops')
207+
test.falsey(io.digitalWrite.calledWith(pins.led, io.HIGH),
208+
'led_stayed_off_after_reset_before_temperature_drops')
217209
} catch (er) {
218210
return callback(er)
219211
}
@@ -230,15 +222,8 @@ function testAlarmResets (analogReadListener, io, cb) {
230222
},
231223
function(callback) {
232224
try {
233-
expect(
234-
io.digitalWrite.calledWith(pins.piezo, io.HIGH),
235-
'Piezo was not turned on when fire started after reset button was pressed'
236-
).to.be.true
237-
238-
expect(
239-
io.digitalWrite.calledWith(pins.led, io.HIGH),
240-
'LED was not turned on when fire started after reset button was pressed'
241-
).to.be.true
225+
test.truthy(io.digitalWrite.calledWith(pins.piezo, io.HIGH), 'speaker_stayed_off_after_fire_after_reset')
226+
test.truthy(io.digitalWrite.calledWith(pins.led, io.HIGH), 'led_stayed_off_after_fire_after_reset')
242227

243228
// Reset
244229
analogReadListener(random(tempToVoltage(0), tempToVoltage(50)))

exercises/fire_alarm/solution/solution.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ board.on('ready', function () {
55
var piezo = new five.Piezo(9)
66
var led = new five.Led(13)
77
var btn = new five.Button(5)
8-
var thermo = new five.Sensor('A0')
8+
var thermo = new five.Thermometer({
9+
controller: 'TMP36',
10+
pin: 'A0'
11+
})
912

1013
var threshold = 50
1114
var isOnFire = false
@@ -44,10 +47,7 @@ board.on('ready', function () {
4447

4548
// Watch the temp
4649
thermo.on('change', function () {
47-
// Convert to celsius (TMP36)
48-
var temp = ((this.value * 0.004882814) - 0.5) * 100
49-
50-
if (temp > threshold) {
50+
if (this.celsius > threshold) {
5151
if (!isReset) {
5252
panic()
5353
}
@@ -56,5 +56,4 @@ board.on('ready', function () {
5656
isReset = false // clear the reset flag when temp drops below threshold
5757
}
5858
})
59-
6059
})

0 commit comments

Comments
 (0)