diff --git a/bower.json b/bower.json index 7a480cb..7162b71 100644 --- a/bower.json +++ b/bower.json @@ -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 } diff --git a/package.json b/package.json index 6b5df31..912f849 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/main/js/protobufUtils/sketchProtoConverter.js b/src/main/js/protobufUtils/sketchProtoConverter.js index 9385f08..58b2ccc 100644 --- a/src/main/js/protobufUtils/sketchProtoConverter.js +++ b/src/main/js/protobufUtils/sketchProtoConverter.js @@ -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 { @@ -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); } }; @@ -107,7 +107,7 @@ proto.type = ObjectType.POINT; } - proto.object = object.sendToProtobuf().toArrayBuffer(); + proto.object = object.toArrayBuffer(); return proto; } diff --git a/src/main/js/sketchLibrary/SketchLibraryException.js b/src/main/js/sketchLibrary/SketchLibraryException.js new file mode 100644 index 0000000..993f46c --- /dev/null +++ b/src/main/js/sketchLibrary/SketchLibraryException.js @@ -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); diff --git a/src/main/js/sketchLibrary/SrlPoint.js b/src/main/js/sketchLibrary/SrlPoint.js index 57ec38e..849bba4 100644 --- a/src/main/js/sketchLibrary/SrlPoint.js +++ b/src/main/js/sketchLibrary/SrlPoint.js @@ -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; @@ -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"); }; /** diff --git a/src/main/js/sketchLibrary/SrlStroke.js b/src/main/js/sketchLibrary/SrlStroke.js index b289d3c..557e71a 100644 --- a/src/main/js/sketchLibrary/SrlStroke.js +++ b/src/main/js/sketchLibrary/SrlStroke.js @@ -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; diff --git a/src/test/js/sketchLibrary/SrlPointTest.js b/src/test/js/sketchLibrary/SrlPointTest.js new file mode 100644 index 0000000..d0be38b --- /dev/null +++ b/src/test/js/sketchLibrary/SrlPointTest.js @@ -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); + }); + }); +});