Skip to content

Commit

Permalink
URL Wildcards for hook matching cybertk#67.
Browse files Browse the repository at this point in the history
  • Loading branch information
suelje committed Mar 2, 2016
1 parent 1b6a171 commit 0751521
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
18 changes: 12 additions & 6 deletions lib/hooks.coffee
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
async = require 'async'
_ = require 'underscore'

class Hooks
constructor: () ->
Expand Down Expand Up @@ -49,23 +48,30 @@ class Hooks
callback(err)

runBefore: (test, callback) =>
return callback() unless (@beforeHooks[test.name] or @beforeEachHooks)
beforeHook = @getMatchingHook @beforeHooks, test.name
return callback() unless (beforeHook or @beforeEachHooks)

hooks = @beforeEachHooks.concat(@beforeHooks[test.name] ? [])
hooks = @beforeEachHooks.concat(beforeHook ? [])
async.eachSeries hooks, (hook, callback) ->
hook test, callback
, callback

runAfter: (test, callback) =>
return callback() unless (@afterHooks[test.name] or @afterEachHooks)
afterHook = @getMatchingHook @afterHooks, test.name
return callback() unless (afterHook or @afterEachHooks)

hooks = (@afterHooks[test.name] ? []).concat(@afterEachHooks)
hooks = (afterHook ? []).concat(@afterEachHooks)
async.eachSeries hooks, (hook, callback) ->
hook test, callback
, callback

hasName: (name) =>
_.has(@beforeHooks, name) || _.has(@afterHooks, name)
(@getMatchingHook @beforeHooks, name) || (@getMatchingHook @afterHooks, name)

getMatchingHook: (hooks, name) =>
for key,value of hooks
if name.match key
return value


module.exports = new Hooks()
Expand Down
8 changes: 8 additions & 0 deletions test/unit/hooks-test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,14 @@ describe 'Hooks', () ->

hooks.beforeHooks = {}

it 'should return true if test name matches regular expression hook', ->
hooks.beforeHooks =
'GET /.* -> [200|404]': (test, done) ->
done()

assert.ok hooks.hasName 'GET /users -> 200'
hooks.beforeHooks = {}

it 'should return true if in after hooks', ->
hooks.afterHooks =
foo: (test, done) ->
Expand Down

0 comments on commit 0751521

Please sign in to comment.