From 88d7d9790a332fa7d30ec08471e1a45e04cb346a Mon Sep 17 00:00:00 2001 From: Muthu Kumar Date: Fri, 24 Aug 2018 12:17:54 +0530 Subject: [PATCH] [assertions] Added library --- src/lib/assertionsLibrary.js | 79 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 src/lib/assertionsLibrary.js diff --git a/src/lib/assertionsLibrary.js b/src/lib/assertionsLibrary.js new file mode 100644 index 0000000..a84a4ae --- /dev/null +++ b/src/lib/assertionsLibrary.js @@ -0,0 +1,79 @@ +const isEq = require('@codefeathers/iseq'); +const U = require('../util'); +const _ = U.taggedStringify; + +module.exports.done = [ + () => true, + () => null, +]; +module.exports.fail = [ + () => false, + () => null, +]; +module.exports.isArray = + [ + val => Array.isArray(val), + val => _`${(val)} is not an array`, + ]; +module.exports.hasLength = + [ + (val, l) => val.length === l, + (val, l) => _`${val} doesn't have length ${l}`, + ]; +module.exports.equals = + [ + (val, thing) => val === thing, + (val, thing) => _`${val} is not equal to ${thing}` + ]; +module.exports.deepEquals = + [ + (val, match) => isEq(val, match), + (val, match) => _`${val} is not deeply equal to ${match}`, + ]; +module.exports.contains = + [ + (val, match) => isEq(val, match, Object.keys(match)), + (val, match) => _`${val} does not contain ${match}`, + ]; +module.exports.isTrue = + [ + val => val === true, + val => _`${val} is not true` + ]; +module.exports.isFalse = + [ + val => val === false, + val => _`${val} is not false` + ]; +module.exports.hasProp = + [ + (val, prop) => val.hasOwnProperty(prop), + (val, prop) => _`${val} does not have property '${prop}'`, + ]; +module.exports.hasPair = + [ + (val, ...pair) => isEq(val[pair[0]], pair[1]), + (val, ...pair) => _`<${pair}> does not exist in ${val}`, + ]; +module.exports.isPromise = + [ + val => U.isPromise(val), + val => _`${val} is not a promise`, + { shouldCatch: true }, + ]; +module.exports.resolvesTo = + [ + (val, thing) => U.isPromise(val) + ? val.then(x => (x === thing) + ? Promise.resolve() : Promise.reject()) + : Promise.reject(`${val} was not a Promise`), + (val, thing) => _`'${val}' does not resolve to '${thing}'`, + ]; + +/* Convenience methods */ +module.exports.success = module.exports.done; +module.exports.succeed = module.exports.done; +module.exports.failure = module.exports.fail; +module.exports.equal = module.exports.equals; +module.exports.deepEqual = module.exports.deepEquals; +module.exports.match = module.exports.deepEquals;