Skip to content

Commit

Permalink
Test cases added with minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
luk3Sky committed Dec 12, 2020
1 parent 149a0c5 commit b77e619
Show file tree
Hide file tree
Showing 7 changed files with 334 additions and 3 deletions.
49 changes: 49 additions & 0 deletions __tests__/color.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
var assert = require('chai').assert;
var chai = require('chai');
var expect = chai.expect;

const { ColorSensor } = require('../');

var s;
var updated;

const SAMPLE_ID = 1;
const INITIAL_VALUES = [0, 0, 0];
const SAMPLE_VALUES_1 = [123, 231, 19];
const SAMPLE_VALUES_2 = [1, 150, 121];

beforeEach(function () {
s = new ColorSensor(SAMPLE_ID);
updated = s.getReading().updated;
});

describe('Color Sensor', function () {
describe('#getReading()', function () {
it('should get the sensor readings', function () {
expect(s.getReading()).to.be.a('object').to.haveOwnProperty('id');
expect(s.getReading()).to.be.a('object').to.haveOwnProperty('value');
expect(s.getReading()).to.be.a('object').to.haveOwnProperty('updated');
expect(s.getReading()).to.not.null;
expect(s.getReading()).to.not.be.empty;
expect(s.getReading()).to.not.undefined;
assert.typeOf(s.getReading(), 'object');
assert.typeOf(s.getReading().id, 'number');
assert.typeOf(s.getReading().value, 'array');
assert.typeOf(s.getReading().updated, 'number');
});
});

describe('#setReading()', function () {
it('should set the sensor values', function () {
assert.deepEqual(s.getReading().value, INITIAL_VALUES);
s.setReading(SAMPLE_VALUES_1);
assert.notEqual(s.getReading().value, INITIAL_VALUES);
assert.deepEqual(s.getReading().value, SAMPLE_VALUES_1);
expect(s.updated).gte(updated);
s.setReading(SAMPLE_VALUES_2);
assert.notEqual(s.getReading().value, INITIAL_VALUES);
assert.deepEqual(s.getReading().value, SAMPLE_VALUES_2);
expect(s.updated).gte(updated);
});
});
});
49 changes: 49 additions & 0 deletions __tests__/distance.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
var assert = require('chai').assert;
var chai = require('chai');
var expect = chai.expect;

const { DistanceSensor } = require('../');

var s;
var updated;

const SAMPLE_ID = 1;
const INITIAL_VALUE = 0;
const SAMPLE_VALUE_1 = 123;
const SAMPLE_VALUE_2 = 126;

beforeEach(function () {
s = new DistanceSensor(SAMPLE_ID);
updated = s.getReading().updated;
});

describe('Distance Sensor', function () {
describe('#getReading()', function () {
it('should get the sensor reading', function () {
expect(s.getReading()).to.be.a('object').to.haveOwnProperty('id');
expect(s.getReading()).to.be.a('object').to.haveOwnProperty('value');
expect(s.getReading()).to.be.a('object').to.haveOwnProperty('updated');
expect(s.getReading()).to.not.null;
expect(s.getReading()).to.not.be.empty;
expect(s.getReading()).to.not.undefined;
assert.typeOf(s.getReading(), 'object');
assert.typeOf(s.getReading().id, 'number');
assert.typeOf(s.getReading().value, 'number');
assert.typeOf(s.getReading().updated, 'number');
});
});

describe('#setReading()', function () {
it('should set the sensor value', function () {
assert.equal(s.getReading().value, INITIAL_VALUE);
s.setReading(SAMPLE_VALUE_1);
assert.notEqual(s.getReading().value, INITIAL_VALUE);
assert.equal(s.getReading().value, SAMPLE_VALUE_1);
expect(s.updated).gte(updated);
s.setReading(SAMPLE_VALUE_2);
assert.notEqual(s.getReading().value, INITIAL_VALUE);
assert.equal(s.getReading().value, SAMPLE_VALUE_2);
expect(s.updated).gte(updated);
});
});
});
160 changes: 160 additions & 0 deletions __tests__/robot.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
var assert = require('chai').assert;
var chai = require('chai');
var expect = chai.expect;

const { VRobot, sensorModuleTypes, Coordinate } = require('../');
// const sensors = require('../../modules/sensors');

var r;
var updated;
var created;
var timestamp;

const SAMPLE_ID_1 = 1;
const SAMPLE_ID_2 = 2;

const INITIAL_HEADING = 0;
const INITIAL_X = 0;
const INITIAL_Y = 0;
const INITIAL_Z = undefined;

const SAMPLE_HEADING_1 = 5;
const SAMPLE_X_1 = 6;
const SAMPLE_Y_1 = 7;
const SAMPLE_Z_1 = 8;

const SAMPLE_HEADING_2 = 10;
const SAMPLE_X_2 = 11;
const SAMPLE_Y_2 = 12;
const SAMPLE_Z_2 = 13;

beforeEach(function () {
r = new VRobot(SAMPLE_ID_1);
updated = r.updated;
created = r.created;
timestamp = r.timestamp;
});

describe('VRobot', function () {
describe('#VRobot()', function () {
it('should create a VRobot instance', function () {
// id
expect(r).to.have.property('id');
assert.typeOf(r.id, 'number');
// coordinates
expect(r).to.have.property('coordinates');
assert.typeOf(r.coordinates, 'object');
// created
expect(r).to.have.property('created');
assert.typeOf(r.created, 'date');
expect(r.created).to.equal(created);
// updated
expect(r).to.have.property('updated');
assert.typeOf(r.updated, 'number');
expect(r.updated).to.equal(updated);
// timestamp
expect(r).to.have.property('timestamp');
assert.typeOf(r.timestamp, 'number');
expect(r.timestamp).to.equal(timestamp);
});
});

describe('#coordinates', function () {
it('should return the coordinates of the robot instance', function () {
const coordinates = r.coordinates;
assert.typeOf(coordinates, 'object');
// id
expect(coordinates).to.haveOwnProperty('id');
assert.typeOf(coordinates.id, 'number');
expect(coordinates.id).to.be.equal(SAMPLE_ID_1);
// heading
expect(coordinates).to.haveOwnProperty('heading');
assert.typeOf(coordinates.heading, 'number');
expect(coordinates.heading).to.be.equal(INITIAL_HEADING);
// x
expect(coordinates).to.haveOwnProperty('x');
assert.typeOf(coordinates.x, 'number');
expect(coordinates.x).to.be.equal(INITIAL_X);
// y
expect(coordinates).to.haveOwnProperty('y');
assert.typeOf(coordinates.y, 'number');
expect(coordinates.y).to.be.equal(INITIAL_Y);
// z
expect(coordinates).to.not.haveOwnProperty('z');
assert.typeOf(coordinates.z, 'undefined');
expect(coordinates.z).to.be.equal(INITIAL_Z);
});
});

describe('#setCoordinates()', function () {
it('should set the coordinates of the robot instance', function () {
r.setCoordinates({ heading: SAMPLE_HEADING_1, x: SAMPLE_X_1, y: SAMPLE_Y_1 });
expect(r.updated).gte(updated);
const coordinates = r.coordinates;
assert.typeOf(coordinates, 'object');
// id
expect(coordinates).to.haveOwnProperty('id');
assert.typeOf(coordinates.id, 'number');
expect(coordinates.id).to.be.equal(SAMPLE_ID_1);
// heading
expect(coordinates).to.haveOwnProperty('heading');
assert.typeOf(coordinates.heading, 'number');
expect(coordinates.heading).to.be.equal(SAMPLE_HEADING_1);
// x
expect(coordinates).to.haveOwnProperty('x');
assert.typeOf(coordinates.x, 'number');
expect(coordinates.x).to.be.equal(SAMPLE_X_1);
// y
expect(coordinates).to.haveOwnProperty('y');
assert.typeOf(coordinates.y, 'number');
expect(coordinates.y).to.be.equal(SAMPLE_Y_1);
// z
expect(coordinates).to.not.haveOwnProperty('z');
assert.typeOf(coordinates.z, 'undefined');
expect(coordinates.z).to.be.equal(INITIAL_Z);
});
});

describe('#sensors()', function () {
it('should return the sensors module of the robot instance', function () {
const sensors = r.sensors;
assert.typeOf(sensors, 'object');
sensorModuleTypes.map((type) => {
expect(sensors).to.haveOwnProperty(type);
});
});
});

describe('#getSensorReadings()', function () {
it('should return the sensor readings of the robot instance', function () {
expect(r.getSensorReadings).to.throw(TypeError);
const sensorReadings = r.getSensorReadings(sensorModuleTypes);
assert.typeOf(sensorReadings, 'array');
});
});

describe('#created', function () {
it('should return the created value of the robot instance', function () {
const createdValue = r.created;
assert.typeOf(createdValue, 'date');
expect(createdValue).to.equal(created);
});
});

describe('#timestamp', function () {
it('should return the timestamp value of the robot instance', function () {
const timestampValue = r.timestamp;
assert.typeOf(timestampValue, 'number');
expect(timestampValue).to.equal(timestamp);
});
});

describe('#updateHeartbeat()', function () {
it('should update and return the updated timestamp of the robot instance', function () {
const heartbeat = r.updateHeartbeat();
assert.typeOf(heartbeat, 'number');
expect(heartbeat).gte(timestamp);
expect(r.updated).gte(updated);
});
});
});
48 changes: 48 additions & 0 deletions __tests__/sensors.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
var assert = require('chai').assert;
var chai = require('chai');
var expect = chai.expect;

const { sensors, ColorSensor, DistanceSensor } = require('../');

var s;
var updated;

const SAMPLE_ID = 1;

const INITIAL_COLOR_SENSOR_VALUES = [0, 0, 0];
const SAMPLE_COLOR_SENSOR_VALUES_1 = [123, 231, 19];
const SAMPLE_COLOR_SENSOR_VALUES_2 = [1, 150, 121];

const INITIAL_DISTANCE_SENSOR_VALUE = 0;
const SAMPLE_DISTANCE_SENSOR_VALUE_1 = 123;
const SAMPLE_DISTANCE_SENSOR_VALUE_2 = 150;

beforeEach(function () {
s = sensors(SAMPLE_ID);
updated = s.updated;
});

describe('Sensors', function () {
describe('#sensors()', function () {
it('should create a sensor instance', function () {
expect(sensors).to.throw(TypeError);
s = sensors(SAMPLE_ID);
// updated
expect(s).to.haveOwnProperty('updated');
assert.typeOf(s.updated, 'number');
expect(s.updated).to.gte(updated);
// color
expect(s).to.haveOwnProperty('color');
expect(s.color).to.be.instanceOf(ColorSensor);
assert.typeOf(s.color, 'object');
expect(s.color.id).to.equal(SAMPLE_ID);
expect(s.color.value).to.deep.equal(INITIAL_COLOR_SENSOR_VALUES);
// distance
expect(s).to.haveOwnProperty('distance');
expect(s.distance).to.be.instanceOf(DistanceSensor);
assert.typeOf(s.distance, 'object');
expect(s.distance.id).to.equal(SAMPLE_ID);
expect(s.distance.value).to.deep.equal(INITIAL_DISTANCE_SENSOR_VALUE);
});
});
});
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
"build": "tsc",
"dev": "tsc --watch",
"format": "prettier --write \"**/*.+(js|jsx|ts|tsx|json|yml|yaml)\"",
"test": "mocha -r ts-node/register './__tests__/**/*test.js'",
"test:watch": "mocha --watch --recursive './__tests__/**/*test.js' ts-node/register"
"test": "mocha -r './__tests__/**/*test.js'",
"test:watch": "mocha --watch --recursive './__tests__/**/*test.js'"
},
"repository": {
"type": "git",
Expand Down
25 changes: 25 additions & 0 deletions src/modules/robot/robot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,22 @@ export class VRobot extends Robot<
return this._sensors;
}

/**
* get date
* @returns {Date} date values
*/
get created(): Date {
return this._created;
}

/**
* get timestamp
* @returns {number} timestamp values
*/
get timestamp(): number {
return this._timestamp;
}

/**
* set sensor array
* @param {SensorModuleType<number, number[], number>} sensors sensors array
Expand Down Expand Up @@ -142,4 +158,13 @@ export class VRobot extends Robot<
throw new TypeError('invalid sensor types');
}
}

/**
* return the updated timestamp
* @returns updated timestamp
*/
updateHeartbeat(): number {
this._updated = Date.now();
return this._updated;
}
}
2 changes: 1 addition & 1 deletion src/modules/sensors/sensor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export abstract class Sensor<TId, TValueType> {
* the coordinate updated
*/
get updated() {
return this._id;
return this._updated;
}

/**
Expand Down

0 comments on commit b77e619

Please sign in to comment.