Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dlindahl committed Jul 19, 2016
1 parent 840f5b2 commit 760a8b1
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 1 deletion.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"main": "index.js",
"scripts": {
"test:lint": "eslint *.js lib/**/*.js test/**/*.js",
"test": "npm run test:lint"
"test:unit": "ava test/*_test.js",
"test": "npm run test:lint && npm run test:unit"
},
"engines": {
"node": ">=4.4.7"
Expand Down Expand Up @@ -33,6 +34,7 @@
"lodash.clamp": "^4.0.2"
},
"devDependencies": {
"ava": "^0.15.2",
"sinon": "^1.17.4"
}
}
45 changes: 45 additions & 0 deletions test/diff_steer_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import test from 'ava';
import diffSteer from '../index';
import INPUTS from './inputs';

test('Full forward', t => {
t.plan(2);
const motorSpeeds = diffSteer(...INPUTS.FULL_FWD);
t.is(motorSpeeds[0], 255);
t.is(motorSpeeds[1], 255);
});

test('Full reverse', t => {
t.plan(2);
const motorSpeeds = diffSteer(...INPUTS.FULL_REV);
t.is(motorSpeeds[0], -255);
t.is(motorSpeeds[1], -255);
});

test('Full right', t => {
t.plan(2);
const motorSpeeds = diffSteer(...INPUTS.FULL_RIGHT);
t.is(motorSpeeds[0], 255);
t.is(motorSpeeds[1], -255);
});

test('Full left', t => {
t.plan(2);
const motorSpeeds = diffSteer(...INPUTS.FULL_LEFT);
t.is(motorSpeeds[0], -255);
t.is(motorSpeeds[1], 255);
});

test('Forward right', t => {
t.plan(2);
const motorSpeeds = diffSteer(...INPUTS.FWD_RIGHT);
t.is(motorSpeeds[0], 255);
t.is(motorSpeeds[1], -6.000150000000013);
});

test('Reverse left', t => {
t.plan(2);
const motorSpeeds = diffSteer(...INPUTS.REV_LEFT);
t.is(motorSpeeds[0], -105.88978213646999);
t.is(motorSpeeds[1], -196.940063931765);
});
8 changes: 8 additions & 0 deletions test/inputs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
FULL_FWD: [0, -1],
FULL_RIGHT: [1, 0],
FULL_LEFT: [-1, 0],
FULL_REV: [0, 1],
FWD_RIGHT: [0.764706, -0.741176],
REV_LEFT: [-0.726881, 0.686763]
};
82 changes: 82 additions & 0 deletions test/motor_control_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import INPUTS from './inputs';
import steerMotor from '../motor_control';
import sinon from 'sinon';
import test from 'ava';

test.beforeEach(t => {
t.context.leftMotor = {
fwd: sinon.stub(),
rev: sinon.stub()
};
t.context.rightMotor = {
fwd: sinon.stub(),
rev: sinon.stub()
};
t.context.wheels = [t.context.leftMotor, t.context.rightMotor];
});

test('Full forward', t => {
const motorCommands = steerMotor(t.context.wheels, ...INPUTS.FULL_FWD);
t.plan(6);
t.is(motorCommands[0].direction, 'fwd');
t.is(motorCommands[0].speed, 255);
t.true(t.context.leftMotor.fwd.calledWith(255));
t.is(motorCommands[1].direction, 'fwd');
t.is(motorCommands[1].speed, 255);
t.true(t.context.rightMotor.fwd.calledWith(255));
});

test('Full reverse', t => {
const motorCommands = steerMotor(t.context.wheels, ...INPUTS.FULL_REV);
t.plan(6);
t.is(motorCommands[0].direction, 'rev');
t.is(motorCommands[0].speed, 255);
t.true(t.context.leftMotor.rev.calledWith(255));
t.is(motorCommands[1].direction, 'rev');
t.is(motorCommands[1].speed, 255);
t.true(t.context.rightMotor.rev.calledWith(255));
});

test('Full right', t => {
const motorCommands = steerMotor(t.context.wheels, ...INPUTS.FULL_RIGHT);
t.plan(6);
t.is(motorCommands[0].direction, 'fwd');
t.is(motorCommands[0].speed, 255);
t.true(t.context.leftMotor.fwd.calledWith(255));
t.is(motorCommands[1].direction, 'rev');
t.is(motorCommands[1].speed, 255);
t.true(t.context.rightMotor.rev.calledWith(255));
});

test('Full left', t => {
const motorCommands = steerMotor(t.context.wheels, ...INPUTS.FULL_LEFT);
t.plan(6);
t.is(motorCommands[0].direction, 'rev');
t.is(motorCommands[0].speed, 255);
t.true(t.context.leftMotor.rev.calledWith(255));
t.is(motorCommands[1].direction, 'fwd');
t.is(motorCommands[1].speed, 255);
t.true(t.context.rightMotor.fwd.calledWith(255));
});

test('Forward right', t => {
const motorCommands = steerMotor(t.context.wheels, ...INPUTS.FWD_RIGHT);
t.plan(6);
t.is(motorCommands[0].direction, 'fwd');
t.is(motorCommands[0].speed, 255);
t.true(t.context.leftMotor.fwd.calledWith(255));
t.is(motorCommands[1].direction, 'rev');
t.is(motorCommands[1].speed, 6.000150000000013);
t.true(t.context.rightMotor.rev.calledWith(6.000150000000013));
});

test('Reverse left', t => {
const motorCommands = steerMotor(t.context.wheels, ...INPUTS.REV_LEFT);
t.plan(6);
t.is(motorCommands[0].direction, 'rev');
t.is(motorCommands[0].speed, 105.88978213646999);
t.true(t.context.leftMotor.rev.calledWith(105.88978213646999));
t.is(motorCommands[1].direction, 'rev');
t.is(motorCommands[1].speed, 196.940063931765);
t.true(t.context.rightMotor.rev.calledWith(196.940063931765));
});

0 comments on commit 760a8b1

Please sign in to comment.