Skip to content

Commit

Permalink
Implemented custom Jasmine matcher for SVG paths that checks pixel ro…
Browse files Browse the repository at this point in the history
…unded values - JavaScript rounding is a bit inconsistent for very small values...
  • Loading branch information
lorem--ipsum committed Nov 11, 2013
1 parent f727179 commit 1a4e5de
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 9 deletions.
2 changes: 1 addition & 1 deletion dist/pie-chart.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*! pie-chart - v1.0.0 - 2013-11-10
/*! pie-chart - v1.0.0 - 2013-11-11
* https://github.com/n3-charts/pie-chart
* Copyright (c) 2013 n3-charts Licensed , */
angular.module('n3-pie-chart', ['n3-pie-utils'])
Expand Down
2 changes: 1 addition & 1 deletion dist/pie-chart.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

70 changes: 66 additions & 4 deletions dist/pie-chart.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*! pie-chart - v1.0.0 - 2013-11-10
/*! pie-chart - v1.0.0 - 2013-11-11
* https://github.com/n3-charts/pie-chart
* Copyright (c) 2013 n3-charts Licensed , */
'use strict';
Expand Down Expand Up @@ -42,8 +42,70 @@ describe('n3-piechart', function() {
}
}

return true;
},

toBeSamePathAs: function(expected) {
var actual = this.actual;
var notText = this.isNot ? " not" : "";

var errorMsg = "";

this.message = function () {
return "Expected " + actual + notText + " to be same path as " + expected + " (" + errorMsg + ")";
};

if (!actual) {
return false;
}

var getInstructions = function(path) {
var a = [];

var sp = path.split(/([A-Z])/g);

for (var i = 1; i < sp.length; i+=2) {
a.push({type: sp[i], values: sp[i+1].split(/[\s,]/)});
}

return a;
};

var expectedArray = getInstructions(expected);
var actualArray = getInstructions(actual);

if (expectedArray.length !== actualArray.length) {
return false;
}

for (var i = 0; i < expectedArray.length; i++) {
var a = expectedArray[i];
var b = actualArray[i];

if (a.type !== b.type) {
errorMsg = a.type + " !== " + b.type;
return false;
}

if (a.values.length !== b.values.length) {
errorMsg = "different values length";
return false;
}

for (var j = 0; j < a.values.length; j++) {
var vA = a.values[j];
var vB = b.values[j];

if (parseInt(vA, 10) !== parseInt(vB, 10) && vA !== "" && vB !== "") {
errorMsg = vA + " !== " + vB;
return false;
}
}
}

return true;
}

});
});

Expand Down Expand Up @@ -156,7 +218,7 @@ describe("gauge mode", function() {

runs(function () {
expected.forEach(function(d, i) {
expect(arcs.childNodes[i].getAttribute("d").trim()).toBe(d);
expect(arcs.childNodes[i].getAttribute("d")).toBeSamePathAs(d);
});
});
});
Expand Down Expand Up @@ -185,7 +247,7 @@ describe("gauge mode", function() {
arcs = content.childNodes[0].childNodes;
runs(function () {
expected.forEach(function(d, i) {
expect(arcs[i].getAttribute("d").trim()).toBe(d);
expect(arcs[i].getAttribute("d")).toBeSamePathAs(d);
});
});
});
Expand Down Expand Up @@ -672,7 +734,7 @@ describe('standard mode', function() {
runs(function () {
expected.forEach(function(d, i) {
expect(arcs.childNodes[i].nodeName).toBe("path");
expect(arcs.childNodes[i].getAttribute("d").trim()).toBe(d);
expect(arcs.childNodes[i].getAttribute("d")).toBeSamePathAs(d);
});
});
});
Expand Down
4 changes: 2 additions & 2 deletions test/gauge_mode.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ describe("gauge mode", function() {

runs(function () {
expected.forEach(function(d, i) {
expect(arcs.childNodes[i].getAttribute("d").trim()).toBe(d);
expect(arcs.childNodes[i].getAttribute("d")).toBeSamePathAs(d);
});
});
});
Expand Down Expand Up @@ -136,7 +136,7 @@ describe("gauge mode", function() {
arcs = content.childNodes[0].childNodes;
runs(function () {
expected.forEach(function(d, i) {
expect(arcs[i].getAttribute("d").trim()).toBe(d);
expect(arcs[i].getAttribute("d")).toBeSamePathAs(d);
});
});
});
Expand Down
62 changes: 62 additions & 0 deletions test/spec.header
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,69 @@ describe('n3-piechart', function() {
}
}

return true;
},

toBeSamePathAs: function(expected) {
var actual = this.actual;
var notText = this.isNot ? " not" : "";

var errorMsg = "";

this.message = function () {
return "Expected " + actual + notText + " to be same path as " + expected + " (" + errorMsg + ")";
};

if (!actual) {
return false;
}

var getInstructions = function(path) {
var a = [];

var sp = path.split(/([A-Z])/g);

for (var i = 1; i < sp.length; i+=2) {
a.push({type: sp[i], values: sp[i+1].split(/[\s,]/)});
}

return a;
};

var expectedArray = getInstructions(expected);
var actualArray = getInstructions(actual);

if (expectedArray.length !== actualArray.length) {
return false;
}

for (var i = 0; i < expectedArray.length; i++) {
var a = expectedArray[i];
var b = actualArray[i];

if (a.type !== b.type) {
errorMsg = a.type + " !== " + b.type;
return false;
}

if (a.values.length !== b.values.length) {
errorMsg = "different values length";
return false;
}

for (var j = 0; j < a.values.length; j++) {
var vA = a.values[j];
var vB = b.values[j];

if (parseInt(vA, 10) !== parseInt(vB, 10) && vA !== "" && vB !== "") {
errorMsg = vA + " !== " + vB;
return false;
}
}
}

return true;
}

});
});
2 changes: 1 addition & 1 deletion test/standard_mode.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ describe('standard mode', function() {
runs(function () {
expected.forEach(function(d, i) {
expect(arcs.childNodes[i].nodeName).toBe("path");
expect(arcs.childNodes[i].getAttribute("d").trim()).toBe(d);
expect(arcs.childNodes[i].getAttribute("d")).toBeSamePathAs(d);
});
});
});
Expand Down

0 comments on commit 1a4e5de

Please sign in to comment.