Browse Source

[refactor]

0.7.0-breaking-rewrite
Muthu Kumar 6 years ago
parent
commit
9e327eb13f
  1. 21
      src/gunner.js
  2. 5
      src/lib/assertPromise.js
  3. 6
      src/lib/expect.js
  4. 11
      src/lib/runTests.js
  5. 27
      src/util/index.js

21
src/gunner.js

@ -80,15 +80,22 @@ class Gunner {
} }
run (options = {}) { 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) return _runTests(this)
.then(results => { .then(results => {
if (shouldLog) { if (shouldLog) {
const success = results.filter(r => r.result === 'pass'); const success = results.filter(r => r.result === 'pass');
results.passing = success.length; results.passing = success.length;
const successPercent = Math.floor(success.length/results.length * 100); const successPercent = Math.floor(
success.length/results.length * 100
);
log( log(
chalk`\n{green ${success.length}} tests passed of ${results.length}`, chalk`\n{green ${success.length}}`,
`tests passed of ${results.length}`,
`[${successPercent}% success]\n` `[${successPercent}% success]\n`
); );
results.forEach(r => { results.forEach(r => {
@ -96,9 +103,13 @@ class Gunner {
? `\n Traceback:\n ${stringify(r.error)}` ? `\n Traceback:\n ${stringify(r.error)}`
: ''; : '';
log(`${r.result === 'pass' ? chalk`{green ✅}` : chalk`{red ❌}`} ::`, log(
`${r.result === 'pass'
? chalk`{green ✅}`
: chalk`{red ❌}`} ::`,
`${r.description}`, `${r.description}`,
`${trace}`); `${trace}`
);
}); });
} }

5
src/lib/assertPromise.js

@ -1,5 +1,8 @@
const { isPromise } = require('../util');
const _assertPromise = (bool, assertion) => { 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); return bool ? Promise.resolve() : Promise.reject(assertion);
}; };

6
src/lib/expect.js

@ -46,9 +46,13 @@ const expect = thing => {
(a, b, c) => a[b] === c, (a, b, c) => a[b] === c,
(a, b, c) => `Pair <${b}, ${c}> does not exist in ${stringify(a)}`, (a, b, c) => `Pair <${b}, ${c}> does not exist in ${stringify(a)}`,
)(thing), )(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( resolvesTo : expectPromise(
(a, b) => isPromise(a) (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`), : Promise.reject(`${a} was not a Promise`),
(a, b) => `${a} does not resolve to ${b}`, (a, b) => `${a} does not resolve to ${b}`,
)(thing), )(thing),

11
src/lib/runTests.js

@ -50,8 +50,15 @@ const runTests = instance => {
return [ return [
state, state,
toTest toTest
.then(() => ({ description: each.description, result: constants.pass })) .then(() => ({
.catch(e => ({ description: each.description, result: constants.fail, error: e })), description: each.description,
result: constants.pass
}))
.catch(e => ({
description: each.description,
result: constants.fail,
error: e
})),
]; ];
}) })

27
src/util/index.js

@ -3,11 +3,25 @@ const stringify = require('json-stringify-safe');
/* Returns true if a promise is passed */ /* Returns true if a promise is passed */
const isPromise = prom => prom && (typeof prom.then === 'function'); 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 = { module.exports = {
/* Returns true if a promise is passed */ /* Returns true if a promise is passed */
isPromise, 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 */ /* Flattens an array of arrays to an array */
flatten : arrData => [].concat.apply([], arrData), flatten : arrData => [].concat.apply([], arrData),
@ -26,13 +40,6 @@ module.exports = {
/* Resolves an array of Promises */ /* Resolves an array of Promises */
promiseAll : x => Promise.all(x), 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 */ /* Pass partial arguments and return a function that accepts the rest */
partial: (fn, ...args) => (...rest) => fn(...args, ...rest), partial: (fn, ...args) => (...rest) => fn(...args, ...rest),
@ -44,12 +51,6 @@ module.exports = {
x => path.match(new RegExp(`/${x}/?$`)) 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 */ /* Stringifies object or coerces to string */
stringify : obj => stringify : obj =>
typeof obj === 'object' typeof obj === 'object'

Loading…
Cancel
Save