diff --git a/test/unit/adapter.js b/test/unit/engine_adapter.js similarity index 100% rename from test/unit/adapter.js rename to test/unit/engine_adapter.js diff --git a/test/unit/blocks.js b/test/unit/engine_blocks.js similarity index 100% rename from test/unit/blocks.js rename to test/unit/engine_blocks.js diff --git a/test/unit/runtime.js b/test/unit/engine_runtime.js similarity index 100% rename from test/unit/runtime.js rename to test/unit/engine_runtime.js diff --git a/test/unit/sequencer.js b/test/unit/engine_sequencer.js similarity index 100% rename from test/unit/sequencer.js rename to test/unit/engine_sequencer.js diff --git a/test/unit/thread.js b/test/unit/engine_thread.js similarity index 100% rename from test/unit/thread.js rename to test/unit/engine_thread.js diff --git a/test/unit/clone.js b/test/unit/sprites_clone.js similarity index 100% rename from test/unit/clone.js rename to test/unit/sprites_clone.js diff --git a/test/unit/util_cast.js b/test/unit/util_cast.js new file mode 100644 index 000000000..dac55f596 --- /dev/null +++ b/test/unit/util_cast.js @@ -0,0 +1,178 @@ +var test = require('tap').test; +var cast = require('../../src/util/cast'); + +test('toNumber', function (t) { + // Numeric + t.equal(cast.toNumber(0), 0); + t.equal(cast.toNumber(1), 1); + t.equal(cast.toNumber(3.14), 3.14); + + // String + t.equal(cast.toNumber('0'), 0); + t.equal(cast.toNumber('1'), 1); + t.equal(cast.toNumber('3.14'), 3.14); + t.equal(cast.toNumber('0.1e10'), 1000000000); + t.equal(cast.toNumber('foobar'), 0); + + // Boolean + t.equal(cast.toNumber(true), 1); + t.equal(cast.toNumber(false), 0); + t.equal(cast.toNumber('true'), 0); + t.equal(cast.toNumber('false'), 0); + + // Undefined & object + t.equal(cast.toNumber(undefined), 0); + t.equal(cast.toNumber({}), 0); + t.end(); +}); + +test('toBoolean', function (t) { + // Numeric + t.equal(cast.toBoolean(0), false); + t.equal(cast.toBoolean(1), true); + t.equal(cast.toBoolean(3.14), true); + + // String + t.equal(cast.toBoolean('0'), false); + t.equal(cast.toBoolean('1'), true); + t.equal(cast.toBoolean('3.14'), true); + t.equal(cast.toBoolean('0.1e10'), true); + t.equal(cast.toBoolean('foobar'), true); + + // Boolean + t.equal(cast.toBoolean(true), true); + t.equal(cast.toBoolean(false), false); + + // Undefined & object + t.equal(cast.toBoolean(undefined), false); + t.equal(cast.toBoolean({}), true); + t.end(); +}); + +test('toString', function (t) { + // Numeric + t.equal(cast.toString(0), '0'); + t.equal(cast.toString(1), '1'); + t.equal(cast.toString(3.14), '3.14'); + + // String + t.equal(cast.toString('0'), '0'); + t.equal(cast.toString('1'), '1'); + t.equal(cast.toString('3.14'), '3.14'); + t.equal(cast.toString('0.1e10'), '0.1e10'); + t.equal(cast.toString('foobar'), 'foobar'); + + // Boolean + t.equal(cast.toString(true), 'true'); + t.equal(cast.toString(false), 'false'); + + // Undefined & object + t.equal(cast.toString(undefined), 'undefined'); + t.equal(cast.toString({}), '[object Object]'); + t.end(); +}); + +test('toRbgColorList', function (t) { + // Hex (minimal, see "color" util tests) + t.deepEqual(cast.toRgbColorList('#000'), [0,0,0]); + t.deepEqual(cast.toRgbColorList('#000000'), [0,0,0]); + t.deepEqual(cast.toRgbColorList('#fff'), [255,255,255]); + t.deepEqual(cast.toRgbColorList('#ffffff'), [255,255,255]); + + // Decimal (minimal, see "color" util tests) + t.deepEqual(cast.toRgbColorList(0), [0,0,0]); + t.deepEqual(cast.toRgbColorList(1), [0,0,1]); + t.deepEqual(cast.toRgbColorList(16777215), [255,255,255]); + + // Malformed + t.deepEqual(cast.toRgbColorList('ffffff'), [0,0,0]); + t.deepEqual(cast.toRgbColorList('foobar'), [0,0,0]); + t.end(); +}); + +test('compare', function (t) { + // Numeric + t.equal(cast.compare(0, 0), 0); + t.equal(cast.compare(1, 0), 1); + t.equal(cast.compare(0, 1), -1); + t.equal(cast.compare(1, 1), 0); + + // String + t.equal(cast.compare('0', '0'), 0); + t.equal(cast.compare('0.1e10', '1000000000'), 0); + t.equal(cast.compare('foobar', 'FOOBAR'), 0); + t.equal(cast.compare('dog', 'cat'), 1); + + // Boolean + t.equal(cast.compare(true, true), 0); + t.equal(cast.compare(true, false), 1); + t.equal(cast.compare(false, true), -1); + t.equal(cast.compare(true, true), 0); + + // Undefined & object + t.equal(cast.compare(undefined, undefined), 0); + t.equal(cast.compare(undefined, 'undefined'), 0); + t.equal(cast.compare({}, {}), 0); + t.equal(cast.compare({}, '[object Object]'), 0); + t.end(); +}); + +test('isInt', function (t) { + // Numeric + t.equal(cast.isInt(0), true); + t.equal(cast.isInt(1), true); + t.equal(cast.isInt(0.0), true); + t.equal(cast.isInt(3.14), false); + t.equal(cast.isInt(NaN), true); + + // String + t.equal(cast.isInt('0'), true); + t.equal(cast.isInt('1'), true); + t.equal(cast.isInt('0.0'), false); // @todo This should be true + t.equal(cast.isInt('0.1e10'), false); // @todo This should be true + t.equal(cast.isInt('3.14'), false); + + // Boolean + t.equal(cast.isInt(true), true); + t.equal(cast.isInt(false), true); + + // Undefined & object + t.equal(cast.isInt(undefined), false); + t.equal(cast.isInt({}), false); + t.end(); +}); + +test('toListIndex', function (t) { + var list = [0,1,2,3,4,5]; + var empty = []; + + // Valid + t.equal(cast.toListIndex(1, list.length), 1); + t.equal(cast.toListIndex(6, list.length), 6); + + // Invalid + t.equal(cast.toListIndex(-1, list.length), 'INVALID'); + t.equal(cast.toListIndex(0.1, list.length), 'INVALID'); + t.equal(cast.toListIndex(0, list.length), 'INVALID'); + t.equal(cast.toListIndex(7, list.length), 'INVALID'); + + // "all" + t.equal(cast.toListIndex('all', list.length), 'ALL'); + + // "last" + t.equal(cast.toListIndex('last', list.length), list.length); + t.equal(cast.toListIndex('last', empty.length), 'INVALID'); + + // "random" + var random = cast.toListIndex('random', list.length); + t.ok(random <= list.length); + t.ok(random > 0); + t.equal(cast.toListIndex('random', empty.length), 'INVALID'); + + // "any" (alias for "random") + var any = cast.toListIndex('any', list.length); + t.ok(any <= list.length); + t.ok(any > 0); + t.equal(cast.toListIndex('any', empty.length), 'INVALID'); + t.end(); +}); diff --git a/test/unit/util_color.js b/test/unit/util_color.js new file mode 100644 index 000000000..0558b4b3c --- /dev/null +++ b/test/unit/util_color.js @@ -0,0 +1,62 @@ +var test = require('tap').test; +var color = require('../../src/util/color'); + +test('decimalToHex', function (t) { + t.equal(color.decimalToHex(0), '#000000'); + t.equal(color.decimalToHex(1), '#000001'); + t.equal(color.decimalToHex(16777215), '#ffffff'); + t.equal(color.decimalToHex(-16777215), '#000001'); // @todo + t.equal(color.decimalToHex(99999999), '#5f5e0ff'); // @todo + t.end(); +}); + +test('decimalToRgb', function (t) { + t.deepEqual(color.decimalToRgb(0), {r:0,g:0,b:0}); + t.deepEqual(color.decimalToRgb(1), {r:0,g:0,b:1}); + t.deepEqual(color.decimalToRgb(16777215), {r:255,g:255,b:255}); + t.deepEqual(color.decimalToRgb(-16777215), {r:0,g:0,b:1}); // @todo + t.deepEqual(color.decimalToRgb(99999999), {r:245,g:224,b:255}); // @todo + t.end(); +}); + +test('hexToRgb', function (t) { + t.deepEqual(color.hexToRgb('#000'), {r:0,g:0,b:0}); + t.deepEqual(color.hexToRgb('#000000'), {r:0,g:0,b:0}); + t.deepEqual(color.hexToRgb('#fff'), {r:255,g:255,b:255}); + t.deepEqual(color.hexToRgb('#ffffff'), {r:255,g:255,b:255}); + t.deepEqual(color.hexToRgb('#0fa'), {r:0,g:255,b:170}); + t.deepEqual(color.hexToRgb('#00ffaa'), {r:0,g:255,b:170}); + + t.deepEqual(color.hexToRgb('000'), {r:0,g:0,b:0}); + t.deepEqual(color.hexToRgb('fff'), {r:255,g:255,b:255}); + t.deepEqual(color.hexToRgb('00ffaa'), {r:0,g:255,b:170}); + + t.deepEqual(color.hexToRgb('0'), null); + t.deepEqual(color.hexToRgb('hello world'), null); + + t.end(); +}); + +test('rgbToHex', function (t) { + t.equal(color.rgbToHex({r:0,g:0,b:0}), '#000000'); + t.equal(color.rgbToHex({r:255,g:255,b:255}), '#ffffff'); + t.equal(color.rgbToHex({r:0,g:255,b:170}), '#00ffaa'); + t.end(); +}); + +test('rgbToDecimal', function (t) { + t.equal(color.rgbToDecimal({r:0,g:0,b:0}), 0); + t.equal(color.rgbToDecimal({r:255,g:255,b:255}), 16777215); + t.equal(color.rgbToDecimal({r:0,g:255,b:170}), 65450); + t.end(); +}); + +test('hexToDecimal', function (t) { + t.deepEqual(color.hexToDecimal('#000'), 0); + t.deepEqual(color.hexToDecimal('#000000'), 0); + t.deepEqual(color.hexToDecimal('#fff'), 16777215); + t.deepEqual(color.hexToDecimal('#ffffff'), 16777215); + t.deepEqual(color.hexToDecimal('#0fa'), 65450); + t.deepEqual(color.hexToDecimal('#00ffaa'), 65450); + t.end(); +}); diff --git a/test/unit/util_math.js b/test/unit/util_math.js new file mode 100644 index 000000000..484b88c4b --- /dev/null +++ b/test/unit/util_math.js @@ -0,0 +1,37 @@ +var test = require('tap').test; +var math = require('../../src/util/math-util'); + +test('degToRad', function (t) { + // @todo This is incorrect + t.equal(math.degToRad(0), 1.5707963267948966); + t.equal(math.degToRad(1), 1.5533430342749535); + t.equal(math.degToRad(180), -1.5707963267948966); + t.equal(math.degToRad(360), -4.71238898038469); + t.equal(math.degToRad(720), -10.995574287564276); + t.end(); +}); + +test('radToDeg', function (t) { + t.equal(math.radToDeg(0), 0); + t.equal(math.radToDeg(1), 57.29577951308232); + t.equal(math.radToDeg(180), 10313.240312354817); + t.equal(math.radToDeg(360), 20626.480624709635); + t.equal(math.radToDeg(720), 41252.96124941927); + t.end(); +}); + +test('clamp', function (t) { + t.equal(math.clamp(0, 0, 10), 0); + t.equal(math.clamp(1, 0, 10), 1); + t.equal(math.clamp(-10, 0, 10), 0); + t.equal(math.clamp(100, 0, 10), 10); + t.end(); +}); + +test('wrapClamp', function (t) { + t.equal(math.wrapClamp(0, 0, 10), 0); + t.equal(math.wrapClamp(1, 0, 10), 1); + t.equal(math.wrapClamp(-10, 0, 10), 1); + t.equal(math.wrapClamp(100, 0, 10), 1); + t.end(); +}); diff --git a/test/unit/timer.js b/test/unit/util_timer.js similarity index 100% rename from test/unit/timer.js rename to test/unit/util_timer.js diff --git a/test/unit/util_xml.js b/test/unit/util_xml.js new file mode 100644 index 000000000..8682d8323 --- /dev/null +++ b/test/unit/util_xml.js @@ -0,0 +1,9 @@ +var test = require('tap').test; +var xml = require('../../src/util/xml-escape'); + +test('escape', function (t) { + var input = '<foo bar="he & llo \'"></foo>'; + var output = '<foo bar="he & llo '"></foo>'; + t.equal(xml(input), output); + t.end(); +});