From a7e96eddd1cc6fa37fd22c2ad2deb5fca9d3bb34 Mon Sep 17 00:00:00 2001 From: almchung Date: Wed, 21 Jun 2017 14:20:53 -0700 Subject: [PATCH 01/37] added validateNumParameters in error helpers and applied them in 2d primitives --- src/core/2d_primitives.js | 258 ++++++++++++++++++++------------ src/core/error_helpers.js | 49 +++++- test/js/testRender.js | 2 +- test/test-friendlyerr.html | 81 ++++++++++ test/unit/core/2d_primitives.js | 194 +++++++++++++++++++++--- test/unit/core/error_helpers.js | 56 +++++++ 6 files changed, 520 insertions(+), 120 deletions(-) create mode 100644 test/test-friendlyerr.html diff --git a/src/core/2d_primitives.js b/src/core/2d_primitives.js index 2fc3017fa1..1545a507e4 100644 --- a/src/core/2d_primitives.js +++ b/src/core/2d_primitives.js @@ -75,6 +75,13 @@ p5.prototype.arc = function(x, y, w, h, start, stop, mode) { for (var i = 0; i < args.length; ++i) { args[i] = arguments[i]; } + // check with FES:validateNumericParameters + var validatePar = (this._validateNumParameters('arc', args, 6)); + if (validatePar[0] === false) { + console.log(validatePar[3]); + throw new Error(validatePar[1]); + } + if (!this._renderer._doStroke && !this._renderer._doFill) { return this; } @@ -102,14 +109,14 @@ p5.prototype.arc = function(x, y, w, h, start, stop, mode) { // Adjust angles to counter linear scaling. if (start <= constants.HALF_PI) { start = Math.atan(w / h * Math.tan(start)); - } else if (start > constants.HALF_PI && start <= 3 * constants.HALF_PI) { + } else if (start > constants.HALF_PI && start <= 3 * constants.HALF_PI) { start = Math.atan(w / h * Math.tan(start)) + constants.PI; } else { start = Math.atan(w / h * Math.tan(start)) + constants.TWO_PI; } if (stop <= constants.HALF_PI) { stop = Math.atan(w / h * Math.tan(stop)); - } else if (stop > constants.HALF_PI && stop <= 3 * constants.HALF_PI) { + } else if (stop > constants.HALF_PI && stop <= 3 * constants.HALF_PI) { stop = Math.atan(w / h * Math.tan(stop)) + constants.PI; } else { stop = Math.atan(w / h * Math.tan(stop)) + constants.TWO_PI; @@ -169,9 +176,19 @@ p5.prototype.ellipse = function() { if (args.length === 3) { args.push(args[2]); } + // check with FES:validateNumericParameters + var validatePar = (this._validateNumParameters('ellipse', args, 4)); + if (validatePar[0] === false) { + console.log(validatePar[3]); + throw new Error(validatePar[1]); + } // p5 supports negative width and heights for rects - if (args[2] < 0){args[2] = Math.abs(args[2]);} - if (args[3] < 0){args[3] = Math.abs(args[3]);} + if (args[2] < 0) { + args[2] = Math.abs(args[2]); + } + if (args[3] < 0) { + args[3] = Math.abs(args[3]); + } if (!this._renderer._doStroke && !this._renderer._doFill) { return this; } @@ -242,8 +259,14 @@ p5.prototype.line = function() { for (var i = 0; i < args.length; ++i) { args[i] = arguments[i]; } + var validatePar; //check whether we should draw a 3d line or 2d - if(this._renderer.isP3D){ + if (this._renderer.isP3D) { + validatePar = (this._validateNumParameters('line', args, 6)); + if (validatePar[0] === false) { + console.log(validatePar[3]); + throw new Error(validatePar[1]); + } this._renderer.line( args[0], args[1], @@ -252,6 +275,11 @@ p5.prototype.line = function() { args[4], args[5]); } else { + validatePar = (this._validateNumParameters('line', args, 4)); + if (validatePar[0] === false) { + console.log(validatePar[3]); + throw new Error(validatePar[1]); + } this._renderer.line( args[0], args[1], @@ -293,14 +321,25 @@ p5.prototype.point = function() { for (var i = 0; i < args.length; ++i) { args[i] = arguments[i]; } + var validatePar; //check whether we should draw a 3d line or 2d - if(this._renderer.isP3D){ + if (this._renderer.isP3D) { + validatePar = (this._validateNumParameters('point', args, 3)); + if (validatePar[0] === false) { + console.log(validatePar[3]); + throw new Error(validatePar[1]); + } this._renderer.point( args[0], args[1], args[2] - ); + ); } else { + validatePar = (this._validateNumParameters('point', args, 2)); + if (validatePar[0] === false) { + console.log(validatePar[3]); + throw new Error(validatePar[1]); + } this._renderer.point( args[0], args[1] @@ -358,7 +397,13 @@ p5.prototype.quad = function() { for (var i = 0; i < args.length; ++i) { args[i] = arguments[i]; } - if(this._renderer.isP3D){ + var validatePar; + if (this._renderer.isP3D) { + validatePar = (this._validateNumParameters('quad', args, 12)); + if (validatePar[0] === false) { + console.log(validatePar[3]); + throw new Error(validatePar[1]); + } this._renderer.quad( args[0], args[1], @@ -372,83 +417,88 @@ p5.prototype.quad = function() { args[9], args[10], args[11] - ); + ); } else { + validatePar = (this._validateNumParameters('quad', args, 8)); + if (validatePar[0] === false) { + console.log(validatePar[3]); + throw new Error(validatePar[1]); + } this._renderer.quad( - args[0], - args[1], - args[2], - args[3], - args[4], - args[5], - args[6], - args[7] + args[0], + args[1], + args[2], + args[3], + args[4], + args[5], + args[6], + args[7] ); } return this; }; /** -* Draws a rectangle to the screen. A rectangle is a four-sided shape with -* every angle at ninety degrees. By default, the first two parameters set -* the location of the upper-left corner, the third sets the width, and the -* fourth sets the height. The way these parameters are interpreted, however, -* may be changed with the rectMode() function. -*

-* The fifth, sixth, seventh and eighth parameters, if specified, -* determine corner radius for the top-right, top-left, lower-right and -* lower-left corners, respectively. An omitted corner radius parameter is set -* to the value of the previously specified radius value in the parameter list. -* -* @method rect -* @param {Number} x x-coordinate of the rectangle. -* @param {Number} y y-coordinate of the rectangle. -* @param {Number} w width of the rectangle. -* @param {Number} h height of the rectangle. -* @param {Number} [tl] optional radius of top-left corner. -* @param {Number} [tr] optional radius of top-right corner. -* @param {Number} [br] optional radius of bottom-right corner. -* @param {Number} [bl] optional radius of bottom-left corner. -* @return {p5} the p5 object. -* @example -*
-* -* // Draw a rectangle at location (30, 20) with a width and height of 55. -* rect(30, 20, 55, 55); -* -*
-* -*
-* -* // Draw a rectangle with rounded corners, each having a radius of 20. -* rect(30, 20, 55, 55, 20); -* -*
-* -*
-* -* // Draw a rectangle with rounded corners having the following radii: -* // top-left = 20, top-right = 15, bottom-right = 10, bottom-left = 5. -* rect(30, 20, 55, 55, 20, 15, 10, 5); -* -*
-* -* @alt -* 55x55 white rect with black outline in mid-right of canvas. -* 55x55 white rect with black outline and rounded edges in mid-right of canvas. -* 55x55 white rect with black outline and rounded edges of different radii. -*/ + * Draws a rectangle to the screen. A rectangle is a four-sided shape with + * every angle at ninety degrees. By default, the first two parameters set + * the location of the upper-left corner, the third sets the width, and the + * fourth sets the height. The way these parameters are interpreted, however, + * may be changed with the rectMode() function. + *

+ * The fifth, sixth, seventh and eighth parameters, if specified, + * determine corner radius for the top-right, top-left, lower-right and + * lower-left corners, respectively. An omitted corner radius parameter is set + * to the value of the previously specified radius value in the parameter list. + * + * @method rect + * @param {Number} x x-coordinate of the rectangle. + * @param {Number} y y-coordinate of the rectangle. + * @param {Number} w width of the rectangle. + * @param {Number} h height of the rectangle. + * @param {Number} [tl] optional radius of top-left corner. + * @param {Number} [tr] optional radius of top-right corner. + * @param {Number} [br] optional radius of bottom-right corner. + * @param {Number} [bl] optional radius of bottom-left corner. + * @return {p5} the p5 object. + * @example + *
+ * + * // Draw a rectangle at location (30, 20) with a width and height of 55. + * rect(30, 20, 55, 55); + * + *
+ * + *
+ * + * // Draw a rectangle with rounded corners, each having a radius of 20. + * rect(30, 20, 55, 55, 20); + * + *
+ * + *
+ * + * // Draw a rectangle with rounded corners having the following radii: + * // top-left = 20, top-right = 15, bottom-right = 10, bottom-left = 5. + * rect(30, 20, 55, 55, 20, 15, 10, 5); + * + *
+ * + * @alt + * 55x55 white rect with black outline in mid-right of canvas. + * 55x55 white rect with black outline and rounded edges in mid-right of canvas. + * 55x55 white rect with black outline and rounded edges of different radii. + */ /** -* @method rect -* @param {Number} x -* @param {Number} y -* @param {Number} w -* @param {Number} h -* @param {Number} [detailX] -* @param {Number} [detailY] -* @return {p5} the p5 object. -*/ -p5.prototype.rect = function () { + * @method rect + * @param {Number} x + * @param {Number} y + * @param {Number} w + * @param {Number} h + * @param {Number} [detailX] + * @param {Number} [detailY] + * @return {p5} the p5 object. + */ +p5.prototype.rect = function() { var args = new Array(arguments.length); for (var i = 0; i < args.length; ++i) { args[i] = arguments[i]; @@ -456,6 +506,11 @@ p5.prototype.rect = function () { if (!this._renderer._doStroke && !this._renderer._doFill) { return; } + var validatePar = (this._validateNumParameters('rect', args, 4)); + if (validatePar[0] === false) { + console.log(validatePar[3]); + throw new Error(validatePar[1]); + } var vals = canvas.modeAdjust( args[0], args[1], @@ -471,29 +526,29 @@ p5.prototype.rect = function () { }; /** -* A triangle is a plane created by connecting three points. The first two -* arguments specify the first point, the middle two arguments specify the -* second point, and the last two arguments specify the third point. -* -* @method triangle -* @param {Number} x1 x-coordinate of the first point -* @param {Number} y1 y-coordinate of the first point -* @param {Number} x2 x-coordinate of the second point -* @param {Number} y2 y-coordinate of the second point -* @param {Number} x3 x-coordinate of the third point -* @param {Number} y3 y-coordinate of the third point -* @return {p5} the p5 object -* @example -*
-* -* triangle(30, 75, 58, 20, 86, 75); -* -*
-* -*@alt -* white triangle with black outline in mid-right of canvas. -* -*/ + * A triangle is a plane created by connecting three points. The first two + * arguments specify the first point, the middle two arguments specify the + * second point, and the last two arguments specify the third point. + * + * @method triangle + * @param {Number} x1 x-coordinate of the first point + * @param {Number} y1 y-coordinate of the first point + * @param {Number} x2 x-coordinate of the second point + * @param {Number} y2 y-coordinate of the second point + * @param {Number} x3 x-coordinate of the third point + * @param {Number} y3 y-coordinate of the third point + * @return {p5} the p5 object + * @example + *
+ * + * triangle(30, 75, 58, 20, 86, 75); + * + *
+ * + *@alt + * white triangle with black outline in mid-right of canvas. + * + */ p5.prototype.triangle = function() { if (!this._renderer._doStroke && !this._renderer._doFill) { @@ -503,6 +558,11 @@ p5.prototype.triangle = function() { for (var i = 0; i < args.length; ++i) { args[i] = arguments[i]; } + var validatePar = (this._validateNumParameters('line', args, 6)); + if (validatePar[0] === false) { + console.log(validatePar[3]); + throw new Error(validatePar[1]); + } this._renderer.triangle(args); return this; }; diff --git a/src/core/error_helpers.js b/src/core/error_helpers.js index fefe9ca448..a5673fd0fe 100644 --- a/src/core/error_helpers.js +++ b/src/core/error_helpers.js @@ -104,7 +104,53 @@ p5._friendlyFileLoadError = function (errorType, filePath) { (errorInfo.message || '') + ' or running a local server.'; report(message, errorInfo.method, FILE_LOAD); }; - +function validateNumParameters(func, input, length) { + /** + * Numberic Type checking + * + * Example: "It looks like ellipse received an empty variable in spot #2." + * Example: "ellipse was expecting a number for parameter #1, + * received "foo" instead." + */ + var message; + if (arguments.length < 3) { + message = 'Missing input values for validating numeric parameters'; + return [false, 'INIT_VALNUMPAR_FAIL', 0, message]; + } + var args = new Array(length); + for (var i = 0; i < input.length; ++i) { + args[i] = input[i]; + } + for (var p = 0; p < length; p++) { + var argType = typeof(args[p]); + message = ''; + if ('undefined' === argType || null === argType) { + message = 'FES: It looks like ' + func + + ' received an empty variable in spot #' + p + + ' (zero-based index). If not intentional, this is often a problem' + + ' with scope: [link to scope].'; + return [false, 'EMPTY_VAR', p, message]; + // report('It looks like ' + func + + // ' received an empty variable in spot #' + (p+1) + + // '. If not intentional, this is often a problem with scope: ' + + // '[link to scope].', func, EMPTY_VAR); + } else if (argType !== 'number') { + message = 'FES: ' + func + ' was expecting a number' + + ' for parameter #' + p + ' (zero-based index), received '; + // Wrap strings in quotes + message += 'string' === argType ? '"' + args[p] + '"' : args[p]; + message += ' instead.'; + return [false, 'WRONG_TYPE', p, message]; + // message = func + ' was expecting a number'+ + // ' for parameter #' + (p+1) + ', received '; + // // Wrap strings in quotes + // message += 'string' === argType ? '"' + args[p] + '"' : args[p]; + // message += ' instead.'; + // report(message, func, WRONG_TYPE); + } + } + return [true]; +} function friendlyWelcome() { // p5.js brand - magenta: #ED225D var astrixBgColor = 'transparent'; @@ -253,6 +299,7 @@ function helpForMisusedAtTopLevelCode(e, log) { // Exposing this primarily for unit testing. p5.prototype._helpForMisusedAtTopLevelCode = helpForMisusedAtTopLevelCode; +p5.prototype._validateNumParameters = validateNumParameters; if (document.readyState !== 'complete') { window.addEventListener('error', helpForMisusedAtTopLevelCode, false); diff --git a/test/js/testRender.js b/test/js/testRender.js index 2e8a3b12f6..e78e602001 100644 --- a/test/js/testRender.js +++ b/test/js/testRender.js @@ -24,4 +24,4 @@ var testRender = function(file, sketch, callback) { same = same && (ctx.pixels.length === p.length); callback(same); }); -} \ No newline at end of file +} diff --git a/test/test-friendlyerr.html b/test/test-friendlyerr.html new file mode 100644 index 0000000000..e4cc698396 --- /dev/null +++ b/test/test-friendlyerr.html @@ -0,0 +1,81 @@ + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/test/unit/core/2d_primitives.js b/test/unit/core/2d_primitives.js index bedb270967..021baf298d 100644 --- a/test/unit/core/2d_primitives.js +++ b/test/unit/core/2d_primitives.js @@ -1,14 +1,63 @@ suite('2D Primitives', function() { - var myp5 = new p5(function( p ) { + var myp5 = new p5(function(p) { p.setup = function() {}; p.draw = function() {}; }); - teardown(function(){ + if (!window.Modernizr.webgl) { + return; + } + + // var myp5webgl; + // + // setup(function() { + // myp5webgl = new p5(function(p) { + // p.setup = function() { + // p.createCanvas(100, 100, p.WEBGL); + // }; + // }); + // }); + + teardown(function() { myp5.clear(); }); + suite('p5.prototype.arc', function() { + var arc = p5.prototype.arc; + suite('arc()', function() { + test('should be a function', function() { + assert.ok(arc); + assert.typeOf(arc, 'function'); + }); + test('parameter check and no err (with MODE value)', function() { + assert.doesNotThrow(function() { + myp5.arc(1, 1, 10.5, 10, 0, Math.PI, 'pie'); + }, + Error, 'got unwanted exception'); + }); + test('parameter check and no err (without MODE value)', function() { + assert.doesNotThrow( + function() { + myp5.arc(1, 1, 10.5, 10, 0, Math.PI); + }, + Error, 'got unwanted exception'); + }); + test('parameter check and throws err (empty variable)', function() { + assert.throws(function() { + myp5.arc(1, 1, 10.5, 10); + }, + /EMPTY_VAR/, + 'did not throw with expected message!'); + }); + test('parameter check and throws err (wrong type)', function() { + assert.throws(function() { + myp5.arc('1', 1, 10.5, 10, 0, Math.PI, 'pie'); + }, + /WRONG_TYPE/, 'did not throw with expected message!'); + }); + }); + }); suite('p5.prototype.ellipse', function() { var ellipse = p5.prototype.ellipse; suite('ellipse()', function() { @@ -16,15 +65,38 @@ suite('2D Primitives', function() { assert.ok(ellipse); assert.typeOf(ellipse, 'function'); }); - test('should draw', function(done) { - myp5.background(155); - myp5.fill(0); - myp5.ellipse(0, 0, 100, 100); - - testRender('unit/assets/renders/ellipse.png', myp5, function(res) { - assert.isTrue(res); - done(); - }); + // test('should draw', function(done) { + // myp5.background(155); + // myp5.fill(0); + // myp5.ellipse(0, 0, 100, 100); + // + // testRender('unit/assets/renders/ellipse.png', myp5, function(res) { + // assert.isTrue(res); + // done(); + // }); + // }); + test('parameter check and no err', function() { + assert.doesNotThrow( + function() { + myp5.ellipse(0, 10.5, 30); + }, + Error, 'got unwanted exception'); + }); + test('parameter check and throws err (empty variable)', function() { + assert.throws(function() { + myp5.ellipse(0, 10.5); + }, + /EMPTY_VAR/, 'did not throw with expected message!'); + }); + test('parameter check and throws err (wrong type I)', function() { + assert.throws(function() { + myp5.ellipse('0', 10.5, 30); + }, /WRONG_TYPE/, 'did not throw with expected message!'); + }); + test('parameter check and throws err (wrong type II)', function() { + assert.throws(function() { + myp5.ellipse(0, 10.5, '30'); + }, /WRONG_TYPE/, 'did not throw with expected message!'); }); }); }); @@ -36,17 +108,101 @@ suite('2D Primitives', function() { assert.ok(line); assert.typeOf(line, 'function'); }); - test('should draw', function(done) { - myp5.background(155); - myp5.fill(0); - myp5.line(0, 0, 100, 100); + // test('should draw', function(done) { + // myp5.background(155); + // myp5.fill(0); + // myp5.line(0, 0, 100, 100); + // + // testRender('unit/assets/renders/line.png', myp5, function(res) { + // assert.isTrue(res); + // done(); + // }); + // }); + test('parameter check and no err 2D', function() { + assert.doesNotThrow(function() { + myp5.line(0, 0.5, 10, 10.5); + }, + Error, 'got unwanted exception'); + }); + // test('parameter check and no err 3D', function() { + // assert.doesNotThrow(function() { + // myp5webgl.line(0, 0.5, 10, 10.5, 20, 20.5); + // }, + // Error, 'got unwanted exception'); + // }); + test('parameter check and throws err (empty variable)', function() { + assert.throws(function() { + myp5.line(0, 0.5, 10); + }, + /EMPTY_VAR/, 'did not throw with expected message!'); + }); + // test('parameter check and throws err (empty variable)', function() { + // assert.throws(function() { + // myp5.line(0, 0.5, 10, 10.5, 20); + // }, + // /EMPTY_VAR/, 'did not throw with expected message!'); + // }); + test('parameter check and throws err (wrong type)', function() { + assert.throws(function() { + myp5.line(0, 10, '30', 30.5); + }, /WRONG_TYPE/, 'did not throw with expected message!'); + }); + }); + }); - testRender('unit/assets/renders/line.png', myp5, function(res) { - assert.isTrue(res); - done(); - }); + suite('p5.prototype.rect', function() { + var rect = p5.prototype.rect; + suite('rect()', function() { + test('should be a function', function() { + assert.ok(rect); + assert.typeOf(rect, 'function'); + }); + test('parameter check and no err', function() { + assert.doesNotThrow( + function() { + myp5.rect(0, 10.5, 30, -20); + }, + Error, 'got unwanted exception'); + }); + test('parameter check and throws err (empty variable)', function() { + assert.throws(function() { + myp5.rect(0, 10.5, 30); + }, + /EMPTY_VAR/, 'did not throw with expected message!'); + }); + test('parameter check and throws err (wrong type)', function() { + assert.throws(function() { + myp5.rect(0, 10.5, '30', -20); + }, /WRONG_TYPE/, 'did not throw with expected message!'); }); }); }); + suite('p5.prototype.triangle', function() { + var triangle = p5.prototype.triangle; + suite('triangle()', function() { + test('should be a function', function() { + assert.ok(triangle); + assert.typeOf(triangle, 'function'); + }); + test('parameter check and no err', function() { + assert.doesNotThrow( + function() { + myp5.triangle(0, 10.5, 30, -20, 10, 10); + }, + Error, 'got unwanted exception'); + }); + test('parameter check and throws err (empty variable)', function() { + assert.throws(function() { + myp5.triangle(0, 10.5, 30, -20, 10); + }, + /EMPTY_VAR/, 'did not throw with expected message!'); + }); + test('parameter check and throws err (wrong type)', function() { + assert.throws(function() { + myp5.triangle(0, 10.5, '30', -20, 10, 10); + }, /WRONG_TYPE/, 'did not throw with expected message!'); + }); + }); + }); }); diff --git a/test/unit/core/error_helpers.js b/test/unit/core/error_helpers.js index 232df112b1..10bf0a3464 100644 --- a/test/unit/core/error_helpers.js +++ b/test/unit/core/error_helpers.js @@ -1,4 +1,60 @@ suite('Error Helpers', function() { + var myp5 = new p5(function( sketch ) { + sketch.setup = function() {}; + sketch.draw = function() {}; + }); + var c; + // add unit test for validateNumParameters inputs + suite('validateNumParameters', function(){ + test('catch if some inputs are missing',function(){ + var result = p5.prototype._validateNumParameters('func',[1,1,1,1]); + assert.isFalse(result[0]); + assert.equal(result[1],'INIT_VALNUMPAR_FAIL'); + }); + }); + suite('validate numeric parameters (all Number type)', function() { + test('four number inputs', function() { + var result = p5.prototype._validateNumParameters('func',[1,1,1,1],4); + assert.isTrue(result[0]); + }); + test('undefined parameter', function() { + var num; + var result = p5.prototype._validateNumParameters('func',[1,num,1,1],4); + assert.isFalse(result[0]); + assert.equal(result[1],'EMPTY_VAR'); + assert.equal(result[2],1); + }); + test('string parameter', function() { + var num = 'string'; + var result = p5.prototype._validateNumParameters('func',[1,num,1,1],4); + assert.isFalse(result[0]); + assert.equal(result[1],'WRONG_TYPE'); + assert.equal(result[2],1); + }); + test('array parameter', function() { + var num = ['value', 'value', 'value']; + var result = p5.prototype._validateNumParameters('func',[1,num,1,1],4); + assert.isFalse(result[0]); + assert.equal(result[1],'WRONG_TYPE'); + assert.equal(result[2],1); + }); + test('boolean parameter', function() { + var num = false; + var result = p5.prototype._validateNumParameters('func',[1,num,1,1],4); + assert.isFalse(result[0]); + assert.equal(result[1],'WRONG_TYPE'); + assert.equal(result[2],1); + }); + setup(function() { + c = myp5.color(255, 0, 102); + }); + test('p5 defined object', function() { + var result = p5.prototype._validateNumParameters('func',[1,c,1,1],4); + assert.isFalse(result[0]); + assert.equal(result[1],'WRONG_TYPE'); + assert.equal(result[2],1); + }); + }); suite('helpForMisusedAtTopLevelCode', function() { var help = function(msg) { var log = []; From 1a6d804fe1ea03ffe71a7a3b0390009b4c2ed3a0 Mon Sep 17 00:00:00 2001 From: almchung Date: Wed, 21 Jun 2017 16:06:15 -0700 Subject: [PATCH 02/37] added examples and cleaned up --- .gitignore | 3 +- src/core/error_helpers.js | 32 +++++++------ test/test-friendlyerr.html | 81 --------------------------------- test/unit/core/2d_primitives.js | 10 ---- 4 files changed, 21 insertions(+), 105 deletions(-) delete mode 100644 test/test-friendlyerr.html diff --git a/.gitignore b/.gitignore index c286bcc2e8..4fdcb507dc 100644 --- a/.gitignore +++ b/.gitignore @@ -10,4 +10,5 @@ docs/yuidoc-p5-theme/assets/js/reference.js* !*.gitkeep examples/3d/ .idea -.vscode \ No newline at end of file +.vscode +test/test-friendlyerr.html diff --git a/src/core/error_helpers.js b/src/core/error_helpers.js index a5673fd0fe..2739e03bc7 100644 --- a/src/core/error_helpers.js +++ b/src/core/error_helpers.js @@ -106,10 +106,26 @@ p5._friendlyFileLoadError = function (errorType, filePath) { }; function validateNumParameters(func, input, length) { /** - * Numberic Type checking + * Validates Number type parameters + * param {String} message the words to be said + * param {String} func the name of the function to link + * param {Integer/Color String} color CSS color string or error type * - * Example: "It looks like ellipse received an empty variable in spot #2." - * Example: "ellipse was expecting a number for parameter #1, + * return {boolean} message the words to be said + * return {String} err the type of error + * return {Integer} index the location of error + * return {String} message friendly err console log + * + * example: + * var a; + * ellipse(10,10,a,5); + * console ouput: + * "It looks like ellipse received an empty variable in spot #2." + * + * example: + * ellipse(10,"foo",5,5); + * console output: + * "ellipse was expecting a number for parameter #1, * received "foo" instead." */ var message; @@ -130,10 +146,6 @@ function validateNumParameters(func, input, length) { ' (zero-based index). If not intentional, this is often a problem' + ' with scope: [link to scope].'; return [false, 'EMPTY_VAR', p, message]; - // report('It looks like ' + func + - // ' received an empty variable in spot #' + (p+1) + - // '. If not intentional, this is often a problem with scope: ' + - // '[link to scope].', func, EMPTY_VAR); } else if (argType !== 'number') { message = 'FES: ' + func + ' was expecting a number' + ' for parameter #' + p + ' (zero-based index), received '; @@ -141,12 +153,6 @@ function validateNumParameters(func, input, length) { message += 'string' === argType ? '"' + args[p] + '"' : args[p]; message += ' instead.'; return [false, 'WRONG_TYPE', p, message]; - // message = func + ' was expecting a number'+ - // ' for parameter #' + (p+1) + ', received '; - // // Wrap strings in quotes - // message += 'string' === argType ? '"' + args[p] + '"' : args[p]; - // message += ' instead.'; - // report(message, func, WRONG_TYPE); } } return [true]; diff --git a/test/test-friendlyerr.html b/test/test-friendlyerr.html deleted file mode 100644 index e4cc698396..0000000000 --- a/test/test-friendlyerr.html +++ /dev/null @@ -1,81 +0,0 @@ - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/test/unit/core/2d_primitives.js b/test/unit/core/2d_primitives.js index 021baf298d..4eb3ab783c 100644 --- a/test/unit/core/2d_primitives.js +++ b/test/unit/core/2d_primitives.js @@ -9,16 +9,6 @@ suite('2D Primitives', function() { return; } - // var myp5webgl; - // - // setup(function() { - // myp5webgl = new p5(function(p) { - // p.setup = function() { - // p.createCanvas(100, 100, p.WEBGL); - // }; - // }); - // }); - teardown(function() { myp5.clear(); }); From 057db87b0cde187ace9219caa8241734117e049c Mon Sep 17 00:00:00 2001 From: almchung Date: Thu, 22 Jun 2017 10:23:04 -0700 Subject: [PATCH 03/37] clean up --- test/unit/core/2d_primitives.js | 52 +++++++++++++-------------------- 1 file changed, 20 insertions(+), 32 deletions(-) diff --git a/test/unit/core/2d_primitives.js b/test/unit/core/2d_primitives.js index 4eb3ab783c..1e84adbfb6 100644 --- a/test/unit/core/2d_primitives.js +++ b/test/unit/core/2d_primitives.js @@ -55,16 +55,16 @@ suite('2D Primitives', function() { assert.ok(ellipse); assert.typeOf(ellipse, 'function'); }); - // test('should draw', function(done) { - // myp5.background(155); - // myp5.fill(0); - // myp5.ellipse(0, 0, 100, 100); - // - // testRender('unit/assets/renders/ellipse.png', myp5, function(res) { - // assert.isTrue(res); - // done(); - // }); - // }); + test('should draw', function(done) { + myp5.background(155); + myp5.fill(0); + myp5.ellipse(0, 0, 100, 100); + + testRender('unit/assets/renders/ellipse.png', myp5, function(res) { + assert.isTrue(res); + done(); + }); + }); test('parameter check and no err', function() { assert.doesNotThrow( function() { @@ -98,40 +98,28 @@ suite('2D Primitives', function() { assert.ok(line); assert.typeOf(line, 'function'); }); - // test('should draw', function(done) { - // myp5.background(155); - // myp5.fill(0); - // myp5.line(0, 0, 100, 100); - // - // testRender('unit/assets/renders/line.png', myp5, function(res) { - // assert.isTrue(res); - // done(); - // }); - // }); + test('should draw', function(done) { + myp5.background(155); + myp5.fill(0); + myp5.line(0, 0, 100, 100); + + testRender('unit/assets/renders/line.png', myp5, function(res) { + assert.isTrue(res); + done(); + }); + }); test('parameter check and no err 2D', function() { assert.doesNotThrow(function() { myp5.line(0, 0.5, 10, 10.5); }, Error, 'got unwanted exception'); }); - // test('parameter check and no err 3D', function() { - // assert.doesNotThrow(function() { - // myp5webgl.line(0, 0.5, 10, 10.5, 20, 20.5); - // }, - // Error, 'got unwanted exception'); - // }); test('parameter check and throws err (empty variable)', function() { assert.throws(function() { myp5.line(0, 0.5, 10); }, /EMPTY_VAR/, 'did not throw with expected message!'); }); - // test('parameter check and throws err (empty variable)', function() { - // assert.throws(function() { - // myp5.line(0, 0.5, 10, 10.5, 20); - // }, - // /EMPTY_VAR/, 'did not throw with expected message!'); - // }); test('parameter check and throws err (wrong type)', function() { assert.throws(function() { myp5.line(0, 10, '30', 30.5); From 8fa3e395421292938fca6f4aa45d3b3e43884916 Mon Sep 17 00:00:00 2001 From: almchung Date: Thu, 22 Jun 2017 11:31:02 -0700 Subject: [PATCH 04/37] validateNumParameters and unit tests cleaned up --- .gitignore | 1 - src/core/2d_primitives.js | 175 ++++++++++++++++---------------- src/core/error_helpers.js | 47 ++++----- test/js/testRender.js | 2 +- test/unit/core/2d_primitives.js | 8 +- test/unit/core/error_helpers.js | 4 +- 6 files changed, 116 insertions(+), 121 deletions(-) diff --git a/.gitignore b/.gitignore index 4fdcb507dc..3bb3076115 100644 --- a/.gitignore +++ b/.gitignore @@ -11,4 +11,3 @@ docs/yuidoc-p5-theme/assets/js/reference.js* examples/3d/ .idea .vscode -test/test-friendlyerr.html diff --git a/src/core/2d_primitives.js b/src/core/2d_primitives.js index 1545a507e4..43cf7cf12b 100644 --- a/src/core/2d_primitives.js +++ b/src/core/2d_primitives.js @@ -81,7 +81,6 @@ p5.prototype.arc = function(x, y, w, h, start, stop, mode) { console.log(validatePar[3]); throw new Error(validatePar[1]); } - if (!this._renderer._doStroke && !this._renderer._doFill) { return this; } @@ -109,14 +108,14 @@ p5.prototype.arc = function(x, y, w, h, start, stop, mode) { // Adjust angles to counter linear scaling. if (start <= constants.HALF_PI) { start = Math.atan(w / h * Math.tan(start)); - } else if (start > constants.HALF_PI && start <= 3 * constants.HALF_PI) { + } else if (start > constants.HALF_PI && start <= 3 * constants.HALF_PI) { start = Math.atan(w / h * Math.tan(start)) + constants.PI; } else { start = Math.atan(w / h * Math.tan(start)) + constants.TWO_PI; } if (stop <= constants.HALF_PI) { stop = Math.atan(w / h * Math.tan(stop)); - } else if (stop > constants.HALF_PI && stop <= 3 * constants.HALF_PI) { + } else if (stop > constants.HALF_PI && stop <= 3 * constants.HALF_PI) { stop = Math.atan(w / h * Math.tan(stop)) + constants.PI; } else { stop = Math.atan(w / h * Math.tan(stop)) + constants.TWO_PI; @@ -183,12 +182,8 @@ p5.prototype.ellipse = function() { throw new Error(validatePar[1]); } // p5 supports negative width and heights for rects - if (args[2] < 0) { - args[2] = Math.abs(args[2]); - } - if (args[3] < 0) { - args[3] = Math.abs(args[3]); - } + if (args[2] < 0) {args[2] = Math.abs(args[2]);} + if (args[3] < 0) {args[3] = Math.abs(args[3]);} if (!this._renderer._doStroke && !this._renderer._doFill) { return this; } @@ -439,65 +434,65 @@ p5.prototype.quad = function() { }; /** - * Draws a rectangle to the screen. A rectangle is a four-sided shape with - * every angle at ninety degrees. By default, the first two parameters set - * the location of the upper-left corner, the third sets the width, and the - * fourth sets the height. The way these parameters are interpreted, however, - * may be changed with the rectMode() function. - *

- * The fifth, sixth, seventh and eighth parameters, if specified, - * determine corner radius for the top-right, top-left, lower-right and - * lower-left corners, respectively. An omitted corner radius parameter is set - * to the value of the previously specified radius value in the parameter list. - * - * @method rect - * @param {Number} x x-coordinate of the rectangle. - * @param {Number} y y-coordinate of the rectangle. - * @param {Number} w width of the rectangle. - * @param {Number} h height of the rectangle. - * @param {Number} [tl] optional radius of top-left corner. - * @param {Number} [tr] optional radius of top-right corner. - * @param {Number} [br] optional radius of bottom-right corner. - * @param {Number} [bl] optional radius of bottom-left corner. - * @return {p5} the p5 object. - * @example - *
- * - * // Draw a rectangle at location (30, 20) with a width and height of 55. - * rect(30, 20, 55, 55); - * - *
- * - *
- * - * // Draw a rectangle with rounded corners, each having a radius of 20. - * rect(30, 20, 55, 55, 20); - * - *
- * - *
- * - * // Draw a rectangle with rounded corners having the following radii: - * // top-left = 20, top-right = 15, bottom-right = 10, bottom-left = 5. - * rect(30, 20, 55, 55, 20, 15, 10, 5); - * - *
- * - * @alt - * 55x55 white rect with black outline in mid-right of canvas. - * 55x55 white rect with black outline and rounded edges in mid-right of canvas. - * 55x55 white rect with black outline and rounded edges of different radii. - */ +* Draws a rectangle to the screen. A rectangle is a four-sided shape with +* every angle at ninety degrees. By default, the first two parameters set +* the location of the upper-left corner, the third sets the width, and the +* fourth sets the height. The way these parameters are interpreted, however, +* may be changed with the rectMode() function. +*

+* The fifth, sixth, seventh and eighth parameters, if specified, +* determine corner radius for the top-right, top-left, lower-right and +* lower-left corners, respectively. An omitted corner radius parameter is set +* to the value of the previously specified radius value in the parameter list. +* +* @method rect +* @param {Number} x x-coordinate of the rectangle. +* @param {Number} y y-coordinate of the rectangle. +* @param {Number} w width of the rectangle. +* @param {Number} h height of the rectangle. +* @param {Number} [tl] optional radius of top-left corner. +* @param {Number} [tr] optional radius of top-right corner. +* @param {Number} [br] optional radius of bottom-right corner. +* @param {Number} [bl] optional radius of bottom-left corner. +* @return {p5} the p5 object. +* @example +*
+* +* // Draw a rectangle at location (30, 20) with a width and height of 55. +* rect(30, 20, 55, 55); +* +*
+* +*
+* +* // Draw a rectangle with rounded corners, each having a radius of 20. +* rect(30, 20, 55, 55, 20); +* +*
+* +*
+* +* // Draw a rectangle with rounded corners having the following radii: +* // top-left = 20, top-right = 15, bottom-right = 10, bottom-left = 5. +* rect(30, 20, 55, 55, 20, 15, 10, 5); +* +*
+* +* @alt +* 55x55 white rect with black outline in mid-right of canvas. +* 55x55 white rect with black outline and rounded edges in mid-right of canvas. +* 55x55 white rect with black outline and rounded edges of different radii. +*/ /** - * @method rect - * @param {Number} x - * @param {Number} y - * @param {Number} w - * @param {Number} h - * @param {Number} [detailX] - * @param {Number} [detailY] - * @return {p5} the p5 object. - */ +* @method rect +* @param {Number} x +* @param {Number} y +* @param {Number} w +* @param {Number} h +* @param {Number} [detailX] +* @param {Number} [detailY] +* @return {p5} the p5 object. +*/ p5.prototype.rect = function() { var args = new Array(arguments.length); for (var i = 0; i < args.length; ++i) { @@ -526,29 +521,29 @@ p5.prototype.rect = function() { }; /** - * A triangle is a plane created by connecting three points. The first two - * arguments specify the first point, the middle two arguments specify the - * second point, and the last two arguments specify the third point. - * - * @method triangle - * @param {Number} x1 x-coordinate of the first point - * @param {Number} y1 y-coordinate of the first point - * @param {Number} x2 x-coordinate of the second point - * @param {Number} y2 y-coordinate of the second point - * @param {Number} x3 x-coordinate of the third point - * @param {Number} y3 y-coordinate of the third point - * @return {p5} the p5 object - * @example - *
- * - * triangle(30, 75, 58, 20, 86, 75); - * - *
- * - *@alt - * white triangle with black outline in mid-right of canvas. - * - */ +* A triangle is a plane created by connecting three points. The first two +* arguments specify the first point, the middle two arguments specify the +* second point, and the last two arguments specify the third point. +* +* @method triangle +* @param {Number} x1 x-coordinate of the first point +* @param {Number} y1 y-coordinate of the first point +* @param {Number} x2 x-coordinate of the second point +* @param {Number} y2 y-coordinate of the second point +* @param {Number} x3 x-coordinate of the third point +* @param {Number} y3 y-coordinate of the third point +* @return {p5} the p5 object +* @example +*
+* +* triangle(30, 75, 58, 20, 86, 75); +* +*
+* +*@alt +* white triangle with black outline in mid-right of canvas. +* +*/ p5.prototype.triangle = function() { if (!this._renderer._doStroke && !this._renderer._doFill) { diff --git a/src/core/error_helpers.js b/src/core/error_helpers.js index 2739e03bc7..4bea4ed36c 100644 --- a/src/core/error_helpers.js +++ b/src/core/error_helpers.js @@ -104,30 +104,31 @@ p5._friendlyFileLoadError = function (errorType, filePath) { (errorInfo.message || '') + ' or running a local server.'; report(message, errorInfo.method, FILE_LOAD); }; + +/** +* Validates Number type parameters +* param {String} message the words to be said +* param {String} func the name of the function to link +* param {Integer/Color String} color CSS color string or error type +* +* return {boolean} message the words to be said +* return {String} err the type of error +* return {Integer} index the location of error +* return {String} message friendly err console log +* +* example: +* var a; +* ellipse(10,10,a,5); +* console ouput: +* "It looks like ellipse received an empty variable in spot #2." +* +* example: +* ellipse(10,"foo",5,5); +* console output: +* "ellipse was expecting a number for parameter #1, +* received "foo" instead." +*/ function validateNumParameters(func, input, length) { - /** - * Validates Number type parameters - * param {String} message the words to be said - * param {String} func the name of the function to link - * param {Integer/Color String} color CSS color string or error type - * - * return {boolean} message the words to be said - * return {String} err the type of error - * return {Integer} index the location of error - * return {String} message friendly err console log - * - * example: - * var a; - * ellipse(10,10,a,5); - * console ouput: - * "It looks like ellipse received an empty variable in spot #2." - * - * example: - * ellipse(10,"foo",5,5); - * console output: - * "ellipse was expecting a number for parameter #1, - * received "foo" instead." - */ var message; if (arguments.length < 3) { message = 'Missing input values for validating numeric parameters'; diff --git a/test/js/testRender.js b/test/js/testRender.js index e78e602001..2e8a3b12f6 100644 --- a/test/js/testRender.js +++ b/test/js/testRender.js @@ -24,4 +24,4 @@ var testRender = function(file, sketch, callback) { same = same && (ctx.pixels.length === p.length); callback(same); }); -} +} \ No newline at end of file diff --git a/test/unit/core/2d_primitives.js b/test/unit/core/2d_primitives.js index 1e84adbfb6..f19fc70a71 100644 --- a/test/unit/core/2d_primitives.js +++ b/test/unit/core/2d_primitives.js @@ -1,14 +1,10 @@ suite('2D Primitives', function() { - var myp5 = new p5(function(p) { + var myp5 = new p5(function( p ) { p.setup = function() {}; p.draw = function() {}; }); - if (!window.Modernizr.webgl) { - return; - } - teardown(function() { myp5.clear(); }); @@ -48,6 +44,7 @@ suite('2D Primitives', function() { }); }); }); + suite('p5.prototype.ellipse', function() { var ellipse = p5.prototype.ellipse; suite('ellipse()', function() { @@ -183,4 +180,5 @@ suite('2D Primitives', function() { }); }); }); + }); diff --git a/test/unit/core/error_helpers.js b/test/unit/core/error_helpers.js index 10bf0a3464..8bbe59abbd 100644 --- a/test/unit/core/error_helpers.js +++ b/test/unit/core/error_helpers.js @@ -4,7 +4,8 @@ suite('Error Helpers', function() { sketch.draw = function() {}; }); var c; - // add unit test for validateNumParameters inputs + + // unit tests for validateNumParameters suite('validateNumParameters', function(){ test('catch if some inputs are missing',function(){ var result = p5.prototype._validateNumParameters('func',[1,1,1,1]); @@ -55,6 +56,7 @@ suite('Error Helpers', function() { assert.equal(result[2],1); }); }); + suite('helpForMisusedAtTopLevelCode', function() { var help = function(msg) { var log = []; From 7bc00d0b49acd1109ed0172da8be601e53f66452 Mon Sep 17 00:00:00 2001 From: almchung Date: Thu, 22 Jun 2017 11:34:41 -0700 Subject: [PATCH 05/37] validateNumParameters and unit tests cleaned up --- .gitignore | 2 +- src/core/2d_primitives.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 3bb3076115..c286bcc2e8 100644 --- a/.gitignore +++ b/.gitignore @@ -10,4 +10,4 @@ docs/yuidoc-p5-theme/assets/js/reference.js* !*.gitkeep examples/3d/ .idea -.vscode +.vscode \ No newline at end of file diff --git a/src/core/2d_primitives.js b/src/core/2d_primitives.js index 43cf7cf12b..d3a1dc015c 100644 --- a/src/core/2d_primitives.js +++ b/src/core/2d_primitives.js @@ -182,8 +182,8 @@ p5.prototype.ellipse = function() { throw new Error(validatePar[1]); } // p5 supports negative width and heights for rects - if (args[2] < 0) {args[2] = Math.abs(args[2]);} - if (args[3] < 0) {args[3] = Math.abs(args[3]);} + if (args[2] < 0){args[2] = Math.abs(args[2]);} + if (args[3] < 0){args[3] = Math.abs(args[3]);} if (!this._renderer._doStroke && !this._renderer._doFill) { return this; } From 91f4688318ac7eb57ecaec1972d6e2df3305ec61 Mon Sep 17 00:00:00 2001 From: almchung Date: Mon, 26 Jun 2017 16:24:15 -0700 Subject: [PATCH 06/37] parameter checking based on data.json --- src/core/error_helpers.js | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/core/error_helpers.js b/src/core/error_helpers.js index 4bea4ed36c..70fa1268bf 100644 --- a/src/core/error_helpers.js +++ b/src/core/error_helpers.js @@ -107,11 +107,11 @@ p5._friendlyFileLoadError = function (errorType, filePath) { /** * Validates Number type parameters -* param {String} message the words to be said -* param {String} func the name of the function to link -* param {Integer/Color String} color CSS color string or error type +* param {String} func the name of the function +* param {String} input the input of the function +* param {Integer} length number of parameters to check * -* return {boolean} message the words to be said +* return {boolean} message returns true if validated * return {String} err the type of error * return {Integer} index the location of error * return {String} message friendly err console log @@ -134,7 +134,6 @@ function validateNumParameters(func, input, length) { message = 'Missing input values for validating numeric parameters'; return [false, 'INIT_VALNUMPAR_FAIL', 0, message]; } - var args = new Array(length); for (var i = 0; i < input.length; ++i) { args[i] = input[i]; } @@ -158,6 +157,12 @@ function validateNumParameters(func, input, length) { } return [true]; } +function validateParameters(func){ + var message; + var queryResult = jsonArray. + filter(function (x) { return x.name == func && x.itemtype == "method"; }); + console.log(queryResult); +} function friendlyWelcome() { // p5.js brand - magenta: #ED225D var astrixBgColor = 'transparent'; From c8b8753e4567ce8acb81d1c90bf73befeabc9822 Mon Sep 17 00:00:00 2001 From: almchung Date: Mon, 26 Jun 2017 16:24:32 -0700 Subject: [PATCH 07/37] parameter checking based on data.json --- src/color/p5.Color.js | 2 +- src/core/error_helpers.js | 100 ++++++++++++++++++++++++-------- src/core/p5.Element.js | 1 + src/core/p5.Graphics.js | 2 +- src/core/p5.Renderer.js | 1 + src/core/p5.Renderer2D.js | 1 + src/image/p5.Image.js | 1 + src/io/p5.Table.js | 1 + src/io/p5.TableRow.js | 1 + src/io/p5.XML.js | 1 + src/math/p5.Vector.js | 1 + src/typography/p5.Font.js | 1 + src/webgl/p5.Geometry.js | 1 + src/webgl/p5.Matrix.js | 1 + src/webgl/p5.RendererGL.js | 1 + test/unit/core/error_helpers.js | 32 ++++++++++ 16 files changed, 122 insertions(+), 26 deletions(-) diff --git a/src/color/p5.Color.js b/src/color/p5.Color.js index 33458912b9..0b73949e59 100644 --- a/src/color/p5.Color.js +++ b/src/color/p5.Color.js @@ -47,7 +47,7 @@ p5.Color = function(renderer, vals) { this.levels = this._array.map(function(level) { return Math.round(level * 255); }); - + this.name = 'p5.Color'; // for friendly debugger system return this; }; diff --git a/src/core/error_helpers.js b/src/core/error_helpers.js index 70fa1268bf..cebb38bac1 100644 --- a/src/core/error_helpers.js +++ b/src/core/error_helpers.js @@ -7,6 +7,10 @@ var p5 = require('./core'); var doFriendlyWelcome = false; // TEMP until we get it all working LM +// for parameter validation +var dataDoc = require('../../docs/reference/data.json'); +var strDoc = JSON.stringify(dataDoc); +var arrDoc = JSON.parse(strDoc); // -- Borrowed from jQuery 1.11.3 -- var class2type = {}; @@ -39,6 +43,7 @@ var getType = function( obj ) { */ // Wrong number of params, undefined param, wrong type var FILE_LOAD = 3; +var ERR_PARAMS = 3; // p5.js blue, p5.js orange, auto dark green; fallback p5.js darkened magenta // See testColors below for all the color codes and names var typeColors = ['#2D7BB6', '#EE9900', '#4DB200', '#C83C00']; @@ -53,23 +58,23 @@ function report(message, func, color) { color = typeColors[color]; } // LM TEMP commenting this out until we get the whole system working - // if (func.substring(0,4) === 'load'){ - // console.log( - // '%c> p5.js says: '+message+'%c'+ - // '[https://github.com/processing/p5.js/wiki/Local-server]', - // 'background-color:' + color + ';color:#FFF;', - // 'background-color:transparent;color:' + color +';', - // 'background-color:' + color + ';color:#FFF;', - // 'background-color:transparent;color:' + color +';' - // ); - // } - // else{ - // console.log( - // '%c> p5.js says: '+message+'%c [http://p5js.org/reference/#p5/'+func+ - // ']', 'background-color:' + color + ';color:#FFF;', - // 'background-color:transparent;color:' + color +';' - // ); - // } + if (func.substring(0,4) === 'load'){ + console.log( + '%c> p5.js says: '+message+'%c'+ + '[https://github.com/processing/p5.js/wiki/Local-server]', + 'background-color:' + color + ';color:#FFF;', + 'background-color:transparent;color:' + color +';', + 'background-color:' + color + ';color:#FFF;', + 'background-color:transparent;color:' + color +';' + ); + } + else{ + console.log( + '%c> p5.js says: '+message+'%c [http://p5js.org/reference/#p5/'+func+ + ']', 'background-color:' + color + ';color:#FFF;', + 'background-color:transparent;color:' + color +';' + ); + } } var errorCases = { @@ -128,15 +133,12 @@ p5._friendlyFileLoadError = function (errorType, filePath) { * "ellipse was expecting a number for parameter #1, * received "foo" instead." */ -function validateNumParameters(func, input, length) { +function validateNumParameters(func, args, length) { var message; if (arguments.length < 3) { message = 'Missing input values for validating numeric parameters'; return [false, 'INIT_VALNUMPAR_FAIL', 0, message]; } - for (var i = 0; i < input.length; ++i) { - args[i] = input[i]; - } for (var p = 0; p < length; p++) { var argType = typeof(args[p]); message = ''; @@ -157,11 +159,60 @@ function validateNumParameters(func, input, length) { } return [true]; } -function validateParameters(func){ +function validateParameters(func, args) { + var arrDoc = lookupDoc(func); var message; - var queryResult = jsonArray. - filter(function (x) { return x.name == func && x.itemtype == "method"; }); + for (var p = 0; p < arrDoc.length; p++) { + var argType = typeof(args[p]); + if ('undefined' === argType || null === argType) { + if (arrDoc[p].optional !== true) { + message = 'FES: It looks like ' + func + + ' received an empty variable in spot #' + p + + ' (zero-based index). If not intentional, this is often a problem' + + ' with scope: [link to scope].'; + report(message, func, ERR_PARAMS); + } + } else { + var count = 0; + var types = arrDoc[p].type.split('|'); // for multi-type parameters + for (var i = 0; i < types.length; i++) { + //console.log(i + ' : ' + argType + ' : ' + types[i]); + if (argType === types[i].toLowerCase()) { + count = count + 1; // type match, pass + } else if (argType === 'object'){ + if (args[p].name === types[i]){ + count = count + 1; // class match, pass + } else if (args[p].name === 'undefined'){ + message = 'FES: ' + func + ' was expecting ' + arrDoc[p].type + + ' for parameter #' + p + ' (zero-based index), received '; + // Wrap strings in quotes + message += 'an object with undefined name instead.'; + report(message, func, ERR_PARAMS); + } + } else if (types[i] === 'Constants'){ + count = count + 1; // if not undefined, pass + } + } + if (count < 1) { // for any cases with at least one pass + message = 'FES: ' + func + ' was expecting ' + arrDoc[p].type + + ' for parameter #' + p + ' (zero-based index), received '; + // Wrap strings in quotes + message += 'string' === argType ? '"' + args[p] + '"' : args[p]; + message += ' instead.'; + report(message, func, ERR_PARAMS); + } + } + } +} +function lookupDoc(func){ + console.log('>> checking documentation for '+func+'()'); + var queryResult = arrDoc.classitems. + filter(function (x) { return x.name === func; }); console.log(queryResult); + if (queryResult.length !== 1){ + console.log('>>>> ERROR: wrong number of query results'); + } + return queryResult[0].params; } function friendlyWelcome() { // p5.js brand - magenta: #ED225D @@ -312,6 +363,7 @@ function helpForMisusedAtTopLevelCode(e, log) { // Exposing this primarily for unit testing. p5.prototype._helpForMisusedAtTopLevelCode = helpForMisusedAtTopLevelCode; p5.prototype._validateNumParameters = validateNumParameters; +p5.prototype._validateParameters = validateParameters; if (document.readyState !== 'complete') { window.addEventListener('error', helpForMisusedAtTopLevelCode, false); diff --git a/src/core/p5.Element.js b/src/core/p5.Element.js index e80b0d0e70..f76b575ab7 100644 --- a/src/core/p5.Element.js +++ b/src/core/p5.Element.js @@ -32,6 +32,7 @@ p5.Element = function(elt, pInst) { this._events = {}; this.width = this.elt.offsetWidth; this.height = this.elt.offsetHeight; + this.name = 'p5.Element'; // for friendly debugger system }; /** diff --git a/src/core/p5.Graphics.js b/src/core/p5.Graphics.js index 5b94c92b15..eacc88ea9e 100644 --- a/src/core/p5.Graphics.js +++ b/src/core/p5.Graphics.js @@ -56,7 +56,7 @@ p5.Graphics = function(w, h, renderer, pInst) { } } } - + this.name = 'p5.Graphics'; // for friendly debugger system return this; }; diff --git a/src/core/p5.Renderer.js b/src/core/p5.Renderer.js index deeaf9b46d..f82bfaa0f2 100644 --- a/src/core/p5.Renderer.js +++ b/src/core/p5.Renderer.js @@ -21,6 +21,7 @@ var constants = require('../core/constants'); */ p5.Renderer = function(elt, pInst, isMainCanvas) { p5.Element.call(this, elt, pInst); + this.name = 'p5.Renderer'; // for friendly debugger system this.canvas = elt; this._pInst = pInst; if (isMainCanvas) { diff --git a/src/core/p5.Renderer2D.js b/src/core/p5.Renderer2D.js index 97177728ed..012bd23d74 100644 --- a/src/core/p5.Renderer2D.js +++ b/src/core/p5.Renderer2D.js @@ -16,6 +16,7 @@ var styleEmpty = 'rgba(0,0,0,0)'; p5.Renderer2D = function(elt, pInst, isMainCanvas){ p5.Renderer.call(this, elt, pInst, isMainCanvas); + this.name = 'p5.Renderer2D'; // for friendly debugger system this.drawingContext = this.canvas.getContext('2d'); this._pInst._setProperty('drawingContext', this.drawingContext); return this; diff --git a/src/image/p5.Image.js b/src/image/p5.Image.js index de941e9882..9aef3037c7 100644 --- a/src/image/p5.Image.js +++ b/src/image/p5.Image.js @@ -170,6 +170,7 @@ p5.Image = function(width, height){ * */ this.pixels = []; + this.name = 'p5.Image'; // for friendly debugger system }; /** diff --git a/src/io/p5.Table.js b/src/io/p5.Table.js index a28f36009d..2f3c009aa7 100644 --- a/src/io/p5.Table.js +++ b/src/io/p5.Table.js @@ -55,6 +55,7 @@ p5.Table = function (rows) { * @type {Array} */ this.rows = []; + this.name = 'p5.Table'; // for friendly debugger system }; /** diff --git a/src/io/p5.TableRow.js b/src/io/p5.TableRow.js index c697296da8..b200bf8b06 100644 --- a/src/io/p5.TableRow.js +++ b/src/io/p5.TableRow.js @@ -37,6 +37,7 @@ p5.TableRow = function (str, separator) { this.arr = arr; this.obj = obj; this.table = null; + this.name = 'p5.TableRow'; // for friendly debugger system }; /** diff --git a/src/io/p5.XML.js b/src/io/p5.XML.js index cdc657a282..9d867268ec 100644 --- a/src/io/p5.XML.js +++ b/src/io/p5.XML.js @@ -60,6 +60,7 @@ p5.XML = function () { this.children = []; this.parent = null; this.content = null; //done + this.name = 'p5.XML'; // for friendly debugger system }; diff --git a/src/math/p5.Vector.js b/src/math/p5.Vector.js index 09a0d4d0dc..9903a13c55 100644 --- a/src/math/p5.Vector.js +++ b/src/math/p5.Vector.js @@ -84,6 +84,7 @@ p5.Vector = function() { * @type {Number} */ this.z = z; + this.name = 'p5.Vector'; // for friendly debugger system }; /** diff --git a/src/typography/p5.Font.js b/src/typography/p5.Font.js index 6ec4f0457d..5a0f47d8f2 100644 --- a/src/typography/p5.Font.js +++ b/src/typography/p5.Font.js @@ -35,6 +35,7 @@ p5.Font = function(p) { * @property font */ this.font = undefined; + this.name = 'p5.Font'; // for friendly debugger system }; p5.Font.prototype.list = function() { diff --git a/src/webgl/p5.Geometry.js b/src/webgl/p5.Geometry.js index 333a2bc698..5df026ff63 100644 --- a/src/webgl/p5.Geometry.js +++ b/src/webgl/p5.Geometry.js @@ -34,6 +34,7 @@ p5.Geometry = function if(callback instanceof Function){ callback.call(this); } + this.name = 'p5.Geometry'; // for friendly debugger system return this; }; diff --git a/src/webgl/p5.Matrix.js b/src/webgl/p5.Matrix.js index 403dec58d9..4a1296abd7 100644 --- a/src/webgl/p5.Matrix.js +++ b/src/webgl/p5.Matrix.js @@ -68,6 +68,7 @@ p5.Matrix = function() { ]); } } + this.name = 'p5.Matrix'; // for friendly debugger system return this; }; diff --git a/src/webgl/p5.RendererGL.js b/src/webgl/p5.RendererGL.js index 76fff2e5c7..f46c22adb7 100755 --- a/src/webgl/p5.RendererGL.js +++ b/src/webgl/p5.RendererGL.js @@ -56,6 +56,7 @@ p5.RendererGL = function(elt, pInst, isMainCanvas) { this.curFillColor = [0.5,0.5,0.5,1.0]; this.curStrokeColor = [0.5,0.5,0.5,1.0]; this.pointSize = 5.0;//default point/stroke + this.name = 'p5.RendererGL'; // for friendly debugger system return this; }; diff --git a/test/unit/core/error_helpers.js b/test/unit/core/error_helpers.js index 8bbe59abbd..7644e99bf7 100644 --- a/test/unit/core/error_helpers.js +++ b/test/unit/core/error_helpers.js @@ -5,6 +5,38 @@ suite('Error Helpers', function() { }); var c; + // unit tests for validateParameters + suite('validateParameters', function(){ + test('documentation look up - arc()', function() { + assert.doesNotThrow(function() { + p5.prototype._validateParameters('arc', + [1, 1, 10.5, 10, 0, Math.PI, 'pie']); + }, + Error, 'got unwanted exception'); + }); + test('documentation look up - arc()', function() { + assert.doesNotThrow(function() { + p5.prototype._validateParameters('arc', + [1, 1, 10.5, 10]); + }, + Error, 'got unwanted exception'); + }); + test('documentation look up - arc()', function() { + assert.doesNotThrow(function() { + p5.prototype._validateParameters('arc', + ['1', 1, 10.5, 10, 0, Math.PI, 'pie']); + }, + Error, 'got unwanted exception'); + }); + test('documentation look up - ambientLight()', function() { + assert.doesNotThrow(function() { + var c = myp5.color(255, 204, 0); + p5.prototype._validateParameters('ambientLight', [c]); + }, + Error, 'got unwanted exception'); + }); + }); + // unit tests for validateNumParameters suite('validateNumParameters', function(){ test('catch if some inputs are missing',function(){ From 09dbce963b66a6ad5304652e9672294aaef68e82 Mon Sep 17 00:00:00 2001 From: almchung Date: Mon, 26 Jun 2017 17:13:31 -0700 Subject: [PATCH 08/37] small bug-fix and clean up --- src/core/error_helpers.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/core/error_helpers.js b/src/core/error_helpers.js index cebb38bac1..bc3b0547cd 100644 --- a/src/core/error_helpers.js +++ b/src/core/error_helpers.js @@ -189,7 +189,7 @@ function validateParameters(func, args) { message += 'an object with undefined name instead.'; report(message, func, ERR_PARAMS); } - } else if (types[i] === 'Constants'){ + } else if (types[i] === 'Constant'){ count = count + 1; // if not undefined, pass } } @@ -205,10 +205,8 @@ function validateParameters(func, args) { } } function lookupDoc(func){ - console.log('>> checking documentation for '+func+'()'); var queryResult = arrDoc.classitems. filter(function (x) { return x.name === func; }); - console.log(queryResult); if (queryResult.length !== 1){ console.log('>>>> ERROR: wrong number of query results'); } From 6f3c9060331b1425bd468db41d8ef447f667c8c7 Mon Sep 17 00:00:00 2001 From: almchung Date: Tue, 27 Jun 2017 15:03:12 -0700 Subject: [PATCH 09/37] removed validateNumParameters() and re-arranged validateParameters() --- src/core/error_helpers.js | 88 ++++++++++++++++----------- test/unit/core/2d_primitives.js | 102 -------------------------------- test/unit/core/error_helpers.js | 66 +++------------------ 3 files changed, 60 insertions(+), 196 deletions(-) diff --git a/src/core/error_helpers.js b/src/core/error_helpers.js index bc3b0547cd..82de478527 100644 --- a/src/core/error_helpers.js +++ b/src/core/error_helpers.js @@ -111,15 +111,9 @@ p5._friendlyFileLoadError = function (errorType, filePath) { }; /** -* Validates Number type parameters +* Validates parameters * param {String} func the name of the function -* param {String} input the input of the function -* param {Integer} length number of parameters to check -* -* return {boolean} message returns true if validated -* return {String} err the type of error -* return {Integer} index the location of error -* return {String} message friendly err console log +* param {Array} args user input arguments * * example: * var a; @@ -160,7 +154,7 @@ function validateNumParameters(func, args, length) { return [true]; } function validateParameters(func, args) { - var arrDoc = lookupDoc(func); + var arrDoc = lookupParamDoc(func); var message; for (var p = 0; p < arrDoc.length; p++) { var argType = typeof(args[p]); @@ -173,38 +167,34 @@ function validateParameters(func, args) { report(message, func, ERR_PARAMS); } } else { - var count = 0; - var types = arrDoc[p].type.split('|'); // for multi-type parameters - for (var i = 0; i < types.length; i++) { - //console.log(i + ' : ' + argType + ' : ' + types[i]); - if (argType === types[i].toLowerCase()) { - count = count + 1; // type match, pass - } else if (argType === 'object'){ - if (args[p].name === types[i]){ - count = count + 1; // class match, pass - } else if (args[p].name === 'undefined'){ - message = 'FES: ' + func + ' was expecting ' + arrDoc[p].type + - ' for parameter #' + p + ' (zero-based index), received '; - // Wrap strings in quotes - message += 'an object with undefined name instead.'; - report(message, func, ERR_PARAMS); - } - } else if (types[i] === 'Constant'){ - count = count + 1; // if not undefined, pass + var types = arrDoc[p].type.split('|'); // case accepting multi-types + var pass; + if (argType === 'object'){ // if obejct, test for class + pass = testParamClass(args[p], types); + if (!pass) { // if fails to pass + message = 'FES: ' + func + ' was expecting ' + arrDoc[p].type + + ' for parameter #' + p + ' (zero-based index), received '; + // Wrap strings in quotes + message += 'an object with name '+ args[p].name +' instead.'; + report(message, func, ERR_PARAMS); + } + }else{ // not object, test for type + pass = testParamType(args[p], types); + if (!pass) { // if fails to pass + message = 'FES: ' + func + ' was expecting ' + arrDoc[p].type + + ' for parameter #' + p + ' (zero-based index), received '; + // Wrap strings in quotes + message += 'string' === argType ? '"' + args[p] + '"' : args[p]; + message += ' instead.'; + report(message, func, ERR_PARAMS); } - } - if (count < 1) { // for any cases with at least one pass - message = 'FES: ' + func + ' was expecting ' + arrDoc[p].type + - ' for parameter #' + p + ' (zero-based index), received '; - // Wrap strings in quotes - message += 'string' === argType ? '"' + args[p] + '"' : args[p]; - message += ' instead.'; - report(message, func, ERR_PARAMS); } } } } -function lookupDoc(func){ +// validateParameters() helper functions: +// lookupParamDoc() for querying data.json +function lookupParamDoc(func){ var queryResult = arrDoc.classitems. filter(function (x) { return x.name === func; }); if (queryResult.length !== 1){ @@ -212,6 +202,32 @@ function lookupDoc(func){ } return queryResult[0].params; } +// testClass() for object type parameter validation +// Returns true if PASS, false if FAIL +function testParamClass(param, types){ + var result = false; + for (var i = 0; i < types.length; i++) { + if (param.name === types[i]) { + result = true; // class name match, pass + } else if (types[i] === 'Constant'){ + result = true; // accepts any constant, pass + } + } + return result; +} +// testType() for non-object type parameter validation +// Returns true if PASS, false if FAIL +function testParamType(param, types){ + var result = false; + for (var i = 0; i < types.length; i++) { + if (typeof(param) === types[i].toLowerCase()) { + result = true; // type match, pass + } else if (types[i] === 'Constant'){ + result = true; // accepts any constant, pass + } + } + return result; +} function friendlyWelcome() { // p5.js brand - magenta: #ED225D var astrixBgColor = 'transparent'; diff --git a/test/unit/core/2d_primitives.js b/test/unit/core/2d_primitives.js index f19fc70a71..8b657cc995 100644 --- a/test/unit/core/2d_primitives.js +++ b/test/unit/core/2d_primitives.js @@ -16,32 +16,6 @@ suite('2D Primitives', function() { assert.ok(arc); assert.typeOf(arc, 'function'); }); - test('parameter check and no err (with MODE value)', function() { - assert.doesNotThrow(function() { - myp5.arc(1, 1, 10.5, 10, 0, Math.PI, 'pie'); - }, - Error, 'got unwanted exception'); - }); - test('parameter check and no err (without MODE value)', function() { - assert.doesNotThrow( - function() { - myp5.arc(1, 1, 10.5, 10, 0, Math.PI); - }, - Error, 'got unwanted exception'); - }); - test('parameter check and throws err (empty variable)', function() { - assert.throws(function() { - myp5.arc(1, 1, 10.5, 10); - }, - /EMPTY_VAR/, - 'did not throw with expected message!'); - }); - test('parameter check and throws err (wrong type)', function() { - assert.throws(function() { - myp5.arc('1', 1, 10.5, 10, 0, Math.PI, 'pie'); - }, - /WRONG_TYPE/, 'did not throw with expected message!'); - }); }); }); @@ -62,29 +36,6 @@ suite('2D Primitives', function() { done(); }); }); - test('parameter check and no err', function() { - assert.doesNotThrow( - function() { - myp5.ellipse(0, 10.5, 30); - }, - Error, 'got unwanted exception'); - }); - test('parameter check and throws err (empty variable)', function() { - assert.throws(function() { - myp5.ellipse(0, 10.5); - }, - /EMPTY_VAR/, 'did not throw with expected message!'); - }); - test('parameter check and throws err (wrong type I)', function() { - assert.throws(function() { - myp5.ellipse('0', 10.5, 30); - }, /WRONG_TYPE/, 'did not throw with expected message!'); - }); - test('parameter check and throws err (wrong type II)', function() { - assert.throws(function() { - myp5.ellipse(0, 10.5, '30'); - }, /WRONG_TYPE/, 'did not throw with expected message!'); - }); }); }); @@ -105,23 +56,6 @@ suite('2D Primitives', function() { done(); }); }); - test('parameter check and no err 2D', function() { - assert.doesNotThrow(function() { - myp5.line(0, 0.5, 10, 10.5); - }, - Error, 'got unwanted exception'); - }); - test('parameter check and throws err (empty variable)', function() { - assert.throws(function() { - myp5.line(0, 0.5, 10); - }, - /EMPTY_VAR/, 'did not throw with expected message!'); - }); - test('parameter check and throws err (wrong type)', function() { - assert.throws(function() { - myp5.line(0, 10, '30', 30.5); - }, /WRONG_TYPE/, 'did not throw with expected message!'); - }); }); }); @@ -132,24 +66,6 @@ suite('2D Primitives', function() { assert.ok(rect); assert.typeOf(rect, 'function'); }); - test('parameter check and no err', function() { - assert.doesNotThrow( - function() { - myp5.rect(0, 10.5, 30, -20); - }, - Error, 'got unwanted exception'); - }); - test('parameter check and throws err (empty variable)', function() { - assert.throws(function() { - myp5.rect(0, 10.5, 30); - }, - /EMPTY_VAR/, 'did not throw with expected message!'); - }); - test('parameter check and throws err (wrong type)', function() { - assert.throws(function() { - myp5.rect(0, 10.5, '30', -20); - }, /WRONG_TYPE/, 'did not throw with expected message!'); - }); }); }); @@ -160,24 +76,6 @@ suite('2D Primitives', function() { assert.ok(triangle); assert.typeOf(triangle, 'function'); }); - test('parameter check and no err', function() { - assert.doesNotThrow( - function() { - myp5.triangle(0, 10.5, 30, -20, 10, 10); - }, - Error, 'got unwanted exception'); - }); - test('parameter check and throws err (empty variable)', function() { - assert.throws(function() { - myp5.triangle(0, 10.5, 30, -20, 10); - }, - /EMPTY_VAR/, 'did not throw with expected message!'); - }); - test('parameter check and throws err (wrong type)', function() { - assert.throws(function() { - myp5.triangle(0, 10.5, '30', -20, 10, 10); - }, /WRONG_TYPE/, 'did not throw with expected message!'); - }); }); }); diff --git a/test/unit/core/error_helpers.js b/test/unit/core/error_helpers.js index 7644e99bf7..443c209242 100644 --- a/test/unit/core/error_helpers.js +++ b/test/unit/core/error_helpers.js @@ -3,32 +3,34 @@ suite('Error Helpers', function() { sketch.setup = function() {}; sketch.draw = function() {}; }); - var c; // unit tests for validateParameters - suite('validateParameters', function(){ - test('documentation look up - arc()', function() { + suite('validateParameters: Numbers + optional Constant', function(){ + test('arc(): no friendly-err-msg', function() { assert.doesNotThrow(function() { p5.prototype._validateParameters('arc', [1, 1, 10.5, 10, 0, Math.PI, 'pie']); }, Error, 'got unwanted exception'); }); - test('documentation look up - arc()', function() { + test('arc(): missing param #4, #5', function() { assert.doesNotThrow(function() { p5.prototype._validateParameters('arc', [1, 1, 10.5, 10]); }, Error, 'got unwanted exception'); }); - test('documentation look up - arc()', function() { + test('arc(): wrong param type at #0', function() { assert.doesNotThrow(function() { p5.prototype._validateParameters('arc', ['1', 1, 10.5, 10, 0, Math.PI, 'pie']); }, Error, 'got unwanted exception'); }); - test('documentation look up - ambientLight()', function() { + }); + + suite('validateParameters: p5.Color|String + optional Numbers', function(){ + test('ambientLight(): no friendly-err-msg', function() { assert.doesNotThrow(function() { var c = myp5.color(255, 204, 0); p5.prototype._validateParameters('ambientLight', [c]); @@ -37,58 +39,6 @@ suite('Error Helpers', function() { }); }); - // unit tests for validateNumParameters - suite('validateNumParameters', function(){ - test('catch if some inputs are missing',function(){ - var result = p5.prototype._validateNumParameters('func',[1,1,1,1]); - assert.isFalse(result[0]); - assert.equal(result[1],'INIT_VALNUMPAR_FAIL'); - }); - }); - suite('validate numeric parameters (all Number type)', function() { - test('four number inputs', function() { - var result = p5.prototype._validateNumParameters('func',[1,1,1,1],4); - assert.isTrue(result[0]); - }); - test('undefined parameter', function() { - var num; - var result = p5.prototype._validateNumParameters('func',[1,num,1,1],4); - assert.isFalse(result[0]); - assert.equal(result[1],'EMPTY_VAR'); - assert.equal(result[2],1); - }); - test('string parameter', function() { - var num = 'string'; - var result = p5.prototype._validateNumParameters('func',[1,num,1,1],4); - assert.isFalse(result[0]); - assert.equal(result[1],'WRONG_TYPE'); - assert.equal(result[2],1); - }); - test('array parameter', function() { - var num = ['value', 'value', 'value']; - var result = p5.prototype._validateNumParameters('func',[1,num,1,1],4); - assert.isFalse(result[0]); - assert.equal(result[1],'WRONG_TYPE'); - assert.equal(result[2],1); - }); - test('boolean parameter', function() { - var num = false; - var result = p5.prototype._validateNumParameters('func',[1,num,1,1],4); - assert.isFalse(result[0]); - assert.equal(result[1],'WRONG_TYPE'); - assert.equal(result[2],1); - }); - setup(function() { - c = myp5.color(255, 0, 102); - }); - test('p5 defined object', function() { - var result = p5.prototype._validateNumParameters('func',[1,c,1,1],4); - assert.isFalse(result[0]); - assert.equal(result[1],'WRONG_TYPE'); - assert.equal(result[2],1); - }); - }); - suite('helpForMisusedAtTopLevelCode', function() { var help = function(msg) { var log = []; From 3eed2e1f97d454214b2da16a485b0e3b182bc4f5 Mon Sep 17 00:00:00 2001 From: almchung Date: Tue, 27 Jun 2017 20:45:31 -0700 Subject: [PATCH 10/37] removed validateNumParameters() and re-arranged validateParameters() --- src/core/2d_primitives.js | 66 +++------------ src/core/error_helpers.js | 144 ++++++++++++++++++++------------ test/unit/core/error_helpers.js | 24 ++++++ 3 files changed, 124 insertions(+), 110 deletions(-) diff --git a/src/core/2d_primitives.js b/src/core/2d_primitives.js index d3a1dc015c..c5c8fadc72 100644 --- a/src/core/2d_primitives.js +++ b/src/core/2d_primitives.js @@ -75,12 +75,8 @@ p5.prototype.arc = function(x, y, w, h, start, stop, mode) { for (var i = 0; i < args.length; ++i) { args[i] = arguments[i]; } - // check with FES:validateNumericParameters - var validatePar = (this._validateNumParameters('arc', args, 6)); - if (validatePar[0] === false) { - console.log(validatePar[3]); - throw new Error(validatePar[1]); - } + // check with FES:validateParameters + this._validateParameters('arc', args); if (!this._renderer._doStroke && !this._renderer._doFill) { return this; } @@ -175,12 +171,8 @@ p5.prototype.ellipse = function() { if (args.length === 3) { args.push(args[2]); } - // check with FES:validateNumericParameters - var validatePar = (this._validateNumParameters('ellipse', args, 4)); - if (validatePar[0] === false) { - console.log(validatePar[3]); - throw new Error(validatePar[1]); - } + // check with FES:validateParameters + this._validateParameters('ellipse', args); // p5 supports negative width and heights for rects if (args[2] < 0){args[2] = Math.abs(args[2]);} if (args[3] < 0){args[3] = Math.abs(args[3]);} @@ -254,14 +246,9 @@ p5.prototype.line = function() { for (var i = 0; i < args.length; ++i) { args[i] = arguments[i]; } - var validatePar; + // check with FES:validateParameters - not implemented yet //check whether we should draw a 3d line or 2d if (this._renderer.isP3D) { - validatePar = (this._validateNumParameters('line', args, 6)); - if (validatePar[0] === false) { - console.log(validatePar[3]); - throw new Error(validatePar[1]); - } this._renderer.line( args[0], args[1], @@ -270,11 +257,6 @@ p5.prototype.line = function() { args[4], args[5]); } else { - validatePar = (this._validateNumParameters('line', args, 4)); - if (validatePar[0] === false) { - console.log(validatePar[3]); - throw new Error(validatePar[1]); - } this._renderer.line( args[0], args[1], @@ -316,25 +298,15 @@ p5.prototype.point = function() { for (var i = 0; i < args.length; ++i) { args[i] = arguments[i]; } - var validatePar; + // check with FES:validateParameters - not implemented yet //check whether we should draw a 3d line or 2d if (this._renderer.isP3D) { - validatePar = (this._validateNumParameters('point', args, 3)); - if (validatePar[0] === false) { - console.log(validatePar[3]); - throw new Error(validatePar[1]); - } this._renderer.point( args[0], args[1], args[2] ); } else { - validatePar = (this._validateNumParameters('point', args, 2)); - if (validatePar[0] === false) { - console.log(validatePar[3]); - throw new Error(validatePar[1]); - } this._renderer.point( args[0], args[1] @@ -392,13 +364,8 @@ p5.prototype.quad = function() { for (var i = 0; i < args.length; ++i) { args[i] = arguments[i]; } - var validatePar; + // check with FES:validateParameters - not implemented yet if (this._renderer.isP3D) { - validatePar = (this._validateNumParameters('quad', args, 12)); - if (validatePar[0] === false) { - console.log(validatePar[3]); - throw new Error(validatePar[1]); - } this._renderer.quad( args[0], args[1], @@ -414,11 +381,6 @@ p5.prototype.quad = function() { args[11] ); } else { - validatePar = (this._validateNumParameters('quad', args, 8)); - if (validatePar[0] === false) { - console.log(validatePar[3]); - throw new Error(validatePar[1]); - } this._renderer.quad( args[0], args[1], @@ -501,11 +463,8 @@ p5.prototype.rect = function() { if (!this._renderer._doStroke && !this._renderer._doFill) { return; } - var validatePar = (this._validateNumParameters('rect', args, 4)); - if (validatePar[0] === false) { - console.log(validatePar[3]); - throw new Error(validatePar[1]); - } + // check with FES:validateParameters - not implemented yet + this._validateParameters('rect', args); var vals = canvas.modeAdjust( args[0], args[1], @@ -553,11 +512,8 @@ p5.prototype.triangle = function() { for (var i = 0; i < args.length; ++i) { args[i] = arguments[i]; } - var validatePar = (this._validateNumParameters('line', args, 6)); - if (validatePar[0] === false) { - console.log(validatePar[3]); - throw new Error(validatePar[1]); - } + // check with FES:validateParameters - not implemented yet + this._validateParameters('triangle', args); this._renderer.triangle(args); return this; }; diff --git a/src/core/error_helpers.js b/src/core/error_helpers.js index 82de478527..32e90eb355 100644 --- a/src/core/error_helpers.js +++ b/src/core/error_helpers.js @@ -127,80 +127,90 @@ p5._friendlyFileLoadError = function (errorType, filePath) { * "ellipse was expecting a number for parameter #1, * received "foo" instead." */ -function validateNumParameters(func, args, length) { - var message; - if (arguments.length < 3) { - message = 'Missing input values for validating numeric parameters'; - return [false, 'INIT_VALNUMPAR_FAIL', 0, message]; +function validateParameters(func, args) { + var arrDoc = lookupParamDoc(func); + if (arrDoc.length > 1){ // multiple format? + var errorListArray = []; + for (var i = 0; i < arrDoc.length; i++) { + errorListArray.push(testParamFormat(args, arrDoc[i])); + } + // compare errors from all formats + var minErrInd = -1; + var minErrCount = 0; + for (var j = 0; j < errorListArray.length; j++) { + var numErr = errorListArray[j].length; + if (numErr > 0){ + if(minErrInd > -1){ // non-first cases w errors + if(minErrCount > numErr){ + minErrInd = j; + } + }else{ // the first case w errors + minErrCount = numErr; + minErrInd = j; + } + } + } + if (minErrInd > -1){ + // generate error msg with a smaller number of errors + for(var n = 0; n < errorListArray[minErrInd].length; n++) { + p5._friendlyParamError(errorListArray[minErrInd][n], func); + } + } + } else { + var errorArray = testParamFormat(args, arrDoc[0]); + if (errorArray.length > 0){ + for(var m = 0; m < errorArray.length; m++) { + p5._friendlyParamError(errorArray[m], func); + } + } } - for (var p = 0; p < length; p++) { - var argType = typeof(args[p]); - message = ''; - if ('undefined' === argType || null === argType) { - message = 'FES: It looks like ' + func + - ' received an empty variable in spot #' + p + - ' (zero-based index). If not intentional, this is often a problem' + - ' with scope: [link to scope].'; - return [false, 'EMPTY_VAR', p, message]; - } else if (argType !== 'number') { - message = 'FES: ' + func + ' was expecting a number' + - ' for parameter #' + p + ' (zero-based index), received '; - // Wrap strings in quotes - message += 'string' === argType ? '"' + args[p] + '"' : args[p]; - message += ' instead.'; - return [false, 'WRONG_TYPE', p, message]; +} +// validateParameters() helper functions: +// lookupParamDoc() for querying data.json +function lookupParamDoc(func){ + var queryResult = arrDoc.classitems. + filter(function (x) { return x.name === func; }); + if (queryResult[0].hasOwnProperty('overloads')){ + var res = []; + for(var i = 0; i < queryResult[0].overloads.length; i++) { + res.push(queryResult[0].overloads[i].params); } + return res; + } else { + return [queryResult[0].params]; } - return [true]; } -function validateParameters(func, args) { - var arrDoc = lookupParamDoc(func); - var message; - for (var p = 0; p < arrDoc.length; p++) { +function testParamFormat(args, format){ + var errorArray = []; + var error; + for (var p = 0; p < format.length; p++) { var argType = typeof(args[p]); if ('undefined' === argType || null === argType) { - if (arrDoc[p].optional !== true) { - message = 'FES: It looks like ' + func + - ' received an empty variable in spot #' + p + - ' (zero-based index). If not intentional, this is often a problem' + - ' with scope: [link to scope].'; - report(message, func, ERR_PARAMS); + if (format[p].optional !== true) { + error = {type:'EMPTY_VAR', position: p}; + errorArray.push(error); } } else { - var types = arrDoc[p].type.split('|'); // case accepting multi-types + var types = format[p].type.split('|'); // case accepting multi-types var pass; if (argType === 'object'){ // if obejct, test for class pass = testParamClass(args[p], types); if (!pass) { // if fails to pass - message = 'FES: ' + func + ' was expecting ' + arrDoc[p].type + - ' for parameter #' + p + ' (zero-based index), received '; - // Wrap strings in quotes - message += 'an object with name '+ args[p].name +' instead.'; - report(message, func, ERR_PARAMS); + error = {type:'WRONG_CLASS', position: p, + correctClass: types[p], wrongClass: args[p].name}; + errorArray.push(error); } }else{ // not object, test for type pass = testParamType(args[p], types); if (!pass) { // if fails to pass - message = 'FES: ' + func + ' was expecting ' + arrDoc[p].type + - ' for parameter #' + p + ' (zero-based index), received '; - // Wrap strings in quotes - message += 'string' === argType ? '"' + args[p] + '"' : args[p]; - message += ' instead.'; - report(message, func, ERR_PARAMS); + error = {type:'WRONG_TYPE', position: p, + correctType: types[p], wrongType: argType}; + errorArray.push(error); } } } } -} -// validateParameters() helper functions: -// lookupParamDoc() for querying data.json -function lookupParamDoc(func){ - var queryResult = arrDoc.classitems. - filter(function (x) { return x.name === func; }); - if (queryResult.length !== 1){ - console.log('>>>> ERROR: wrong number of query results'); - } - return queryResult[0].params; + return errorArray; } // testClass() for object type parameter validation // Returns true if PASS, false if FAIL @@ -228,6 +238,31 @@ function testParamType(param, types){ } return result; } +p5._friendlyParamError = function (errorObj, func) { + var message; + switch (errorObj.type){ + case 'EMPTY_VAR': + message = 'FES: It looks like ' + func + + ' received an empty variable in spot #' + errorObj.position + + ' (zero-based index). If not intentional, this is often a problem' + + ' with scope: [link to scope].'; + report(message, func, ERR_PARAMS); + break; + case 'WRONG_CLASS': + message = 'FES: ' + func + ' was expecting ' + errorObj.correctClass + + ' for parameter #' + errorObj.position + ' (zero-based index), received '; + // Wrap strings in quotes + message += 'an object with name '+ errorObj.wrongClass +' instead.'; + report(message, func, ERR_PARAMS); + break; + case 'WRONG_TYPE': + message = 'FES: ' + func + ' was expecting ' + errorObj.correctType + + ' for parameter #' + errorObj.position + ' (zero-based index), received '; + // Wrap strings in quotes + message += errorObj.wrongType + ' instead.'; + report(message, func, ERR_PARAMS); + } +}; function friendlyWelcome() { // p5.js brand - magenta: #ED225D var astrixBgColor = 'transparent'; @@ -376,7 +411,6 @@ function helpForMisusedAtTopLevelCode(e, log) { // Exposing this primarily for unit testing. p5.prototype._helpForMisusedAtTopLevelCode = helpForMisusedAtTopLevelCode; -p5.prototype._validateNumParameters = validateNumParameters; p5.prototype._validateParameters = validateParameters; if (document.readyState !== 'complete') { diff --git a/test/unit/core/error_helpers.js b/test/unit/core/error_helpers.js index 443c209242..a5fd554809 100644 --- a/test/unit/core/error_helpers.js +++ b/test/unit/core/error_helpers.js @@ -29,6 +29,30 @@ suite('Error Helpers', function() { }); }); + suite('validateParameters: Numbers + optional Constant', function(){ + test('rect(): no friendly-err-msg', function() { + assert.doesNotThrow(function() { + p5.prototype._validateParameters('rect', + [1, 1, 10.5, 10]); + }, + Error, 'got unwanted exception'); + }); + test('rect(): missing param #3', function() { + assert.doesNotThrow(function() { + p5.prototype._validateParameters('rect', + [1, 1, 10.5]); + }, + Error, 'got unwanted exception'); + }); + test('rect(): wrong param type at #0,#5', function() { + assert.doesNotThrow(function() { + p5.prototype._validateParameters('rect', + ['1', 1, 10.5, 10, 0, Math.PI, 'pie']); + }, + Error, 'got unwanted exception'); + }); + }); + suite('validateParameters: p5.Color|String + optional Numbers', function(){ test('ambientLight(): no friendly-err-msg', function() { assert.doesNotThrow(function() { From 7e81a296a9eda7558e3497f006485a6fd06b9425 Mon Sep 17 00:00:00 2001 From: almchung Date: Tue, 27 Jun 2017 21:22:16 -0700 Subject: [PATCH 11/37] removed validateNumParameters() and re-arranged validateParameters() --- src/core/error_helpers.js | 10 +++++----- test/unit/core/error_helpers.js | 25 +++++++++++++++++++++++-- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/src/core/error_helpers.js b/src/core/error_helpers.js index 32e90eb355..b008347985 100644 --- a/src/core/error_helpers.js +++ b/src/core/error_helpers.js @@ -139,14 +139,14 @@ function validateParameters(func, args) { var minErrCount = 0; for (var j = 0; j < errorListArray.length; j++) { var numErr = errorListArray[j].length; - if (numErr > 0){ - if(minErrInd > -1){ // non-first cases w errors + if (numErr >= arrDoc.length){ // exclude clean cases + if(j===0){ // the first case + minErrCount = numErr; + minErrInd = j; + }else{ if(minErrCount > numErr){ minErrInd = j; } - }else{ // the first case w errors - minErrCount = numErr; - minErrInd = j; } } } diff --git a/test/unit/core/error_helpers.js b/test/unit/core/error_helpers.js index a5fd554809..c76eb76ee2 100644 --- a/test/unit/core/error_helpers.js +++ b/test/unit/core/error_helpers.js @@ -44,7 +44,7 @@ suite('Error Helpers', function() { }, Error, 'got unwanted exception'); }); - test('rect(): wrong param type at #0,#5', function() { + test('rect(): wrong param type at #0,#6', function() { assert.doesNotThrow(function() { p5.prototype._validateParameters('rect', ['1', 1, 10.5, 10, 0, Math.PI, 'pie']); @@ -53,7 +53,7 @@ suite('Error Helpers', function() { }); }); - suite('validateParameters: p5.Color|String + optional Numbers', function(){ + suite('validateParameters: class, multi-types + optional Numbers', function(){ test('ambientLight(): no friendly-err-msg', function() { assert.doesNotThrow(function() { var c = myp5.color(255, 204, 0); @@ -63,6 +63,27 @@ suite('Error Helpers', function() { }); }); + suite('validateParameters: multi-format', function(){ + test('color(): no friendly-err-msg', function() { + assert.doesNotThrow(function() { + p5.prototype._validateParameters('color', [65]); + }, + Error, 'got unwanted exception'); + }); + test('color(): no friendly-err-msg', function() { + assert.doesNotThrow(function() { + p5.prototype._validateParameters('color', [65, 0.5]); + }, + Error, 'got unwanted exception'); + }); + test('color(): no friendly-err-msg', function() { + assert.doesNotThrow(function() { + p5.prototype._validateParameters('color', [255, 204, 0]); + }, + Error, 'got unwanted exception'); + }); + }); + suite('helpForMisusedAtTopLevelCode', function() { var help = function(msg) { var log = []; From 4791a2b4e1dfd87e3dec0655f424ffa8e61a8629 Mon Sep 17 00:00:00 2001 From: almchung Date: Thu, 29 Jun 2017 10:52:20 -0700 Subject: [PATCH 12/37] fixes and improvements based on comments --- src/core/error_helpers.js | 39 ++++++++++++++------------------------- 1 file changed, 14 insertions(+), 25 deletions(-) diff --git a/src/core/error_helpers.js b/src/core/error_helpers.js index b008347985..653b06dd99 100644 --- a/src/core/error_helpers.js +++ b/src/core/error_helpers.js @@ -9,8 +9,7 @@ var p5 = require('./core'); var doFriendlyWelcome = false; // TEMP until we get it all working LM // for parameter validation var dataDoc = require('../../docs/reference/data.json'); -var strDoc = JSON.stringify(dataDoc); -var arrDoc = JSON.parse(strDoc); +var arrDoc = JSON.parse(JSON.stringify(dataDoc)); // -- Borrowed from jQuery 1.11.3 -- var class2type = {}; @@ -136,17 +135,13 @@ function validateParameters(func, args) { } // compare errors from all formats var minErrInd = -1; - var minErrCount = 0; + var minErrCount = 999999; for (var j = 0; j < errorListArray.length; j++) { var numErr = errorListArray[j].length; if (numErr >= arrDoc.length){ // exclude clean cases - if(j===0){ // the first case - minErrCount = numErr; + if(minErrCount > numErr){ minErrInd = j; - }else{ - if(minErrCount > numErr){ - minErrInd = j; - } + minErrCount = numErr; } } } @@ -158,10 +153,8 @@ function validateParameters(func, args) { } } else { var errorArray = testParamFormat(args, arrDoc[0]); - if (errorArray.length > 0){ - for(var m = 0; m < errorArray.length; m++) { - p5._friendlyParamError(errorArray[m], func); - } + for(var m = 0; m < errorArray.length; m++) { + p5._friendlyParamError(errorArray[m], func); } } } @@ -194,15 +187,13 @@ function testParamFormat(args, format){ var types = format[p].type.split('|'); // case accepting multi-types var pass; if (argType === 'object'){ // if obejct, test for class - pass = testParamClass(args[p], types); - if (!pass) { // if fails to pass + if (!testParamClass(args[p], types)) { // if fails to pass error = {type:'WRONG_CLASS', position: p, correctClass: types[p], wrongClass: args[p].name}; errorArray.push(error); } }else{ // not object, test for type - pass = testParamType(args[p], types); - if (!pass) { // if fails to pass + if (!testParamType(args[p], types)) { // if fails to pass error = {type:'WRONG_TYPE', position: p, correctType: types[p], wrongType: argType}; errorArray.push(error); @@ -215,28 +206,26 @@ function testParamFormat(args, format){ // testClass() for object type parameter validation // Returns true if PASS, false if FAIL function testParamClass(param, types){ - var result = false; for (var i = 0; i < types.length; i++) { if (param.name === types[i]) { - result = true; // class name match, pass + return true; // class name match, pass } else if (types[i] === 'Constant'){ - result = true; // accepts any constant, pass + return true; // accepts any constant, pass } } - return result; + return false; } // testType() for non-object type parameter validation // Returns true if PASS, false if FAIL function testParamType(param, types){ - var result = false; for (var i = 0; i < types.length; i++) { if (typeof(param) === types[i].toLowerCase()) { - result = true; // type match, pass + return true; // type match, pass } else if (types[i] === 'Constant'){ - result = true; // accepts any constant, pass + return true; // accepts any constant, pass } } - return result; + return false; } p5._friendlyParamError = function (errorObj, func) { var message; From 5bd56b12ea2ff8efc2c1f233c75566bf5f4ff302 Mon Sep 17 00:00:00 2001 From: almchung Date: Mon, 10 Jul 2017 18:37:22 -0700 Subject: [PATCH 13/37] validateParameters() modification + small fixes --- src/core/error_helpers.js | 42 ++++++++++++++++++--------------------- 1 file changed, 19 insertions(+), 23 deletions(-) diff --git a/src/core/error_helpers.js b/src/core/error_helpers.js index 653b06dd99..0dfddcaf21 100644 --- a/src/core/error_helpers.js +++ b/src/core/error_helpers.js @@ -128,31 +128,26 @@ p5._friendlyFileLoadError = function (errorType, filePath) { */ function validateParameters(func, args) { var arrDoc = lookupParamDoc(func); - if (arrDoc.length > 1){ // multiple format? - var errorListArray = []; + var errorArray = []; + var minErrCount = 999999; + if (arrDoc.length > 1){ // func has multiple formats for (var i = 0; i < arrDoc.length; i++) { - errorListArray.push(testParamFormat(args, arrDoc[i])); - } - // compare errors from all formats - var minErrInd = -1; - var minErrCount = 999999; - for (var j = 0; j < errorListArray.length; j++) { - var numErr = errorListArray[j].length; - if (numErr >= arrDoc.length){ // exclude clean cases - if(minErrCount > numErr){ - minErrInd = j; - minErrCount = numErr; - } + var arrError = testParamFormat(args, arrDoc[i]); + if( arrError.length === 0) { + return; // no error } - } - if (minErrInd > -1){ - // generate error msg with a smaller number of errors - for(var n = 0; n < errorListArray[minErrInd].length; n++) { - p5._friendlyParamError(errorListArray[minErrInd][n], func); + // see if this is the format with min number of err + if( minErrCount > arrError.length) { + minErrCount = arrError.length; + errorArray = arrError; } } - } else { - var errorArray = testParamFormat(args, arrDoc[0]); + // generate err msg + for (var n = 0; n < errorArray.length; n++) { + p5._friendlyParamError(errorArray[n], func); + } + } else { // func has a single format + errorArray = testParamFormat(args, arrDoc[0]); for(var m = 0; m < errorArray.length; m++) { p5._friendlyParamError(errorArray[m], func); } @@ -163,6 +158,7 @@ function validateParameters(func, args) { function lookupParamDoc(func){ var queryResult = arrDoc.classitems. filter(function (x) { return x.name === func; }); + // different JSON structure for funct with multi-format if (queryResult[0].hasOwnProperty('overloads')){ var res = []; for(var i = 0; i < queryResult[0].overloads.length; i++) { @@ -185,8 +181,7 @@ function testParamFormat(args, format){ } } else { var types = format[p].type.split('|'); // case accepting multi-types - var pass; - if (argType === 'object'){ // if obejct, test for class + if (argType === 'object'){ // if object, test for class if (!testParamClass(args[p], types)) { // if fails to pass error = {type:'WRONG_CLASS', position: p, correctClass: types[p], wrongClass: args[p].name}; @@ -227,6 +222,7 @@ function testParamType(param, types){ } return false; } +// function for generating console.log() msg p5._friendlyParamError = function (errorObj, func) { var message; switch (errorObj.type){ From 672f3157f880ee942dffabb1ca85e231d5c6eb8b Mon Sep 17 00:00:00 2001 From: almchung Date: Mon, 10 Jul 2017 18:47:31 -0700 Subject: [PATCH 14/37] unit test typo --- test/unit/core/error_helpers.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/unit/core/error_helpers.js b/test/unit/core/error_helpers.js index c76eb76ee2..2b4cbf3c12 100644 --- a/test/unit/core/error_helpers.js +++ b/test/unit/core/error_helpers.js @@ -44,10 +44,10 @@ suite('Error Helpers', function() { }, Error, 'got unwanted exception'); }); - test('rect(): wrong param type at #0,#6', function() { + test('rect(): wrong param type at #0', function() { assert.doesNotThrow(function() { p5.prototype._validateParameters('rect', - ['1', 1, 10.5, 10, 0, Math.PI, 'pie']); + ['1', 1, 10.5, 10, 0, Math.PI]); }, Error, 'got unwanted exception'); }); From 9f50bc3db7b0d48839591e49548a41ed233c3ca5 Mon Sep 17 00:00:00 2001 From: almchung Date: Wed, 12 Jul 2017 11:25:08 -0700 Subject: [PATCH 15/37] 2d primitives unit tests --- src/core/2d_primitives.js | 3 ++- test/unit/core/2d_primitives.js | 43 +++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/src/core/2d_primitives.js b/src/core/2d_primitives.js index c5c8fadc72..3768b77a9c 100644 --- a/src/core/2d_primitives.js +++ b/src/core/2d_primitives.js @@ -246,7 +246,8 @@ p5.prototype.line = function() { for (var i = 0; i < args.length; ++i) { args[i] = arguments[i]; } - // check with FES:validateParameters - not implemented yet + // check with FES:validateParameters + this._validateParameters('line', args); //check whether we should draw a 3d line or 2d if (this._renderer.isP3D) { this._renderer.line( diff --git a/test/unit/core/2d_primitives.js b/test/unit/core/2d_primitives.js index 8b657cc995..1abcd82f16 100644 --- a/test/unit/core/2d_primitives.js +++ b/test/unit/core/2d_primitives.js @@ -16,6 +16,24 @@ suite('2D Primitives', function() { assert.ok(arc); assert.typeOf(arc, 'function'); }); + test('arc(): no friendly-err-msg', function() { + assert.doesNotThrow(function() { + myp5.arc(1, 1, 10.5, 10, 0, Math.PI, 'pie'); + }, + Error, 'got unwanted exception'); + }); + test('arc(): missing param #4, #5', function() { + assert.doesNotThrow(function() { + myp5.arc(1, 1, 10.5, 10); + }, + Error, 'got unwanted exception'); + }); + test('arc(): wrong param type at #0', function() { + assert.doesNotThrow(function() { + myp5.arc('1', 1, 10.5, 10, 0, Math.PI, 'pie'); + }, + Error, 'got unwanted exception'); + }); }); }); @@ -36,6 +54,31 @@ suite('2D Primitives', function() { done(); }); }); + test('ellipse(): no friendly-err-msg', function() { + assert.doesNotThrow(function() { + myp5.ellipse(0, 0, 100); + }, + Error, 'got unwanted exception'); + }); + test('ellipse(): missing param #2', function() { + assert.doesNotThrow(function() { + myp5.ellipse(0, 0); + }, + Error, 'got unwanted exception'); + }); + test('ellipse(): missing param #2', function() { + assert.doesNotThrow(function() { + var size; + myp5.ellipse(0, 0, size); + }, + Error, 'got unwanted exception'); + }); + test('ellipse(): wrong param type at #0', function() { + assert.doesNotThrow(function() { + myp5.ellipse('0', 0, 100, 100); + }, + Error, 'got unwanted exception'); + }); }); }); From 57d006a4640f630cab3e5e882a63c9d29c6d0526 Mon Sep 17 00:00:00 2001 From: almchung Date: Wed, 12 Jul 2017 12:52:11 -0700 Subject: [PATCH 16/37] 2d primitives unit tests --- src/core/2d_primitives.js | 10 ++-- test/unit/core/2d_primitives.js | 82 +++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+), 4 deletions(-) diff --git a/src/core/2d_primitives.js b/src/core/2d_primitives.js index 3768b77a9c..9fad5c2e45 100644 --- a/src/core/2d_primitives.js +++ b/src/core/2d_primitives.js @@ -299,7 +299,8 @@ p5.prototype.point = function() { for (var i = 0; i < args.length; ++i) { args[i] = arguments[i]; } - // check with FES:validateParameters - not implemented yet + // check with FES:validateParameters + this._validateParameters('point', args); //check whether we should draw a 3d line or 2d if (this._renderer.isP3D) { this._renderer.point( @@ -365,7 +366,8 @@ p5.prototype.quad = function() { for (var i = 0; i < args.length; ++i) { args[i] = arguments[i]; } - // check with FES:validateParameters - not implemented yet + // check with FES:validateParameters + this._validateParameters('quad', args); if (this._renderer.isP3D) { this._renderer.quad( args[0], @@ -464,7 +466,7 @@ p5.prototype.rect = function() { if (!this._renderer._doStroke && !this._renderer._doFill) { return; } - // check with FES:validateParameters - not implemented yet + // check with FES:validateParameters this._validateParameters('rect', args); var vals = canvas.modeAdjust( args[0], @@ -513,7 +515,7 @@ p5.prototype.triangle = function() { for (var i = 0; i < args.length; ++i) { args[i] = arguments[i]; } - // check with FES:validateParameters - not implemented yet + // check with FES:validateParameters this._validateParameters('triangle', args); this._renderer.triangle(args); return this; diff --git a/test/unit/core/2d_primitives.js b/test/unit/core/2d_primitives.js index 1abcd82f16..6ff77a9514 100644 --- a/test/unit/core/2d_primitives.js +++ b/test/unit/core/2d_primitives.js @@ -99,6 +99,88 @@ suite('2D Primitives', function() { done(); }); }); + test('line(): no friendly-err-msg, 2D', function() { + assert.doesNotThrow(function() { + myp5.line(0, 0, 100, 100); + }, + Error, 'got unwanted exception'); + }); + test('line(): no friendly-err-msg, 3D', function() { + assert.doesNotThrow(function() { + myp5.line(0, 0, 100, 100, 20, Math.PI); + }, + Error, 'got unwanted exception'); + }); + test('line(): missing param #3', function() { + assert.doesNotThrow(function() { + myp5.line(0, 0, Math.PI); + }, + Error, 'got unwanted exception'); + }); + test('line(): missing param #4', function() { // this err case escapes + assert.doesNotThrow(function() { + var x3; + myp5.line(0, 0, 100, 100, x3, Math.PI); + }, + Error, 'got unwanted exception'); + }); + test('line(): wrong param type at #1', function() { + assert.doesNotThrow(function() { + myp5.line(0, '0', 100, 100); + }, + Error, 'got unwanted exception'); + }); + }); + }); + + suite('p5.prototype.point', function() { + var point = p5.prototype.point; + suite('point()', function() { + test('should be a function', function() { + assert.ok(point); + assert.typeOf(point, 'function'); + }); + test('point(): no friendly-err-msg, 2D', function() { + assert.doesNotThrow(function() { + myp5.point(Math.PI, 0); + }, + Error, 'got unwanted exception'); + }); + test('point(): no friendly-err-msg, 3D', function() { + assert.doesNotThrow(function() { + myp5.point(Math.PI, 0, 100); + }, + Error, 'got unwanted exception'); + }); + test('point(): missing param #1', function() { + assert.doesNotThrow(function() { + myp5.point(0); + }, + Error, 'got unwanted exception'); + }); + test('point(): missing param #3', function() { // this err case escapes + assert.doesNotThrow(function() { + var z; + myp5.point(0, Math.PI, z); + }, + Error, 'got unwanted exception'); + }); + test('point(): wrong param type at #1', function() { + assert.doesNotThrow(function() { + myp5.point(Math.PI, '0'); + }, + Error, 'got unwanted exception'); + }); + }); + }); + + suite('p5.prototype.quad', function() { + var quad = p5.prototype.quad; + suite('quad()', function() { + test('should be a function', function() { + assert.ok(quad); + assert.typeOf(quad, 'function'); + }); }); }); From 74a7590ebfd1dab77cf9e124572bebc1fc2dbe76 Mon Sep 17 00:00:00 2001 From: almchung Date: Wed, 12 Jul 2017 13:07:18 -0700 Subject: [PATCH 17/37] 2d primitives unit tests --- src/core/error_helpers.js | 6 +++--- test/unit/core/2d_primitives.js | 31 +++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/src/core/error_helpers.js b/src/core/error_helpers.js index 0dfddcaf21..23353c5193 100644 --- a/src/core/error_helpers.js +++ b/src/core/error_helpers.js @@ -228,20 +228,20 @@ p5._friendlyParamError = function (errorObj, func) { switch (errorObj.type){ case 'EMPTY_VAR': message = 'FES: It looks like ' + func + - ' received an empty variable in spot #' + errorObj.position + + '() received an empty variable in spot #' + errorObj.position + ' (zero-based index). If not intentional, this is often a problem' + ' with scope: [link to scope].'; report(message, func, ERR_PARAMS); break; case 'WRONG_CLASS': - message = 'FES: ' + func + ' was expecting ' + errorObj.correctClass + + message = 'FES: ' + func + '() was expecting ' + errorObj.correctClass + ' for parameter #' + errorObj.position + ' (zero-based index), received '; // Wrap strings in quotes message += 'an object with name '+ errorObj.wrongClass +' instead.'; report(message, func, ERR_PARAMS); break; case 'WRONG_TYPE': - message = 'FES: ' + func + ' was expecting ' + errorObj.correctType + + message = 'FES: ' + func + '() was expecting ' + errorObj.correctType + ' for parameter #' + errorObj.position + ' (zero-based index), received '; // Wrap strings in quotes message += errorObj.wrongType + ' instead.'; diff --git a/test/unit/core/2d_primitives.js b/test/unit/core/2d_primitives.js index 6ff77a9514..9ca0ad2b2a 100644 --- a/test/unit/core/2d_primitives.js +++ b/test/unit/core/2d_primitives.js @@ -191,6 +191,37 @@ suite('2D Primitives', function() { assert.ok(rect); assert.typeOf(rect, 'function'); }); + test('rect(): no friendly-err-msg, format I', function() { + assert.doesNotThrow(function() { + myp5.rect(0, 0, 100, 100); + }, + Error, 'got unwanted exception'); + }); + test('rect(): no friendly-err-msg, format II', function() { + assert.doesNotThrow(function() { + myp5.rect(0, 0, 100, 100, 1, Math.PI, 1, Math.PI); + }, + Error, 'got unwanted exception'); + }); + test('rect(): missing param #3', function() { + assert.doesNotThrow(function() { + myp5.rect(0, 0, Math.PI); + }, + Error, 'got unwanted exception'); + }); + test('rect(): missing param #4', function() { // this err case escapes + assert.doesNotThrow(function() { + var r1; + myp5.rect(0, 0, 100, 100, r1, Math.PI, 1, Math.PI); + }, + Error, 'got unwanted exception'); + }); + test('rect(): wrong param type at #1', function() { + assert.doesNotThrow(function() { + myp5.rect(0, '0', 100, 100); + }, + Error, 'got unwanted exception'); + }); }); }); From 065f54e0ee73e5a248484c67b5d152a3ae35a80b Mon Sep 17 00:00:00 2001 From: almchung Date: Thu, 13 Jul 2017 11:53:01 -0700 Subject: [PATCH 18/37] 2d primitives unit tests --- test/unit/core/2d_primitives.js | 36 +++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/test/unit/core/2d_primitives.js b/test/unit/core/2d_primitives.js index 9ca0ad2b2a..769157a32b 100644 --- a/test/unit/core/2d_primitives.js +++ b/test/unit/core/2d_primitives.js @@ -181,6 +181,24 @@ suite('2D Primitives', function() { assert.ok(quad); assert.typeOf(quad, 'function'); }); + test('quad(): no friendly-err-msg, 2D', function() { + assert.doesNotThrow(function() { + myp5.quad(Math.PI, 0, Math.PI, 5.1, 10, 5.1, 10, 0); + }, + Error, 'got unwanted exception'); + }); + test('quad(): missing param #7', function() { + assert.doesNotThrow(function() { + myp5.quad(Math.PI, 0, Math.PI, 5.1, 10, 5.1, 10); + }, + Error, 'got unwanted exception'); + }); + test('quad(): wrong param type at #1', function() { + assert.doesNotThrow(function() { + myp5.quad(Math.PI, '0', Math.PI, 5.1, 10, 5.1, 10, 0); + }, + Error, 'got unwanted exception'); + }); }); }); @@ -232,6 +250,24 @@ suite('2D Primitives', function() { assert.ok(triangle); assert.typeOf(triangle, 'function'); }); + test('triangle(): no friendly-err-msg', function() { + assert.doesNotThrow(function() { + myp5.triangle(Math.PI, 0, Math.PI, 5.1, 10, 5.1); + }, + Error, 'got unwanted exception'); + }); + test('triangle(): missing param #5', function() { + assert.doesNotThrow(function() { + myp5.triangle(Math.PI, 0, Math.PI, 5.1, 10); + }, + Error, 'got unwanted exception'); + }); + test('triangle(): wrong param type at #1', function() { + assert.doesNotThrow(function() { + myp5.triangle(Math.PI, '0', Math.PI, 5.1, 10, 5.1); + }, + Error, 'got unwanted exception'); + }); }); }); From 827f496db1f96c2092471aa3492d42dbe1e31144 Mon Sep 17 00:00:00 2001 From: almchung Date: Thu, 20 Jul 2017 21:50:59 -0700 Subject: [PATCH 19/37] minor updates --- src/core/error_helpers.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/core/error_helpers.js b/src/core/error_helpers.js index 23353c5193..24325f0f25 100644 --- a/src/core/error_helpers.js +++ b/src/core/error_helpers.js @@ -127,6 +127,10 @@ p5._friendlyFileLoadError = function (errorType, filePath) { * received "foo" instead." */ function validateParameters(func, args) { + if (p5.disableFriendlyErrors || + typeof(IS_MINIFIED) !== 'undefined') { + return; // skip FES + } var arrDoc = lookupParamDoc(func); var errorArray = []; var minErrCount = 999999; @@ -227,21 +231,21 @@ p5._friendlyParamError = function (errorObj, func) { var message; switch (errorObj.type){ case 'EMPTY_VAR': - message = 'FES: It looks like ' + func + + message = 'It looks like ' + func + '() received an empty variable in spot #' + errorObj.position + ' (zero-based index). If not intentional, this is often a problem' + ' with scope: [link to scope].'; report(message, func, ERR_PARAMS); break; case 'WRONG_CLASS': - message = 'FES: ' + func + '() was expecting ' + errorObj.correctClass + + message = func + '() was expecting ' + errorObj.correctClass + ' for parameter #' + errorObj.position + ' (zero-based index), received '; // Wrap strings in quotes message += 'an object with name '+ errorObj.wrongClass +' instead.'; report(message, func, ERR_PARAMS); break; case 'WRONG_TYPE': - message = 'FES: ' + func + '() was expecting ' + errorObj.correctType + + message = func + '() was expecting ' + errorObj.correctType + ' for parameter #' + errorObj.position + ' (zero-based index), received '; // Wrap strings in quotes message += errorObj.wrongType + ' instead.'; From 3ac10a55dcd0934df037997c62611c3b87df13ea Mon Sep 17 00:00:00 2001 From: almchung Date: Wed, 26 Jul 2017 15:15:13 -0700 Subject: [PATCH 20/37] added Array checking to validateParameters(); implementing FES to curves and color/creating_reading functions --- src/color/creating_reading.js | 112 +++++++++++++++++++--------------- src/core/curves.js | 4 ++ src/core/error_helpers.js | 14 +++-- 3 files changed, 78 insertions(+), 52 deletions(-) diff --git a/src/color/creating_reading.js b/src/color/creating_reading.js index 586e0baf8c..bfc891d3e4 100644 --- a/src/color/creating_reading.js +++ b/src/color/creating_reading.js @@ -16,7 +16,7 @@ require('./p5.Color'); * Extracts the alpha value from a color or pixel array. * * @method alpha - * @param {Object} obj p5.Color object or pixel array + * @param {p5.Color|Array} color p5.Color object or pixel array * @example *
* @@ -51,18 +51,20 @@ require('./p5.Color'); * deep pink rect on left and grey rect on right, both 35x60. */ p5.prototype.alpha = function(c) { - if (c instanceof p5.Color || c instanceof Array) { - return this.color(c)._getAlpha(); - } else { - throw new Error('Needs p5.Color or pixel array as argument.'); - } + // if (c instanceof p5.Color || c instanceof Array) { + // return this.color(c)._getAlpha(); + // } else { + // throw new Error('Needs p5.Color or pixel array as argument.'); + // } + this._validateParameters('alpha', arguments); + return this.color(c)._getAlpha(); }; /** * Extracts the blue value from a color or pixel array. * * @method blue - * @param {Object} obj p5.Color object or pixel array + * @param {p5.Color|Array} color p5.Color object or pixel array * @example *
* @@ -82,18 +84,20 @@ p5.prototype.alpha = function(c) { * */ p5.prototype.blue = function(c) { - if (c instanceof p5.Color || c instanceof Array) { - return this.color(c)._getBlue(); - } else { - throw new Error('Needs p5.Color or pixel array as argument.'); - } + // if (c instanceof p5.Color || c instanceof Array) { + // return this.color(c)._getBlue(); + // } else { + // throw new Error('Needs p5.Color or pixel array as argument.'); + // } + this._validateParameters('blue', arguments); + return this.color(c)._getBlue(); }; /** * Extracts the HSB brightness value from a color or pixel array. * * @method brightness - * @param {Object} color p5.Color object or pixel array + * @param {p5.Color|Array} color p5.Color object or pixel array * @example *
* @@ -113,11 +117,13 @@ p5.prototype.blue = function(c) { * */ p5.prototype.brightness = function(c) { - if (c instanceof p5.Color || c instanceof Array) { - return this.color(c)._getBrightness(); - } else { - throw new Error('Needs p5.Color or pixel array as argument.'); - } + // if (c instanceof p5.Color || c instanceof Array) { + // return this.color(c)._getBrightness(); + // } else { + // throw new Error('Needs p5.Color or pixel array as argument.'); + // } + this._validateParameters('brightness', arguments); + return this.color(c)._getBrightness(); }; /** @@ -311,7 +317,7 @@ p5.prototype.color = function() { * Extracts the green value from a color or pixel array. * * @method green - * @param {Object} color p5.Color object or pixel array + * @param {p5.Color|Array} color p5.Color object or pixel array * @example *
* @@ -332,11 +338,13 @@ p5.prototype.color = function() { */ p5.prototype.green = function(c) { - if (c instanceof p5.Color || c instanceof Array) { - return this.color(c)._getGreen(); - } else { - throw new Error('Needs p5.Color or pixel array as argument.'); - } + // if (c instanceof p5.Color || c instanceof Array) { + // return this.color(c)._getGreen(); + // } else { + // throw new Error('Needs p5.Color or pixel array as argument.'); + // } + this._validateParameters('green', arguments); + return this.color(c)._getGreen(); }; /** @@ -349,7 +357,7 @@ p5.prototype.green = function(c) { * maximum hue setting for each system is different.) * * @method hue - * @param {Object} color p5.Color object or pixel array + * @param {p5.Color|Array} color p5.Color object or pixel array * @example *
* @@ -370,11 +378,13 @@ p5.prototype.green = function(c) { */ p5.prototype.hue = function(c) { - if (c instanceof p5.Color || c instanceof Array) { - return this.color(c)._getHue(); - } else { - throw new Error('Needs p5.Color or pixel array as argument.'); - } + // if (c instanceof p5.Color || c instanceof Array) { + // return this.color(c)._getHue(); + // } else { + // throw new Error('Needs p5.Color or pixel array as argument.'); + // } + this._validateParameters('hue', arguments); + return this.color(c)._getHue(); }; /** @@ -469,7 +479,7 @@ p5.prototype.lerpColor = function(c1, c2, amt) { * Extracts the HSL lightness value from a color or pixel array. * * @method lightness - * @param {Object} color p5.Color object or pixel array + * @param {p5.Color|Array} color p5.Color object or pixel array * @example *
* @@ -489,18 +499,20 @@ p5.prototype.lerpColor = function(c1, c2, amt) { * */ p5.prototype.lightness = function(c) { - if (c instanceof p5.Color || c instanceof Array) { - return this.color(c)._getLightness(); - } else { - throw new Error('Needs p5.Color or pixel array as argument.'); - } + // if (c instanceof p5.Color || c instanceof Array) { + // return this.color(c)._getLightness(); + // } else { + // throw new Error('Needs p5.Color or pixel array as argument.'); + // } + this._validateParameters('lightness', arguments); + return this.color(c)._getLightness(); }; /** * Extracts the red value from a color or pixel array. * * @method red - * @param {Object} obj p5.Color object or pixel array + * @param {p5.Color|Array} color p5.Color object or pixel array * @example *
* @@ -530,11 +542,13 @@ p5.prototype.lightness = function(c) { * grey canvas */ p5.prototype.red = function(c) { - if (c instanceof p5.Color || c instanceof Array) { - return this.color(c)._getRed(); - } else { - throw new Error('Needs p5.Color or pixel array as argument.'); - } + // if (c instanceof p5.Color || c instanceof Array) { + // return this.color(c)._getRed(); + // } else { + // throw new Error('Needs p5.Color or pixel array as argument.'); + // } + this._validateParameters('red', arguments); + return this.color(c)._getRed(); }; /** @@ -546,7 +560,7 @@ p5.prototype.red = function(c) { * HSL saturation otherwise. * * @method saturation - * @param {Object} color p5.Color object or pixel array + * @param {p5.Color|Array} color p5.Color object or pixel array * @example *
* @@ -567,11 +581,13 @@ p5.prototype.red = function(c) { */ p5.prototype.saturation = function(c) { - if (c instanceof p5.Color || c instanceof Array) { - return this.color(c)._getSaturation(); - } else { - throw new Error('Needs p5.Color or pixel array as argument.'); - } + // if (c instanceof p5.Color || c instanceof Array) { + // return this.color(c)._getSaturation(); + // } else { + // throw new Error('Needs p5.Color or pixel array as argument.'); + // } + this._validateParameters('saturation', arguments); + return this.color(c)._getSaturation(); }; module.exports = p5; diff --git a/src/core/curves.js b/src/core/curves.js index 0bb1661d0c..6feebb233b 100644 --- a/src/core/curves.js +++ b/src/core/curves.js @@ -76,6 +76,8 @@ var curveDetail = 20; */ p5.prototype.bezier = function() { var args = new Array(arguments.length); + // check with FES:validateParameters + this._validateParameters('bezier', args); for (var i = 0; i < args.length; ++i) { args[i] = arguments[i]; } @@ -317,6 +319,8 @@ p5.prototype.bezierTangent = function(a, b, c, d, t) { */ p5.prototype.curve = function() { var args = new Array(arguments.length); + // check with FES:validateParameters + this._validateParameters('curve', args); for (var i = 0; i < args.length; ++i) { args[i] = arguments[i]; } diff --git a/src/core/error_helpers.js b/src/core/error_helpers.js index 24325f0f25..5eb58756b7 100644 --- a/src/core/error_helpers.js +++ b/src/core/error_helpers.js @@ -206,10 +206,16 @@ function testParamFormat(args, format){ // Returns true if PASS, false if FAIL function testParamClass(param, types){ for (var i = 0; i < types.length; i++) { - if (param.name === types[i]) { - return true; // class name match, pass - } else if (types[i] === 'Constant'){ - return true; // accepts any constant, pass + if (types[i] === 'Array'){ + if(param instanceof Array){ + return true; + } + }else{ + if (param.name === types[i]) { + return true; // class name match, pass + } else if (types[i] === 'Constant'){ + return true; // accepts any constant, pass + } } } return false; From 21b14ffa961b6fbb1385c08b5a1d33f0fdde97a7 Mon Sep 17 00:00:00 2001 From: almchung Date: Thu, 3 Aug 2017 14:28:19 -0700 Subject: [PATCH 21/37] core/curves functions added FES & unit tests; color/creading_reading in progress. color() not working --- src/color/creating_reading.js | 31 ++++++++------ src/core/curves.js | 81 ++++++++++++++++++++++------------- test/unit/core/curves.js | 66 ++++++++++++++++++++++++++-- 3 files changed, 132 insertions(+), 46 deletions(-) diff --git a/src/color/creating_reading.js b/src/color/creating_reading.js index bfc891d3e4..df3578f451 100644 --- a/src/color/creating_reading.js +++ b/src/color/creating_reading.js @@ -11,6 +11,7 @@ var p5 = require('../core/core'); var constants = require('../core/constants'); require('./p5.Color'); +require('../core/error_helpers'); /** * Extracts the alpha value from a color or pixel array. @@ -296,6 +297,7 @@ p5.prototype.brightness = function(c) { */ p5.prototype.color = function() { + //p5.prototype._validateParameters('color', arguments); if (arguments[0] instanceof p5.Color) { return arguments[0]; // Do nothing if argument is already a color object. } else if (arguments[0] instanceof Array) { @@ -399,8 +401,8 @@ p5.prototype.hue = function(c) { * The way that colours are interpolated depends on the current color mode. * * @method lerpColor - * @param {Array|Number} c1 interpolate from this color - * @param {Array|Number} c2 interpolate to this color + * @param {p5.Color|Array} c1 interpolate from this color + * @param {p5.Color|Array} c2 interpolate to this color * @param {Number} amt number between 0 and 1 * @return {Array|Number} interpolated color * @example @@ -430,35 +432,36 @@ p5.prototype.hue = function(c) { * */ -p5.prototype.lerpColor = function(c1, c2, amt) { +p5.prototype.lerpColor = function() { + this._validateParameters('lerpColor', arguments); var mode = this._renderer._colorMode; var maxes = this._renderer._colorMaxes; var l0, l1, l2, l3; var fromArray, toArray; if (mode === constants.RGB) { - fromArray = c1.levels.map(function(level) { + fromArray = arguments[0].levels.map(function(level) { return level / 255; }); - toArray = c2.levels.map(function(level) { + toArray = arguments[1].levels.map(function(level) { return level / 255; }); } else if (mode === constants.HSB) { - c1._getBrightness(); // Cache hsba so it definitely exists. - c2._getBrightness(); - fromArray = c1.hsba; - toArray = c2.hsba; + arguments[0]._getBrightness(); // Cache hsba so it definitely exists. + arguments[1]._getBrightness(); + fromArray = arguments[0].hsba; + toArray = arguments[1].hsba; } else if (mode === constants.HSL) { - c1._getLightness(); // Cache hsla so it definitely exists. - c2._getLightness(); - fromArray = c1.hsla; - toArray = c2.hsla; + arguments[0]._getLightness(); // Cache hsla so it definitely exists. + arguments[1]._getLightness(); + fromArray = arguments[0].hsla; + toArray = arguments[1].hsla; } else { throw new Error (mode + 'cannot be used for interpolation.'); } // Prevent extrapolation. - amt = Math.max(Math.min(amt, 1), 0); + var amt = Math.max(Math.min(arguments[2], 1), 0); // Perform interpolation. l0 = this.lerp(fromArray[0], toArray[0], amt); diff --git a/src/core/curves.js b/src/core/curves.js index 6feebb233b..8505e2fa88 100644 --- a/src/core/curves.js +++ b/src/core/curves.js @@ -8,7 +8,6 @@ 'use strict'; var p5 = require('./core'); - require('./error_helpers'); var bezierDetail = 20; @@ -76,11 +75,11 @@ var curveDetail = 20; */ p5.prototype.bezier = function() { var args = new Array(arguments.length); - // check with FES:validateParameters - this._validateParameters('bezier', args); for (var i = 0; i < args.length; ++i) { args[i] = arguments[i]; } + // check with FES:validateParameters + this._validateParameters('bezier', args); if (!this._renderer._doStroke && !this._renderer._doFill) { return this; } @@ -160,11 +159,17 @@ p5.prototype.bezierDetail = function(d) { * */ p5.prototype.bezierPoint = function(a, b, c, d, t) { - var adjustedT = 1-t; - return Math.pow(adjustedT,3)*a + - 3*(Math.pow(adjustedT,2))*t*b + - 3*adjustedT*Math.pow(t,2)*c + - Math.pow(t,3)*d; + var args = new Array(arguments.length); + for (var i = 0; i < args.length; ++i) { + args[i] = arguments[i]; + } + // check with FES:validateParameters + p5.prototype._validateParameters('bezierPoint', args); + var adjustedT = 1-args[4]; + return Math.pow(adjustedT,3)*args[0] + + 3*(Math.pow(adjustedT,2))*args[4]*args[1] + + 3*adjustedT*Math.pow(args[4],2)*args[2] + + Math.pow(args[4],3)*args[3]; }; /** @@ -233,13 +238,19 @@ p5.prototype.bezierPoint = function(a, b, c, d, t) { * */ p5.prototype.bezierTangent = function(a, b, c, d, t) { - var adjustedT = 1-t; - return 3*d*Math.pow(t,2) - - 3*c*Math.pow(t,2) + - 6*c*adjustedT*t - - 6*b*adjustedT*t + - 3*b*Math.pow(adjustedT,2) - - 3*a*Math.pow(adjustedT,2); + var args = new Array(arguments.length); + for (var i = 0; i < args.length; ++i) { + args[i] = arguments[i]; + } + // check with FES:validateParameters + p5.prototype._validateParameters('bezierTangent', args); + var adjustedT = 1-args[4]; + return 3*args[3]*Math.pow(args[4],2) - + 3*args[2]*Math.pow(args[4],2) + + 6*args[2]*adjustedT*args[4] - + 6*args[1]*adjustedT*args[4] + + 3*args[1]*Math.pow(adjustedT,2) - + 3*args[0]*Math.pow(adjustedT,2); }; /** @@ -319,11 +330,11 @@ p5.prototype.bezierTangent = function(a, b, c, d, t) { */ p5.prototype.curve = function() { var args = new Array(arguments.length); - // check with FES:validateParameters - this._validateParameters('curve', args); for (var i = 0; i < args.length; ++i) { args[i] = arguments[i]; } + // check with FES:validateParameters + this._validateParameters('curve', args); if (!this._renderer._doStroke) { return this; } @@ -447,13 +458,19 @@ p5.prototype.curveTightness = function (t) { *line hooking down to right-bottom with 13 5x5 white ellipse points */ p5.prototype.curvePoint = function(a, b, c, d, t) { - var t3 = t*t*t, - t2 = t*t, - f1 = -0.5 * t3 + t2 - 0.5 * t, + var args = new Array(arguments.length); + for (var i = 0; i < args.length; ++i) { + args[i] = arguments[i]; + } + // check with FES:validateParameters + p5.prototype._validateParameters('curvePoint', args); + var t3 = args[4]*args[4]*args[4], + t2 = args[4]*args[4], + f1 = -0.5 * t3 + t2 - 0.5 * args[4], f2 = 1.5 * t3 - 2.5 * t2 + 1.0, - f3 = -1.5 * t3 + 2.0 * t2 + 0.5 * t, + f3 = -1.5 * t3 + 2.0 * t2 + 0.5 * args[4], f4 = 0.5 * t3 - 0.5 * t2; - return a*f1 + b*f2 + c*f3 + d*f4; + return args[0]*f1 + args[1]*f2 + args[2]*f3 + args[3]*f4; }; /** @@ -491,13 +508,19 @@ p5.prototype.curvePoint = function(a, b, c, d, t) { * @alt *right curving line mid-right of canvas with 7 short lines radiating from it. */ -p5.prototype.curveTangent = function(a, b,c, d, t) { - var t2 = t*t, - f1 = (-3*t2)/2 + 2*t - 0.5, - f2 = (9*t2)/2 - 5*t, - f3 = (-9*t2)/2 + 4*t + 0.5, - f4 = (3*t2)/2 - t; - return a*f1 + b*f2 + c*f3 + d*f4; +p5.prototype.curveTangent = function(a, b, c, d, t) { + var args = new Array(arguments.length); + for (var i = 0; i < args.length; ++i) { + args[i] = arguments[i]; + } + // check with FES:validateParameters + p5.prototype._validateParameters('curveTangent', args); + var t2 = args[4]*args[4], + f1 = (-3*t2)/2 + 2*args[4] - 0.5, + f2 = (9*t2)/2 - 5*args[4], + f3 = (-9*t2)/2 + 4*args[4] + 0.5, + f4 = (3*t2)/2 - args[4]; + return args[0]*f1 + args[1]*f2 + args[2]*f3 + args[3]*f4; }; module.exports = p5; diff --git a/test/unit/core/curves.js b/test/unit/core/curves.js index c112d715cf..dcc0e95e1a 100644 --- a/test/unit/core/curves.js +++ b/test/unit/core/curves.js @@ -1,4 +1,36 @@ suite('Curves', function() { + var myp5 = new p5(function( p ) { + p.setup = function() {}; + p.draw = function() {}; + }); + + suite('p5.prototype.bezier', function() { + var bezier = p5.prototype.bezier; + suite('bezier()', function() { + test('should be a function', function() { + assert.ok(bezier); + assert.typeOf(bezier, 'function'); + }); + test('bezier(): no friendly-err-msg', function() { + assert.doesNotThrow(function() { + myp5.bezier(85, 20, 10, 10, 90, 90, 15, 80); + }, + Error, 'got unwanted exception'); + }); + test('bezier(): no friendly-err-msg. missing param #6, #7', function() { + assert.doesNotThrow(function() { + myp5.bezier(85, 20, 10, 10, 90, 90); + }, + Error, 'got unwanted exception'); + }); + test('bezier(): wrong param type at #0', function() { + assert.doesNotThrow(function() { + myp5.bezier('85', 20, 10, 10, 90, 90, 15, 80); + }, + Error, 'got unwanted exception'); + }); + }); + }); suite('p5.prototype.bezierPoint', function() { var bezierPoint = p5.prototype.bezierPoint; @@ -29,7 +61,7 @@ suite('Curves', function() { assert.typeOf(bezierTangent, 'function'); }); test('should return a number', function() { - result = bezierTangent(95, 73, 73, 15, 0.5); + result = bezierTangent(); assert.typeOf(result, 'number'); }); test('should return the correct point on a Bezier Curve', function() { @@ -39,6 +71,34 @@ suite('Curves', function() { }); }); + suite('p5.prototype.curve', function() { + var curve = p5.prototype.curve; + suite('curve()', function() { + test('should be a function', function() { + assert.ok(curve); + assert.typeOf(curve, 'function'); + }); + test('curve(): no friendly-err-msg', function() { + assert.doesNotThrow(function() { + myp5.curve(5, 26, 5, 26, 73, 24, 73, 61); + }, + Error, 'got unwanted exception'); + }); + test('curve(): no friendly-err-msg. missing param #6, #7', function() { + assert.doesNotThrow(function() { + myp5.curve(5, 26, 5, 26, 73, 24); + }, + Error, 'got unwanted exception'); + }); + test('curve(): wrong param type at #0', function() { + assert.doesNotThrow(function() { + myp5.curve('5', 26, 5, 26, 73, 24, 73, 61); + }, + Error, 'got unwanted exception'); + }); + }); + }); + suite('p5.prototype.curvePoint', function() { var curvePoint = p5.prototype.curvePoint; var result; @@ -48,7 +108,7 @@ suite('Curves', function() { assert.typeOf(curvePoint, 'function'); }); test('should return a number', function() { - result = curvePoint(5, 5, 73, 73, 0.5); + result = curvePoint(); assert.typeOf(result, 'number'); }); test('should return the correct point on a Catmull-Rom Curve', function() { @@ -68,7 +128,7 @@ suite('Curves', function() { assert.typeOf(curveTangent, 'function'); }); test('should return a number', function() { - result = curveTangent(95, 73, 73, 15, 0.5); + result = curveTangent(); assert.typeOf(result, 'number'); }); test('should return the correct point on a Catmull-Rom Curve', function() { From 0f777f1f5de1d59b344986b0a3354d5e4f68c6d6 Mon Sep 17 00:00:00 2001 From: almchung Date: Thu, 3 Aug 2017 14:54:13 -0700 Subject: [PATCH 22/37] color/creading_reading: color() fixed via updating documentation --- src/color/creating_reading.js | 38 +++++++++++++++++------------------ 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/src/color/creating_reading.js b/src/color/creating_reading.js index df3578f451..a896f72aad 100644 --- a/src/color/creating_reading.js +++ b/src/color/creating_reading.js @@ -18,6 +18,7 @@ require('../core/error_helpers'); * * @method alpha * @param {p5.Color|Array} color p5.Color object or pixel array + * @return {Number} the alpha value * @example *
* @@ -52,11 +53,6 @@ require('../core/error_helpers'); * deep pink rect on left and grey rect on right, both 35x60. */ p5.prototype.alpha = function(c) { - // if (c instanceof p5.Color || c instanceof Array) { - // return this.color(c)._getAlpha(); - // } else { - // throw new Error('Needs p5.Color or pixel array as argument.'); - // } this._validateParameters('alpha', arguments); return this.color(c)._getAlpha(); }; @@ -66,6 +62,7 @@ p5.prototype.alpha = function(c) { * * @method blue * @param {p5.Color|Array} color p5.Color object or pixel array + * @return {Number} the blue value * @example *
* @@ -85,11 +82,6 @@ p5.prototype.alpha = function(c) { * */ p5.prototype.blue = function(c) { - // if (c instanceof p5.Color || c instanceof Array) { - // return this.color(c)._getBlue(); - // } else { - // throw new Error('Needs p5.Color or pixel array as argument.'); - // } this._validateParameters('blue', arguments); return this.color(c)._getBlue(); }; @@ -99,6 +91,7 @@ p5.prototype.blue = function(c) { * * @method brightness * @param {p5.Color|Array} color p5.Color object or pixel array + * @return {Number} the brightness value * @example *
* @@ -118,11 +111,6 @@ p5.prototype.blue = function(c) { * */ p5.prototype.brightness = function(c) { - // if (c instanceof p5.Color || c instanceof Array) { - // return this.color(c)._getBrightness(); - // } else { - // throw new Error('Needs p5.Color or pixel array as argument.'); - // } this._validateParameters('brightness', arguments); return this.color(c)._getBrightness(); }; @@ -148,7 +136,7 @@ p5.prototype.brightness = function(c) { * and black. * @param {Number} [alpha] alpha value relative to current color range * (default is 0-255) - * @return {Array} resulting color + * @return {p5.Color} resulting color * * @example *
@@ -284,16 +272,28 @@ p5.prototype.brightness = function(c) { * Dark blue rect on left and light teal rect on right of canvas, both 45x80. * */ - /** * @method color - * @param {Number|String} v1 red or hue value relative to - * the current color range, or a color string + * @param {Number} v1 red or hue value relative to + * the current color range * @param {Number} v2 green or saturation value * relative to the current color range * @param {Number} v3 blue or brightness value * relative to the current color range * @param {Number} [alpha] + * @return {p5.Color} + */ +/** + * @method color + * @param {String} value a color string + * @param {Number} [alpha] + * @return {p5.Color} + */ +/** + * @method color + * @param {Array} values an array containing the red,green,blue & + * and alpha components of the color + * @return {p5.Color} */ p5.prototype.color = function() { From 868cf10418fce6f0311b33a4ceca40365390664e Mon Sep 17 00:00:00 2001 From: almchung Date: Thu, 3 Aug 2017 15:01:32 -0700 Subject: [PATCH 23/37] color/creading_reading: updated documentation and clean ups --- src/color/creating_reading.js | 36 ++++++++--------------------------- 1 file changed, 8 insertions(+), 28 deletions(-) diff --git a/src/color/creating_reading.js b/src/color/creating_reading.js index a896f72aad..75c30d1568 100644 --- a/src/color/creating_reading.js +++ b/src/color/creating_reading.js @@ -320,6 +320,7 @@ p5.prototype.color = function() { * * @method green * @param {p5.Color|Array} color p5.Color object or pixel array + * @return {Number} the green value * @example *
* @@ -340,11 +341,6 @@ p5.prototype.color = function() { */ p5.prototype.green = function(c) { - // if (c instanceof p5.Color || c instanceof Array) { - // return this.color(c)._getGreen(); - // } else { - // throw new Error('Needs p5.Color or pixel array as argument.'); - // } this._validateParameters('green', arguments); return this.color(c)._getGreen(); }; @@ -360,6 +356,7 @@ p5.prototype.green = function(c) { * * @method hue * @param {p5.Color|Array} color p5.Color object or pixel array + * @return {Number} the hue * @example *
* @@ -380,11 +377,6 @@ p5.prototype.green = function(c) { */ p5.prototype.hue = function(c) { - // if (c instanceof p5.Color || c instanceof Array) { - // return this.color(c)._getHue(); - // } else { - // throw new Error('Needs p5.Color or pixel array as argument.'); - // } this._validateParameters('hue', arguments); return this.color(c)._getHue(); }; @@ -401,10 +393,10 @@ p5.prototype.hue = function(c) { * The way that colours are interpolated depends on the current color mode. * * @method lerpColor - * @param {p5.Color|Array} c1 interpolate from this color - * @param {p5.Color|Array} c2 interpolate to this color + * @param {p5.Color} c1 interpolate from this color + * @param {p5.Color} c2 interpolate to this color * @param {Number} amt number between 0 and 1 - * @return {Array|Number} interpolated color + * @return {p5.Color} interpolated color * @example *
* @@ -483,6 +475,7 @@ p5.prototype.lerpColor = function() { * * @method lightness * @param {p5.Color|Array} color p5.Color object or pixel array + * @return {Number} the lightness * @example *
* @@ -502,11 +495,6 @@ p5.prototype.lerpColor = function() { * */ p5.prototype.lightness = function(c) { - // if (c instanceof p5.Color || c instanceof Array) { - // return this.color(c)._getLightness(); - // } else { - // throw new Error('Needs p5.Color or pixel array as argument.'); - // } this._validateParameters('lightness', arguments); return this.color(c)._getLightness(); }; @@ -516,6 +504,7 @@ p5.prototype.lightness = function(c) { * * @method red * @param {p5.Color|Array} color p5.Color object or pixel array + * @return {Number} the red value * @example *
* @@ -545,11 +534,6 @@ p5.prototype.lightness = function(c) { * grey canvas */ p5.prototype.red = function(c) { - // if (c instanceof p5.Color || c instanceof Array) { - // return this.color(c)._getRed(); - // } else { - // throw new Error('Needs p5.Color or pixel array as argument.'); - // } this._validateParameters('red', arguments); return this.color(c)._getRed(); }; @@ -564,6 +548,7 @@ p5.prototype.red = function(c) { * * @method saturation * @param {p5.Color|Array} color p5.Color object or pixel array + * @return {Number} the saturation value * @example *
* @@ -584,11 +569,6 @@ p5.prototype.red = function(c) { */ p5.prototype.saturation = function(c) { - // if (c instanceof p5.Color || c instanceof Array) { - // return this.color(c)._getSaturation(); - // } else { - // throw new Error('Needs p5.Color or pixel array as argument.'); - // } this._validateParameters('saturation', arguments); return this.color(c)._getSaturation(); }; From 4692678f75b2767a32de70d676655492e208a120 Mon Sep 17 00:00:00 2001 From: almchung Date: Mon, 7 Aug 2017 14:54:06 -0700 Subject: [PATCH 24/37] added unit tests for color/creating_reading, added FES for utilities/string_functions --- src/utilities/string_functions.js | 11 +++ test/unit/color/creating_reading.js | 106 ++++++++++++++++++++++++++++ 2 files changed, 117 insertions(+) diff --git a/src/utilities/string_functions.js b/src/utilities/string_functions.js index b3231b4732..4865917cec 100644 --- a/src/utilities/string_functions.js +++ b/src/utilities/string_functions.js @@ -8,6 +8,7 @@ 'use strict'; var p5 = require('../core/core'); +require('../core/error_helpers'); //return p5; //LM is this a mistake? @@ -36,6 +37,7 @@ var p5 = require('../core/core'); * */ p5.prototype.join = function(list, separator) { + this._validateParameters('join', arguments); return list.join(separator); }; @@ -76,6 +78,7 @@ p5.prototype.join = function(list, separator) { * */ p5.prototype.match = function(str, reg) { + this._validateParameters('match', arguments); return str.match(reg); }; @@ -113,6 +116,7 @@ p5.prototype.match = function(str, reg) { */ p5.prototype.matchAll = function(str, reg) { + this._validateParameters('matchAll', arguments); var re = new RegExp(reg, 'g'); var match = re.exec(str); var matches = []; @@ -169,6 +173,7 @@ p5.prototype.matchAll = function(str, reg) { * */ p5.prototype.nf = function () { + this._validateParameters('nf', arguments); if (arguments[0] instanceof Array) { var a = arguments[1]; var b = arguments[2]; @@ -270,6 +275,7 @@ function doNf() { * */ p5.prototype.nfc = function () { + this._validateParameters('nfc', arguments); if (arguments[0] instanceof Array) { var a = arguments[1]; return arguments[0].map(function (x) { @@ -345,6 +351,7 @@ function doNfc() { * */ p5.prototype.nfp = function() { + this._validateParameters('nfp', arguments); var nfRes = this.nf.apply(this, arguments); if (nfRes instanceof Array) { return nfRes.map(addNfp); @@ -402,6 +409,7 @@ function addNfp() { * */ p5.prototype.nfs = function() { + this._validateParameters('nfs', arguments); var nfRes = this.nf.apply(this, arguments); if (nfRes instanceof Array) { return nfRes.map(addNfs); @@ -447,6 +455,7 @@ function addNfs() { * */ p5.prototype.split = function(str, delim) { + this._validateParameters('split', arguments); return str.split(delim); }; @@ -477,6 +486,7 @@ p5.prototype.split = function(str, delim) { *
*/ p5.prototype.splitTokens = function() { + this._validateParameters('splitTokens', arguments); var d,sqo,sqc,str; str = arguments[1]; if (arguments.length > 1) { @@ -523,6 +533,7 @@ p5.prototype.splitTokens = function() { * */ p5.prototype.trim = function(str) { + this._validateParameters('trim', arguments); if (str instanceof Array) { return str.map(this.trim); } else { diff --git a/test/unit/color/creating_reading.js b/test/unit/color/creating_reading.js index b2f608fa47..acf80fe5e1 100644 --- a/test/unit/color/creating_reading.js +++ b/test/unit/color/creating_reading.js @@ -6,6 +6,106 @@ suite('CreatingReading', function() { var fromColor; var toColor; + var c; + var val; + + suite('p5.prototype.alpha', function() { + setup(function() { + myp5.colorMode(myp5.RGB); + }); + test('alpha(): no friendly-err-msg I', function() { + assert.doesNotThrow(function() { + c = myp5.color('magenta'); + val = myp5.alpha(c); + assert.equal(val, 255); + }, + Error, 'got unwanted exception'); + }); + test('alpha(): no friendly-err-msg II', function() { + assert.doesNotThrow(function() { + c = myp5.color('hsba(160, 100%, 50%, 0.5)'); + val = myp5.alpha(c); + assert.equal(val, 127.5); + }, + Error, 'got unwanted exception'); + }); + test('alpha(): wrong param type at #1', function() { + assert.doesNotThrow(function() { + c = 20; + val = myp5.alpha(c); + }, + Error, 'got unwanted exception'); + }); + }); + + suite('p5.prototype.red, green, blue', function() { + setup(function() { + myp5.colorMode(myp5.RGB); + }); + test('red(): no friendly-err-msg', function() { + assert.doesNotThrow(function() { + c = myp5.color('hsl(126, 100%, 60%)'); + val = myp5.red(c); + expect(Math.abs(val-51)).to.be.at.most(2); // max approx err 2 + }, + Error, 'got unwanted exception'); + }); + test('green(): no friendly-err-msg', function() { + assert.doesNotThrow(function() { + c = myp5.color('hsl(126, 100%, 60%)'); + val = myp5.green(c); + assert.equal(val, 255); + }, + Error, 'got unwanted exception'); + }); + test('blue(): no friendly-err-msg', function() { + assert.doesNotThrow(function() { + c = myp5.color('hsl(126, 100%, 60%)'); + val = myp5.blue(c); + expect(Math.abs(val-70)).to.be.at.most(2); // max approx err 2 + }, + Error, 'got unwanted exception'); + }); + }); + + suite('p5.prototype.hue, brightness, lightness, saturation', function() { + setup(function() { + myp5.colorMode(myp5.HSL); + }); + test('hue(): no friendly-err-msg', function() { + assert.doesNotThrow(function() { + c = myp5.color('#7fffd4'); + val = myp5.hue(c); + expect(Math.abs(val-160)).to.be.at.most(2); // max approx err 2 + }, + Error, 'got unwanted exception'); + }); + test('brightness(): no friendly-err-msg', function() { + assert.doesNotThrow(function() { + c = myp5.color('#7fffd4'); + val = myp5.brightness(c); + assert.equal(val, 100); + }, + Error, 'got unwanted exception'); + }); + test('lightness(): no friendly-err-msg', function() { + assert.doesNotThrow(function() { + c = myp5.color('#7fffd4'); + val = myp5.lightness(c); + expect(Math.abs(val-75)).to.be.at.most(2); // max approx err 2 + }, + Error, 'got unwanted exception'); + }); + test('saturation(): no friendly-err-msg', function() { + assert.doesNotThrow(function() { + c = myp5.color('#7fffd4'); + val = myp5.saturation(c); + assert.equal(val, 100); + }, + Error, 'got unwanted exception'); + }); + }); + suite('p5.prototype.lerpColor', function() { setup(function() { myp5.colorMode(myp5.RGB); @@ -38,6 +138,12 @@ suite('CreatingReading', function() { assert.deepEqual(interA.levels, [218, 165, 32, 255]); assert.deepEqual(interB.levels, [72, 61, 139, 255]); }); + test('lerpColor(): missing param #2', function() { + assert.doesNotThrow(function() { + myp5.lerpColor(fromColor, toColor); + }, + Error, 'got unwanted exception'); + }); }); suite('p5.prototype.lerpColor with alpha', function() { setup(function() { From 9bae08ece370386dfab259ab0965d4232e441f58 Mon Sep 17 00:00:00 2001 From: almchung Date: Wed, 9 Aug 2017 13:52:13 -0700 Subject: [PATCH 25/37] added unit tests for string_functions; updated string_functions inline doc --- src/utilities/string_functions.js | 79 +++++++---- test/unit/utilities/string_functions.js | 170 ++++++++++++++++++++++++ 2 files changed, 225 insertions(+), 24 deletions(-) create mode 100644 test/unit/utilities/string_functions.js diff --git a/src/utilities/string_functions.js b/src/utilities/string_functions.js index 4865917cec..1721c634d1 100644 --- a/src/utilities/string_functions.js +++ b/src/utilities/string_functions.js @@ -37,7 +37,7 @@ require('../core/error_helpers'); * */ p5.prototype.join = function(list, separator) { - this._validateParameters('join', arguments); + p5.prototype._validateParameters('join', arguments); return list.join(separator); }; @@ -78,7 +78,7 @@ p5.prototype.join = function(list, separator) { * */ p5.prototype.match = function(str, reg) { - this._validateParameters('match', arguments); + p5.prototype._validateParameters('match', arguments); return str.match(reg); }; @@ -113,10 +113,9 @@ p5.prototype.match = function(str, reg) { * matchAll(string, regexp); * *
- */ p5.prototype.matchAll = function(str, reg) { - this._validateParameters('matchAll', arguments); + p5.prototype._validateParameters('matchAll', arguments); var re = new RegExp(reg, 'g'); var match = re.exec(str); var matches = []; @@ -137,12 +136,19 @@ p5.prototype.matchAll = function(str, reg) { * be positive integers. * * @method nf - * @param {Number|Array} num the Number to format + * @param {Number} num the Number to format * @param {Number} [left] number of digits to the left of the * decimal point * @param {Number} [right] number of digits to the right of the * decimal point - * @return {String|Array} formatted String + * @return {String} formatted String + */ +/** + * @method nf + * @param {Array} nums the Numbers to format + * @param {Number} [left] + * @param {Number} [right] + * @return {Array} formatted Strings * @example *
* @@ -173,7 +179,7 @@ p5.prototype.matchAll = function(str, reg) { * */ p5.prototype.nf = function () { - this._validateParameters('nf', arguments); + p5.prototype._validateParameters('nf', arguments); if (arguments[0] instanceof Array) { var a = arguments[1]; var b = arguments[2]; @@ -243,10 +249,16 @@ function doNf() { * for the right parameter should always be a positive integer. * * @method nfc - * @param {Number|Array} num the Number to format - * @param {Number} [right] number of digits to the right of the + * @param {Number} num the Number to format + * @param {Number} [right] number of digits to the right of the * decimal point - * @return {String|Array} formatted String + * @return {String} formatted String + */ +/** + * @method nfc + * @param {Array} nums the Numbers to format + * @param {Number} [right] + * @return {Array} formatted Strings * @example *
* @@ -275,7 +287,7 @@ function doNf() { * */ p5.prototype.nfc = function () { - this._validateParameters('nfc', arguments); + p5.prototype._validateParameters('nfc', arguments); if (arguments[0] instanceof Array) { var a = arguments[1]; return arguments[0].map(function (x) { @@ -317,12 +329,19 @@ function doNfc() { * should always be positive integers. * * @method nfp - * @param {Number|Array} num the Number to format + * @param {Number} num the Number to format * @param {Number} [left] number of digits to the left of the decimal * point * @param {Number} [right] number of digits to the right of the * decimal point - * @return {String|Array} formatted String + * @return {String} formatted String + */ +/** + * @method nfp + * @param {Number[]} nums the Numbers to format + * @param {Number} [left] + * @param {Number} [right] + * @return {String[]} formatted Strings * @example *
* @@ -351,8 +370,8 @@ function doNfc() { * */ p5.prototype.nfp = function() { - this._validateParameters('nfp', arguments); - var nfRes = this.nf.apply(this, arguments); + p5.prototype._validateParameters('nfp', arguments); + var nfRes = p5.prototype.nf.apply(this, arguments); if (nfRes instanceof Array) { return nfRes.map(addNfp); } else { @@ -375,12 +394,19 @@ function addNfp() { * parameters should always be positive integers. * * @method nfs - * @param {Number|Array} num the Number to format + * @param {Number} num the Number to format * @param {Number} [left] number of digits to the left of the decimal * point * @param {Number} [right] number of digits to the right of the * decimal point - * @return {String|Array} formatted String + * @return {String} formatted String + */ +/** + * @method nfs + * @param {Array} nums the Numbers to format + * @param {Number} [left] + * @param {Number} [right] + * @return {Array} formatted Strings * @example *
* @@ -409,8 +435,8 @@ function addNfp() { * */ p5.prototype.nfs = function() { - this._validateParameters('nfs', arguments); - var nfRes = this.nf.apply(this, arguments); + p5.prototype._validateParameters('nfs', arguments); + var nfRes = p5.prototype.nf.apply(this, arguments); if (nfRes instanceof Array) { return nfRes.map(addNfs); } else { @@ -455,7 +481,7 @@ function addNfs() { * */ p5.prototype.split = function(str, delim) { - this._validateParameters('split', arguments); + p5.prototype._validateParameters('split', arguments); return str.split(delim); }; @@ -486,7 +512,7 @@ p5.prototype.split = function(str, delim) { *
*/ p5.prototype.splitTokens = function() { - this._validateParameters('splitTokens', arguments); + p5.prototype._validateParameters('splitTokens', arguments); var d,sqo,sqc,str; str = arguments[1]; if (arguments.length > 1) { @@ -518,8 +544,13 @@ p5.prototype.splitTokens = function() { * and tab, this function also removes the Unicode "nbsp" character. * * @method trim - * @param {String|Array} str a String or Array of Strings to be trimmed - * @return {String|Array} a trimmed String or Array of Strings + * @param {String} str a String to be trimmed + * @return {String} a trimmed String + */ +/** + * @method trim + * @param {Array} strs an Array of Strings to be trimmed + * @return {Array} an Array of trimmed Strings * @example *
* @@ -533,7 +564,7 @@ p5.prototype.splitTokens = function() { * */ p5.prototype.trim = function(str) { - this._validateParameters('trim', arguments); + p5.prototype._validateParameters('trim', arguments); if (str instanceof Array) { return str.map(this.trim); } else { diff --git a/test/unit/utilities/string_functions.js b/test/unit/utilities/string_functions.js new file mode 100644 index 0000000000..3851462e21 --- /dev/null +++ b/test/unit/utilities/string_functions.js @@ -0,0 +1,170 @@ +suite('String functions', function() { + + var result; + + suite('p5.prototype.join', function() { + var join = p5.prototype.join; + suite('join()', function() { + test('should be a function', function() { + assert.ok(join); + }); + test('should return joined string', function() { + var arr = ['foo', 'bar']; + var sep = '-'; + result = join(arr, sep); + assert.equal(result, 'foo-bar'); + }); + }); + }); + + suite('p5.prototype.match', function() { + var match = p5.prototype.match; + suite('match()', function() { + test('should be a function', function() { + assert.ok(match); + }); + test('should return correct index of match strings', function() { + var str = 'Where is the duckling in this ducky duck string?'; + var regexp = 'duck'; + result = match(str, regexp); + assert.equal(result.index, 13); + }); + }); + }); + + suite('p5.prototype.matchAll', function() { + var matchAll = p5.prototype.matchAll; + suite('matchAll()', function() { + test('should be a function', function() { + assert.ok(matchAll); + }); + test('should return correct array of strings', function() { + var str = 'Where is the duckling in this ducky duck string?'; + var regexp = 'duck'; + result = matchAll(str, regexp); + assert.equal(result.length, 3); + }); + }); + }); + + suite('p5.prototype.nf', function() { + var nf = p5.prototype.nf; + suite('nf()', function() { + test('should be a function', function() { + assert.ok(nf); + }); + test('should return correct string', function() { + var num = 3.141516; + result = nf(num, 2); + assert.equal(result, '03.141516'); + }); + test('FES: false positive case (#1)', function() { + var num = 3.141516; + result = nf(num, '2'); // automatic conversion? + assert.equal(result, '03.141516'); + }); + }); + }); + + suite('p5.prototype.nfc', function() { + var nfc = p5.prototype.nfc; + suite('nfc()', function() { + test('should be a function', function() { + assert.ok(nfc); + }); + test('should return correct string', function() { + var num = 32000; + result = nfc(num, 3); + assert.equal(result, '32,000.000'); + }); + test('FES: false positive case (#1)', function() { + var num = 32000; + result = nfc(num, '3'); // automatic conversion? + assert.equal(result, '32,000.000'); + }); + }); + }); + + suite('p5.prototype.nfp', function() { + var nfp = p5.prototype.nfp; + suite('nfp()', function() { + test('should be a function', function() { + assert.ok(nfp); + }); + test('should return correct string', function() { + var num = -32000; + result = nfp(num, 3); + assert.equal(result, '-32000'); + }); + test('should return correct string', function() { + var num = 32000; + result = nfp(num, 3); // automatic conversion? + assert.equal(result, '+32000'); + }); + }); + }); + + suite('p5.prototype.nfs', function() { + var nfs = p5.prototype.nfs; + suite('nfs()', function() { + test('should be a function', function() { + assert.ok(nfs); + }); + test('should return correct string', function() { + var num = -32000; + result = nfs(num, 3); + assert.equal(result, '-32000'); + }); + test('should return correct string', function() { + var num = 32000; + result = nfs(num, 3); // automatic conversion? + assert.equal(result, ' 32000'); + }); + }); + }); + + suite('p5.prototype.split', function() { + var split = p5.prototype.split; + suite('split()', function() { + test('should be a function', function() { + assert.ok(split); + }); + test('should return correct index of match strings', function() { + var str = 'parsely, sage, rosemary, thyme'; + var regexp = ','; + result = split(str, regexp); + assert.equal(result.length, 4); + }); + }); + }); + + suite('p5.prototype.splitTokens', function() { + var splitTokens = p5.prototype.splitTokens; + suite('splitTokens()', function() { + test('should be a function', function() { + assert.ok(splitTokens); + }); + test('should return correct index of match strings', function() { + var str = 'parsely, sage, rosemary, thyme'; + var regexp = ','; + result = splitTokens(str, regexp); + assert.equal(result.length, 4); + }); + }); + }); + + suite('p5.prototype.trim', function() { + var trim = p5.prototype.trim; + suite('trim()', function() { + test('should be a function', function() { + assert.ok(trim); + }); + test('should return correct strings', function() { + var str = ' oh so roomy '; + result = trim(str); + assert.equal(result, 'oh so roomy'); + }); + }); + }); + +}); From 7b38599475e9a591e61941309ba4b0e15f04c396 Mon Sep 17 00:00:00 2001 From: almchung Date: Tue, 15 Aug 2017 19:30:49 -0700 Subject: [PATCH 26/37] clean up for merge; fixed FES bugs --- Gruntfile.js | 2 +- src/color/creating_reading.js | 11 +++++-- src/core/curves.js | 8 +++--- src/utilities/string_functions.js | 38 ++++++++++++------------- test/test.html | 3 +- test/unit/color/creating_reading.js | 5 ++-- test/unit/core/2d_primitives.js | 20 ------------- test/unit/core/curves.js | 26 ++++++++--------- test/unit/utilities/string_functions.js | 38 ++++++++++++++----------- 9 files changed, 71 insertions(+), 80 deletions(-) diff --git a/Gruntfile.js b/Gruntfile.js index a151ed4cf5..b34b39b716 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -212,7 +212,7 @@ module.exports = function(grunt) { run: true, log: true, logErrors: true, - timeout: 5000 + timeout: 50000 } }, }, diff --git a/src/color/creating_reading.js b/src/color/creating_reading.js index 75c30d1568..9c19ec8f6d 100644 --- a/src/color/creating_reading.js +++ b/src/color/creating_reading.js @@ -295,9 +295,14 @@ p5.prototype.brightness = function(c) { * and alpha components of the color * @return {p5.Color} */ +/** + * @method color + * @param {p5.Color} color + * @return {p5.Color} + */ p5.prototype.color = function() { - //p5.prototype._validateParameters('color', arguments); + //this._validateParameters('color', arguments); if (arguments[0] instanceof p5.Color) { return arguments[0]; // Do nothing if argument is already a color object. } else if (arguments[0] instanceof Array) { @@ -424,7 +429,7 @@ p5.prototype.hue = function(c) { * */ -p5.prototype.lerpColor = function() { +p5.prototype.lerpColor = function(c1, c2, amt) { this._validateParameters('lerpColor', arguments); var mode = this._renderer._colorMode; var maxes = this._renderer._colorMaxes; @@ -453,7 +458,7 @@ p5.prototype.lerpColor = function() { } // Prevent extrapolation. - var amt = Math.max(Math.min(arguments[2], 1), 0); + amt = Math.max(Math.min(arguments[2], 1), 0); // Perform interpolation. l0 = this.lerp(fromArray[0], toArray[0], amt); diff --git a/src/core/curves.js b/src/core/curves.js index 8505e2fa88..97a21b13cb 100644 --- a/src/core/curves.js +++ b/src/core/curves.js @@ -164,7 +164,7 @@ p5.prototype.bezierPoint = function(a, b, c, d, t) { args[i] = arguments[i]; } // check with FES:validateParameters - p5.prototype._validateParameters('bezierPoint', args); + this._validateParameters('bezierPoint', args); var adjustedT = 1-args[4]; return Math.pow(adjustedT,3)*args[0] + 3*(Math.pow(adjustedT,2))*args[4]*args[1] + @@ -243,7 +243,7 @@ p5.prototype.bezierTangent = function(a, b, c, d, t) { args[i] = arguments[i]; } // check with FES:validateParameters - p5.prototype._validateParameters('bezierTangent', args); + this._validateParameters('bezierTangent', args); var adjustedT = 1-args[4]; return 3*args[3]*Math.pow(args[4],2) - 3*args[2]*Math.pow(args[4],2) + @@ -463,7 +463,7 @@ p5.prototype.curvePoint = function(a, b, c, d, t) { args[i] = arguments[i]; } // check with FES:validateParameters - p5.prototype._validateParameters('curvePoint', args); + this._validateParameters('curvePoint', args); var t3 = args[4]*args[4]*args[4], t2 = args[4]*args[4], f1 = -0.5 * t3 + t2 - 0.5 * args[4], @@ -514,7 +514,7 @@ p5.prototype.curveTangent = function(a, b, c, d, t) { args[i] = arguments[i]; } // check with FES:validateParameters - p5.prototype._validateParameters('curveTangent', args); + this._validateParameters('curveTangent', args); var t2 = args[4]*args[4], f1 = (-3*t2)/2 + 2*args[4] - 0.5, f2 = (9*t2)/2 - 5*args[4], diff --git a/src/utilities/string_functions.js b/src/utilities/string_functions.js index 1721c634d1..e2794f32d5 100644 --- a/src/utilities/string_functions.js +++ b/src/utilities/string_functions.js @@ -37,7 +37,7 @@ require('../core/error_helpers'); * */ p5.prototype.join = function(list, separator) { - p5.prototype._validateParameters('join', arguments); + this._validateParameters('join', arguments); return list.join(separator); }; @@ -78,7 +78,7 @@ p5.prototype.join = function(list, separator) { * */ p5.prototype.match = function(str, reg) { - p5.prototype._validateParameters('match', arguments); + this._validateParameters('match', arguments); return str.match(reg); }; @@ -115,7 +115,7 @@ p5.prototype.match = function(str, reg) { *
*/ p5.prototype.matchAll = function(str, reg) { - p5.prototype._validateParameters('matchAll', arguments); + this._validateParameters('matchAll', arguments); var re = new RegExp(reg, 'g'); var match = re.exec(str); var matches = []; @@ -136,19 +136,19 @@ p5.prototype.matchAll = function(str, reg) { * be positive integers. * * @method nf - * @param {Number} num the Number to format - * @param {Number} [left] number of digits to the left of the + * @param {Number|String} num the Number to format + * @param {Number|String} [left] number of digits to the left of the * decimal point - * @param {Number} [right] number of digits to the right of the + * @param {Number|String} [right] number of digits to the right of the * decimal point * @return {String} formatted String */ /** * @method nf * @param {Array} nums the Numbers to format - * @param {Number} [left] - * @param {Number} [right] - * @return {Array} formatted Strings + * @param {Number|String} [left] + * @param {Number|String} [right] + * @return {Array} formatted Strings\ * @example *
* @@ -179,7 +179,7 @@ p5.prototype.matchAll = function(str, reg) { * */ p5.prototype.nf = function () { - p5.prototype._validateParameters('nf', arguments); + this._validateParameters('nf', arguments); if (arguments[0] instanceof Array) { var a = arguments[1]; var b = arguments[2]; @@ -249,15 +249,15 @@ function doNf() { * for the right parameter should always be a positive integer. * * @method nfc - * @param {Number} num the Number to format - * @param {Number} [right] number of digits to the right of the + * @param {Number|String} num the Number to format + * @param {Number|String} [right] number of digits to the right of the * decimal point * @return {String} formatted String */ /** * @method nfc * @param {Array} nums the Numbers to format - * @param {Number} [right] + * @param {Number|String} [right] * @return {Array} formatted Strings * @example *
@@ -287,7 +287,7 @@ function doNf() { * */ p5.prototype.nfc = function () { - p5.prototype._validateParameters('nfc', arguments); + this._validateParameters('nfc', arguments); if (arguments[0] instanceof Array) { var a = arguments[1]; return arguments[0].map(function (x) { @@ -370,7 +370,7 @@ function doNfc() { * */ p5.prototype.nfp = function() { - p5.prototype._validateParameters('nfp', arguments); + this._validateParameters('nfp', arguments); var nfRes = p5.prototype.nf.apply(this, arguments); if (nfRes instanceof Array) { return nfRes.map(addNfp); @@ -435,7 +435,7 @@ function addNfp() { * */ p5.prototype.nfs = function() { - p5.prototype._validateParameters('nfs', arguments); + this._validateParameters('nfs', arguments); var nfRes = p5.prototype.nf.apply(this, arguments); if (nfRes instanceof Array) { return nfRes.map(addNfs); @@ -481,7 +481,7 @@ function addNfs() { * */ p5.prototype.split = function(str, delim) { - p5.prototype._validateParameters('split', arguments); + this._validateParameters('split', arguments); return str.split(delim); }; @@ -512,7 +512,7 @@ p5.prototype.split = function(str, delim) { *
*/ p5.prototype.splitTokens = function() { - p5.prototype._validateParameters('splitTokens', arguments); + this._validateParameters('splitTokens', arguments); var d,sqo,sqc,str; str = arguments[1]; if (arguments.length > 1) { @@ -564,7 +564,7 @@ p5.prototype.splitTokens = function() { * */ p5.prototype.trim = function(str) { - p5.prototype._validateParameters('trim', arguments); + this._validateParameters('trim', arguments); if (str instanceof Array) { return str.map(this.trim); } else { diff --git a/test/test.html b/test/test.html index 7519c3a034..7110104540 100644 --- a/test/test.html +++ b/test/test.html @@ -35,7 +35,7 @@ - + @@ -50,6 +50,7 @@ + diff --git a/test/unit/color/creating_reading.js b/test/unit/color/creating_reading.js index acf80fe5e1..971ed66349 100644 --- a/test/unit/color/creating_reading.js +++ b/test/unit/color/creating_reading.js @@ -15,7 +15,8 @@ suite('CreatingReading', function() { }); test('alpha(): no friendly-err-msg I', function() { assert.doesNotThrow(function() { - c = myp5.color('magenta'); + var string = 'magenta'; + c = myp5.color(string); val = myp5.alpha(c); assert.equal(val, 255); }, @@ -29,7 +30,7 @@ suite('CreatingReading', function() { }, Error, 'got unwanted exception'); }); - test('alpha(): wrong param type at #1', function() { + test('alpha(): wrong param type at #0', function() { assert.doesNotThrow(function() { c = 20; val = myp5.alpha(c); diff --git a/test/unit/core/2d_primitives.js b/test/unit/core/2d_primitives.js index 769157a32b..0f5cd9213b 100644 --- a/test/unit/core/2d_primitives.js +++ b/test/unit/core/2d_primitives.js @@ -44,16 +44,6 @@ suite('2D Primitives', function() { assert.ok(ellipse); assert.typeOf(ellipse, 'function'); }); - test('should draw', function(done) { - myp5.background(155); - myp5.fill(0); - myp5.ellipse(0, 0, 100, 100); - - testRender('unit/assets/renders/ellipse.png', myp5, function(res) { - assert.isTrue(res); - done(); - }); - }); test('ellipse(): no friendly-err-msg', function() { assert.doesNotThrow(function() { myp5.ellipse(0, 0, 100); @@ -89,16 +79,6 @@ suite('2D Primitives', function() { assert.ok(line); assert.typeOf(line, 'function'); }); - test('should draw', function(done) { - myp5.background(155); - myp5.fill(0); - myp5.line(0, 0, 100, 100); - - testRender('unit/assets/renders/line.png', myp5, function(res) { - assert.isTrue(res); - done(); - }); - }); test('line(): no friendly-err-msg, 2D', function() { assert.doesNotThrow(function() { myp5.line(0, 0, 100, 100); diff --git a/test/unit/core/curves.js b/test/unit/core/curves.js index dcc0e95e1a..a3f42f0a95 100644 --- a/test/unit/core/curves.js +++ b/test/unit/core/curves.js @@ -40,12 +40,12 @@ suite('Curves', function() { assert.ok(bezierPoint); assert.typeOf(bezierPoint, 'function'); }); - test('should return a number', function() { - result = bezierPoint(); + test('should return a number: missing param #0~4', function() { + result = myp5.bezierPoint(); assert.typeOf(result, 'number'); }); test('should return the correct point on a Bezier Curve', function() { - result = bezierPoint(85, 10, 90, 15, 0.5); + result = myp5.bezierPoint(85, 10, 90, 15, 0.5); assert.equal(result, 50); assert.notEqual(result, -1); }); @@ -55,17 +55,17 @@ suite('Curves', function() { suite('p5.prototype.bezierTangent', function() { var bezierTangent = p5.prototype.bezierTangent; var result; - suite('curveTangent()', function() { + suite('bezierTangent()', function() { test('should be a function', function() { assert.ok(bezierTangent); assert.typeOf(bezierTangent, 'function'); }); - test('should return a number', function() { - result = bezierTangent(); + test('should return a number: missing param #0~4', function() { + result = myp5.bezierTangent(); assert.typeOf(result, 'number'); }); test('should return the correct point on a Bezier Curve', function() { - result = bezierTangent(95, 73, 73, 15, 0.5); + result = myp5.bezierTangent(95, 73, 73, 15, 0.5); assert.equal(result, -60); }); }); @@ -107,12 +107,12 @@ suite('Curves', function() { assert.ok(curvePoint); assert.typeOf(curvePoint, 'function'); }); - test('should return a number', function() { - result = curvePoint(); + test('should return a number: missing param #0~4', function() { + result = myp5.curvePoint(); assert.typeOf(result, 'number'); }); test('should return the correct point on a Catmull-Rom Curve', function() { - result = curvePoint(5, 5, 73, 73, 0.5); + result = myp5.curvePoint(5, 5, 73, 73, 0.5); assert.equal(result, 39); assert.notEqual(result, -1); }); @@ -127,12 +127,12 @@ suite('Curves', function() { assert.ok(curveTangent); assert.typeOf(curveTangent, 'function'); }); - test('should return a number', function() { - result = curveTangent(); + test('should return a number: missing param #0~4', function() { + result = myp5.curveTangent(); assert.typeOf(result, 'number'); }); test('should return the correct point on a Catmull-Rom Curve', function() { - result = curveTangent(95, 73, 73, 15, 0.5); + result = myp5.curveTangent(95, 73, 73, 15, 0.5); assert.equal(result, 10); assert.notEqual(result, -1); }); diff --git a/test/unit/utilities/string_functions.js b/test/unit/utilities/string_functions.js index 3851462e21..2be59206bb 100644 --- a/test/unit/utilities/string_functions.js +++ b/test/unit/utilities/string_functions.js @@ -1,4 +1,8 @@ suite('String functions', function() { + var myp5 = new p5(function( p ) { + p.setup = function() {}; + p.draw = function() {}; + }); var result; @@ -11,7 +15,7 @@ suite('String functions', function() { test('should return joined string', function() { var arr = ['foo', 'bar']; var sep = '-'; - result = join(arr, sep); + result = myp5.join(arr, sep); assert.equal(result, 'foo-bar'); }); }); @@ -26,7 +30,7 @@ suite('String functions', function() { test('should return correct index of match strings', function() { var str = 'Where is the duckling in this ducky duck string?'; var regexp = 'duck'; - result = match(str, regexp); + result = myp5.match(str, regexp); assert.equal(result.index, 13); }); }); @@ -41,7 +45,7 @@ suite('String functions', function() { test('should return correct array of strings', function() { var str = 'Where is the duckling in this ducky duck string?'; var regexp = 'duck'; - result = matchAll(str, regexp); + result = myp5.matchAll(str, regexp); assert.equal(result.length, 3); }); }); @@ -55,13 +59,13 @@ suite('String functions', function() { }); test('should return correct string', function() { var num = 3.141516; - result = nf(num, 2); + result = myp5.nf(num, 2); assert.equal(result, '03.141516'); }); - test('FES: false positive case (#1)', function() { + test('should return correct string', function() { var num = 3.141516; - result = nf(num, '2'); // automatic conversion? - assert.equal(result, '03.141516'); + result = myp5.nf(num, '2', '2'); // automatic conversion? + assert.equal(result, '03.14'); }); }); }); @@ -74,12 +78,12 @@ suite('String functions', function() { }); test('should return correct string', function() { var num = 32000; - result = nfc(num, 3); + result = myp5.nfc(num, 3); assert.equal(result, '32,000.000'); }); - test('FES: false positive case (#1)', function() { + test('should return correct string', function() { var num = 32000; - result = nfc(num, '3'); // automatic conversion? + result = myp5.nfc(num, '3'); // automatic conversion? assert.equal(result, '32,000.000'); }); }); @@ -93,12 +97,12 @@ suite('String functions', function() { }); test('should return correct string', function() { var num = -32000; - result = nfp(num, 3); + result = myp5.nfp(num, 3); assert.equal(result, '-32000'); }); test('should return correct string', function() { var num = 32000; - result = nfp(num, 3); // automatic conversion? + result = myp5.nfp(num, 3); // automatic conversion? assert.equal(result, '+32000'); }); }); @@ -112,12 +116,12 @@ suite('String functions', function() { }); test('should return correct string', function() { var num = -32000; - result = nfs(num, 3); + result = myp5.nfs(num, 3); assert.equal(result, '-32000'); }); test('should return correct string', function() { var num = 32000; - result = nfs(num, 3); // automatic conversion? + result = myp5.nfs(num, 3); // automatic conversion? assert.equal(result, ' 32000'); }); }); @@ -132,7 +136,7 @@ suite('String functions', function() { test('should return correct index of match strings', function() { var str = 'parsely, sage, rosemary, thyme'; var regexp = ','; - result = split(str, regexp); + result = myp5.split(str, regexp); assert.equal(result.length, 4); }); }); @@ -147,7 +151,7 @@ suite('String functions', function() { test('should return correct index of match strings', function() { var str = 'parsely, sage, rosemary, thyme'; var regexp = ','; - result = splitTokens(str, regexp); + result = myp5.splitTokens(str, regexp); assert.equal(result.length, 4); }); }); @@ -161,7 +165,7 @@ suite('String functions', function() { }); test('should return correct strings', function() { var str = ' oh so roomy '; - result = trim(str); + result = myp5.trim(str); assert.equal(result, 'oh so roomy'); }); }); From 708823a028a5bc6de8893425e74be8bb433f4e25 Mon Sep 17 00:00:00 2001 From: almchung Date: Tue, 15 Aug 2017 20:08:38 -0700 Subject: [PATCH 27/37] p5 vs this --- Gruntfile.js | 2 +- src/color/creating_reading.js | 2 +- src/core/error_helpers.js | 1 + test/unit/core/error_helpers.js | 20 ++++++++++---------- 4 files changed, 13 insertions(+), 12 deletions(-) diff --git a/Gruntfile.js b/Gruntfile.js index b34b39b716..c9501a0ee0 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -212,7 +212,7 @@ module.exports = function(grunt) { run: true, log: true, logErrors: true, - timeout: 50000 + timeout: 1000000 } }, }, diff --git a/src/color/creating_reading.js b/src/color/creating_reading.js index 9c19ec8f6d..1b514a8dd4 100644 --- a/src/color/creating_reading.js +++ b/src/color/creating_reading.js @@ -302,7 +302,6 @@ p5.prototype.brightness = function(c) { */ p5.prototype.color = function() { - //this._validateParameters('color', arguments); if (arguments[0] instanceof p5.Color) { return arguments[0]; // Do nothing if argument is already a color object. } else if (arguments[0] instanceof Array) { @@ -312,6 +311,7 @@ p5.prototype.color = function() { return new p5.Color(this._renderer, arguments[0]); } } else { + p5._validateParameters('color', arguments); if (this instanceof p5.Renderer) { return new p5.Color(this, arguments); } else { diff --git a/src/core/error_helpers.js b/src/core/error_helpers.js index 5eb58756b7..ac6bcfe975 100644 --- a/src/core/error_helpers.js +++ b/src/core/error_helpers.js @@ -407,6 +407,7 @@ function helpForMisusedAtTopLevelCode(e, log) { // Exposing this primarily for unit testing. p5.prototype._helpForMisusedAtTopLevelCode = helpForMisusedAtTopLevelCode; p5.prototype._validateParameters = validateParameters; +p5._validateParameters = validateParameters; if (document.readyState !== 'complete') { window.addEventListener('error', helpForMisusedAtTopLevelCode, false); diff --git a/test/unit/core/error_helpers.js b/test/unit/core/error_helpers.js index 2b4cbf3c12..082a099b9a 100644 --- a/test/unit/core/error_helpers.js +++ b/test/unit/core/error_helpers.js @@ -8,21 +8,21 @@ suite('Error Helpers', function() { suite('validateParameters: Numbers + optional Constant', function(){ test('arc(): no friendly-err-msg', function() { assert.doesNotThrow(function() { - p5.prototype._validateParameters('arc', + p5._validateParameters('arc', [1, 1, 10.5, 10, 0, Math.PI, 'pie']); }, Error, 'got unwanted exception'); }); test('arc(): missing param #4, #5', function() { assert.doesNotThrow(function() { - p5.prototype._validateParameters('arc', + p5._validateParameters('arc', [1, 1, 10.5, 10]); }, Error, 'got unwanted exception'); }); test('arc(): wrong param type at #0', function() { assert.doesNotThrow(function() { - p5.prototype._validateParameters('arc', + p5._validateParameters('arc', ['1', 1, 10.5, 10, 0, Math.PI, 'pie']); }, Error, 'got unwanted exception'); @@ -32,21 +32,21 @@ suite('Error Helpers', function() { suite('validateParameters: Numbers + optional Constant', function(){ test('rect(): no friendly-err-msg', function() { assert.doesNotThrow(function() { - p5.prototype._validateParameters('rect', + p5._validateParameters('rect', [1, 1, 10.5, 10]); }, Error, 'got unwanted exception'); }); test('rect(): missing param #3', function() { assert.doesNotThrow(function() { - p5.prototype._validateParameters('rect', + p5._validateParameters('rect', [1, 1, 10.5]); }, Error, 'got unwanted exception'); }); test('rect(): wrong param type at #0', function() { assert.doesNotThrow(function() { - p5.prototype._validateParameters('rect', + p5._validateParameters('rect', ['1', 1, 10.5, 10, 0, Math.PI]); }, Error, 'got unwanted exception'); @@ -57,7 +57,7 @@ suite('Error Helpers', function() { test('ambientLight(): no friendly-err-msg', function() { assert.doesNotThrow(function() { var c = myp5.color(255, 204, 0); - p5.prototype._validateParameters('ambientLight', [c]); + p5._validateParameters('ambientLight', [c]); }, Error, 'got unwanted exception'); }); @@ -66,19 +66,19 @@ suite('Error Helpers', function() { suite('validateParameters: multi-format', function(){ test('color(): no friendly-err-msg', function() { assert.doesNotThrow(function() { - p5.prototype._validateParameters('color', [65]); + p5._validateParameters('color', [65]); }, Error, 'got unwanted exception'); }); test('color(): no friendly-err-msg', function() { assert.doesNotThrow(function() { - p5.prototype._validateParameters('color', [65, 0.5]); + p5._validateParameters('color', [65, 0.5]); }, Error, 'got unwanted exception'); }); test('color(): no friendly-err-msg', function() { assert.doesNotThrow(function() { - p5.prototype._validateParameters('color', [255, 204, 0]); + p5._validateParameters('color', [255, 204, 0]); }, Error, 'got unwanted exception'); }); From efaa50ff1f2ad4ce6b187b0426af1f4783609515 Mon Sep 17 00:00:00 2001 From: almchung Date: Tue, 15 Aug 2017 20:25:41 -0700 Subject: [PATCH 28/37] this and p5 unified to p5 --- src/color/creating_reading.js | 18 +++++++++--------- src/core/2d_primitives.js | 14 +++++++------- src/core/curves.js | 12 ++++++------ src/utilities/string_functions.js | 20 ++++++++++---------- test/unit/core/2d_primitives.js | 2 +- 5 files changed, 33 insertions(+), 33 deletions(-) diff --git a/src/color/creating_reading.js b/src/color/creating_reading.js index 1b514a8dd4..fe71888679 100644 --- a/src/color/creating_reading.js +++ b/src/color/creating_reading.js @@ -53,7 +53,7 @@ require('../core/error_helpers'); * deep pink rect on left and grey rect on right, both 35x60. */ p5.prototype.alpha = function(c) { - this._validateParameters('alpha', arguments); + p5._validateParameters('alpha', arguments); return this.color(c)._getAlpha(); }; @@ -82,7 +82,7 @@ p5.prototype.alpha = function(c) { * */ p5.prototype.blue = function(c) { - this._validateParameters('blue', arguments); + p5._validateParameters('blue', arguments); return this.color(c)._getBlue(); }; @@ -111,7 +111,7 @@ p5.prototype.blue = function(c) { * */ p5.prototype.brightness = function(c) { - this._validateParameters('brightness', arguments); + p5._validateParameters('brightness', arguments); return this.color(c)._getBrightness(); }; @@ -346,7 +346,7 @@ p5.prototype.color = function() { */ p5.prototype.green = function(c) { - this._validateParameters('green', arguments); + p5._validateParameters('green', arguments); return this.color(c)._getGreen(); }; @@ -382,7 +382,7 @@ p5.prototype.green = function(c) { */ p5.prototype.hue = function(c) { - this._validateParameters('hue', arguments); + p5._validateParameters('hue', arguments); return this.color(c)._getHue(); }; @@ -430,7 +430,7 @@ p5.prototype.hue = function(c) { */ p5.prototype.lerpColor = function(c1, c2, amt) { - this._validateParameters('lerpColor', arguments); + p5._validateParameters('lerpColor', arguments); var mode = this._renderer._colorMode; var maxes = this._renderer._colorMaxes; var l0, l1, l2, l3; @@ -500,7 +500,7 @@ p5.prototype.lerpColor = function(c1, c2, amt) { * */ p5.prototype.lightness = function(c) { - this._validateParameters('lightness', arguments); + p5._validateParameters('lightness', arguments); return this.color(c)._getLightness(); }; @@ -539,7 +539,7 @@ p5.prototype.lightness = function(c) { * grey canvas */ p5.prototype.red = function(c) { - this._validateParameters('red', arguments); + p5._validateParameters('red', arguments); return this.color(c)._getRed(); }; @@ -574,7 +574,7 @@ p5.prototype.red = function(c) { */ p5.prototype.saturation = function(c) { - this._validateParameters('saturation', arguments); + p5._validateParameters('saturation', arguments); return this.color(c)._getSaturation(); }; diff --git a/src/core/2d_primitives.js b/src/core/2d_primitives.js index 9fad5c2e45..cbcaa94b8a 100644 --- a/src/core/2d_primitives.js +++ b/src/core/2d_primitives.js @@ -76,7 +76,7 @@ p5.prototype.arc = function(x, y, w, h, start, stop, mode) { args[i] = arguments[i]; } // check with FES:validateParameters - this._validateParameters('arc', args); + p5._validateParameters('arc', args); if (!this._renderer._doStroke && !this._renderer._doFill) { return this; } @@ -172,7 +172,7 @@ p5.prototype.ellipse = function() { args.push(args[2]); } // check with FES:validateParameters - this._validateParameters('ellipse', args); + p5._validateParameters('ellipse', args); // p5 supports negative width and heights for rects if (args[2] < 0){args[2] = Math.abs(args[2]);} if (args[3] < 0){args[3] = Math.abs(args[3]);} @@ -247,7 +247,7 @@ p5.prototype.line = function() { args[i] = arguments[i]; } // check with FES:validateParameters - this._validateParameters('line', args); + p5._validateParameters('line', args); //check whether we should draw a 3d line or 2d if (this._renderer.isP3D) { this._renderer.line( @@ -300,7 +300,7 @@ p5.prototype.point = function() { args[i] = arguments[i]; } // check with FES:validateParameters - this._validateParameters('point', args); + p5._validateParameters('point', args); //check whether we should draw a 3d line or 2d if (this._renderer.isP3D) { this._renderer.point( @@ -367,7 +367,7 @@ p5.prototype.quad = function() { args[i] = arguments[i]; } // check with FES:validateParameters - this._validateParameters('quad', args); + p5._validateParameters('quad', args); if (this._renderer.isP3D) { this._renderer.quad( args[0], @@ -467,7 +467,7 @@ p5.prototype.rect = function() { return; } // check with FES:validateParameters - this._validateParameters('rect', args); + p5._validateParameters('rect', args); var vals = canvas.modeAdjust( args[0], args[1], @@ -516,7 +516,7 @@ p5.prototype.triangle = function() { args[i] = arguments[i]; } // check with FES:validateParameters - this._validateParameters('triangle', args); + p5._validateParameters('triangle', args); this._renderer.triangle(args); return this; }; diff --git a/src/core/curves.js b/src/core/curves.js index 97a21b13cb..3898f9a24b 100644 --- a/src/core/curves.js +++ b/src/core/curves.js @@ -79,7 +79,7 @@ p5.prototype.bezier = function() { args[i] = arguments[i]; } // check with FES:validateParameters - this._validateParameters('bezier', args); + p5._validateParameters('bezier', args); if (!this._renderer._doStroke && !this._renderer._doFill) { return this; } @@ -164,7 +164,7 @@ p5.prototype.bezierPoint = function(a, b, c, d, t) { args[i] = arguments[i]; } // check with FES:validateParameters - this._validateParameters('bezierPoint', args); + p5._validateParameters('bezierPoint', args); var adjustedT = 1-args[4]; return Math.pow(adjustedT,3)*args[0] + 3*(Math.pow(adjustedT,2))*args[4]*args[1] + @@ -243,7 +243,7 @@ p5.prototype.bezierTangent = function(a, b, c, d, t) { args[i] = arguments[i]; } // check with FES:validateParameters - this._validateParameters('bezierTangent', args); + p5._validateParameters('bezierTangent', args); var adjustedT = 1-args[4]; return 3*args[3]*Math.pow(args[4],2) - 3*args[2]*Math.pow(args[4],2) + @@ -334,7 +334,7 @@ p5.prototype.curve = function() { args[i] = arguments[i]; } // check with FES:validateParameters - this._validateParameters('curve', args); + p5._validateParameters('curve', args); if (!this._renderer._doStroke) { return this; } @@ -463,7 +463,7 @@ p5.prototype.curvePoint = function(a, b, c, d, t) { args[i] = arguments[i]; } // check with FES:validateParameters - this._validateParameters('curvePoint', args); + p5._validateParameters('curvePoint', args); var t3 = args[4]*args[4]*args[4], t2 = args[4]*args[4], f1 = -0.5 * t3 + t2 - 0.5 * args[4], @@ -514,7 +514,7 @@ p5.prototype.curveTangent = function(a, b, c, d, t) { args[i] = arguments[i]; } // check with FES:validateParameters - this._validateParameters('curveTangent', args); + p5._validateParameters('curveTangent', args); var t2 = args[4]*args[4], f1 = (-3*t2)/2 + 2*args[4] - 0.5, f2 = (9*t2)/2 - 5*args[4], diff --git a/src/utilities/string_functions.js b/src/utilities/string_functions.js index e2794f32d5..88e0b14003 100644 --- a/src/utilities/string_functions.js +++ b/src/utilities/string_functions.js @@ -37,7 +37,7 @@ require('../core/error_helpers'); * */ p5.prototype.join = function(list, separator) { - this._validateParameters('join', arguments); + p5._validateParameters('join', arguments); return list.join(separator); }; @@ -78,7 +78,7 @@ p5.prototype.join = function(list, separator) { * */ p5.prototype.match = function(str, reg) { - this._validateParameters('match', arguments); + p5._validateParameters('match', arguments); return str.match(reg); }; @@ -115,7 +115,7 @@ p5.prototype.match = function(str, reg) { *
*/ p5.prototype.matchAll = function(str, reg) { - this._validateParameters('matchAll', arguments); + p5._validateParameters('matchAll', arguments); var re = new RegExp(reg, 'g'); var match = re.exec(str); var matches = []; @@ -179,7 +179,7 @@ p5.prototype.matchAll = function(str, reg) { * */ p5.prototype.nf = function () { - this._validateParameters('nf', arguments); + p5._validateParameters('nf', arguments); if (arguments[0] instanceof Array) { var a = arguments[1]; var b = arguments[2]; @@ -287,7 +287,7 @@ function doNf() { * */ p5.prototype.nfc = function () { - this._validateParameters('nfc', arguments); + p5._validateParameters('nfc', arguments); if (arguments[0] instanceof Array) { var a = arguments[1]; return arguments[0].map(function (x) { @@ -370,7 +370,7 @@ function doNfc() { * */ p5.prototype.nfp = function() { - this._validateParameters('nfp', arguments); + p5._validateParameters('nfp', arguments); var nfRes = p5.prototype.nf.apply(this, arguments); if (nfRes instanceof Array) { return nfRes.map(addNfp); @@ -435,7 +435,7 @@ function addNfp() { * */ p5.prototype.nfs = function() { - this._validateParameters('nfs', arguments); + p5._validateParameters('nfs', arguments); var nfRes = p5.prototype.nf.apply(this, arguments); if (nfRes instanceof Array) { return nfRes.map(addNfs); @@ -481,7 +481,7 @@ function addNfs() { * */ p5.prototype.split = function(str, delim) { - this._validateParameters('split', arguments); + p5._validateParameters('split', arguments); return str.split(delim); }; @@ -512,7 +512,7 @@ p5.prototype.split = function(str, delim) { *
*/ p5.prototype.splitTokens = function() { - this._validateParameters('splitTokens', arguments); + p5._validateParameters('splitTokens', arguments); var d,sqo,sqc,str; str = arguments[1]; if (arguments.length > 1) { @@ -564,7 +564,7 @@ p5.prototype.splitTokens = function() { * */ p5.prototype.trim = function(str) { - this._validateParameters('trim', arguments); + p5._validateParameters('trim', arguments); if (str instanceof Array) { return str.map(this.trim); } else { diff --git a/test/unit/core/2d_primitives.js b/test/unit/core/2d_primitives.js index 0f5cd9213b..e821a3bdd4 100644 --- a/test/unit/core/2d_primitives.js +++ b/test/unit/core/2d_primitives.js @@ -97,7 +97,7 @@ suite('2D Primitives', function() { }, Error, 'got unwanted exception'); }); - test('line(): missing param #4', function() { // this err case escapes + test('line(): missing param #4 ', function() { // this err case escapes assert.doesNotThrow(function() { var x3; myp5.line(0, 0, 100, 100, x3, Math.PI); From 5758c612e6146e37de2ed75f8a5d6f41d404b8ca Mon Sep 17 00:00:00 2001 From: almchung Date: Tue, 15 Aug 2017 20:49:28 -0700 Subject: [PATCH 29/37] unit test update --- test/unit/color/color_conversion.js | 2 +- test/unit/color/creating_reading.js | 2 +- test/unit/color/p5.Color.js | 6 ++++++ test/unit/color/setting.js | 2 +- 4 files changed, 9 insertions(+), 3 deletions(-) diff --git a/test/unit/color/color_conversion.js b/test/unit/color/color_conversion.js index ffe92d8417..86741f3f82 100644 --- a/test/unit/color/color_conversion.js +++ b/test/unit/color/color_conversion.js @@ -1,4 +1,4 @@ -suite('p5.ColorConversion', function() { +suite('color/p5.ColorConversion', function() { var rgba = [1, 0, 0.4, 0.8]; var rgbaWithHighLightness = [0.969, 0.753, 0.122, 0.8]; var hsla = [336/360, 1, 0.5, 0.8]; diff --git a/test/unit/color/creating_reading.js b/test/unit/color/creating_reading.js index 971ed66349..44401848f9 100644 --- a/test/unit/color/creating_reading.js +++ b/test/unit/color/creating_reading.js @@ -1,4 +1,4 @@ -suite('CreatingReading', function() { +suite('color/CreatingReading', function() { var myp5 = new p5(function( sketch ) { sketch.setup = function() {}; sketch.draw = function() {}; diff --git a/test/unit/color/p5.Color.js b/test/unit/color/p5.Color.js index 9ecd717374..a3582b4f24 100644 --- a/test/unit/color/p5.Color.js +++ b/test/unit/color/p5.Color.js @@ -24,6 +24,12 @@ suite('p5.Color', function() { test('shouldn\'t set HSLA property before hsb access func is called', function() { assert.equal(c.hsla, undefined); }); + + test('color(): missing param #0 + throws error', function() { + expect(function() { + c = myp5.color(); + }).to.throw(); + }); }); suite('p5.prototype.color("#rgb")', function() { diff --git a/test/unit/color/setting.js b/test/unit/color/setting.js index f1e6036562..7bc8b6c07a 100644 --- a/test/unit/color/setting.js +++ b/test/unit/color/setting.js @@ -1,4 +1,4 @@ -suite('Color', function() { +suite('color/Setting', function() { // p5 instance var myp5 = new p5(function( p ) { From 148571beecf3878294628270e7a7342793fb50ac Mon Sep 17 00:00:00 2001 From: almchung Date: Tue, 15 Aug 2017 22:37:39 -0700 Subject: [PATCH 30/37] style clean up --- Gruntfile.js | 2 +- src/core/2d_primitives.js | 14 +++++++------- src/core/curves.js | 12 ++++++------ src/core/error_helpers.js | 10 +++++----- 4 files changed, 19 insertions(+), 19 deletions(-) diff --git a/Gruntfile.js b/Gruntfile.js index c9501a0ee0..7e0b74e316 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -212,7 +212,7 @@ module.exports = function(grunt) { run: true, log: true, logErrors: true, - timeout: 1000000 + timeout: 100000 } }, }, diff --git a/src/core/2d_primitives.js b/src/core/2d_primitives.js index cbcaa94b8a..a45e139510 100644 --- a/src/core/2d_primitives.js +++ b/src/core/2d_primitives.js @@ -75,7 +75,7 @@ p5.prototype.arc = function(x, y, w, h, start, stop, mode) { for (var i = 0; i < args.length; ++i) { args[i] = arguments[i]; } - // check with FES:validateParameters + p5._validateParameters('arc', args); if (!this._renderer._doStroke && !this._renderer._doFill) { return this; @@ -171,7 +171,7 @@ p5.prototype.ellipse = function() { if (args.length === 3) { args.push(args[2]); } - // check with FES:validateParameters + p5._validateParameters('ellipse', args); // p5 supports negative width and heights for rects if (args[2] < 0){args[2] = Math.abs(args[2]);} @@ -246,7 +246,7 @@ p5.prototype.line = function() { for (var i = 0; i < args.length; ++i) { args[i] = arguments[i]; } - // check with FES:validateParameters + p5._validateParameters('line', args); //check whether we should draw a 3d line or 2d if (this._renderer.isP3D) { @@ -299,7 +299,7 @@ p5.prototype.point = function() { for (var i = 0; i < args.length; ++i) { args[i] = arguments[i]; } - // check with FES:validateParameters + p5._validateParameters('point', args); //check whether we should draw a 3d line or 2d if (this._renderer.isP3D) { @@ -366,7 +366,7 @@ p5.prototype.quad = function() { for (var i = 0; i < args.length; ++i) { args[i] = arguments[i]; } - // check with FES:validateParameters + p5._validateParameters('quad', args); if (this._renderer.isP3D) { this._renderer.quad( @@ -466,7 +466,7 @@ p5.prototype.rect = function() { if (!this._renderer._doStroke && !this._renderer._doFill) { return; } - // check with FES:validateParameters + p5._validateParameters('rect', args); var vals = canvas.modeAdjust( args[0], @@ -515,7 +515,7 @@ p5.prototype.triangle = function() { for (var i = 0; i < args.length; ++i) { args[i] = arguments[i]; } - // check with FES:validateParameters + p5._validateParameters('triangle', args); this._renderer.triangle(args); return this; diff --git a/src/core/curves.js b/src/core/curves.js index 3898f9a24b..e3ff225185 100644 --- a/src/core/curves.js +++ b/src/core/curves.js @@ -78,7 +78,7 @@ p5.prototype.bezier = function() { for (var i = 0; i < args.length; ++i) { args[i] = arguments[i]; } - // check with FES:validateParameters + p5._validateParameters('bezier', args); if (!this._renderer._doStroke && !this._renderer._doFill) { return this; @@ -163,7 +163,7 @@ p5.prototype.bezierPoint = function(a, b, c, d, t) { for (var i = 0; i < args.length; ++i) { args[i] = arguments[i]; } - // check with FES:validateParameters + p5._validateParameters('bezierPoint', args); var adjustedT = 1-args[4]; return Math.pow(adjustedT,3)*args[0] + @@ -242,7 +242,7 @@ p5.prototype.bezierTangent = function(a, b, c, d, t) { for (var i = 0; i < args.length; ++i) { args[i] = arguments[i]; } - // check with FES:validateParameters + p5._validateParameters('bezierTangent', args); var adjustedT = 1-args[4]; return 3*args[3]*Math.pow(args[4],2) - @@ -333,7 +333,7 @@ p5.prototype.curve = function() { for (var i = 0; i < args.length; ++i) { args[i] = arguments[i]; } - // check with FES:validateParameters + p5._validateParameters('curve', args); if (!this._renderer._doStroke) { return this; @@ -462,7 +462,7 @@ p5.prototype.curvePoint = function(a, b, c, d, t) { for (var i = 0; i < args.length; ++i) { args[i] = arguments[i]; } - // check with FES:validateParameters + p5._validateParameters('curvePoint', args); var t3 = args[4]*args[4]*args[4], t2 = args[4]*args[4], @@ -513,7 +513,7 @@ p5.prototype.curveTangent = function(a, b, c, d, t) { for (var i = 0; i < args.length; ++i) { args[i] = arguments[i]; } - // check with FES:validateParameters + p5._validateParameters('curveTangent', args); var t2 = args[4]*args[4], f1 = (-3*t2)/2 + 2*args[4] - 0.5, diff --git a/src/core/error_helpers.js b/src/core/error_helpers.js index ac6bcfe975..a632af5784 100644 --- a/src/core/error_helpers.js +++ b/src/core/error_helpers.js @@ -206,14 +206,14 @@ function testParamFormat(args, format){ // Returns true if PASS, false if FAIL function testParamClass(param, types){ for (var i = 0; i < types.length; i++) { - if (types[i] === 'Array'){ - if(param instanceof Array){ + if (types[i] === 'Array') { + if(param instanceof Array) { return true; } - }else{ + } else { if (param.name === types[i]) { return true; // class name match, pass - } else if (types[i] === 'Constant'){ + } else if (types[i] === 'Constant') { return true; // accepts any constant, pass } } @@ -226,7 +226,7 @@ function testParamType(param, types){ for (var i = 0; i < types.length; i++) { if (typeof(param) === types[i].toLowerCase()) { return true; // type match, pass - } else if (types[i] === 'Constant'){ + } else if (types[i] === 'Constant') { return true; // accepts any constant, pass } } From 9e8236eebe3bf1c09bcb84304de93166ef7ffe7d Mon Sep 17 00:00:00 2001 From: almchung Date: Wed, 16 Aug 2017 16:32:44 -0700 Subject: [PATCH 31/37] small fixes --- src/core/error_helpers.js | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/core/error_helpers.js b/src/core/error_helpers.js index a632af5784..769e2c4b38 100644 --- a/src/core/error_helpers.js +++ b/src/core/error_helpers.js @@ -56,7 +56,6 @@ function report(message, func, color) { } else if (getType(color) === 'number') { // Type to color color = typeColors[color]; } - // LM TEMP commenting this out until we get the whole system working if (func.substring(0,4) === 'load'){ console.log( '%c> p5.js says: '+message+'%c'+ @@ -126,7 +125,7 @@ p5._friendlyFileLoadError = function (errorType, filePath) { * "ellipse was expecting a number for parameter #1, * received "foo" instead." */ -function validateParameters(func, args) { +p5._validateParameters = function validateParameters(func, args) { if (p5.disableFriendlyErrors || typeof(IS_MINIFIED) !== 'undefined') { return; // skip FES @@ -156,7 +155,7 @@ function validateParameters(func, args) { p5._friendlyParamError(errorArray[m], func); } } -} +}; // validateParameters() helper functions: // lookupParamDoc() for querying data.json function lookupParamDoc(func){ @@ -406,8 +405,7 @@ function helpForMisusedAtTopLevelCode(e, log) { // Exposing this primarily for unit testing. p5.prototype._helpForMisusedAtTopLevelCode = helpForMisusedAtTopLevelCode; -p5.prototype._validateParameters = validateParameters; -p5._validateParameters = validateParameters; +p5.prototype._validateParameters = p5.validateParameters; if (document.readyState !== 'complete') { window.addEventListener('error', helpForMisusedAtTopLevelCode, false); From ae68b101f4d3ad0582956290aab7c9bf6970e88f Mon Sep 17 00:00:00 2001 From: Lauren McCarthy Date: Wed, 16 Aug 2017 18:55:50 -0700 Subject: [PATCH 32/37] fix point color closes #2119 --- src/core/p5.Renderer2D.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/core/p5.Renderer2D.js b/src/core/p5.Renderer2D.js index 8acb95a95b..609b91dc00 100644 --- a/src/core/p5.Renderer2D.js +++ b/src/core/p5.Renderer2D.js @@ -507,8 +507,11 @@ p5.Renderer2D.prototype.point = function(x, y) { } else if(this._getStroke() === styleEmpty){ return this; } + var s = this._getStroke(); + var f = this._getFill(); x = Math.round(x); y = Math.round(y); + this._setFill(s); if (ctx.lineWidth > 1) { ctx.beginPath(); ctx.arc( @@ -523,6 +526,7 @@ p5.Renderer2D.prototype.point = function(x, y) { } else { ctx.fillRect(x, y, 1, 1); } + this._setFill(f); }; p5.Renderer2D.prototype.quad = From 7c6a47261b052df12c9d7f26591077916a726d98 Mon Sep 17 00:00:00 2001 From: Lauren McCarthy Date: Wed, 16 Aug 2017 18:56:35 -0700 Subject: [PATCH 33/37] adding comment to make stroke fill swap in point clear --- src/core/p5.Renderer2D.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/core/p5.Renderer2D.js b/src/core/p5.Renderer2D.js index 609b91dc00..f4e22f9f81 100644 --- a/src/core/p5.Renderer2D.js +++ b/src/core/p5.Renderer2D.js @@ -511,6 +511,7 @@ p5.Renderer2D.prototype.point = function(x, y) { var f = this._getFill(); x = Math.round(x); y = Math.round(y); + // swapping fill color to stroke and back after for correct point rendering this._setFill(s); if (ctx.lineWidth > 1) { ctx.beginPath(); From 39b2b1675f9a45049e7cd664ab230c44aafdcff5 Mon Sep 17 00:00:00 2001 From: Evelyn Masso Date: Fri, 18 Aug 2017 08:45:38 -0400 Subject: [PATCH 34/37] fix for ordering of test grunt task #2125 --- Gruntfile.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Gruntfile.js b/Gruntfile.js index 34a37284ad..cecda7ed4c 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -370,7 +370,7 @@ module.exports = function(grunt) { // Create the multitasks. grunt.registerTask('build', ['browserify', 'uglify', 'requirejs']); - grunt.registerTask('test', ['jshint', 'jscs', 'build', 'yuidoc:dev', 'connect', 'mocha', 'mochaTest']); + grunt.registerTask('test', ['jshint', 'jscs', 'yuidoc:dev', 'build', 'connect', 'mocha', 'mochaTest']); grunt.registerTask('test:nobuild', ['jshint:test', 'jscs:test', 'connect', 'mocha']); grunt.registerTask('yui', ['yuidoc:prod', 'minjson']); grunt.registerTask('yui:dev', ['yuidoc:dev', 'minjson']); From 5fe2f2832eda308e29f1153369bed2acf856d5cd Mon Sep 17 00:00:00 2001 From: Lauren McCarthy Date: Sun, 20 Aug 2017 15:31:10 -0700 Subject: [PATCH 35/37] removing grunt yui:dev task, now grunt yui generates both prod and testing docs issue #1954 --- Gruntfile.js | 14 +++------- docs/preprocessor.js | 2 +- docs/yuidoc-p5-theme/layouts/main.handlebars | 28 ++++++++++---------- 3 files changed, 19 insertions(+), 25 deletions(-) diff --git a/Gruntfile.js b/Gruntfile.js index a5cc34db8d..f6e0a76124 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -13,13 +13,6 @@ * served from the /reference/ folder of the p5js * website (https://github.com/processing/p5.js-website). * - * grunt yui:dev - This will build the inline documentation but linking to - * remote JS/CSS and assets so pages look correct in local - * testing. The generated documentation is assumed - * to be served from a development web server running - * at the root of the repository. "grunt yui" should - * be run to build docs ready for production. - * * grunt test - This rebuilds the source and runs the automated tests on * both the minified and unminified code. If you need to debug * a test suite in a browser, `grunt test --keepalive` will @@ -64,6 +57,8 @@ function getYuidocOptions() { } }; + // note dev is no longer used, prod is used to build both testing and production ready docs + var o = { prod: JSON.parse(JSON.stringify(BASE_YUIDOC_OPTIONS)), dev: JSON.parse(JSON.stringify(BASE_YUIDOC_OPTIONS)) @@ -370,11 +365,10 @@ module.exports = function(grunt) { // Create the multitasks. grunt.registerTask('build', ['browserify', 'uglify', 'requirejs']); - grunt.registerTask('test', ['jshint', 'jscs', 'build', 'yuidoc:dev', 'connect', 'mocha', 'mochaTest']); + grunt.registerTask('test', ['jshint', 'jscs', 'build', 'yuidoc:prod', 'connect', 'mocha', 'mochaTest']); grunt.registerTask('test:nobuild', ['jshint:test', 'jscs:test', 'connect', 'mocha']); grunt.registerTask('yui', ['yuidoc:prod', 'minjson']); - grunt.registerTask('yui:dev', ['yuidoc:dev', 'minjson']); - grunt.registerTask('yui:test', ['yuidoc:dev', 'connect', 'mocha:yui']); + grunt.registerTask('yui:test', ['yuidoc:prod', 'connect', 'mocha:yui']); grunt.registerTask('default', ['test']); grunt.registerTask('saucetest', ['connect', 'saucelabs-mocha']); }; diff --git a/docs/preprocessor.js b/docs/preprocessor.js index 80e7134052..cc3a8e343d 100644 --- a/docs/preprocessor.js +++ b/docs/preprocessor.js @@ -143,7 +143,7 @@ module.exports.register = function(Handlebars, options) { // } else { // return '/'+this.language; // } - return window.location.pathname+' hi' + return window.location.pathname }); }; diff --git a/docs/yuidoc-p5-theme/layouts/main.handlebars b/docs/yuidoc-p5-theme/layouts/main.handlebars index ef16db28ed..9b033cbf52 100755 --- a/docs/yuidoc-p5-theme/layouts/main.handlebars +++ b/docs/yuidoc-p5-theme/layouts/main.handlebars @@ -6,23 +6,23 @@ p5.js | reference - + - - - - + + + + - - - - - + + + + + @@ -42,7 +42,7 @@
- +

Processing Intuition times JavaScript power

@@ -96,7 +96,7 @@

 

- + * @@ -120,8 +120,8 @@ - - + +