From 7c15d44d2a52c4b92c1e9f7711b19ec1d9a9ce43 Mon Sep 17 00:00:00 2001 From: Kevin Jahns Date: Thu, 28 Feb 2019 13:36:36 +0100 Subject: [PATCH] add message info to compare functions --- logging.test.js | 5 ----- test.js | 6 ++++++ testing.js | 40 ++++++++++++++++++++++++++++++++-------- 3 files changed, 38 insertions(+), 13 deletions(-) diff --git a/logging.test.js b/logging.test.js index ffdcdba..e62e0cb 100644 --- a/logging.test.js +++ b/logging.test.js @@ -1,10 +1,5 @@ import * as log from './logging.js' -import { isBrowser } from './environment.js' - -if (isBrowser) { - log.createVConsole(document.body) -} export const testLogging = () => { log.print(log.BLUE, 'blue ') diff --git a/test.js b/test.js index 745249c..8d34242 100644 --- a/test.js +++ b/test.js @@ -7,5 +7,11 @@ import * as diff from './diff.test.js' import * as testing from './testing.test.js' import * as indexeddb from './indexeddb.test.js' import * as prng from './prng.test.js' +import * as log from './logging.js' +import { isBrowser } from './environment.js' + +if (isBrowser) { + log.createVConsole(document.body) +} runTests({ logging, string, encoding, diff, testing, indexeddb, prng }) diff --git a/testing.js b/testing.js index b92241e..229739c 100644 --- a/testing.js +++ b/testing.js @@ -28,6 +28,7 @@ export class TestCase { } const perf = typeof performance === 'undefined' + // @ts-ignore ? require('perf_hooks').performance : performance // eslint-disable-line no-undef @@ -85,23 +86,36 @@ export const group = (description, f) => { } } -export const compareArrays = (as, bs) => { +/** + * @template T + * @param {Array} as + * @param {Array} bs + * @param {string} [m] + * @return {boolean} + */ +export const compareArrays = (as, bs, m = 'Arrays match') => { if (as.length !== bs.length) { - return false + fail(m) } for (let i = 0; i < as.length; i++) { if (as[i] !== bs[i]) { - return false + fail(m) } } return true } -export const compareStrings = (a, b) => { +/** + * @param {string} a + * @param {string} b + * @param {string} [m] + * @throws {TestError} Throws if tests fails + */ +export const compareStrings = (a, b, m = 'Strings match') => { if (a !== b) { const diff = simpleDiff(a, b) log.print(log.GREY, a.slice(0, diff.pos), log.RED, a.slice(diff.pos, diff.remove), log.GREEN, diff.insert, log.GREY, a.slice(diff.pos + diff.remove)) - fail('Strings don\'t match') + fail(m) } } @@ -109,9 +123,10 @@ export const compareStrings = (a, b) => { * @template K,V * @param {Object} a * @param {Object} b - * @return {boolean} + * @param {string} [m] + * @throws {TestError} Throws if test fails */ -export const campareObjects = (a, b) => object.equalFlat(a, b) || fail('Objects don\'t match') +export const campareObjects = (a, b, m = 'Objects match') => object.equalFlat(a, b) || fail(m) const compareValues = (a, b, path) => { if (a !== b) { @@ -192,12 +207,21 @@ export const runTests = async tests => { class TestError extends Error {} +/** + * @param {string} reason + * @throws {TestError} + */ export const fail = reason => { - throw new TestError(reason) + log.print(log.RED, log.BOLD, 'X', log.UNBOLD, reason) + throw new TestError('Test Failed') } class SkipError extends Error {} +/** + * @param {boolean} cond If true, this tests will be skipped + * @throws {SkipError} + */ export const skip = (cond = true) => { if (cond) { throw new SkipError('skipping..')