Comparing version 5.1.1 to 5.2.0
import deq from 'dequal' | ||
// TODO: same (members) | ||
// TODO: almost | ||
export function fail(msg) { | ||
@@ -28,3 +25,3 @@ this.log(false, 'fail', msg) | ||
export function equal(a, b, msg = 'should be equal') { | ||
export function equal(a, b, msg = 'should equal') { | ||
this.log(Object.is(a, b), 'equal', msg, { | ||
@@ -36,3 +33,3 @@ actual: a, | ||
export function notEqual(a, b, msg = 'should not be equal') { | ||
export function notEqual(a, b, msg = 'should not equal') { | ||
this.log(!Object.is(a, b), 'notEqual', msg, { | ||
@@ -44,2 +41,9 @@ actual: a, | ||
export function equalAny(a, list, msg = 'should equal any') { | ||
this.log(list.some(b => Object.is(a, b)), 'equalAny', msg, { | ||
actual: a, | ||
expected: new (class Any extends Array {})(...list) | ||
}) | ||
} | ||
export function deepEqual(a, b, msg = 'should deep equal') { | ||
@@ -59,2 +63,11 @@ this.log(deq(a, b), 'deepEqual', msg, { | ||
export function deepEqualAny(a, list, msg = 'should deep equal any') { | ||
this.log(list.some(b => deq(a, b)), 'deepEqualAny', msg, { | ||
actual: isPrimitive(a) ? a : a.slice ? a.slice() : Object.assign({}, a), | ||
expected: new (class Any extends Array {})(...list.map(b => | ||
isPrimitive(b) ? b : b.slice ? b.slice() : Object.assign({}, b) | ||
)) | ||
}) | ||
} | ||
export function is(a, b, msg = 'should be the same') { | ||
@@ -67,2 +80,29 @@ this.log(isPrimitive(a) || isPrimitive(b) ? Object.is(a, b) : deq(a, b), 'is', msg, { | ||
export function same(a, b, msg = 'should have same members') { | ||
this.log(sameMembers(a, b), 'same', msg, { | ||
actual: a, | ||
expected: b | ||
}) | ||
} | ||
export function oneOf(a, list, msg = 'should be one of') { | ||
this.log(list.some(b => | ||
isPrimitive(a) || isPrimitive(b) ? Object.is(a, b) : deq(a, b) | ||
), 'oneOf', msg, { | ||
actual: isPrimitive(a) ? a : a.slice ? a.slice() : Object.assign({}, a), | ||
expected: new (class Any extends Array { })(...list.map(b => | ||
isPrimitive(b) ? b : b.slice ? b.slice() : Object.assign({}, b) | ||
)) | ||
}) | ||
} | ||
export function almost (a, b, eps, msg = 'should almost equal') { | ||
this.log(isPrimitive(a) || isPrimitive(b) ? almostEqual(a, b, eps) : | ||
Array.prototype.slice.call(a).every((a0, i) => a0 === b[i] || almostEqual(a0, b[i], eps)), | ||
'almost', msg, { | ||
actual: isPrimitive(a) ? a : a.slice ? a.slice() : Object.assign({}, a), | ||
expected: isPrimitive(b) ? b : b.slice ? b.slice() : Object.assign({}, b) | ||
}) | ||
} | ||
export function throws(fn, expected, msg = 'should throw') { | ||
@@ -101,1 +141,38 @@ try { | ||
} | ||
function almostEqual(a, b, eps) { | ||
if (eps === undefined) { | ||
eps = Math.min( | ||
Math.max( | ||
Math.abs(a - new Float32Array([a])[0]), | ||
Math.abs(b - new Float32Array([b])[0]) | ||
), | ||
1.19209290e-7 | ||
) | ||
} | ||
var d = Math.abs(a - b) | ||
if (d <= eps) { | ||
return true | ||
} | ||
return a === b | ||
} | ||
function sameMembers(a, b) { | ||
a = Array.from(a), b = Array.from(b) | ||
if (a.length !== b.length) return false; | ||
if (!b.every(function (item) { | ||
var idx = a.indexOf(item); | ||
if (idx < 0) return false; | ||
a.splice(idx, 1); | ||
return true; | ||
})) return false; | ||
if (a.length) return false; | ||
return true; | ||
} |
13
index.js
@@ -80,3 +80,3 @@ import * as assert from './assert.js' | ||
end: () => { }, | ||
log: (ok, operator, msg, info = {}) => { | ||
log: (ok, operator, msg, info) => { | ||
assertIndex += 1 | ||
@@ -94,7 +94,10 @@ if (ok) { | ||
console.log(`${RED}ā ${assertIndex} ā ${msg}`), | ||
console.info(`actual:${RESET}`, typeof info.actual === 'string' ? JSON.stringify(info.actual) : info.actual, RED), | ||
console.info(`expected:${RESET}`, typeof info.expected === 'string' ? JSON.stringify(info.expected) : info.expected, RED), | ||
console.error(new Error, RESET) | ||
info && ( | ||
console.info(`actual:${RESET}`, typeof info.actual === 'string' ? JSON.stringify(info.actual) : info.actual, RED), | ||
console.info(`expected:${RESET}`, typeof info.expected === 'string' ? JSON.stringify(info.expected) : info.expected, RED), | ||
console.error(new Error, RESET) | ||
) | ||
) : | ||
console.assert(false, `${assertIndex} ā ${msg}${RESET}`, info, new Error) | ||
info ? console.assert(false, `${assertIndex} ā ${msg}${RESET}`, info, new Error) : | ||
console.assert(false, `${assertIndex} ā ${msg}${RESET}`, new Error) | ||
if (!test.demo) { | ||
@@ -101,0 +104,0 @@ test.assertion.push({ idx: assertIndex, msg, info, error: new Error() }) |
{ | ||
"name": "tst", | ||
"description": "Tape-compatible test runner", | ||
"version": "5.1.1", | ||
"version": "5.2.0", | ||
"repository": "dy/tst", | ||
@@ -6,0 +6,0 @@ "author": "Dmitry Yv", |
@@ -8,6 +8,9 @@ # tst | ||
* `t.demo` - demo-run (can fail) | ||
* `t.is` - assert with `t.equal` for primitives and `t.deepEqual` for other | ||
* `t.oneOf(item, list, msg)` - assert optional results | ||
* `t.almost(a, b, eps, msg)` - assert approx value/array | ||
* `t.same(listA, listB, msg)` - assert same members | ||
* `console.group` in browser | ||
* inspectable errors | ||
* correct stacktrace with sourcemaps | ||
* `t.is` for generic comparison <!-- almost, same --> | ||
* muted skipped | ||
@@ -18,2 +21,3 @@ * better colors | ||
* ES export | ||
<!-- same --> | ||
@@ -30,2 +34,4 @@ ## Install | ||
## Asserts | ||
## Use | ||
@@ -32,0 +38,0 @@ |
22
test.js
@@ -22,2 +22,12 @@ import t from './index.js' | ||
t.equalAny(3, [1, 2, 3]) | ||
t.equalAny(1, [1, 2, 3], 'equals any msg') | ||
t.oneOf(1, [1, 2, 3], 'one of') | ||
t.deepEqualAny(['b'], [['a'], ['b']]) | ||
t.almost(0.1, new Float32Array([0.1])[0]) | ||
t.almost([0.1], new Float32Array([0.1])) | ||
t.same([0, 1], [1, 0]) | ||
t.pass('ok') | ||
@@ -29,5 +39,13 @@ }) | ||
t.equal({}, {}); | ||
t.deepEqual([1,2,3], [4,5,6]); | ||
t.deepEqual([1,2,3], [4,5,6]) | ||
t.fail('nok') | ||
t.equalAny(1, [2, 3]) | ||
t.deepEqualAny(['a'], [['b'], ['c']]) | ||
t.almost(0.11, new Float32Array([0.1])[0]) | ||
t.almost([0.11], new Float32Array([0.1])) | ||
t.same([0, 1], [1, 0, 1]) | ||
t.fail('test failed') | ||
}) | ||
@@ -34,0 +52,0 @@ |
15350
353
71