From 332a88a342a3d6100a5a7ce3e8e4544c2bfa16c5 Mon Sep 17 00:00:00 2001 From: Freeyorp Date: Fri, 28 Jun 2013 18:31:17 +1200 Subject: Extend assert for array and image assertions This also provides the much needed assert.arrayEqual --- test/assert.js | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ test/mp/dye.js | 2 +- test/mp/future.js | 29 +++++++---------------------- 3 files changed, 62 insertions(+), 23 deletions(-) create mode 100644 test/assert.js diff --git a/test/assert.js b/test/assert.js new file mode 100644 index 0000000..6063ea5 --- /dev/null +++ b/test/assert.js @@ -0,0 +1,54 @@ +var assert = require("assert"); + +assert = module.exports = Object.create(assert); + +assert.isArray = function(actual, message) { + if (!Array.isArray(actual)) { + assert.fail(actual, null, message || "expected {actual} to be an Array", null, assert.isArray); + } +}; + +assert.arrayEqual = function(actual, expected, message) { + message = message || "expected {expected}, found {actual}"; + assert.isArray(actual); + if (actual.length != expected.length) { + assert.fail(actual, expected, message); + } + for (var i in actual) { + if (Array.isArray(actual[i])) { + assert.arrayEqual(actual[i], expected[i], message); + } else if (actual[i] != expected[i]) { + assert.fail(actual, expected, message); + } + } +}; + +assert.imageDataEqual = function(actual, expected, input, width) { + assert.equal(actual.length, expected.length, "expected same " + expected.length + " pixel components, found " + actual.length); + for (var i = 0; i != actual.length; i += 4) { + var p = i / 4; + var y = Math.floor(p / width); + var x = p - y * width; + var msg = "At (" + x + "," + y + "): " + + "Input rgba(" + input [i ] + "," + input [i + 1] + "," + input [i + 2] + "," + input [i + 3] + ") " + + "should become rgba(" + expected[i ] + "," + expected[i + 1] + "," + expected[i + 2] + "," + expected[i + 3] + "); " + + "found rgba(" + actual [i ] + "," + actual [i + 1] + "," + actual [i + 2] + "," + actual [i + 3] + ")"; + assert.equal(actual[i ], expected[i ], msg); + assert.equal(actual[i + 1], expected[i + 1], msg); + assert.equal(actual[i + 2], expected[i + 2], msg); + assert.equal(actual[i + 3], expected[i + 3], msg); + } +}; + + +assert.dyeDataEqual = function(actual, expected) { + var channels = ["R", "G", "Y", "B", "M", "C", "W"]; + for (var k in channels) { + var shouldExist = channels[k] in expected; + assert.equal(channels[k] in actual, shouldExist, (shouldExist ? "expected" : "unexpected") + " channel " + channels[k] + " in dye data."); + if (!shouldExist) { + continue; + } + assert.arrayEqual(actual[channels[k]], expected[channels[k]]); + } +}; diff --git a/test/mp/dye.js b/test/mp/dye.js index 26b33c2..ac0d9b3 100644 --- a/test/mp/dye.js +++ b/test/mp/dye.js @@ -1,7 +1,7 @@ "use strict"; var vows = require("vows"), load = require("../load"), - assert = require("assert"); + assert = require("../assert"); var suite = vows.describe("mp.dye"); diff --git a/test/mp/future.js b/test/mp/future.js index 25f7a21..735342e 100644 --- a/test/mp/future.js +++ b/test/mp/future.js @@ -1,7 +1,7 @@ "use strict"; var vows = require("vows"), load = require("../load"), - assert = require("assert"), + assert = require("../assert"), jsdom = require("jsdom"); var suite = vows.describe("mp.dye"); @@ -18,23 +18,6 @@ var dyeData = { ] }; -function assertImageDataEqual(input, expected, actual, width) { - assert.equal(actual.length, expected.length, "expected same " + expected.length + " pixel components, found " + actual.length); - for (var i = 0; i != actual.length; i += 4) { - var p = i / 4; - var y = Math.floor(p / width); - var x = p - y * width; - var msg = "At (" + x + "," + y + "): " - + "Input rgba(" + input [i ] + "," + input [i + 1] + "," + input [i + 2] + "," + input [i + 3] + ") " - + "should dye to rgba(" + expected[i ] + "," + expected[i + 1] + "," + expected[i + 2] + "," + expected[i + 3] + "); " - + "found rgba(" + actual [i ] + "," + actual [i + 1] + "," + actual [i + 2] + "," + actual [i + 3] + ")"; - assert.equal(actual[i ], expected[i ], msg); - assert.equal(actual[i + 1], expected[i + 1], msg); - assert.equal(actual[i + 2], expected[i + 2], msg); - assert.equal(actual[i + 3], expected[i + 3], msg); - } -} - function unshiftLoadImageBind(url, tests) { tests.topic = function() { var mp = arguments[arguments.length - 1]; @@ -50,21 +33,23 @@ function unshiftLoadImageBind(url, tests) { return tests; } +function testDyeParse(expected, input, mp) { + assert.dyeDataEqual(mp.dye.parseDyeString(input), expected); +} + function testDye(err, dyed, dyeable, mp) { var input = dyeable.data; var expected = dyed.data; var actual = new Uint8ClampedArray(input); mp.dye.dyeImage(actual, dyeData); - assertImageDataEqual(input, expected, actual, dyed.width); + assert.imageDataEqual(actual, expected, input, dyed.width); } suite.addBatch({ "The manaportal dye": { topic: load("mp/dye", "mp/resource").expression("mp").document(), "parseDyeString": { - "extracts the dye channel data from the dyestring": function(mp) { - assert.equal(mp.dye.parseDyeString(dyeString), dyeData); - } + "extracts the dye channel data from the dyestring": testDyeParse.bind(null, dyeData, dyeString) }, "asDyeString": { "reconstructs the dyestring from the dye channel data": function(mp) { -- cgit v1.2.3-60-g2f50