Skip to content

Commit

Permalink
Merge pull request #30 from Ghustavh97/master
Browse files Browse the repository at this point in the history
test: refactor tests
  • Loading branch information
Ghustavh97 committed Oct 16, 2020
2 parents 8fc2f08 + 0e76538 commit ac4c241
Show file tree
Hide file tree
Showing 12 changed files with 310 additions and 248 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,10 +274,11 @@ module.exports = {
},
overrides: [
{
files: ["test/src/test.*"],
files: ["test/src/**"],
rules: {
"max-len": "off",
"no-magic-numbers": "off",
"newline-per-chained-call": "off",
},
},
],
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "src/index.js",
"scripts": {
"debug": "node scripts/debug.js",
"test": "nyc mocha && mocha test/src/test.output",
"test": "nyc mocha test/main.test.js && mocha test/output.test.js",
"coverage": "nyc report --reporter=text-lcov | coveralls"
},
"repository": {
Expand Down
4 changes: 3 additions & 1 deletion test/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ const { prepare } = require("./src");

prepare();

require("./src/test.parameters");
require("./src/test.pathing");
require("./src/test.exceptions");
require("./src/test.svgfixer");
require("./src/test.output");
4 changes: 4 additions & 0 deletions test/output.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
"use strict";

require("./src/test.output");
require("./src/test.attributes");
2 changes: 2 additions & 0 deletions test/src/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const fs = require("fs");
const path = require("path");
const fg = require("fast-glob");
const SVGFixer = require("../../");
const { assert } = require("chai");

Expand Down Expand Up @@ -47,6 +48,7 @@ const path2 = (function () {
})();

module.exports = {
fg,
path2,
assert,
SVGFixer,
Expand Down
43 changes: 43 additions & 0 deletions test/src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
"use strict";

const fs = require("fs-extra");
const Svg2 = require("oslllo-svg2");
const { path2 } = require("./helper");
const looksame = require("looks-same");

/**
* @description Prepare for tests.
Expand All @@ -28,7 +30,48 @@ function emptyDir (dir) {
}
}

/**
* @description - Check if directory is empty.
* @param {String} dir - Directory path string
*/
function isEmptyDir (dir) {
return fs.readdirSync(dir).length === 0;
}

/**
* @description - Check if the broken and fixed svg match visually.
* @param {String} brokenSvgPath - Path to broken svg.
* @param {String} fixedSvgPath - Path to fixed svg.
*/
function brokenAndFixedSvgsMatch (brokenSvgPath, fixedSvgPath) {
return new Promise(async (resolve, reject) => {
var resize = { width: 250, height: Svg2.AUTO };
var buffers = await Promise.all(
[brokenSvgPath, fixedSvgPath].map(function (svgPath) {
return Svg2(svgPath).svg.resize(resize).extend(20).png().toBuffer();
})
);
looksame(
buffers[0],
buffers[1],
{ strict: false, tolerance: 3.5 },
(err, results) => {
if (err) {
reject(err);
} else {
resolve({
equal: results.equal,
buffer: { broken: buffers[0], fixed: buffers[1] },
});
}
}
);
});
}

module.exports = {
prepare,
emptyDir,
isEmptyDir,
brokenAndFixedSvgsMatch,
};
45 changes: 45 additions & 0 deletions test/src/test.attributes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"use strict";

const path = require("path");
const Svg2 = require("oslllo-svg2");
const { assert, path2, fg } = require("./helper");

describe("test.attributes", () => {
var fixed = fg.sync(path2.fixed.relative + "/*.svg");
var broken = fg.sync(path2.multiple.relative + "/*.svg");
fixed.forEach((svg, index) => {
var basename = path.basename(svg);
it(`outputs svg with the correct attributes for ${basename}`, () => {
var elements = [broken[index], fixed[index]].map(function (svgPath) {
return Svg2(svgPath).toElement();
});
var svgs = {
broken: elements[0],
fixed: elements[1],
};
assert.equal(
svgs.broken.attributes.length,
svgs.fixed.attributes.length,
`Attributes length does not match the original for ${basename}`
);
/**
* @description - Get element attributes object.
* @param {SVGSVGElement} element
* @returns {Object}
*/
function getElementAttributes (element) {
return Array.prototype.slice.call(element.attributes).map((attribute) => {
var value = {};
value[attribute.nodeName] = attribute.nodeValue;

return value;
});
}
var attributes = {
broken: getElementAttributes(svgs.broken),
fixed: getElementAttributes(svgs.fixed),
};
assert.deepEqual(attributes.broken, attributes.fixed);
});
});
});
102 changes: 102 additions & 0 deletions test/src/test.exceptions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
"use strict";

const path = require("path");
const is = require("oslllo-validator");
const error = require("../../src/error");
const { SVGFixer, assert, path2 } = require("./helper");

describe("test.exceptions", () => {
/**
* Get test exception message
* @param {*} name
* @param {*} arg
*/
function message (name, arg) {
return is.string(arg)
? error.invalidPathError(name, arg).message
: error.invalidParameterError(name, "string", arg).message;
}
describe("invalid source arguments", () => {
var invalid = [
1,
"invalid/path",
"invalid/path/icon.svg",
path.resolve("invalid/path/icon.svg"),
path.resolve("invalid/path"),
];
invalid.forEach((arg) => {
it(`throws with invalid source argument (${arg.toString()})`, () => {
assert.throws(
() => SVGFixer(arg, path2.fixed.absolute),
TypeError,
message("source", arg)
);
});
});
});

describe("invalid destination arguments", () => {
var invalid = [
1,
"invalid/path",
"invalid/path/icon.svg",
path.resolve("invalid/path/icon.svg"),
path.resolve("invalid/path"),
];
invalid.forEach((arg) => {
it(`throws with invalid destination argument (${arg.toString()})`, () => {
assert.throws(
() => SVGFixer(path2.single.absolute, arg),
TypeError,
message("destination", arg)
);
});
});
});

describe("invalid options arguments", () => {
var invalid = [
1,
"invalid/path",
"invalid/path/icon.svg",
path.resolve("invalid/path/icon.svg"),
path.resolve("invalid/path"),
];
invalid.forEach((arg) => {
it(`throws with invalid options argument (${arg.toString()})`, () => {
assert.throws(
() => SVGFixer(path2.single.absolute, path2.fixed.absolute, arg),
TypeError,
error.invalidParameterError("options", "object", arg).message
);
});
});
});

describe("options", () => {
it("throws when you try to get an option key/value that does not exist", () => {
var instance = SVGFixer(path2.single.absolute, path2.fixed.absolute);
var options = Object.keys(instance.options.all());
assert.throws(
() => instance.options.get("invalid"),
TypeError,
error.invalidParameterError(
"setting",
`one of ${options.toString()}`,
"invalid"
).message
);
});
var invalid = [undefined, 1];
var instance = SVGFixer(path2.single.absolute, path2.fixed.absolute);
invalid.forEach((arg) => {
it(`throws when you try to get an option key/value that is (${arg})`, () => {
assert.throws(
() => instance.options.get(arg),
TypeError,
error.invalidParameterError("option", "string", arg).message
);
});
});
});
});
88 changes: 22 additions & 66 deletions test/src/test.output.js
Original file line number Diff line number Diff line change
@@ -1,76 +1,32 @@
"use strict";

const path = require("path");
const fs = require("fs-extra");
const Svg2 = require("oslllo-svg2");
const looksame = require("looks-same");
const { assert, path2 } = require("./helper");
const { assert, path2, fg } = require("./helper");
const { brokenAndFixedSvgsMatch } = require("./");

describe("test.output", () => {
var fixedArray = fs.readdirSync(path2.fixed.absolute);
var fixed = path2.fixed.absolute;
var broken = path2.multiple.absolute;
var failed = path2.failed.absolute;
fixedArray.forEach((svg, index) => {
it(`${svg} matches expected output`, async () => {
var resize = { width: 250, height: Svg2.AUTO };
var brokenBuffer = await Svg2(`${broken}/${svg}`)
.svg.resize(resize)
.extend(20)
.png()
.toBuffer();
var fixedBuffer = await Svg2(`${fixed}/${svg}`)
.svg.resize(resize)
.extend(20)
.png()
.toBuffer();

return new Promise((resolve, reject) => {
looksame(
brokenBuffer,
fixedBuffer,
{ strict: false, tolerance: 3.5 },
(err, { equal }) => {
if (err) {
reject(err);
} else {
if (!equal) {
fs.writeFileSync(`${failed}/${index}.png`, brokenBuffer);
fs.writeFileSync(
`${failed}/${index}-fixed.png`,
fixedBuffer
);
}
assert.isTrue(
equal,
`SVG ${svg} did not match expected output`
);
resolve();
}
}
);
});
});
});
fixedArray.forEach((svg) => {
it(`outputs svg with the correct attributes for ${svg}`, () => {
var brokenSvg = Svg2(`${broken}/${svg}`).toElement();
var fixedSvg = Svg2(`${fixed}/${svg}`).toElement();
assert.equal(brokenSvg.attributes.length, fixedSvg.attributes.length);
/**
* @description - Get svg element attributes.
* @param {SVGSVGElement} element
*/
function getAttributesObj (element) {
return Array.prototype.slice.call(element.attributes).map((attribute) => {
var value = {};
value[attribute.nodeName] = attribute.nodeValue;

return value;
});
var fixed = fg.sync(path2.fixed.relative + "/*.svg");
var broken = fg.sync(path2.multiple.relative + "/*.svg");
assert.equal(
fixed.length,
broken.length,
"The number of fixed and broken icons does not match."
);
fixed.forEach((svg, index) => {
var basename = path.basename(svg);
it(`${basename} matches expected output`, async () => {
var { equal, buffer } = await brokenAndFixedSvgsMatch(
broken[index],
fixed[index]
);
if (!equal) {
var filename = path.parse(basename).name;
fs.writeFileSync(failed + "/" + filename + ".broken.png", buffer.broken);
fs.writeFileSync(failed + "/" + filename + ".fixed.png", buffer.fixed);
}
var brokenAttributes = getAttributesObj(brokenSvg);
var fixedAttributes = getAttributesObj(fixedSvg);
assert.deepEqual(brokenAttributes, fixedAttributes);
assert.isTrue(equal, `SVG ${basename} did not match expected output`);
});
});
});
24 changes: 24 additions & 0 deletions test/src/test.parameters.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"use strict";

const { SVGFixer, assert, path2 } = require("./helper");

describe("test.parameters", () => {
it("can set the options parameter", async () => {
var options = {
showProgressBar: true,
throwIfDestinationDoesNotExist: false,
};
var instance = await SVGFixer(
path2.direct.absolute,
path2.fixed.absolute,
options
).fix();
var set = instance.options.all();
var keys = {
set: Object.values(set),
options: Object.values(options),
};
assert.deepEqual(keys.set, keys.options, "option parameter keys do not match");
assert.deepEqual(set, options, "option parameters do not match");
});
});

0 comments on commit ac4c241

Please sign in to comment.