Skip to content

Commit

Permalink
test(set_mode): Add Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
miketrebilcock committed Jan 11, 2017
1 parent 495bdb4 commit f4cdca2
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 9 deletions.
18 changes: 9 additions & 9 deletions js-pigpio/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@ const net = require('net');
const assert = require('assert');
const Put = require('put');
const reverse_string = require('./utils.js').reverse_string;
const define = require("node-constants")(exports);

const _LOCKS = [];

define({
/** @class */
function pigpio() {
"use strict";

}

pigpio.prototype = {
// GPIO levels
OFF: 0,
LOW: 0,
Expand Down Expand Up @@ -41,13 +46,8 @@ define({
PUD_OFF: 0,
PUD_DOWN: 1,
PUD_UP: 2
});

/** @class */
function pigpio() {
"use strict";
};

}

/**
*
Expand Down Expand Up @@ -185,7 +185,7 @@ pigpio.prototype.getHardwareRevision = function(cb) {
pigpio.prototype.set_mode = function (gpio, mode) {
"use strict";
assert(gpio>=0 && gpio <=53, "userGpio must be in the range 0-31");
assert([pigpio.INPUT, pigpio.OUTPUT, pigpio.ALT0, pigpio.ALT1, pigpio.ALT2, pigpio.ALT3, pigpio.ALT4, pigpio.ALT5].includes(mode), "Mode must be INPUT, OUTPUT, ALT0, ALT1, ALT2, ALT3, ALT4, ALT5");
assert([this.INPUT, this.OUTPUT, this.ALT0, this.ALT1, this.ALT2, this.ALT3, this.ALT4, this.ALT5].includes(mode), "Mode must be INPUT, OUTPUT, ALT0, ALT1, ALT2, ALT3, ALT4, ALT5");
this._pi_gpio_command(def.PI_CMD_MODES, gpio, mode);

};
Expand Down
149 changes: 149 additions & 0 deletions test/pigpio.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,155 @@ describe('js-pigpio', () => {
});
});
context('Beginner', () => {
it('set_mode as INPUT', (done) => {
const pigpio = new PiGPIO();
pigpio.pi('127.0.0.1', 5000, () => {
"use strict";
pigpio.set_mode(1, pigpio.INPUT);
setTimeout((done)=>{
assert(last_command[1]==='0', "Wrong Command Send");
assert(last_command[9]==='1', "Wrong Command Send");
assert(last_command[17]==='0', "Wrong Command Send");
done();
}, 500, done);
pigpio.close();

});
});

it('set_mode as OUTPUT', (done) => {
const pigpio = new PiGPIO();
pigpio.pi('127.0.0.1', 5000, () => {
"use strict";
pigpio.set_mode(1, pigpio.OUTPUT);
setTimeout((done)=>{
assert(last_command[1]==='0', "Wrong Command Send");
assert(last_command[9]==='1', "Wrong Command Send");
assert(last_command[17]==='1', "Wrong Command Send");
done();
}, 500, done);
pigpio.close();

});
});

it('set_mode as ALT0', (done) => {
const pigpio = new PiGPIO();
pigpio.pi('127.0.0.1', 5000, () => {
"use strict";
pigpio.set_mode(1, pigpio.ALT0);
setTimeout((done)=>{
assert(last_command[1]==='0', "Wrong Command Send");
assert(last_command[9]==='1', "Wrong Command Send");
assert(last_command[17]==='4', "Wrong Command Send");
done();
}, 500, done);
pigpio.close();

});
});

it('set_mode as ALT1', (done) => {
const pigpio = new PiGPIO();
pigpio.pi('127.0.0.1', 5000, () => {
"use strict";
pigpio.set_mode(1, pigpio.ALT1);
setTimeout((done)=>{
assert(last_command[1]==='0', "Wrong Command Send");
assert(last_command[9]==='1', "Wrong Command Send");
assert(last_command[17]==='5', "Wrong Command Send");
done();
}, 500, done);
pigpio.close();

});
});

it('set_mode as ALT2', (done) => {
const pigpio = new PiGPIO();
pigpio.pi('127.0.0.1', 5000, () => {
"use strict";
pigpio.set_mode(1, pigpio.ALT2);
setTimeout((done)=>{
assert(last_command[1]==='0', "Wrong Command Send");
assert(last_command[9]==='1', "Wrong Command Send");
assert(last_command[17]==='6', "Wrong Command Send");
done();
}, 500, done);
pigpio.close();

});
});

it('set_mode as ALT3', (done) => {
const pigpio = new PiGPIO();
pigpio.pi('127.0.0.1', 5000, () => {
"use strict";
pigpio.set_mode(1, pigpio.ALT3);
setTimeout((done)=>{
assert(last_command[1]==='0', "Wrong Command Send");
assert(last_command[9]==='1', "Wrong Command Send");
assert(last_command[17]==='7', "Wrong Command Send");
done();
}, 500, done);
pigpio.close();

});
});

it('set_mode as ALT4', (done) => {
const pigpio = new PiGPIO();
pigpio.pi('127.0.0.1', 5000, () => {
"use strict";
pigpio.set_mode(1, pigpio.ALT4);
setTimeout((done)=>{
assert(last_command[1]==='0', "Wrong Command Send");
assert(last_command[9]==='1', "Wrong Command Send");
assert(last_command[17]==='3', "Wrong Command Send");
done();
}, 500, done);
pigpio.close();

});
});

it('set_mode as ALT5', (done) => {
const pigpio = new PiGPIO();
pigpio.pi('127.0.0.1', 5000, () => {
"use strict";
pigpio.set_mode(1, pigpio.ALT5);
setTimeout((done)=>{
assert(last_command[1]==='0', "Wrong Command Send");
assert(last_command[9]==='1', "Wrong Command Send");
assert(last_command[17]==='2', "Wrong Command Send");
done();
}, 500, done);
pigpio.close();

});
});

it('set_mode validates inputs', () => {
const pigpio = new PiGPIO();
pigpio.pi('127.0.0.1', 5000, () => {
"use strict";
expect(() => {
pigpio.set_mode(-1, pigpio.OUTPUT);
}).to.throw(Error);
expect(() => {
pigpio.set_mode(54, pigpio.OUTPUT);
}).to.throw(Error);
expect(() => {
pigpio.set_mode(1, "TEST");
}).to.throw(Error);
expect(() => {
pigpio.set_mode();
}).to.throw(Error);
pigpio.close();

});
});

it('setServoPulsewidth', (done) => {
const pigpio = new PiGPIO();
pigpio.pi('127.0.0.1', 5000, () => {
Expand Down

0 comments on commit f4cdca2

Please sign in to comment.