Skip to content

Commit

Permalink
Compass: improved test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
rwaldron committed Oct 26, 2016
1 parent 307e3df commit b9b70d2
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/compass.js
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ function Compass(opts) {
bearing: {
get: function() {
var length = Compass.Points.length;
var heading = Math.floor(state.heading);
var heading = Math.floor(this.heading);
var point;

for (var i = 0; i < length; i++) {
Expand Down
162 changes: 162 additions & 0 deletions test/compass.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,116 @@ exports["Compass - MAG3110"] = {
},
};

exports["Compass - BNO055"] = {
setUp: function(done) {
this.sandbox = sinon.sandbox.create();
this.clock = this.sandbox.useFakeTimers();
this.board = newBoard();

this.i2cConfig = this.sandbox.spy(MockFirmata.prototype, "i2cConfig");
this.i2cWrite = this.sandbox.spy(MockFirmata.prototype, "i2cWrite");
this.i2cReadOnce = this.sandbox.spy(MockFirmata.prototype, "i2cReadOnce");

this.compass = new Compass({
board: this.board,
controller: "BNO055",
offsets: {
x: [-819, -335],
y: [702, 1182],
z: [-293, -13],
},
});

this.properties = [{
name: "bearing"
}, {
name: "heading"
}];

done();
},

tearDown: function(done) {
Board.purge();
this.sandbox.restore();
done();
},

shape: function(test) {
test.expect(this.properties.length);

this.properties.forEach(function(property) {
test.notEqual(typeof this.compass[property.name], "undefined");
}, this);
test.done();
},

fwdOptionsToi2cConfig: function(test) {
test.expect(3);

this.i2cConfig.reset();

new Compass({
controller: "BNO055",
address: 0xff,
bus: "i2c-1",
board: this.board
});

var forwarded = this.i2cConfig.lastCall.args[0];

test.equal(this.i2cConfig.callCount, 1);
test.equal(forwarded.address, 0xff);
test.equal(forwarded.bus, "i2c-1");

test.done();
},

dataAndChange: function(test) {
test.expect(4);

var driver = IMU.Drivers.get(this.board, "BNO055");
var data = this.sandbox.spy();
var change = this.sandbox.spy();

this.compass.on("data", data);
this.compass.on("change", change);

driver.emit("data", {
magnetometer: {
x: -52,
y: -417,
z: -200,
},
});
this.clock.tick(20);

driver.emit("data", {
magnetometer: {
x: -52,
y: -0,
z: -200,
},
});
this.clock.tick(20);

test.equal(data.callCount, 1);
test.equal(change.callCount, 1);

test.equal(this.compass.heading, 180);
test.deepEqual(this.compass.bearing, {
name: "South",
abbr: "S",
low: 174.38,
high: 185.62,
heading: 180,
});
test.done();
},

};


exports["Invalid or missing controller"] = {
missing: function(test) {
test.expect(1);
Expand All @@ -273,3 +383,55 @@ exports["Invalid or missing controller"] = {
test.done();
},
};

exports["Compass.Scale"] = {
expectedRegistersAndScales: function(test) {
var expects = [{
gauss: 0.88,
register: 0x00 << 5,
scale: 0.73,
}, {
gauss: 1.3,
register: 0x01 << 5,
scale: 0.92,
}, {
gauss: 1.9,
register: 0x02 << 5,
scale: 1.22,
}, {
gauss: 2.5,
register: 0x03 << 5,
scale: 1.52,
}, {
gauss: 4.0,
register: 0x04 << 5,
scale: 2.27,
}, {
gauss: 4.7,
register: 0x05 << 5,
scale: 2.56,
}, {
gauss: 5.6,
register: 0x06 << 5,
scale: 3.03,
}, {
gauss: 8.1,
register: 0x07 << 5,
scale: 4.35,
}, {
gauss: undefined,
register: 0x00 << 5,
scale: 1,
}];

test.expect(expects.length * 2);

expects.forEach(function(expect) {
var cs = new Compass.Scale(expect.gauss);

test.equal(cs.register, expect.register);
test.equal(cs.scale, expect.scale);
});
test.done();
}
};

0 comments on commit b9b70d2

Please sign in to comment.