Skip to content

Commit

Permalink
Merge pull request #222 from rwaldron/221
Browse files Browse the repository at this point in the history
Make all calls to Tessel.LED.prototype.write() pass 1 or 0 as an argument. Fixes gh-221
  • Loading branch information
HipsterBrown authored Jan 11, 2017
2 parents 170a228 + cd4e565 commit b13dc7a
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 13 deletions.
8 changes: 6 additions & 2 deletions node/tessel-export.js
Original file line number Diff line number Diff line change
Expand Up @@ -1217,11 +1217,11 @@ Tessel.LED = function(color, path) {
};

Tessel.LED.prototype.high = function(callback) {
this.write(true, callback);
this.write(1, callback);
};

Tessel.LED.prototype.low = function(callback) {
this.write(false, callback);
this.write(0, callback);
};

Tessel.LED.prototype.on = function() {
Expand All @@ -1243,6 +1243,10 @@ Tessel.LED.prototype.write = function(value, callback) {
callback = function() {};
}

// Setting this.value will invoke the setter
// defined inside the constructor body. That
// ensure that any truthy value will result
// in a value of 1 and falsy results in 0.
this.value = value;

fs.writeFile(this.path, String(this.value), callback);
Expand Down
52 changes: 41 additions & 11 deletions node/test/unit/tessel.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ exports['Tessel'] = {

ledsLazyInitializedAndOff: function(test) {
test.expect(5);
test.equal(this.tessel.led[0].value, 0);
test.strictEqual(this.tessel.led[0].value, 0);
test.equal(this.fsWrite.callCount, 1);
test.equal(this.fsWrite.lastCall.args[0], '/sys/devices/leds/leds/tessel:red:error/brightness');
test.equal(this.fsWrite.lastCall.args[1], '0');
Expand Down Expand Up @@ -304,6 +304,7 @@ exports['Tessel.LED'] = {
this.LED = sandbox.spy(Tessel, 'LED');
this.Port = sandbox.stub(Tessel, 'Port');
this.fsWrite = sandbox.stub(fs, 'writeFile');
this.ledWrite = sandbox.spy(Tessel.LED.prototype, 'write');
this.tessel = new Tessel();
done();
},
Expand All @@ -315,37 +316,41 @@ exports['Tessel.LED'] = {
},

high: function(test) {
test.expect(2);
test.expect(3);

this.tessel.led[0].high();

test.equal(this.tessel.led[0].value, '1');
test.strictEqual(this.tessel.led[0].value, 1);
test.strictEqual(this.ledWrite.lastCall.args[0], 1);
test.equal(this.fsWrite.lastCall.args[1], '1');
test.done();
},

on: function(test) {
test.expect(3);
test.expect(4);
test.equal(this.tessel.led[0].on(), this.tessel.led[0]);
test.equal(this.tessel.led[0].value, '1');
test.strictEqual(this.tessel.led[0].value, 1);
test.strictEqual(this.ledWrite.lastCall.args[0], 1);
test.equal(this.fsWrite.lastCall.args[1], '1');
test.done();
},

low: function(test) {
test.expect(2);
test.expect(3);

this.tessel.led[0].low();

test.equal(this.tessel.led[0].value, '0');
test.strictEqual(this.tessel.led[0].value, 0);
test.strictEqual(this.ledWrite.lastCall.args[0], 0);
test.equal(this.fsWrite.lastCall.args[1], '0');
test.done();
},

off: function(test) {
test.expect(3);
test.expect(4);
test.equal(this.tessel.led[0].off(), this.tessel.led[0]);
test.equal(this.tessel.led[0].value, '0');
test.strictEqual(this.tessel.led[0].value, 0);
test.strictEqual(this.ledWrite.lastCall.args[0], 0);
test.equal(this.fsWrite.lastCall.args[1], '0');
test.done();
},
Expand All @@ -367,16 +372,41 @@ exports['Tessel.LED'] = {

outputIsTheSameAsWrite: function(test) {
test.expect(1);
this.ledWrite.restore();
test.equal(Tessel.LED.prototype.output, Tessel.LED.prototype.write);
test.done();
},

writeUpdatesTheValue: function(test) {
test.expect(2);

test.equal(this.tessel.led[0].value, '0');
test.strictEqual(this.tessel.led[0].value, 0);
this.tessel.led[0].write(1);
test.equal(this.tessel.led[0].value, '1');
test.strictEqual(this.tessel.led[0].value, 1);

test.done();
},

writeAcceptsBooleanOrNumberForBackCompat: function(test) {
test.expect(9);

test.strictEqual(this.tessel.led[0].value, 0);

this.tessel.led[0].write(true);
test.strictEqual(this.tessel.led[0].value, 1);
test.equal(this.fsWrite.lastCall.args[1], '1');

this.tessel.led[0].write(false);
test.strictEqual(this.tessel.led[0].value, 0);
test.equal(this.fsWrite.lastCall.args[1], '0');

this.tessel.led[0].write(1);
test.strictEqual(this.tessel.led[0].value, 1);
test.equal(this.fsWrite.lastCall.args[1], '1');

this.tessel.led[0].write(0);
test.strictEqual(this.tessel.led[0].value, 0);
test.equal(this.fsWrite.lastCall.args[1], '0');

test.done();
},
Expand Down

0 comments on commit b13dc7a

Please sign in to comment.