Skip to content

Commit

Permalink
testing: more meaningful messages
Browse files Browse the repository at this point in the history
  • Loading branch information
dmonad committed Feb 28, 2019
1 parent 7c15d44 commit 7044390
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
6 changes: 3 additions & 3 deletions prng.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const runGenTest = (tc, gen) => {
const average = count / tc.repititions
const expectedAverage = 100 / 2
t.info(`Average is: ${average}. Expected average is ${expectedAverage}.`)
t.assert(Math.abs(average - expectedAverage) <= 1, 'Expected average is at most 1 off.')
t.assert(Math.abs(average - expectedAverage) <= 2, 'Expected average is at most 1 off.')
})

t.group('int32 - generates integer with 32 bits', () => {
Expand Down Expand Up @@ -106,8 +106,8 @@ const runGenTest = (tc, gen) => {
* @param {t.TestCase} tc
*/
export const testGeneratorXoroshiro128plus = tc => runGenTest(tc, new Xoroshiro128plus(tc.seed))
// export const testGeneratorXorshift32 = tc => runGenTest(tc, new Xorshift32(tc.seed))
// export const testGeneratorMt19937 = tc => runGenTest(tc, new Mt19937(tc.seed))
export const testGeneratorXorshift32 = tc => runGenTest(tc, new Xorshift32(tc.seed))
export const testGeneratorMt19937 = tc => runGenTest(tc, new Mt19937(tc.seed))

/**
* @param {prng.PRNG} gen
Expand Down
14 changes: 10 additions & 4 deletions testing.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,12 @@ export const compareStrings = (a, b, m = 'Strings match') => {
*/
export const campareObjects = (a, b, m = 'Objects match') => object.equalFlat(a, b) || fail(m)

/**
* @param {any} a
* @param {any} b
* @param {string} path
* @throws {TestError}
*/
const compareValues = (a, b, path) => {
if (a !== b) {
fail(`Values ${a} and ${b} don't match (${path})`)
Expand All @@ -136,7 +142,7 @@ const compareValues = (a, b, path) => {

const _compare = (a, b, path, message) => {
if (a == null || b == null) {
compareValues(a, b)
compareValues(a, b, path)
}
if (a.constructor !== b.constructor) {
fail(`Constructors don't match ${path}`)
Expand All @@ -146,7 +152,7 @@ const _compare = (a, b, path, message) => {
if (object.length(a) !== object.length(b)) {
fail(`Objects have a different number of attributes ${path}`)
}
object.forEach(a, (value, key) => _compare(value, b[key], `${path}.${key}`, message))
object.forEach(a, (value, key) => _compare(value, b[key], `${path}["${key}"]`, message))
break
case Array:
if (a.length !== b.length) {
Expand All @@ -159,7 +165,7 @@ const _compare = (a, b, path, message) => {
}
}

export const compare = (a, b, message = '') => _compare(a, b, 'object', message)
export const compare = (a, b, message = 'Values match (deep comparison)') => _compare(a, b, 'obj', message)

export const assert = (condition, message = '') => condition
? log.print(log.GREEN, log.BOLD, '√ ', log.UNBOLD, message)
Expand Down Expand Up @@ -212,7 +218,7 @@ class TestError extends Error {}
* @throws {TestError}
*/
export const fail = reason => {
log.print(log.RED, log.BOLD, 'X', log.UNBOLD, reason)
log.print(log.RED, log.BOLD, 'X ', log.UNBOLD, reason)
throw new TestError('Test Failed')
}

Expand Down
2 changes: 1 addition & 1 deletion testing.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const testComparing = () => {

export const testFailing = () => {
t.fails(() => {
t.fail('m?')
t.fail('This fail is expected')
})
}

Expand Down

0 comments on commit 7044390

Please sign in to comment.