thunk-test
Advanced tools
Comparing version 0.5.1 to 0.6.0
{ | ||
"name": "thunk-test", | ||
"version": "0.5.1", | ||
"version": "0.6.0", | ||
"description": "Modular testing for JavaScript", | ||
@@ -5,0 +5,0 @@ "author": "Richard Tong", |
12
test.js
@@ -11,2 +11,14 @@ const ThunkTest = require('./thunk-test') | ||
ThunkTest('adds two values', add) | ||
.after(function () { | ||
assert.strictEqual(this.hello, 'world') | ||
console.log('I should be at the end') | ||
}) | ||
.before(function () { | ||
this.hello = 'world' | ||
console.log('I should be at the beginning') | ||
}) | ||
.before(function () { | ||
assert.strictEqual(this.hello, 'world') | ||
console.log('I should be second at the beginning') | ||
}) | ||
.case(5, 5, 10) // assert.strictEqual(add(5, 5), 10) | ||
@@ -13,0 +25,0 @@ .case('abcde', 'fg', result => { // supply your own callback |
@@ -11,2 +11,4 @@ const noop = function () {} | ||
const callPropUnary = (value, property, arg0) => value[property](arg0) | ||
const tapSync = func => function tapping(...args) { | ||
@@ -38,2 +40,6 @@ func(...args) | ||
const thunkify3 = (func, arg0, arg1, arg2) => function thunk() { | ||
return func(arg0, arg1, arg2) | ||
} | ||
const __ = Symbol.for('placeholder') | ||
@@ -428,2 +434,47 @@ | ||
/** | ||
* @name thunkTestExecAsync | ||
* | ||
* @synopsis | ||
* ```coffeescript [specscript] | ||
* Thunk = ()=>any | ||
* | ||
* thunkTestExecAsync(operations Array<Thunk>, operationsIndex number) -> Promise<void> | ||
* ``` | ||
*/ | ||
const thunkTestExecAsync = async function ( | ||
operations, operationsIndex, | ||
) { | ||
const operationsLength = operations.length | ||
while (++operationsIndex < operationsLength) { | ||
const execution = operations[operationsIndex]() | ||
if (isPromise(execution)) { | ||
await execution | ||
} | ||
} | ||
} | ||
/** | ||
* @name thunkTestExec | ||
* | ||
* @synopsis | ||
* ```coffeescript [specscript] | ||
* Thunk = ()=>any | ||
* | ||
* thunkTestExecAsync(operations Array<Thunk>, operationsIndex number) -> ()|Promise<void> | ||
* ``` | ||
*/ | ||
const thunkTestExec = function (operations) { | ||
const operationsLength = operations.length | ||
let operationsIndex = -1 | ||
while (++operationsIndex < operationsLength) { | ||
const execution = operations[operationsIndex]() | ||
if (isPromise(execution)) { | ||
return execution.then(thunkify2( | ||
thunkTestExecAsync, operations, operationsIndex)) | ||
} | ||
} | ||
} | ||
/** | ||
* @name ThunkTest | ||
@@ -448,10 +499,35 @@ * | ||
*/ | ||
const ThunkTest = function (name, ...funcs) { | ||
const operations = [] | ||
const operations = [], | ||
preprocessing = [], | ||
postprocessing = [] | ||
return objectAssign(function thunkTest() { | ||
log('--', name) | ||
let cursor = null | ||
cursor = thunkTestExec(preprocessing) | ||
if (isPromise(cursor)) { | ||
return cursor.then(funcConcat( | ||
thunkify1(thunkTestExec, operations), | ||
thunkify1(thunkTestExec, postprocessing), | ||
)) | ||
} | ||
cursor = thunkTestExec(operations) | ||
if (isPromise(cursor)) { | ||
return cursor.then(thunkify1(thunkTestExec, postprocessing)) | ||
} | ||
cursor = thunkTestExec(postprocessing) | ||
if (isPromise(cursor)) { | ||
return cursor.then(noop) | ||
} | ||
/* | ||
log('--', name) | ||
const operationsLength = operations.length, | ||
preprocessingLength = preprocessing.length, | ||
postprocessingLength = postprocessing.length | ||
promises = [] | ||
let operationsIndex = -1 | ||
let operationsIndex = -1, | ||
preprocessingIndex = -1, | ||
postProcessingIndex = -1 | ||
while (++preprocessingIndex < ) | ||
while (++operationsIndex < operationsLength) { | ||
@@ -464,4 +540,21 @@ const execution = operations[operationsIndex]() | ||
return promises.length == 0 ? this : promiseAll(promises).then(always(this)) | ||
*/ | ||
}, { | ||
before(callback) { | ||
preprocessing.push(funcConcat( | ||
thunkify3(callPropUnary, callback, 'call', this), | ||
tapSync(thunkify1(log, `-- ${funcInspect(callback)}`)), | ||
)) | ||
return this | ||
}, | ||
after(callback) { | ||
postprocessing.push(funcConcat( | ||
thunkify3(callPropUnary, callback, 'call', this), | ||
tapSync(thunkify1(log, `-- ${funcInspect(callback)}`)), | ||
)) | ||
return this | ||
}, | ||
case(...args) { | ||
@@ -468,0 +561,0 @@ const expected = args.pop() |
21176
633