Browse Source

[assertions] Added assertions

0.7.0-breaking-rewrite
Muthu Kumar 6 years ago
parent
commit
2565c1247b
  1. 2
      package.json
  2. 4
      src/lib/assertPromise.js
  3. 51
      src/lib/assertionsLibrary.js

2
package.json

@ -1,6 +1,6 @@
{ {
"name": "@klenty/gunner", "name": "@klenty/gunner",
"version": "0.7.0", "version": "0.7.5",
"description": "Zero magic, fast test-runner and assertion framework. No magic globals.", "description": "Zero magic, fast test-runner and assertion framework. No magic globals.",
"main": "index.js", "main": "index.js",
"repository": { "repository": {

4
src/lib/assertPromise.js

@ -1,17 +1,19 @@
const Promise = require('bluebird');
const { isPromise } = require('../util'); const { isPromise } = require('../util');
const createRejectionStatement = (statement, ...args) => const createRejectionStatement = (statement, ...args) =>
Promise.reject(statement ? statement(...args) : ''); Promise.reject(statement ? statement(...args) : '');
const _assertPromise = (bool, statementTuple) => { const _assertPromise = (bool, statementTuple) => {
const [ statement, ...args ] = statementTuple; const [ statement, ...args ] = statementTuple;
if(isPromise(bool)) if(isPromise(bool))
return bool.catch(() => return bool.catch(() =>
createRejectionStatement(statement, ...args)); createRejectionStatement(statement, ...args));
return bool return bool
? Promise.resolve() ? Promise.resolve()
: createRejectionStatement(statement, ...args); : createRejectionStatement(statement, ...args);
}; };
module.exports = _assertPromise; module.exports = _assertPromise;

51
src/lib/assertionsLibrary.js

@ -2,14 +2,16 @@ const isEq = require('@codefeathers/iseq');
const U = require('../util'); const U = require('../util');
const _ = U.taggedStringify; const _ = U.taggedStringify;
module.exports.done = [ module.exports.done =
() => true, [
() => null, resolve => resolve,
]; () => null,
module.exports.fail = [ ];
() => false, module.exports.fail =
() => null, [
]; () => false,
(_, rejection) => rejection,
];
module.exports.exists = module.exports.exists =
[ [
val => typeof val !== 'undefined', val => typeof val !== 'undefined',
@ -74,11 +76,42 @@ module.exports.resolvesTo =
: Promise.reject(`${val} was not a Promise`), : Promise.reject(`${val} was not a Promise`),
(val, thing) => _`'${val}' does not resolve to '${thing}'`, (val, thing) => _`'${val}' does not resolve to '${thing}'`,
]; ];
module.exports.isType =
[
(val, type) => (type === 'nil'
&& (val === 'null' || val === 'undefined'))
|| (typeof val === type)
|| (Array.isArray(val) && type === "array")
&& (val === null && type !== 'object'),
(val, type) => _`'${val}' is not of type '${type}'`,
];
module.exports.greaterThan =
[
(val, compare) => val > compare,
(val, compare) => _`'${val}' is not greater than ${compare}`,
];
module.exports.lessThan =
[
(val, compare) => val < compare,
(val, compare) => _`'${val}' is not less than ${compare}`
];
module.exports.gte =
[
(val, compare) => val >= compare,
(val, compare) => _`'${val}' is less than ${compare}`
];
module.exports.lte =
[
(val, compare) => val <= compare,
(val, compare) => _`'${val}' is greater than ${compare}`,
];
/* Convenience methods */ /* Convenience aliases */
module.exports.success = module.exports.done; module.exports.success = module.exports.done;
module.exports.succeed = module.exports.done; module.exports.succeed = module.exports.done;
module.exports.failure = module.exports.fail; module.exports.failure = module.exports.fail;
module.exports.equal = module.exports.equals; module.exports.equal = module.exports.equals;
module.exports.deepEqual = module.exports.deepEquals; module.exports.deepEqual = module.exports.deepEquals;
module.exports.match = module.exports.deepEquals; module.exports.match = module.exports.deepEquals;
module.exports.greaterThanOrEqualTo = module.exports.gte;
module.exports.lessThanOrEqualTo = module.exports.lte;

Loading…
Cancel
Save