/
board.js
346 lines (272 loc) · 6.55 KB
/
board.js
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
require("es6-shim");
var SerialPort = require("./util/mock-serial").SerialPort,
MockFirmata = require("./util/mock-firmata"),
five = require("../lib/johnny-five.js"),
Repl = require("../lib/repl"),
sinon = require("sinon"),
__ = require("../lib/fn.js"),
_ = require("lodash"),
Board = five.Board,
board = new Board({
io: new MockFirmata(),
debug: false,
repl: false
});
exports["Initialization"] = {
// setUp: function(done) {
// done();
// },
// tearDown: function(done) {
// done();
// },
explicit: function(test) {
test.expect(1);
var sp = new SerialPort("/dev/foo", {
baudrate: 57600,
buffersize: 128
});
var board = new Board({
port: sp,
debug: false,
repl: false
});
test.equal(board.io.sp, sp);
test.done();
},
ioIsReady: function(test) {
test.expect(2);
var io = new MockFirmata();
// Emit connection and ready BEFORE
// using the instance to initialize
// a new Board.
io.emit("connect");
io.emit("ready");
var board = new Board({
io: io,
debug: false,
repl: false
});
board.on("connect", function() {
test.ok(true);
});
board.on("ready", function() {
test.ok(true);
test.done();
});
},
ioHasError: function(test) {
test.expect(1);
var sp = new SerialPort("/dev/foo", {
baudrate: 57600,
buffersize: 128
});
var board = new Board({
port: sp,
debug: false,
repl: false
});
board.on("error", function(msg) {
test.equals("ioHasError", msg);
test.done();
});
sp.emit("error", "ioHasError");
}
};
exports["samplingInterval"] = {
samplingInterval: function(test) {
test.expect(1);
board.io.setSamplingInterval = sinon.spy();
board.samplingInterval(100);
test.ok(board.io.setSamplingInterval.calledOnce);
test.done();
}
};
exports["static"] = {
"Board.cache": function(test) {
test.expect(2);
test.equal(typeof five.Board.cache, "object", "Board.cache");
test.ok(Array.isArray(five.Board.cache), "Board.cache");
test.done();
},
"Board.Options": function(test) {
test.expect(1);
test.ok(five.Board.Options);
test.done();
},
"Board.Pins": function(test) {
test.expect(1);
test.ok(five.Board.Pins, "Board.Pins");
test.done();
},
"Board.Event": function(test) {
test.expect(2);
var serial = {},
boardEvent = new five.Board.Event({
type: "read",
target: serial
});
test.ok(boardEvent.type === "read");
test.ok(boardEvent.target === serial);
test.done();
},
"Boards": function(test) {
test.expect(1);
test.equal(five.Boards, five.Board.Array);
test.done();
},
"Boards - connect, ready after": function(test) {
test.expect(2);
var io = new MockFirmata();
var boards = new five.Boards([{
id: "A",
repl: false,
debug: false,
io: io
}, {
id: "B",
repl: false,
debug: false,
io: io
}]);
test.equals(2, boards.length);
boards.on("ready", function() {
test.ok(true);
test.done();
});
io.emit("connect");
io.emit("ready");
},
"Boards - connect, ready before": function(test) {
test.expect(2);
var io = new MockFirmata();
io.emit("connect");
io.emit("ready");
var boards = new five.Boards([{
id: "A",
repl: false,
debug: false,
io: io
}, {
id: "B",
repl: false,
debug: false,
io: io
}]);
test.equals(2, boards.length);
boards.on("ready", function() {
test.ok(true);
test.done();
});
}
};
exports["instance"] = {
cache: function(test) {
test.expect(1);
test.ok(_.contains(five.Board.cache, board));
test.done();
},
instance: function(test) {
test.expect(1);
test.ok(board);
test.done();
},
io: function(test) {
test.expect(1);
test.ok(board.io instanceof MockFirmata);
test.done();
},
id: function(test) {
test.expect(1);
test.ok(board.id);
test.done();
},
pins: function(test) {
test.expect(1);
test.ok(board.pins);
test.done();
},
};
exports["Board.mount"] = {
setUp: function(done) {
this.board = new Board({
io: new MockFirmata(),
debug: false,
repl: false
});
done();
},
tearDown: function(done) {
Board.purge();
done();
},
"Board.mount()": function(test) {
test.expect(1);
test.equal(typeof five.Board.mount, "function", "Board.mount");
test.done();
},
"Board.mount(obj)": function(test) {
test.expect(2);
test.ok(five.Board.mount({
board: this.board
}), "five.Board.mount({ board: board })");
test.deepEqual(five.Board.mount({
board: this.board
}), this.board, "five.Board.mount({ board: board }) deep equals board");
test.done();
},
"Board.mount(index)": function(test) {
test.expect(2);
test.ok(five.Board.mount(0), "five.Board.mount(0)");
test.deepEqual(five.Board.mount(0), this.board, "five.Board.mount(0)");
test.done();
},
"Board.mount(/*none*/)": function(test) {
test.expect(2);
test.ok(five.Board.mount(), "five.Board.mount()");
test.deepEqual(five.Board.mount(), this.board, "five.Board.mount() matches board instance");
test.done();
},
};
exports["bubbled events from io"] = {
string: function(test) {
test.expect(1);
var io = new MockFirmata();
var board = new Board({
io: io,
debug: false,
repl: false
});
board.on("ready", function() {
board.on("string", function(data) {
test.equal(data, 1);
test.done();
});
io.emit("string", 1);
});
board.emit("ready");
}
};
exports["fn"] = {
cache: function(test) {
test.expect(6);
test.equal(__.scale(10, 0, 20, 0, 100), 50, "scale up");
test.equal(__.scale(10, 0, 20, 100, 0), 50, "scale up reversed");
test.equal(__.scale(10, 0, 10, 0, 180), 180, "max is 180");
test.equal(__.scale(10, 0, 10, 180, 0), 0, "max is 0");
test.equal(__.scale(0, 0, 10, 180, 0), 180, "min is 180");
test.equal(__.scale(0, 0, 10, 0, 180), 0, "min is 0");
test.done();
}
};
// TODO: need tests for board.shiftOut
// TODO: need mock io object
// exports["modules"] = {
// "optional-new": function( test ) {
// var modules = Object.keys(five);
// // test.expect(modules * 2);
// modules.forEach(function( module ) {
// var instance = new five[ module ]({});
// console.log( instance );
// });
// }
// };