From 9e327eb13fc67336e094958f2048631610508e4d Mon Sep 17 00:00:00 2001 From: Muthu Kumar Date: Mon, 13 Aug 2018 23:19:37 +0530 Subject: [PATCH] [refactor] --- src/gunner.js | 21 ++++++++++++++++----- src/lib/assertPromise.js | 5 ++++- src/lib/expect.js | 6 +++++- src/lib/runTests.js | 11 +++++++++-- src/util/index.js | 27 ++++++++++++++------------- 5 files changed, 48 insertions(+), 22 deletions(-) diff --git a/src/gunner.js b/src/gunner.js index c0b8ace..b25ef15 100644 --- a/src/gunner.js +++ b/src/gunner.js @@ -80,15 +80,22 @@ class Gunner { } run (options = {}) { - const shouldLog = (hasProp(options)('log') && options.log) || !(hasProp(options)('log')); + const shouldLog = ( + (hasProp(options)('log') + && options.log) + || !(hasProp(options)('log')) + ); return _runTests(this) .then(results => { if (shouldLog) { const success = results.filter(r => r.result === 'pass'); results.passing = success.length; - const successPercent = Math.floor(success.length/results.length * 100); + const successPercent = Math.floor( + success.length/results.length * 100 + ); log( - chalk`\n{green ${success.length}} tests passed of ${results.length}`, + chalk`\n{green ${success.length}}`, + `tests passed of ${results.length}`, `[${successPercent}% success]\n` ); results.forEach(r => { @@ -96,9 +103,13 @@ class Gunner { ? `\n Traceback:\n ${stringify(r.error)}` : ''; - log(`${r.result === 'pass' ? chalk`{green ✅}` : chalk`{red ❌}`} ::`, + log( + `${r.result === 'pass' + ? chalk`{green ✅}` + : chalk`{red ❌}`} ::`, `${r.description}`, - `${trace}`); + `${trace}` + ); }); } diff --git a/src/lib/assertPromise.js b/src/lib/assertPromise.js index 6b5e0fe..c877879 100644 --- a/src/lib/assertPromise.js +++ b/src/lib/assertPromise.js @@ -1,5 +1,8 @@ +const { isPromise } = require('../util'); + const _assertPromise = (bool, assertion) => { - if(bool && typeof bool.then === 'function') return bool.catch(() => Promise.reject(assertion)); + if(isPromise(bool)) + return bool.catch(() => Promise.reject(assertion)); return bool ? Promise.resolve() : Promise.reject(assertion); }; diff --git a/src/lib/expect.js b/src/lib/expect.js index bc8afae..b13009d 100644 --- a/src/lib/expect.js +++ b/src/lib/expect.js @@ -46,9 +46,13 @@ const expect = thing => { (a, b, c) => a[b] === c, (a, b, c) => `Pair <${b}, ${c}> does not exist in ${stringify(a)}`, )(thing), + hasPairDeepEquals : expectPromise( + (a, b, c) => isEq(a[b], c), + (a, b, c) => `Pair <${b}, ${c}> does not exist in ${stringify(a)}`, + )(thing), resolvesTo : expectPromise( (a, b) => isPromise(a) - ? a.then(x => x === b ? Promise.resolve() : Promise.reject()) + ? a.then(x => isEq(x, b) ? Promise.resolve() : Promise.reject()) : Promise.reject(`${a} was not a Promise`), (a, b) => `${a} does not resolve to ${b}`, )(thing), diff --git a/src/lib/runTests.js b/src/lib/runTests.js index 1fb331f..524d75f 100644 --- a/src/lib/runTests.js +++ b/src/lib/runTests.js @@ -50,8 +50,15 @@ const runTests = instance => { return [ state, toTest - .then(() => ({ description: each.description, result: constants.pass })) - .catch(e => ({ description: each.description, result: constants.fail, error: e })), + .then(() => ({ + description: each.description, + result: constants.pass + })) + .catch(e => ({ + description: each.description, + result: constants.fail, + error: e + })), ]; }) diff --git a/src/util/index.js b/src/util/index.js index 9491444..fb395e7 100644 --- a/src/util/index.js +++ b/src/util/index.js @@ -3,11 +3,25 @@ const stringify = require('json-stringify-safe'); /* Returns true if a promise is passed */ const isPromise = prom => prom && (typeof prom.then === 'function'); +/* Lift a value or promise into a function */ +const liftPromise = (fn, thing) => + isPromise(thing) + ? thing.then(fn) + : fn(thing); + module.exports = { /* Returns true if a promise is passed */ isPromise, + /* Lift a value or promise into a function */ + liftPromise, + + /* Pipe a value or promise through any number of unary functions */ + pipe: (...fns) => + arg => fns.reduce((acc, fn) => + liftPromise(fn, acc), arg), + /* Flattens an array of arrays to an array */ flatten : arrData => [].concat.apply([], arrData), @@ -26,13 +40,6 @@ module.exports = { /* Resolves an array of Promises */ promiseAll : x => Promise.all(x), - /* Pipe a value or promise through any number of unary functions */ - pipe: (...fns) => - arg => fns.reduce((acc, fn) => - isPromise(acc) - ? acc.then(fn) - : fn(acc), arg), - /* Pass partial arguments and return a function that accepts the rest */ partial: (fn, ...args) => (...rest) => fn(...args, ...rest), @@ -44,12 +51,6 @@ module.exports = { x => path.match(new RegExp(`/${x}/?$`)) ), - /* Lift promises into a function */ - liftPromise : (fn, thing) => - isPromise(thing) - ? thing.then(fn) - : fn(thing), - /* Stringifies object or coerces to string */ stringify : obj => typeof obj === 'object'