Skip to content

Commit

Permalink
Created tests for SrlPoint
Browse files Browse the repository at this point in the history
Created some tests for the SrlPoint
  • Loading branch information
dtracers committed May 31, 2016
1 parent e673f9e commit a9e5616
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 14 deletions.
8 changes: 1 addition & 7 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,8 @@
"version": "0.0.1",
"dependencies": {
"stacktrace-js": "0.6.4",
"protobufjs": "dcodeIO/protobuf.js#473ee72417d3e2ced7c0b46371ec316e8c305bed",
"protobufjs": "dcodeIO/protobuf.js#c7bf7fc571a9bb1a8c63a2e33f5fddd897f4b098",
"requirejs": "2.2.0"
},
"devDependencies": {
"blanket": "1.1.7",
"qunit": "1.23.0",
"qunit-once": "0.1.1",
"sinon": "http://sinonjs.org/releases/sinon-1.17.3.js"
},
"private": true
}
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"name": "SketchRecognitionFramework",
"version": "0.0.5",
"dependencies": {
"stacktrace-js": "1.2.0"
},
"devDependencies": {
"grunt": "~0.4.2",
"grunt-cli": "~0.1.9",
Expand Down
10 changes: 5 additions & 5 deletions src/main/js/protobufUtils/sketchProtoConverter.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* @return {Message | undefined} decoded protobuf object. (This may return undefined)
*/
var decode = function(data, proto, onError) {
if (module.exports.isUndefined(data) || data === null || typeof data !== 'object') {
if (protobufUtils.isUndefined(data) || data === null || typeof data !== 'object') {
throw 'Data type is not supported:' + typeof data;
}
try {
Expand Down Expand Up @@ -79,11 +79,11 @@
var objectType = object.type;
switch (objectType) {
case ObjectType.SHAPE:
return SrlShape.createFromProtobuf(decode(object.object, ShapeMessage));
return SrlShape.decode(object.object);
case ObjectType.STROKE:
return SrlStroke.createFromProtobuf(decode(object.object, StrokeMessage));
return SrlStroke.decode(object.object);
case ObjectType.POINT:
return SrlPoint.createFromProtobuf(decode(object.object, PointMessage));
return SrlPoint.decode(object.object);
}
};

Expand All @@ -107,7 +107,7 @@
proto.type = ObjectType.POINT;
}

proto.object = object.sendToProtobuf().toArrayBuffer();
proto.object = object.toArrayBuffer();
return proto;
}

Expand Down
80 changes: 80 additions & 0 deletions src/main/js/sketchLibrary/SketchLibraryException.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/**
* Created by dtracers on 5/17/2016.
*/
(function (module) {
var StackTrace = require('stacktrace-js');
var errback = function(err) { console.log(err.message); };
module.exports = function SketchLibraryException(message, cause) {

this.name = 'SketchLibraryException';
/**
* The level defines how bad it is. level 5 is the okayest exception
* (with 6+ typically being ignored completely) and level 0 is the worst
* exception (with <0 being treated as 0).
*/
this.message = undefined;
this.stackTrace = undefined;
this.cause = undefined;

/**
* @returns {String} A string representation of the exception.
*/
this.toString = function() {
return this.name + ': ' + this.message + (this.specificMessage ? '\n' + this.specificMessage : '\n') + this.stackTrace.join('\n\n');
};

/**
* Sets the message of the Exception.
*
* @param {messageValue} messageValue - is a string that contains the description
* of the the exception that occurred.
*/
this.setMessage = function(messageValue) {
this.specificMessage = messageValue;
};

/**
* Used to access the stacktrace of the exception without modifying it.
* @return {stackTrace} Returns a string that contains the entire stacktrace of the exception.
*/
this.getStackTrace = function() {
return this.stackTrace;
};

/**
* Used to log the stacktrace object in BaseException.
*/
this.printStackTrace = function() {
console.log(printStackTrace().join('\n\n'));
};

/**
* Assigns the stacktrace object to an existing stacktrace.
*/
this.createStackTrace = function() {
StackTrace.get().then(function (stackframes) {
this.stackTrace = stackframes;
}).catch(errback);
};

/**
* Sets the cause of baseException to the causeValue passed in.
*
* @param {causeValue} causeValue - Is the cause of the exception.
*/
this.setCause = function(causeValue) {
this.cause = causeValue;
};

/**
* A getter function used to access the cause of the stacktrace without the risk of manipulating it.
*/
this.getCause = function() {
return this.cause;
};

this.setMessage(message);
this.setCause(cause);
this.createStackTrace();
};
})(module);
6 changes: 4 additions & 2 deletions src/main/js/sketchLibrary/SrlPoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

var protoSketch = require("./../generated_proto/sketch");
var protobufUtils = require("./../protobufUtils/classCreator");
var objectConversionUtils = require("./../protobufUtils/sketchProtoConverter");
var SketchException = require('./SketchLibraryException');

var sketch = protoSketch.protobuf.srl.sketch;

Expand Down Expand Up @@ -62,14 +64,14 @@
* @param {Number} x
*/
this.setX = function(x) {
throw "can't call set x must call setP";
throw new SketchException("can't call set x must call setP");
};

/**
* @param {Number} y
*/
this.setY = function(y) {
throw "can't call set y must call setP";
throw new SketchException("can't call set y must call setP");
};

/**
Expand Down
1 change: 1 addition & 0 deletions src/main/js/sketchLibrary/SrlStroke.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

var protoSketch = require("./../generated_proto/sketch");
var protobufUtils = require("./../protobufUtils/classCreator");
var objectConversionUtils = require("./../protobufUtils/sketchProtoConverter");

var sketch = protoSketch.protobuf.srl.sketch;

Expand Down
72 changes: 72 additions & 0 deletions src/test/js/sketchLibrary/SrlPointTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
var assert = require('chai').assert;
var expect = require('chai').expect;

var basePath = '../../../';
var srcPath = 'main/js/';
var SrlPoint = require(basePath + srcPath + 'sketchLibrary/SrlPoint');
var SketchException = require(basePath + srcPath + 'sketchLibrary/SketchLibraryException');
var ProtoPoint = require(basePath + srcPath + 'generated_proto/sketch').protobuf.srl.sketch.SrlPoint;

describe('Point Tests', function () {
var x = 10;
var y = 25.6;
var time = 80;
var id = 'id';
describe('initializations', function () {
it('should be able to create an instance of the point class', function () {
var sketch = new SrlPoint();
});
it('should be able to create an instance of the point class initialized with two values', function () {
var point = new SrlPoint(x, y);
expect(point.getX()).to.equal(x);
expect(point.getY()).to.equal(y);
});
it('set all values of the point', function () {
var point = new SrlPoint(x, y);
point.setTime(time);
point.setId(id);
expect(point.getX()).to.equal(x);
expect(point.getY()).to.equal(y);
expect('' + point.getTime()).to.equal('' + time);
expect(point.getId()).to.equal(id);
});
it('can not set x and y separately', function () {
var point = new SrlPoint(x, y);
expect(function () {
point.setX(5);
}).to.throw(SketchException);
expect(function () {
point.setY(5);
}).to.throw(SketchException);
});
});

describe('protobuf', function () {
it('should be able to create an arraybuffer', function () {
var point = new SrlPoint();
point.setP(x, y);
point.setTime(time);
point.setId(id);
var arrayBuffer = point.toArrayBuffer();
var decoded = ProtoPoint.decode(arrayBuffer);
expect(decoded.getX()).to.equal(x);
expect(decoded.getY()).to.equal(y);
expect('' + decoded.getTime()).to.equal('' + time);
expect(decoded.getId()).to.equal(id);
});

it('should be able to parse an array buffer', function () {
var protoPoint = new ProtoPoint();
protoPoint.setX(x);
protoPoint.setY(y);
protoPoint.setTime(time);
protoPoint.setId(id);
var arrayBuffer = protoPoint.toArrayBuffer();
var decoded = SrlPoint.decode(arrayBuffer);
expect(decoded.getX()).to.equal(x);
expect(decoded.getY()).to.equal(y);
expect('' + decoded.getTime()).to.equal('' + time);
expect(decoded.getId()).to.equal(id);
});
});
});

0 comments on commit a9e5616

Please sign in to comment.