/
avrgirl-arduino.spec.js
150 lines (124 loc) · 4.15 KB
/
avrgirl-arduino.spec.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
var test = require('tape');
var proxyquire = require('proxyquire');
var sinon = require('sinon');
// proxyquired connection module
var Connection = proxyquire.noCallThru().load('../lib/connection',
{ serialport: {
list: function() { return Promise.resolve(
[
{ comName: '/dev/cu.sierravsp', manufacturer: '', serialNumber: '',
pnpId: '', locationId: '', vendorId: '', productId: '' },
{ comName: '/dev/cu.Bluetooth-Incoming-Port', manufacturer: '',
serialNumber: '', pnpId: '', locationId: '', vendorId: '',
productId: '' },
{ comName: '/dev/cu.usbmodem1421', manufacturer: 'Arduino (www.arduino.cc)',
serialNumber: '55432333038351F03170', pnpId: '', locationId: '0x14200000',
vendorId: '0x2341', productId: '0x0043' }
])
}
},
SerialPort: require('./helpers/mockSerial').SerialPort
});
// module to test
var Avrgirl = proxyquire('../', { Connection: Connection });
// default options
var DEF_OPTS2 = {
board: 'uno'
};
test('[ AVRGIRL-ARDUINO ] method presence', function(t) {
var a = new Avrgirl(DEF_OPTS2);
function isFn(name) {
return typeof a[name] === 'function';
}
var methods = ['flash', '_validateBoard', 'listPorts'];
for (var i = 0; i < methods.length; i += 1) {
t.ok(isFn(methods[i]), methods[i]);
if (i === methods.length - 1) {
t.end();
}
}
});
test('[ AVRGIRL-ARDUINO ] new creation', function(t) {
t.plan(3);
var a = new Avrgirl(DEF_OPTS2);
t.ok(a.connection, 'connection was established');
t.ok(a.options.board.baud, 'board was established');
t.ok(a.protocol.chip, 'protocol was established');
});
test('[ AVRGIRL-ARDUINO ] ::_validateBoard (GOOD)', function(t) {
t.plan(1);
var a = new Avrgirl(DEF_OPTS2);
a._validateBoard(function(error) {
t.error(error, 'no error');
});
});
test('[ AVRGIRL-ARDUINO ] ::_validateBoard (SPARSE)', function(t) {
t.plan(1);
var a = new Avrgirl({ board: { name: DEF_OPTS2.board } });
a._validateBoard(function(error) {
t.error(error, 'no error');
});
});
test('[ AVRGIRL-ARDUINO ] ::_validateBoard (SPARSE NO NAME)', function(t) {
t.plan(1);
var a = new Avrgirl({ board: { notName: DEF_OPTS2.board } });
a._validateBoard(function(error) {
t.ok(error, 'error returned');
});
});
test('[ AVRGIRL-ARDUINO ] ::_validateBoard (NO BOARD)', function(t) {
t.plan(1);
var a = new Avrgirl({ board: 'bacon' });
a._validateBoard(function(error) {
t.ok(error, 'error returned');
});
});
test('[ AVRGIRL-ARDUINO ] ::_validateBoard (NO PROTOCOL)', function(t) {
t.plan(1);
var a = new Avrgirl(DEF_OPTS2);
a.protocol = 'bacon';
a._validateBoard(function(error) {
t.ok(error, 'error returned');
});
});
test('[ AVRGIRL-ARDUINO ] ::_validateBoard (NO PORT & PRO-MINI)', function(t) {
t.plan(1);
var a = new Avrgirl({ board: 'pro-mini' });
a._validateBoard(function(error) {
t.ok(error, 'error returned');
});
});
test('[ AVRGIRL-ARDUINO ] ::listPorts', function(t) {
t.plan(3);
Avrgirl.listPorts(function(error, ports) {
t.ok(ports.length, 'got a list of ports');
t.ok(ports[2]._standardPid, 'added _standardPid property');
t.error(error, 'no error on listing');
});
});
test('[ AVRGIRL-ARDUINO ] ::listPorts (prototype)', function(t) {
t.plan(3);
var a = new Avrgirl(DEF_OPTS2);
a.listPorts(function(error, ports) {
t.ok(ports.length, 'got a list of ports');
t.ok(ports[2]._standardPid, 'added _standardPid property');
t.error(error, 'no error on listing');
});
});
test('[ AVRGIRL-ARDUINO ] ::flash (shallow)', function(t) {
t.plan(4);
var a = new Avrgirl(DEF_OPTS2);
var spyInit = sinon.stub(a.connection, '_init').callsFake(function(callback) {
return callback(null);
});
var spyUpload = sinon.stub(a.protocol, '_upload').callsFake(function(file, callback) {
return callback(null);
});
var spyValidate = sinon.spy(a, '_validateBoard');
a.flash(__dirname + '/../junk/hex/uno/Blink.cpp.hex', function(error) {
t.ok(spyValidate.calledOnce, 'validated board');
t.ok(spyInit.calledOnce, 'connection init');
t.ok(spyUpload.calledOnce, 'upload to board attempt');
t.error(error, 'no error');
});
});