Skip to content

Commit

Permalink
(testing) support comparison of Map and Set
Browse files Browse the repository at this point in the history
  • Loading branch information
dmonad committed Mar 11, 2019
1 parent 406ccda commit 169b62a
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"dist": "rollup -c",
"debug": "live-server --port=3443 --entry-file=test.html",
"test": "npm run dist && npm run lint && nyc --check-coverage --lines 100 --branches 100 --functions 100 --statements 100 node ./dist/test.js --repitition-time 50",
"test-extensive": "npm run test -- --repitition-time 30000",
"test-extensive": "npm test -- --repitition-time 30000",
"test-code-coverage": "npm run dist && nyc --reporter html node ./dist/test.js",
"trace-deopt": "clear && rollup -c && node --trace-deopt dist/test.js",
"trace-opt": "clear && rollup -c && node --trace-opt dist/test.js",
Expand Down
30 changes: 26 additions & 4 deletions testing.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ const _failMessage = (message, reason, path) => fail(
)

const _compare = (a, b, path, message, customCompare) => {
// we don't use assert here because we want to test all branches (istanbul errors if one branch is not tested)
if (a == null || b == null) {
return compareValues(null, a, b, path)
}
Expand All @@ -231,6 +232,29 @@ const _compare = (a, b, path, message, customCompare) => {
}
break
}
case Set: {
if (a.size !== b.size) {
_failMessage(message, 'Sets have different number of attributes', path)
}
a.forEach(value => {
if (!b.has(value)) {
_failMessage(message, `b.${path} does have ${value}`, path)
}
})
break
}
case Map: {
if (a.size !== b.size) {
_failMessage(message, 'Maps have different number of attributes', path)
}
a.forEach((value, key) => {
if (!b.has(key)) {
_failMessage(message, `Property ${path}["${key}"] does not exist on second argument`, path)
}
_compare(value, b.get(key), `${path}["${key}"]`, message, customCompare)
})
break
}
case Object:
if (object.length(a) !== object.length(b)) {
_failMessage(message, 'Objects have a different number of attributes', path)
Expand All @@ -239,7 +263,7 @@ const _compare = (a, b, path, message, customCompare) => {
if (!b.hasOwnProperty(key)) {
_failMessage(message, `Property ${path} does not exist on second argument`, path)
}
b.hasOwnProperty(key) && _compare(value, b[key], `${path}["${key}"]`, message, customCompare)
_compare(value, b[key], `${path}["${key}"]`, message, customCompare)
})
break
case Array:
Expand All @@ -261,9 +285,7 @@ const _compare = (a, b, path, message, customCompare) => {
export const compare = (a, b, message = null, customCompare = compareValues) => _compare(a, b, 'obj', message, customCompare)

/* istanbul ignore next */
export const assert = (condition, message = null) => condition
? (message !== null && log.print(log.GREEN, log.BOLD, '√ ', log.UNBOLD, message))
: fail(message ? `Failed condition: ${message}` : 'Assertion failed')
export const assert = (condition, message = null) => condition || fail(`Assertion failed${message !== null ? `: ${message}` : ''}`)

export const fails = f => {
let err = null
Expand Down
39 changes: 39 additions & 0 deletions testing.test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
import * as t from './testing.js'
import * as math from './math.js'
import * as buffer from './buffer.js'
import * as map from './map.js'

export const testComparing = () => {
t.compare({}, {})
t.compare({ a: 4 }, { a: 4 }, 'simple compare (object)')
t.compare([1, 2], [1, 2], 'simple compare (array)')
t.compare({ a: [1, 2] }, { a: [1, 2] }, 'simple compare nested')
t.compare(new Set(['3', 1234]), new Set(['3', 1234]), 'compare Sets')
const map1 = map.create()
map1.set(1, 2)
map1.set('x', {})
map1.set(98, 'tst')
const map2 = new Map()
map2.set(1, 2)
map2.set('x', {})
map2.set(98, 'tst')
t.compare(map1, map2, 'compare Maps')

t.describe('The following errors are expected!')
t.fails(() => {
Expand Down Expand Up @@ -53,6 +64,34 @@ export const testComparing = () => {
t.fails(() => {
t.compareObjects({ x: undefined }, { y: 1 }, 'compare correctly handles undefined')
})
t.describe('Map fails')
t.fails(() => {
const m1 = new Map()
m1.set(1, 2)
const m2 = new Map()
m2.set(1, 3)
t.compare(m1, m2) // childs have different length (array) -- no message
})
t.fails(() => {
const m1 = new Map()
m1.set(2, 2)
const m2 = new Map()
m2.set(1, 2)
t.compare(m1, m2) // childs have different length (array) -- no message
})
t.fails(() => {
const m1 = new Map()
m1.set(1, 2)
const m2 = new Map()
t.compare(m1, m2) // childs have different length (array) -- no message
})
t.describe('Set fails')
t.fails(() => {
t.compare(new Set([1]), new Set([1, 2])) // childs have different length (array) -- no message
})
t.fails(() => {
t.compare(new Set([1]), new Set([2])) // childs have different length (array) -- no message
})
}

export const testFailing = () => {
Expand Down

0 comments on commit 169b62a

Please sign in to comment.