forked from EndangeredMassa/bond
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bond.coffee
136 lines (99 loc) · 2.85 KB
/
bond.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
isFunction = (obj) ->
typeof obj == 'function'
createThroughSpy = (getValue, bondApi) ->
spy = ->
args = Array::slice.call(arguments)
spy.calledArgs[spy.called] = args
spy.called++
result = getValue.apply(this, args)
isConstructor = (this instanceof arguments.callee)
return this if isConstructor and typeof result != 'object'
result
enhanceSpy(spy, getValue, bondApi)
createReturnSpy = (getValue, bondApi) ->
spy = ->
args = Array::slice.call(arguments)
spy.calledArgs[spy.called] = args
spy.called++
getValue.apply(this, args)
enhanceSpy(spy, getValue, bondApi)
createAnonymousSpy = ->
returnValue = null
spy = ->
args = Array::slice.call(arguments)
spy.calledArgs[spy.called] = args
spy.called++
returnValue
enhanceSpy(spy)
spy.return = (newReturnValue) ->
returnValue = newReturnValue
spy
spy
enhanceSpy = (spy, original, bondApi) ->
spy.prototype = original?.prototype
spy.called = 0
spy.calledArgs = []
spy.calledWith = (args...) ->
return false if !spy.called
lastArgs = spy.calledArgs[spy.called-1]
arrayEqual(args, lastArgs)
spy[k] = v for k, v of bondApi if bondApi
spy
arrayEqual = (A, B) ->
for a, i in A
b = B[i]
return false if a != b
true
nextTick = do ->
return process.nextTick if isFunction(process?.nextTick)
return setImmediate if setImmediate? && isFunction(setImmediate)
(fn) ->
setTimeout(fn, 0)
allStubs = []
registered = false
registerCleanupHook = ->
return if registered
after = afterEach ? testDone ? QUnit?.testDone ? bond.cleanup ? ->
throw new Error('bond.cleanup must be specified if your test runner does not use afterEach or testDone')
after ->
for stubRestore in allStubs
stubRestore()
allStubs = []
registered = true
bond = (obj, property) ->
registerCleanupHook()
return createAnonymousSpy() if arguments.length == 0
previous = obj[property]
registerRestore = ->
allStubs.push restore
restore = ->
obj[property] = previous
to = (newValue) ->
registerRestore()
if isFunction(newValue)
newValue = createThroughSpy(newValue, this)
obj[property] = newValue
obj[property]
returnMethod = (returnValue) ->
registerRestore()
returnValueFn = -> returnValue
obj[property] = createReturnSpy(returnValueFn, this)
obj[property]
asyncReturn = (returnValues...) ->
to (args..., callback) ->
if !isFunction(callback)
throw new Error('asyncReturn expects last argument to be a function')
nextTick ->
callback(returnValues...)
through = ->
obj[property] = createThroughSpy(previous, this)
obj[property]
{
'to': to
'return': returnMethod
'asyncReturn': asyncReturn
'through': through
'restore': restore
}
window.bond = bond if window?
module.exports = bond if module?.exports