/
hooks.coffee
62 lines (47 loc) · 1.66 KB
/
hooks.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# READ THIS! Disclaimer:
# Do not add any functinoality to this class unless you want expose it to the Hooks API
# This class is only an interface for users of Dredd hooks.
class Hooks
constructor: ->
@beforeHooks = {}
@afterHooks = {}
@transactions = {}
@beforeAllHooks = []
@afterAllHooks = []
@beforeEachHooks = []
@afterEachHooks = []
before: (name, hook) =>
@addHook(@beforeHooks, name, hook)
after: (name, hook) =>
@addHook(@afterHooks, name, hook)
beforeAll: (hook) =>
@beforeAllHooks.push hook
afterAll: (hook) =>
@afterAllHooks.push hook
beforeEach: (hook) =>
@beforeEachHooks.push hook
afterEach: (hook) =>
@afterEachHooks.push hook
addHook: (hooks, name, hook) ->
if hooks[name]
hooks[name].push hook
else
hooks[name] = [hook]
# This is not part of hooks API
# This is here only because it has to be injected into sandboxed context
dumpHooksFunctionsToStrings: () ->
# prepare JSON friendly object
toReturn = JSON.parse(JSON.stringify(@))
# don't fiddle with transactions, they are not part of sandboxed sync API
delete toReturn['transactions']
hookTargets = Object.keys toReturn
for hookTarget in hookTargets
if Array.isArray @[hookTarget]
for index, hookFunc of @[hookTarget]
toReturn[hookTarget][index] = hookFunc.toString()
else if typeof(@[hookTarget]) == 'object' and not Array.isArray(@[hookTarget])
for transactionName, funcArray of @[hookTarget]
for index, hookFunc of funcArray
toReturn[hookTarget][transactionName][index] = hookFunc.toString()
return toReturn
module.exports = Hooks