Skip to content

Commit

Permalink
Extending test coverage (#3148)
Browse files Browse the repository at this point in the history
* adding tests
  • Loading branch information
asturur authored Aug 14, 2016
1 parent c0f5dc3 commit cf5f327
Show file tree
Hide file tree
Showing 10 changed files with 1,017 additions and 20 deletions.
26 changes: 19 additions & 7 deletions src/intersection.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,34 @@

fabric.Intersection.prototype = /** @lends fabric.Intersection.prototype */ {

constructor: Intersection,

/**
* Appends a point to intersection
* @param {fabric.Point} point
* @return {fabric.Intersection} thisArg
* @chainable
*/
appendPoint: function (point) {
this.points.push(point);
return this;
},

/**
* Appends points to intersection
* @param {Array} points
* @return {fabric.Intersection} thisArg
* @chainable
*/
appendPoints: function (points) {
this.points = this.points.concat(points);
return this;
}
};

/**
* Checks if one line intersects another
* TODO: rename in intersectSegmentSegment
* @static
* @param {fabric.Point} a1
* @param {fabric.Point} a2
Expand All @@ -61,7 +70,7 @@
ub = ubT / uB;
if (0 <= ua && ua <= 1 && 0 <= ub && ub <= 1) {
result = new Intersection('Intersection');
result.points.push(new fabric.Point(a1.x + ua * (a2.x - a1.x), a1.y + ua * (a2.y - a1.y)));
result.appendPoint(new fabric.Point(a1.x + ua * (a2.x - a1.x), a1.y + ua * (a2.y - a1.y)));
}
else {
result = new Intersection();
Expand All @@ -80,6 +89,8 @@

/**
* Checks if line intersects polygon
* TODO: rename in intersectSegmentPolygon
* fix detection of coincident
* @static
* @param {fabric.Point} a1
* @param {fabric.Point} a2
Expand All @@ -88,12 +99,13 @@
*/
fabric.Intersection.intersectLinePolygon = function(a1, a2, points) {
var result = new Intersection(),
length = points.length;
length = points.length,
b1, b2, inter;

for (var i = 0; i < length; i++) {
var b1 = points[i],
b2 = points[(i + 1) % length],
inter = Intersection.intersectLineLine(a1, a2, b1, b2);
b1 = points[i],
b2 = points[(i + 1) % length],
inter = Intersection.intersectLineLine(a1, a2, b1, b2);

result.appendPoints(inter.points);
}
Expand Down Expand Up @@ -131,8 +143,8 @@
* Checks if polygon intersects rectangle
* @static
* @param {Array} points
* @param {Number} r1
* @param {Number} r2
* @param {fabric.Point} r1
* @param {fabric.Point} r2
* @return {fabric.Intersection}
*/
fabric.Intersection.intersectPolygonRectangle = function (points, r1, r2) {
Expand Down
19 changes: 13 additions & 6 deletions src/mixins/collection.mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,21 @@
*/
fabric.Collection = {

_objects: [],

/**
* Adds objects to collection, then renders canvas (if `renderOnAddRemove` is not `false`)
* Objects should be instances of (or inherit from) fabric.Object
* @param {...fabric.Object} object Zero or more fabric instances
* @return {Self} thisArg
* @chainable
*/
add: function () {
this._objects.push.apply(this._objects, arguments);
for (var i = 0, length = arguments.length; i < length; i++) {
this._onObjectAdded(arguments[i]);
if (this._onObjectAdded) {
for (var i = 0, length = arguments.length; i < length; i++) {
this._onObjectAdded(arguments[i]);
}
}
this.renderOnAddRemove && this.renderAll();
return this;
Expand All @@ -35,7 +40,7 @@ fabric.Collection = {
else {
objects.splice(index, 0, object);
}
this._onObjectAdded(object);
this._onObjectAdded && this._onObjectAdded(object);
this.renderOnAddRemove && this.renderAll();
return this;
},
Expand All @@ -48,19 +53,20 @@ fabric.Collection = {
*/
remove: function() {
var objects = this.getObjects(),
index;
index, somethingRemoved = false;

for (var i = 0, length = arguments.length; i < length; i++) {
index = objects.indexOf(arguments[i]);

// only call onObjectRemoved if an object was actually removed
if (index !== -1) {
somethingRemoved = true;
objects.splice(index, 1);
this._onObjectRemoved(arguments[i]);
this._onObjectRemoved && this._onObjectRemoved(arguments[i]);
}
}

this.renderOnAddRemove && this.renderAll();
this.renderOnAddRemove && somethingRemoved && this.renderAll();
return this;
},

Expand All @@ -75,6 +81,7 @@ fabric.Collection = {
*
* @param {Object} context Context (aka thisObject)
* @return {Self} thisArg
* @chainable
*/
forEachObject: function(callback, context) {
var objects = this.getObjects(),
Expand Down
52 changes: 50 additions & 2 deletions src/point.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@

Point.prototype = /** @lends fabric.Point.prototype */ {

type: 'point',

constructor: Point,

/**
Expand All @@ -44,6 +46,7 @@
* Adds another point to this one
* @param {fabric.Point} that
* @return {fabric.Point} thisArg
* @chainable
*/
addEquals: function (that) {
this.x += that.x;
Expand All @@ -64,6 +67,7 @@
* Adds value to this point
* @param {Number} scalar
* @return {fabric.Point} thisArg
* @chainable
*/
scalarAddEquals: function (scalar) {
this.x += scalar;
Expand All @@ -84,6 +88,7 @@
* Subtracts another point from this point
* @param {fabric.Point} that
* @return {fabric.Point} thisArg
* @chainable
*/
subtractEquals: function (that) {
this.x -= that.x;
Expand All @@ -104,6 +109,7 @@
* Subtracts value from this point
* @param {Number} scalar
* @return {fabric.Point} thisArg
* @chainable
*/
scalarSubtractEquals: function (scalar) {
this.x -= scalar;
Expand All @@ -113,6 +119,7 @@

/**
* Miltiplies this point by a value and returns a new one
* TODO: rename in scalarMultiply in 2.0
* @param {Number} scalar
* @return {fabric.Point}
*/
Expand All @@ -122,8 +129,10 @@

/**
* Miltiplies this point by a value
* TODO: rename in scalarMultiplyEquals in 2.0
* @param {Number} scalar
* @return {fabric.Point} thisArg
* @chainable
*/
multiplyEquals: function (scalar) {
this.x *= scalar;
Expand All @@ -133,6 +142,7 @@

/**
* Divides this point by a value and returns a new one
* TODO: rename in scalarDivide in 2.0
* @param {Number} scalar
* @return {fabric.Point}
*/
Expand All @@ -142,8 +152,10 @@

/**
* Divides this point by a value
* TODO: rename in scalarDivideEquals in 2.0
* @param {Number} scalar
* @return {fabric.Point} thisArg
* @chainable
*/
divideEquals: function (scalar) {
this.x /= scalar;
Expand Down Expand Up @@ -200,10 +212,14 @@
/**
* Returns new point which is the result of linear interpolation with this one and another one
* @param {fabric.Point} that
* @param {Number} t
* @param {Number} t , position of interpolation, between 0 and 1 default 0.5
* TODO: lock t between 0 and 1 in fabric 2.0
* @return {fabric.Point}
*/
lerp: function (that, t) {
if (typeof t === 'undefined') {
t = 0.5;
}
return new Point(this.x + (that.x - this.x) * t, this.y + (that.y - this.y) * t);
},

Expand All @@ -224,7 +240,7 @@
* @return {fabric.Point}
*/
midPointFrom: function (that) {
return new Point(this.x + (that.x - this.x)/2, this.y + (that.y - this.y)/2);
return this.lerp(that);
},

/**
Expand Down Expand Up @@ -257,19 +273,43 @@
* Sets x/y of this point
* @param {Number} x
* @param {Number} y
* @chainable
*/
setXY: function (x, y) {
this.x = x;
this.y = y;
return this;
},

/**
* Sets x of this point
* @param {Number} x
* @chainable
*/
setX: function (x) {
this.x = x;
return this;
},

/**
* Sets y of this point
* @param {Number} y
* @chainable
*/
setY: function (y) {
this.y = y;
return this;
},

/**
* Sets x/y of this point from another point
* @param {fabric.Point} that
* @chainable
*/
setFromPoint: function (that) {
this.x = that.x;
this.y = that.y;
return this;
},

/**
Expand All @@ -283,6 +323,14 @@
this.y = that.y;
that.x = x;
that.y = y;
},

/**
* return a cloned instance of the point
* @return {fabric.Point}
*/
clone: function () {
return new Point(this.x, this.y);
}
};

Expand Down
1 change: 0 additions & 1 deletion src/static_canvas.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,6 @@
_initStatic: function(el, options) {
var cb = fabric.StaticCanvas.prototype.renderAll.bind(this);
this._objects = [];

this._createLowerCanvas(el);
this._initOptions(options);
this._setImageSmoothing();
Expand Down
9 changes: 6 additions & 3 deletions src/util/misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -638,11 +638,14 @@
/**
* Clear char widths cache for a font family.
* @memberOf fabric.util
* @param {String} fontFamily
* @param {String} [fontFamily] font family to clear
*/
clearFabricFontCache: function(fontFamily) {
if (fabric.charWidthsCache[fontFamily]) {
fabric.charWidthsCache[fontFamily] = { };
if (!fontFamily) {
fabric.charWidthsCache = { };
}
else if (fabric.charWidthsCache[fontFamily]) {
delete fabric.charWidthsCache[fontFamily];
}
}
};
Expand Down
5 changes: 4 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ testrunner.run({
'./test/unit/object_geometry.js',
'./test/unit/object_origin.js',
'./test/unit/itext.js',
'./test/unit/itext_key_behaviour.js'
'./test/unit/itext_key_behaviour.js',
'./test/unit/collection.js',
'./test/unit/point.js',
'./test/unit/intersection.js',
]
}, function(err, report) {
if (err) {
Expand Down
Loading

0 comments on commit cf5f327

Please sign in to comment.