diff --git a/tests/arithmetics/counters.js b/tests/arithmetics/counters.js index 9bcbd63..0935dda 100644 --- a/tests/arithmetics/counters.js +++ b/tests/arithmetics/counters.js @@ -3,8 +3,8 @@ return new UnitTest('counters') .add('postfix decrement', function () { var i = 10; i-- === 10; }) .add('prefix decrement', function () { var i = 10; --i === 9; }) .add('prefix increment', function () { var i = 10; ++i === 11; }) - .add('ostfix increment of non-number', function () { var i = 'hi mom'; isNaN(i++); }) - .add('ostfix decrement of non-number', function () { var i = 'hi mom'; isNaN(i--); }) + .add('postfix increment of non-number', function () { var i = 'hi mom'; isNaN(i++); }) + .add('postfix decrement of non-number', function () { var i = 'hi mom'; isNaN(i--); }) .add('prefix increment of non-number', function () { var i = 'hi mom'; isNaN(++i); }) .add('prefix decrement of non-number', function () { var i = 'hi mom'; isNaN(--i); }) .add('postfix increment of convertible to number', function () { var i = '10'; i++; i === 11; }) \ No newline at end of file diff --git a/tests/arithmetics/index.js b/tests/arithmetics/index.js index a1cb82c..aabe7ba 100644 --- a/tests/arithmetics/index.js +++ b/tests/arithmetics/index.js @@ -1,2 +1,2 @@ return new UnitTest('Arithmetics') - .add(include('counters.js')) \ No newline at end of file + .add(require('counters.js')) \ No newline at end of file diff --git a/tests/array/constructor.js b/tests/array/constructor.js new file mode 100644 index 0000000..536719b --- /dev/null +++ b/tests/array/constructor.js @@ -0,0 +1,12 @@ +return new UnitTest('constructor', function() { return typeof Array === 'function'; }) + .add('no args', function () { return match(new Array(), []); }) + .add('length', function () { + var res = new Array(3); + return res.length === 3 && + !(0 in res) && + !(1 in res) && + !(2 in res); + }) + .add('elements', function () { + return match(new Array(1, 2, 3), [1, 2, 3]); + }) diff --git a/tests/array/fill.js b/tests/array/fill.js new file mode 100644 index 0000000..2b9dfc9 --- /dev/null +++ b/tests/array/fill.js @@ -0,0 +1,19 @@ +return new UnitTest('fill', function() { return typeof Array.prototype.push === 'function'; }) + .add('simple fill', function() { + return match([5, 5, 5, 5, 5], [1, 2, 3, 4, 5].fill(5)) + }) + .add('fill empty', function() { + return match([], [].fill(5)) + }) + .add('fill from', function() { + return match([1, 'a', 'a', 'a', 'a'], [1, 2, 3, 4, 5].fill('a', 1)) + }) + .add('fill range', function() { + return match([1, 'a', 'a', 'a', 5], [1, 2, 3, 4, 5].fill('a', 1, 4)) + }) + .add('fill wrap', function() { + return match([1, 'a', 'a', 4, 5], [1, 2, 3, 4, 5].fill('a', 1, -2)) + }) + .add('fill out of range', function() { + return match([1, 2, 'a', 'a', 'a'], [1, 2, 3, 4, 5].fill('a', 2, 8)) + }) \ No newline at end of file diff --git a/tests/array/find.js b/tests/array/find.js new file mode 100644 index 0000000..db0321a --- /dev/null +++ b/tests/array/find.js @@ -0,0 +1,19 @@ +var a = { id: 10, name: 'test1' }; +var b = { id: 15, name: 'test2' }; +var c = { id: 20, name: 'test3' }; + +return new UnitTest('find', function() { return typeof Array.prototype.find === 'function'; }) + .add('simple', function() { + return [ a, b, c ].find(function (v) { return v.id === 15; }) === b; + }) + .add('sparse', function() { + var n = 0; + [ a, b,,,, c ].find(function (v) { n++; return v === undefined; }); + return n === 3; + }) + .add('no occurence', function() { + return [ a, b, c ].find(function (v) { return v.id === 30 }) === undefined; + }) + .add('pass this', function() { + return [ a, b ].find(function (v) { return this === c; }, c) === a; + }) \ No newline at end of file diff --git a/tests/array/index.js b/tests/array/index.js index ee18fa2..ef94a2a 100644 --- a/tests/array/index.js +++ b/tests/array/index.js @@ -1,22 +1,12 @@ -function runIterator(arr, method, func, n) { - var res = []; - var j = 1; - var args = [ function() { - var pushed = []; - for (var i = 0; i < n; i++) pushed[i] = arguments[i]; - res[j++] = pushed; - return func.apply(this, arguments); - } ]; - - for (var i = 4; i < arguments.length; i++) args[i - 3] = arguments[i]; - - res[0] = method.apply(arr, args); - - return res; -} - return new UnitTest('Array', function() { []; }) - .add(include('length.js')) - .add(include('reduce.js')) - .add(include('sparse.js')) - .add(include('concat.js')) + .add(require('constructor.js')) + .add(require('length.js')) + .add(require('reduce.js')('reduce')) + .add(require('reduce.js')('reduceRight')) + .add(require('sparse.js')) + .add(require('concat.js')) + .add(require('sort.js')) + .add(require('push.js')) + .add(require('pop.js')) + .add(require('fill.js')) + .add(require('find.js')) diff --git a/tests/array/indexOf.js b/tests/array/indexOf.js new file mode 100644 index 0000000..2b9dfc9 --- /dev/null +++ b/tests/array/indexOf.js @@ -0,0 +1,19 @@ +return new UnitTest('fill', function() { return typeof Array.prototype.push === 'function'; }) + .add('simple fill', function() { + return match([5, 5, 5, 5, 5], [1, 2, 3, 4, 5].fill(5)) + }) + .add('fill empty', function() { + return match([], [].fill(5)) + }) + .add('fill from', function() { + return match([1, 'a', 'a', 'a', 'a'], [1, 2, 3, 4, 5].fill('a', 1)) + }) + .add('fill range', function() { + return match([1, 'a', 'a', 'a', 5], [1, 2, 3, 4, 5].fill('a', 1, 4)) + }) + .add('fill wrap', function() { + return match([1, 'a', 'a', 4, 5], [1, 2, 3, 4, 5].fill('a', 1, -2)) + }) + .add('fill out of range', function() { + return match([1, 2, 'a', 'a', 'a'], [1, 2, 3, 4, 5].fill('a', 2, 8)) + }) \ No newline at end of file diff --git a/tests/array/length.js b/tests/array/length.js index 9bee0e1..c107cea 100644 --- a/tests/array/length.js +++ b/tests/array/length.js @@ -8,12 +8,12 @@ return new UnitTest('length & capacity', function() { return 'length' in Array.p }) .add('length after set', function() { var a = []; - a [5]= 5; + a[5] = 5; return a.length === 6; }) - .add('length after set (big', function() { + .add('length after set (big)', function() { var a = [1, 2]; - a [5000]= 5; + a[5000] = 5; return a.length === 5001; }) .add('expand test', function() { diff --git a/tests/array/pop.js b/tests/array/pop.js new file mode 100644 index 0000000..41308cd --- /dev/null +++ b/tests/array/pop.js @@ -0,0 +1,8 @@ +return new UnitTest('pop', function() { return typeof Array.prototype.pop === 'function'; }) + .add('simple pop', function() { + var arr = [1, 2, 3]; + return match(3, arr.pop()) + }) + .add('pop from empty', function() { + return match(undefined, [].pop()) + }) \ No newline at end of file diff --git a/tests/array/push.js b/tests/array/push.js new file mode 100644 index 0000000..7f60e14 --- /dev/null +++ b/tests/array/push.js @@ -0,0 +1,16 @@ +return new UnitTest('push', function() { return typeof Array.prototype.push === 'function'; }) + .add('simple push', function() { + var arr = []; + arr.push(1, 2, 3); + return match(arr, [1, 2, 3]) + }) + .add('push array', function() { + var arr = []; + arr.push([1, 2, 3]); + return match(arr, [[1, 2, 3]]) + }) + .add('push as concat', function() { + var arr = [1, 2, 3]; + arr.push(4, 5, 6); + return match(arr, [1, 2, 3, 4, 5, 6]) + }) \ No newline at end of file diff --git a/tests/array/reduce.js b/tests/array/reduce.js index c1c2892..727de4c 100644 --- a/tests/array/reduce.js +++ b/tests/array/reduce.js @@ -1,44 +1,62 @@ -var res = []; +function runIterator(arr, method, func, n) { + var res = []; + var j = 1; + var args = [ function() { + var pushed = []; + for (var i = 0; i < n; i++) pushed[i] = arguments[i]; + res[j++] = pushed; + return func.apply(this, arguments); + } ]; -return new UnitTest('reduceRight', function () { return typeof Array.prototype.reduceRight === 'function' }) - .add('empty function', function () {match( - [ undefined, [4, 3, 2], [undefined, 2, 1], [undefined, 1, 0], ], - runIterator([1, 2, 3, 4], Array.prototype.reduceRight, function() { }, 3), 1 - )}) - .add('adder', function () {match( - [ 10, [4, 3, 2], [7, 2, 1], [9, 1, 0], ], - runIterator([1, 2, 3, 4], Array.prototype.reduceRight, function(a, b) { return a + b; }, 3), 1 - )}) - .add('sparse array', function () {match( - [ 10, [4, 3, 11], [7, 2, 7], [9, 1, 3], ], - runIterator([,,,1,,,, 2,,,, 3,,,, 4,,,,], Array.prototype.reduceRight, function(a, b) { return a + b }, 3), 1 - )}) - .add('sparse array with one element', function () {match( - [ 1 ], - runIterator([,,,1,,,,], Array.prototype.reduceRight, function(v) { return v; }, 3), 1 - )}) - .add('sparse array with no elements', function () {match( - [ undefined ], - runIterator([,,,,,,,], Array.prototype.reduceRight, function(v) { return v; }, 3), 1 - )}) + for (var i = 4; i < arguments.length; i++) args[i - 3] = arguments[i]; - .add('initial value and empty function', function () {match( - [ undefined, [0, 4, 3], [undefined, 3, 2], [undefined, 2, 1], [undefined, 1, 0] ], - runIterator([1, 2, 3, 4], Array.prototype.reduceRight, function() { }, 3, 0), 1 - )}) - .add('initial value and adder', function () {match( - [ 15, [5, 4, 3], [9, 3, 2], [12, 2, 1], [14, 1, 0] ], - runIterator([1, 2, 3, 4], Array.prototype.reduceRight, function(a, b) { return a + b; }, 3, 5), 1 - )}) - .add('initial value, sparce array and adder', function () {match( - [ 15, [5, 4, 15], [9, 3, 11], [12, 2, 7], [14, 1, 3] ], - runIterator([,,,1,,,, 2,,,, 3,,,, 4,,,,], Array.prototype.reduceRight, function(a, b) { return a + b; }, 3, 5), 1 - )}) - .add('initial value and sparse array with one element', function () {match( - [ 6, [5, 1, 3] ], - runIterator([,,,1,,,,], Array.prototype.reduceRight, function(a, b) { return a + b; }, 3, 5), 1 - )}) - .add('initial value and sparse array with no elements', function () {match( - [ 5 ], - runIterator([,,,,,,,], Array.prototype.reduceRight, function(v) { return v; }, 3, 5), 1 - )}); \ No newline at end of file + res[0] = method.apply(arr, args); + + return res; +} + +return function(name) { + var func = Array.prototype[name]; + return new UnitTest(name, function () { return typeof func === 'function' }) + .add('empty function', function () {match( + [ undefined, [4, 3, 2], [undefined, 2, 1], [undefined, 1, 0], ], + runIterator([1, 2, 3, 4], func, function() { }, 3), 1 + )}) + .add('adder', function () {match( + [ 10, [4, 3, 2], [7, 2, 1], [9, 1, 0], ], + runIterator([1, 2, 3, 4], func, function(a, b) { return a + b; }, 3), 1 + )}) + .add('sparse array', function () {match( + [ 10, [4, 3, 11], [7, 2, 7], [9, 1, 3], ], + runIterator([,,,1,,,, 2,,,, 3,,,, 4,,,,], func, function(a, b) { return a + b }, 3), 1 + )}) + .add('sparse array with one element', function () {match( + [ 1 ], + runIterator([,,,1,,,,], func, function(v) { return v; }, 3), 1 + )}) + .add('sparse array with no elements', function () {match( + [ undefined ], + runIterator([,,,,,,,], func, function(v) { return v; }, 3), 1 + )}) + + .add('initial value and empty function', function () {match( + [ undefined, [0, 4, 3], [undefined, 3, 2], [undefined, 2, 1], [undefined, 1, 0] ], + runIterator([1, 2, 3, 4], func, function() { }, 3, 0), 1 + )}) + .add('initial value and adder', function () {match( + [ 15, [5, 4, 3], [9, 3, 2], [12, 2, 1], [14, 1, 0] ], + runIterator([1, 2, 3, 4], func, function(a, b) { return a + b; }, 3, 5), 1 + )}) + .add('initial value, sparce array and adder', function () {match( + [ 15, [5, 4, 15], [9, 3, 11], [12, 2, 7], [14, 1, 3] ], + runIterator([,,,1,,,, 2,,,, 3,,,, 4,,,,], func, function(a, b) { return a + b; }, 3, 5), 1 + )}) + .add('initial value and sparse array with one element', function () {match( + [ 6, [5, 1, 3] ], + runIterator([,,,1,,,,], func, function(a, b) { return a + b; }, 3, 5), 1 + )}) + .add('initial value and sparse array with no elements', function () {match( + [ 5 ], + runIterator([,,,,,,,], func, function(v) { return v; }, 3, 5), 1 + )}); +} \ No newline at end of file diff --git a/tests/array/sort.js b/tests/array/sort.js index bab004f..965196e 100644 --- a/tests/array/sort.js +++ b/tests/array/sort.js @@ -1,4 +1,6 @@ -return new UnitTest('concat', function() { return typeof Array.prototype.concat === 'function'; }) - .add('two arrays', function() { return match([1, 2, 3], [1].concat([2], [3])) }) - .add('simple spread', function() { return match([1, 2, 3, 4, 5], [1].concat([2], 3, [4, 5])) }) - .add('sparse concat', function() { return match([1,, 2,,, 3,,, 4, 5], [1,,2].concat([,,3,,,4], 5)) }) \ No newline at end of file +return new UnitTest('sort', function() { return typeof Array.prototype.sort === 'function'; }) + .add('simple', function() { return match([4, 6, 2].sort(), [2, 4, 6]) }) + .add('stringify', function() { return match([4, 6, 2, 10].sort(), [10, 2, 4, 6]) }) + .add('undefined and empty', function() { return match([4, undefined, 6, , 2].sort(), [2, 4, 6, undefined,,]) }) + .add('function ascend', function() { return match([3, 1, 2].sort(function (a, b) { return a - b; }), [1, 2, 3]) }) + .add('function descend', function() { return match([3, 1, 2].sort(function (a, b) { return b - a; }), [3, 2, 1]) }) \ No newline at end of file diff --git a/tests/entry.js b/tests/entry.js new file mode 100644 index 0000000..9804afb --- /dev/null +++ b/tests/entry.js @@ -0,0 +1 @@ +require('tests/index.js')(); \ No newline at end of file diff --git a/tests/index.js b/tests/index.js index 6290b85..82bc508 100644 --- a/tests/index.js +++ b/tests/index.js @@ -2,19 +2,24 @@ function assert(cond, msg, locDepth) { if (locDepth < 0 || locDepth === undefined) locDepth = 0; if (!cond) { log('Assert failed', (typeof locDepth === 'string' ? locDepth : Error().stack[locDepth + 1]) + ': ', msg); + return false; } + return true; } function assertMatch(expected, actual, depth, msg) { if (!match(expected, actual, depth)) { log('Assert failed', Error().stack[1] + ': ', msg); log('Expected:', expected); log('Actual:', actual); + return false; } + return true; } function match(expected, actual, depth) { if (!Array.isArray(expected) || !Array.isArray(actual)) return expected === actual; else if (expected.length !== actual.length) return false; - else if (depth === undefined || depth < 0) depth = 0; + else if (depth === undefined) depth = Infinity; + else if (depth < 0) depth = 0; for (var i = 0; i < expected.length; i++) { if (!(i in expected) || !(i in actual)) return !(i in expected) && !(i in actual); @@ -43,6 +48,7 @@ UnitTest.prototype.run = function(path) { if (path === undefined) path = []; path.push(this.name); + var res = true; if (typeof this.exec === 'function') { var res = true, err = 'exec() returned false.'; @@ -50,13 +56,15 @@ UnitTest.prototype.run = function(path) { if (this.exec() === false) res = false; } catch (e) { res = false; err = e; } - assert(res, path.join('/') + ': ' + err, this.exec.location()); + res &= assert(res, path.join('/') + ': ' + err, this.exec.location()); } for (var i = 0; i < this.subtests.length; i++) { - this.subtests[i].run(path); + res &= this.subtests[i].run(path); } path.pop(); + + return res; } UnitTest.prototype.add = function(test, exec) { if (test instanceof UnitTest) this.subtests.push(test); @@ -64,7 +72,10 @@ UnitTest.prototype.add = function(test, exec) { return this; } -include('arithmetics/index.js').run(); -include('array/index.js').run(); - -log('Tests complete.'); \ No newline at end of file +return function() { + if ( + require('arithmetics/index.js').run() && + require('array/index.js').run() + ) log('All tests passed!'); + exit(); +}