remove old test
This commit is contained in:
parent
af35d7f20b
commit
3f826cc85d
@ -1,10 +0,0 @@
|
||||
return new UnitTest('counters')
|
||||
.add('postfix increment', function () { var i = 10; i++ === 10; })
|
||||
.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('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; })
|
@ -1,2 +0,0 @@
|
||||
return new UnitTest('Arithmetics')
|
||||
.add(require('counters.js'))
|
@ -1,4 +0,0 @@
|
||||
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)) })
|
@ -1,12 +0,0 @@
|
||||
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]);
|
||||
})
|
@ -1,19 +0,0 @@
|
||||
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))
|
||||
})
|
@ -1,19 +0,0 @@
|
||||
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;
|
||||
})
|
@ -1,12 +0,0 @@
|
||||
return new UnitTest('Array', function() { []; })
|
||||
.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'))
|
@ -1,19 +0,0 @@
|
||||
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))
|
||||
})
|
@ -1,26 +0,0 @@
|
||||
return new UnitTest('length & capacity', function() { return 'length' in Array.prototype; })
|
||||
.add('empty literal', function() { return [].length === 0 })
|
||||
.add('filled literal', function() { return [1, 2, 3].length === 3 })
|
||||
.add('set length', function() {
|
||||
var a = [];
|
||||
a.length = 10;
|
||||
return a.length === 10;
|
||||
})
|
||||
.add('length after set', function() {
|
||||
var a = [];
|
||||
a[5] = 5;
|
||||
return a.length === 6;
|
||||
})
|
||||
.add('length after set (big)', function() {
|
||||
var a = [1, 2];
|
||||
a[5000] = 5;
|
||||
return a.length === 5001;
|
||||
})
|
||||
.add('expand test', function() {
|
||||
var a = [];
|
||||
for (var i = 0; i < 1000; i++) {
|
||||
a[i] = i * 50;
|
||||
if (a[i] !== i * 50) return false;
|
||||
}
|
||||
return a.length === 1000;
|
||||
})
|
@ -1,8 +0,0 @@
|
||||
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())
|
||||
})
|
@ -1,16 +0,0 @@
|
||||
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])
|
||||
})
|
@ -1,62 +0,0 @@
|
||||
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 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
|
||||
)});
|
||||
}
|
@ -1,6 +0,0 @@
|
||||
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]) })
|
@ -1,5 +0,0 @@
|
||||
return new UnitTest('sparse', function() { return !(0 in [,,]) })
|
||||
.add('empty in start', function() { var a = [,1]; return !(0 in a) && (1 in a); })
|
||||
.add('empty in middle', function() { var a = [1,,2]; return !(1 in a) && (2 in a) && (0 in a); })
|
||||
.add('empty in end', function() { var a = [1,,]; return !(1 in a) && (0 in a); })
|
||||
.add('trailing comma', function() { var a = [1,]; return a.length === 1; })
|
@ -1 +0,0 @@
|
||||
require('tests/index.js')();
|
@ -1,81 +0,0 @@
|
||||
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 = 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);
|
||||
|
||||
if (
|
||||
expected[i] === actual[i] ||
|
||||
depth > 0 &&
|
||||
Array.isArray(expected) &&
|
||||
Array.isArray(actual) &&
|
||||
match(expected[i], actual[i], depth - 1)
|
||||
) continue;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/** @class */ function UnitTest(msg, exec) {
|
||||
this.name = msg;
|
||||
this.exec = exec;
|
||||
this.subtests = [];
|
||||
}
|
||||
|
||||
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.';
|
||||
try {
|
||||
if (this.exec() === false) res = false;
|
||||
}
|
||||
catch (e) { res = false; err = e; }
|
||||
res &= assert(res, path.join('/') + ': ' + err, this.exec.location());
|
||||
}
|
||||
for (var i = 0; i < this.subtests.length; i++) {
|
||||
res &= this.subtests[i].run(path);
|
||||
}
|
||||
|
||||
path.pop();
|
||||
|
||||
return res;
|
||||
}
|
||||
UnitTest.prototype.add = function(test, exec) {
|
||||
if (test instanceof UnitTest) this.subtests.push(test);
|
||||
else this.subtests.push(new UnitTest(test, exec));
|
||||
return this;
|
||||
}
|
||||
|
||||
return function() {
|
||||
if (
|
||||
require('arithmetics/index.js').run() &&
|
||||
require('array/index.js').run()
|
||||
) log('All tests passed!');
|
||||
exit();
|
||||
}
|
Loading…
Reference in New Issue
Block a user