Skip to content

Instantly share code, notes, and snippets.

@balupton
Last active December 12, 2015 04:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save balupton/4714965 to your computer and use it in GitHub Desktop.
Save balupton/4714965 to your computer and use it in GitHub Desktop.
@balupton's flow control and testing approach

@balupton's flow control and testing approach

Uses bal-util for flow and joe for testing. Feedback welcome. Inspired by this gist.

Pre-Requisites

npm install -g coffee-script
npm install bal-util joe

Example 1 returns:

$ coffee bal-util-flow-1.coffee
joe
joe ➞  moods tests
joe ➞  moods tests ➞  should do something useful with moods
joe ➞  moods tests ➞  should do something useful with moods ✔   
joe ➞  moods tests ✔  
joe ✔  

1/1 tests ran successfully, everything passed

Example 2 returns:

$ coffee bal-util-flow-2.coffee
joe
joe ➞  moods tests
joe ➞  moods tests ➞  should do something useful with moods
joe ➞  moods tests ➞  should do something useful with moods ✔   
joe ➞  moods tests ✔  
joe ✔  

1/1 tests ran successfully, everything passed
balUtil = require('bal-util')
joe = require('joe')
assert = require('assert')
# next is given (err,result)
store = (fixture,next) ->
return next(null,fixture) # example
# next is given (err,results)
load = (fixtures,next) ->
# Prepare
results = []
tasks = new balUtil.Group (err) ->
return next(err,results)
# Cycle
balUtil.each fixtures, (fixture) -> tasks.push (complete) ->
store fixture, (err,result) ->
return complete(err) if err
results.push(result)
return complete()
# Run
tasks.run('serial')
@
joe.describe "moods tests", (describe,it) ->
moods = [
"2013-02-01:n1k0:sunny"
"2013-02-02:n1k0:cloudy"
"2013-02-03:n1k0:stormy"
"2013-02-04:n1k0:rainy"
# … we could add many more
]
it "should do something useful with moods", (done) ->
load moods, (err,results) ->
return done(err) if err
assert.deepEqual(moods,results)
return done()
balUtil = require('bal-util')
joe = require('joe')
assert = require('assert')
# next is given (err,result)
store = (fixture,next) ->
return next(null,fixture) # example
# next is given (err,results)
load = (fixtures,next) ->
# Prepare
tasks = new balUtil.Group (err,lastTaskResult,taskResults) ->
results = (taskResult[1] for taskResult in taskResults)
return next(err,results)
# Cycle
balUtil.each fixtures, (fixture) -> tasks.push (complete) ->
store(fixture,complete) # complete is given (err,result)
# Run
tasks.run('serial')
@
joe.describe "moods tests", (describe,it) ->
moods = [
"2013-02-01:n1k0:sunny"
"2013-02-02:n1k0:cloudy"
"2013-02-03:n1k0:stormy"
"2013-02-04:n1k0:rainy"
# … we could add many more
]
it "should do something useful with moods", (done) ->
load moods, (err,results) ->
return done(err) if err
assert.deepEqual(moods,results)
return done()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment