Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make all calls to Tessel.LED.prototype.write() pass 1 or 0 as an argument. Fixes gh-221 #222

Merged
merged 1 commit into from
Jan 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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