From 2cf8ba330dc60fa4c269c4fdb16716a1d9ad1e90 Mon Sep 17 00:00:00 2001 From: Freeyorp Date: Tue, 25 Jun 2013 23:26:50 +1200 Subject: Add dye API, canvas loading, future tests The provisional API generates structured dye data from a dye string, and dyes an image when provided dyeable image data and the associated dyeing data. The splitting of an image URI and the dye string should probably be handled by a resource manager. Tests are distinguished between stable tests and future tests. Stable tests should pass at all times. Future tests should pass once implemented, and be moved to stable test suites. To run the future tests as well, run ./todo.sh. The current logic to load and extract image data should probably be refactored out from the test file and into some common file or resource manager file. Might need a custom assertion function for comparing image data to avoid flooding the console if something doesn't fully match. Finally, declare "use strict". --- .gitignore | 1 + package.json | 5 ++-- public/js/mp/dye.js | 10 +++++++- test/data/bigcake.png | Bin 0 -> 957 bytes test/data/whitecake.png | Bin 0 -> 548 bytes test/load.js | 65 +++++++++++++++++++++++++++------------------- test/mp/dye.js | 1 + test/mp/future.js | 67 ++++++++++++++++++++++++++++++++++++++++++++++++ todo.sh | 2 ++ 9 files changed, 122 insertions(+), 29 deletions(-) create mode 100644 test/data/bigcake.png create mode 100644 test/data/whitecake.png create mode 100644 test/mp/future.js create mode 100755 todo.sh diff --git a/.gitignore b/.gitignore index b25c15b..5396b94 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ *~ +node_modules diff --git a/package.json b/package.json index 661b533..cca8995 100644 --- a/package.json +++ b/package.json @@ -6,9 +6,10 @@ "smash": "~0.0.8", "uglify-js": "2.2.x", "vows": "0.7.x", - "jsdom": "0.5.x" + "jsdom": "0.5.x", + "canvas": "1.0.x" }, "scripts": { - "test": "node_modules/vows/bin/vows --spec 'test/*/*.js$'" + "test": "ls test/*/*.js | grep -v 'future\\.js$' | xargs node_modules/vows/bin/vows" } } diff --git a/public/js/mp/dye.js b/public/js/mp/dye.js index d4c016b..530b6c4 100644 --- a/public/js/mp/dye.js +++ b/public/js/mp/dye.js @@ -1,7 +1,9 @@ "use strict"; var mp = function(mp) { mp.dye = { - getChannel: getChannel + getChannel: getChannel, + parseDyeString: parseDyeString, + dyeImage: dyeImage }; var channel = [null, "R", "G", "Y", "B", "M", "C", "W"]; function getChannel(color) { @@ -26,5 +28,11 @@ var mp = function(mp) { return { channel: channel[idx], intensity: max }; } + function parseDyeString(dyeString) { + /* TODO */ + } + function dyeImage(imageData, dyeData) { + /* TODO */ + } return mp; }(mp || {}); diff --git a/test/data/bigcake.png b/test/data/bigcake.png new file mode 100644 index 0000000..7cc92b5 Binary files /dev/null and b/test/data/bigcake.png differ diff --git a/test/data/whitecake.png b/test/data/whitecake.png new file mode 100644 index 0000000..9bc96cb Binary files /dev/null and b/test/data/whitecake.png differ diff --git a/test/load.js b/test/load.js index d04c647..97bee69 100644 --- a/test/load.js +++ b/test/load.js @@ -1,44 +1,57 @@ +"use strict"; process.env.TZ = "UTC"; var smash = require("smash"), jsdom = require("jsdom"); module.exports = function() { - var files = [].slice.call(arguments).map(function(d) { return "public/js/" + d; }), - expression = "mp", - sandbox = {}; + var files = [].slice.call(arguments).map(function(d) { return "public/js/" + d; }), + expression = "mp", + sandbox = {}, + domString = ""; + + function topic() { + smash.load(files, expression, sandbox, this.callback); + } + + topic.expression = function(_) { + expression = _; + return topic; + }; - function topic() { - smash.load(files, expression, sandbox, this.callback); - } + topic.dom = function(_) { + if (!arguments.length) return domString; + domString = _; + return topic; + }; - topic.expression = function(_) { - expression = _; - return topic; - }; + topic.body = function(_) { + topic.dom("" + _ + ""); + return topic; + }; - topic.document = function(_) { - var document = jsdom.jsdom(""); + topic.document = function(_) { + var document = arguments.length ? _ : jsdom.jsdom(domString); - document.createRange = function() { - return { - selectNode: function() {}, - createContextualFragment: jsdom.jsdom - }; - }; + document.createRange = function() { + return { + selectNode: function() {}, + createContextualFragment: jsdom.jsdom + }; + }; + + sandbox = { + console: console, + document: document, + window: document.createWindow(), + }; - sandbox = { - console: console, - document: document, - window: document.createWindow(), + return topic; }; return topic; - }; - - return topic; }; process.on("uncaughtException", function(e) { - console.trace(e.stack); + console.trace(e.stack); }); diff --git a/test/mp/dye.js b/test/mp/dye.js index a4819ae..36acfa3 100644 --- a/test/mp/dye.js +++ b/test/mp/dye.js @@ -1,3 +1,4 @@ +"use strict"; var vows = require("vows"), load = require("../load"), assert = require("assert"); diff --git a/test/mp/future.js b/test/mp/future.js new file mode 100644 index 0000000..6f491ae --- /dev/null +++ b/test/mp/future.js @@ -0,0 +1,67 @@ +"use strict"; +var vows = require("vows"), + load = require("../load"), + assert = require("assert"), + jsdom = require("jsdom"), + Canvas = require("canvas"), + Image = Canvas.Image; + +var suite = vows.describe("mp.dye"); + +var canvas = new Canvas(32,32); +var context = canvas.getContext("2d"); + +var dyeString = "R:#ede5b2,fff7bf;G:#cccccc,ffffff"; +var dyeData = { + "R": [ + [0xed, 0xe5, 0xb2], + [0xff, 0xf7, 0xbf] + ], + "G": [ + [0xcc, 0xcc, 0xcc], + [0xff, 0xff, 0xff] + ] +}; + +function loadImage(url, tests) { + tests.topic = function() { + var image = new Image; + var tester = this; + var callback = this.callback; + var args = arguments; + image.onload = function() { + canvas.width = image.width; + canvas.height = image.height; + context.drawImage(image, 0, 0); + callback = callback.bind(tester, false, context.getImageData(0, 0, image.width, image.height)); + callback.apply(tester, args); + } + image.onerror = function(err) { + throw new Error("Error loading '" + url + "': " + err); + } + image.src = url; + }; + return tests; +} + +suite.addBatch({ + "The manaportal dye": { + topic: load("mp/dye").expression("mp.dye").document(), + "parseDyeString": { + "Extracts a the dye channel data from the dyestring": function(dye) { + assert.equal(dye.parseDyeString(dyeString), dyeData); + } + }, + "dyeImage": { + "with the big recolorable cake": loadImage("test/data/bigcake.png", { + "to the big white cake": loadImage("test/data/whitecake.png", { + "dyes correctly when given the correct dye data": function(err1, whiteCake, dyeableCake, dye) { + assert.deepEqual(dye.dyeImage(dyeableCake.data, dyeData), whiteCake.data); + } + }) + }) + } + } +}); + +suite.export(module); diff --git a/todo.sh b/todo.sh new file mode 100755 index 0000000..ce775d2 --- /dev/null +++ b/todo.sh @@ -0,0 +1,2 @@ +#!/bin/bash +node_modules/vows/bin/vows --spec 'test/*/*.js$' -- cgit v1.2.3-60-g2f50