-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from Ghustavh97/master
test: refactor tests
- Loading branch information
Showing
12 changed files
with
310 additions
and
248 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
"use strict"; | ||
|
||
require("./src/test.output"); | ||
require("./src/test.attributes"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
}); | ||
}); |
Oops, something went wrong.