|
|
@ -1,55 +1,19 @@ |
|
|
|
const path = obj => path => |
|
|
|
path.reduce((result, segment) => result && result[segment], obj); |
|
|
|
|
|
|
|
const pickSelector = path => { |
|
|
|
const match = path.match(/^(res|unit)(::|$)/)[0]; |
|
|
|
if (match === 'res::') return 0; |
|
|
|
if (match === 'unit::') return 1; |
|
|
|
return -1; |
|
|
|
}; |
|
|
|
const Shotgun = require('shotgun-query'); |
|
|
|
|
|
|
|
const parsePath = obj => pathString => { |
|
|
|
module.exports = (expectable, expect, docs) => { |
|
|
|
const unitExpects = []; |
|
|
|
|
|
|
|
const paths = []; |
|
|
|
if (expectable.checks) { |
|
|
|
|
|
|
|
const normalised = pathString |
|
|
|
.trim() |
|
|
|
.replace(/^(res|unit)::/, '') |
|
|
|
.split('::'); |
|
|
|
unitExpects.push(expect(expectable.checks).isArray()); |
|
|
|
|
|
|
|
if (normalised[normalised.length - 1] === '&*') { |
|
|
|
const push = path(obj)(normalised.slice(0, normalised.length - 1)); |
|
|
|
if (Array.isArray(push)) Array.prototype.push.apply(paths, push); |
|
|
|
else return Promise.reject( |
|
|
|
`${push} is not an array. Cannot run '&*' operator!`); |
|
|
|
} else { |
|
|
|
paths.push(path(obj)(normalised)); |
|
|
|
} |
|
|
|
expectable.checks.forEach(check => |
|
|
|
|
|
|
|
return paths; |
|
|
|
unitExpects.push( |
|
|
|
new Shotgun(check).eval(docs).call(expect)) |
|
|
|
|
|
|
|
}; |
|
|
|
); |
|
|
|
|
|
|
|
const obj = { |
|
|
|
doc: { |
|
|
|
a: |
|
|
|
{ b: [5, 10] } |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
|
module.exports = (expectable, expect, docs) => { |
|
|
|
const unitExpects = []; |
|
|
|
|
|
|
|
if (expectable.checks) { |
|
|
|
unitExpects.push(expect(expectable.checks).isArray()); |
|
|
|
expectable.checks.forEach(check => { |
|
|
|
const [ pathToCheck, method, ...args ] = check; |
|
|
|
const selector = pickSelector(pathToCheck); |
|
|
|
const toChecks = parsePath(docs[selector])(pathToCheck); |
|
|
|
toChecks.forEach(toCheck => |
|
|
|
unitExpects.push( |
|
|
|
expect(toCheck)[method](...args))); |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
return Promise.all(unitExpects); |
|
|
|