Skip to content

Commit

Permalink
add message info to compare functions
Browse files Browse the repository at this point in the history
  • Loading branch information
dmonad committed Feb 28, 2019
1 parent ac57a9b commit 7c15d44
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 13 deletions.
5 changes: 0 additions & 5 deletions logging.test.js
Original file line number Diff line number Diff line change
@@ -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 ')
Expand Down
6 changes: 6 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
40 changes: 32 additions & 8 deletions testing.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export class TestCase {
}

const perf = typeof performance === 'undefined'
// @ts-ignore
? require('perf_hooks').performance
: performance // eslint-disable-line no-undef

Expand Down Expand Up @@ -85,33 +86,47 @@ export const group = (description, f) => {
}
}

export const compareArrays = (as, bs) => {
/**
* @template T
* @param {Array<T>} as
* @param {Array<T>} 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)
}
}

/**
* @template K,V
* @param {Object<K,V>} a
* @param {Object<K,V>} 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) {
Expand Down Expand Up @@ -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..')
Expand Down

0 comments on commit 7c15d44

Please sign in to comment.