-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathjasmine-given.coffee
212 lines (171 loc) · 6.39 KB
/
jasmine-given.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
((jasmine) ->
mostRecentlyUsed = null
root = `(1, eval)('this')`
currentSpec = null
beforeEach ->
currentSpec = this
root.Given = ->
mostRecentlyUsed = root.Given
beforeEach getBlock(arguments)
whenList = []
root.When = ->
mostRecentlyUsed = root.When
b = getBlock(arguments)
beforeEach ->
whenList.push(b)
afterEach ->
whenList.pop()
invariantList = []
root.Invariant = ->
mostRecentlyUsed = root.Invariant
invariantBehavior = getBlock(arguments)
beforeEach ->
invariantList.push(invariantBehavior)
afterEach ->
invariantList.pop()
getBlock = (thing) ->
setupFunction = o(thing).firstThat (arg) -> o(arg).isFunction()
assignResultTo = o(thing).firstThat (arg) -> o(arg).isString()
doneWrapperFor setupFunction, (done) ->
context = currentSpec
result = setupFunction.call(context, done)
if assignResultTo
unless context[assignResultTo]
context[assignResultTo] = result
else
throw new Error("Unfortunately, the variable '#{assignResultTo}' is already assigned to: #{context[assignResultTo]}")
mostRecentExpectations = null
mostRecentStacks = null
declareJasmineSpec = (specArgs, itFunction = it) ->
label = o(specArgs).firstThat (arg) -> o(arg).isString()
expectationFunction = o(specArgs).firstThat (arg) -> o(arg).isFunction()
mostRecentlyUsed = root.subsequentThen
mostRecentExpectations = expectations = [expectationFunction]
mostRecentStacks = stacks = [errorWithRemovedLines("failed expectation", 3)]
itFunction "then #{label ? stringifyExpectation(expectations)}", (jasmineDone) ->
userCommands = [].concat(whenList, invariantList, wrapAsExpectations(expectations, stacks))
new Waterfall(userCommands, jasmineDone).flow()
return { Then: subsequentThen, And: subsequentThen }
wrapAsExpectations = (expectations, stacks) ->
for expectation, i in expectations
do (expectation, i) ->
doneWrapperFor expectation, (maybeDone) ->
expect(expectation).not.toHaveReturnedFalseFromThen(currentSpec, i + 1, stacks[i], maybeDone)
doneWrapperFor = (func, toWrap) ->
if func.length == 0
-> toWrap()
else
(done) -> toWrap(done)
root.Then = ->
declareJasmineSpec(arguments)
root.Then.only = ->
declareJasmineSpec(arguments, it.only)
root.subsequentThen = (additionalExpectation) ->
mostRecentExpectations.push additionalExpectation
mostRecentStacks.push(errorWithRemovedLines("failed expectation", 3))
this
errorWithRemovedLines = (msg, n) ->
if stack = new Error(msg).stack
[error, lines...] = stack.split("\n")
"#{error}\n#{lines.slice(n).join("\n")}"
mostRecentlyUsed = root.Given
root.And = ->
mostRecentlyUsed.apply this, jasmine.util.argsToArray(arguments)
o = (thing) ->
isFunction: ->
Object::toString.call(thing) is "[object Function]"
isString: ->
Object::toString.call(thing) is "[object String]"
firstThat: (test) ->
i = 0
while i < thing.length
return thing[i] if test(thing[i]) is true
i++
return undefined
class Waterfall
constructor: (functions = [], @finalCallback = ->) ->
@functions = cloneArray(functions)
flow: ->
return @finalCallback() if @functions.length == 0
func = @functions.shift()
if func.length > 0
func(=> @flow() )
else
func()
@flow()
cloneArray = (a) -> a.slice(0)
jasmine._given =
matchers:
toHaveReturnedFalseFromThen: (context, n, stackTrace, done) ->
result = false
exception = undefined
try
result = @actual.call(context, done)
catch e
exception = e
@message = ->
stringyExpectation = stringifyExpectation(@actual)
msg = "Then clause#{if n > 1 then " ##{n}" else ""} `#{stringyExpectation}` failed by "
if exception
msg += "throwing: " + exception.toString()
else
msg += "returning false"
msg += additionalInsightsForErrorMessage(stringyExpectation)
msg += "\n\n" + stackTrace if stackTrace?
msg
result == false
__Waterfall__: Waterfall
stringifyExpectation = (expectation) ->
matches = expectation.toString().replace(/\n/g,'').match(/function\s?\(.*\)\s?{\s*(return\s+)?(.*?)(;)?\s*}/i)
if matches and matches.length >= 3 then matches[2].replace(/\s+/g, ' ') else ""
additionalInsightsForErrorMessage = (expectationString) ->
expectation = finalStatementFrom(expectationString)
if comparison = wasComparison(expectation)
comparisonInsight(expectation, comparison)
else
""
finalStatementFrom = (expectationString) ->
if multiStatement = expectationString.match(/.*return (.*)/)
multiStatement[multiStatement.length - 1]
else
expectationString
wasComparison = (expectation) ->
if comparison = expectation.match(/(.*) (===|!==|==|!=|>|>=|<|<=) (.*)/)
[s, left, comparator, right] = comparison
{left, comparator, right}
comparisonInsight = (expectation, comparison) ->
left = evalInContextOfSpec(comparison.left)
right = evalInContextOfSpec(comparison.right)
return "" if apparentReferenceError(left) && apparentReferenceError(right)
msg = """
\n
This comparison was detected:
#{expectation}
#{left} #{comparison.comparator} #{right}
"""
msg += "\n\n#{deepEqualsNotice(comparison.left, comparison.right)}" if attemptedEquality(left, right, comparison.comparator)
msg
apparentReferenceError = (result) ->
/^<Error: "ReferenceError/.test(result)
evalInContextOfSpec = (operand) ->
try
(-> eval(operand)).call(currentSpec)
catch e
"<Error: \"#{e?.message?() || e}\">"
attemptedEquality = (left, right, comparator) ->
return false unless comparator is "==" || comparator is "==="
if jasmine.matchersUtil?.equals?
jasmine.matchersUtil.equals(left, right)
else
jasmine.getEnv().equals_(left, right)
deepEqualsNotice = (left, right) ->
"""
However, these items are deeply equal! Try an expectation like this instead:
expect(#{left}).toEqual(#{right})
"""
beforeEach ->
if jasmine.addMatchers?
jasmine.addMatchers(jasmine.matcherWrapper.wrap(jasmine._given.matchers))
else
@addMatchers(jasmine._given.matchers)
) jasmine