forked from jashkenas/coffeescript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_functions.coffee
More file actions
389 lines (269 loc) · 6.43 KB
/
Copy pathtest_functions.coffee
File metadata and controls
389 lines (269 loc) · 6.43 KB
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
x = 1
y = {}
y.x = -> 3
ok x is 1
ok typeof(y.x) is 'function'
ok y.x instanceof Function
ok y.x() is 3
# The empty function should not cause a syntax error.
->
() ->
# Multiple nested function declarations mixed with implicit calls should not
# cause a syntax error.
(one) -> (two) -> three four, (five) -> six seven, eight, (nine) ->
obj = {
name: 'Fred'
bound: ->
(=> eq this, obj)()
unbound: ->
(-> ok this isnt obj)()
nested: ->
(=>
(=>
(=>
eq this, obj
)()
)()
)()
}
obj.unbound()
obj.bound()
obj.nested()
# Python decorator style wrapper that memoizes any function
memoize = (fn) ->
cache = {}
self = this
(args...) ->
key = args.toString()
return cache[key] if cache[key]
cache[key] = fn.apply(self, args)
Math = {
Add: (a, b) -> a + b
AnonymousAdd: (a, b) -> a + b
FastAdd: memoize (a, b) -> a + b
}
ok Math.Add(5, 5) is 10
ok Math.AnonymousAdd(10, 10) is 20
ok Math.FastAdd(20, 20) is 40
okFunc = (f) -> ok(f())
okFunc -> true
# Optional parens can be used in a nested fashion.
call = (func) -> func()
result = call ->
inner = call ->
Math.Add(5, 5)
ok result is 10
# More fun with optional parens.
fn = (arg) -> arg
ok fn(fn {prop: 101}).prop is 101
# Multi-blocks with optional parens.
result = fn( ->
fn ->
"Wrapped"
)
ok result()() is 'Wrapped'
# And even with strange things like this:
funcs = [((x) -> x), ((x) -> x * x)]
result = funcs[1] 5
ok result is 25
result = ("hello".slice) 3
ok result is 'lo'
# And with multiple single-line functions on the same line.
func = (x) -> (x) -> (x) -> x
ok func(1)(2)(3) is 3
# Ensure that functions with the same name don't clash with helper functions.
del = -> 5
ok del() is 5
# Ensure that functions can have a trailing comma in their argument list
mult = (x, mids..., y) ->
x *= n for n in mids
x *= y
ok mult(1, 2,) is 2
ok mult(1, 2, 3,) is 6
ok mult(10, (i for i in [1..6])...) is 7200
# Test for inline functions with parentheses and implicit calls.
combine = (func, num) -> func() * num
result = combine (-> 1 + 2), 3
ok result is 9
# Test for calls/parens/multiline-chains.
f = (x) -> x
result = (f 1).toString()
.length
ok result is 1
# Test implicit calls in functions in parens:
result = ((val) ->
[].push val
val
)(10)
ok result is 10
# More paren compilation tests:
reverse = (obj) -> obj.reverse()
ok reverse([1, 2].concat 3).join(' ') is '3 2 1'
# Passing multiple functions without paren-wrapping is legal, and should compile.
sum = (one, two) -> one() + two()
result = sum ->
7 + 9
, ->
1 + 3
ok result is 20
# Implicit call with a trailing if statement as a param.
func = -> arguments[1]
result = func 'one', if false then 100 else 13
ok result is 13
# Test more function passing:
result = sum( ->
1 + 2
, ->
2 + 1
)
ok result is 6
sum = (a, b) -> a + b
result = sum(1
, 2)
ok result is 3
# This is a crazy one.
x = (obj, func) -> func obj
ident = (x) -> x
result = x {one: ident 1}, (obj) ->
inner = ident(obj)
ident inner
ok result.one is 1
# Assignment to a Object.prototype-named variable should not leak to outer scope.
# FIXME: fails on IE
(->
constructor = 'word'
)()
ok constructor isnt 'word'
# Trying an implicit object call with a trailing function.
a = null
meth = (arg, obj, func) -> a = [obj.a, arg, func()].join ' '
meth 'apple', b: 1, a: 13, ->
'orange'
ok a is '13 apple orange'
# Ensure that empty functions don't return mistaken values.
obj =
func: (@param, @rest...) ->
ok obj.func(101, 102, 103, 104) is undefined
ok obj.param is 101
ok obj.rest.join(' ') is '102 103 104'
# `@` and `this` should both be able to invoke a method.
func = (arg) -> ok arg is true
func.withAt = -> @ true
func.withThis = -> this true
func.withAt()
func.withThis()
# Ensure that constructors invoked with splats return a new object.
args = [1, 2, 3]
Type = (@args) ->
type = new Type args
ok type and type instanceof Type
ok type.args and type.args instanceof Array
ok v is args[i] for v, i in type.args
Type1 = (@a, @b, @c) ->
type1 = new Type1 args...
ok type1 instanceof Type1
eq type1.constructor, Type1
ok type1.a is args[0] and type1.b is args[1] and type1.c is args[2]
# Ensure that constructors invoked with splats cache the function.
called = 0
get = -> if called++ then false else class Type
new get() args...
# Chained blocks, with proper indentation levels:
counter =
results: []
tick: (func) ->
@results.push func()
this
counter
.tick ->
3
.tick ->
2
.tick ->
1
eq counter.results.join(' '), '3 2 1'
# Make incorrect indentation safe.
func = ->
obj = {
key: 10
}
obj.key - 5
eq func(), 5
# Ensure that chained calls with indented implicit object literals below are
# alright.
result = null
obj =
method: (val) -> this
second: (hash) -> result = hash.three
obj
.method(
101
).second(
one:
two: 2
three: 3
)
eq result, 3
# Test newline-supressed call chains with nested functions.
obj =
call: -> this
func = ->
obj
.call ->
one two
.call ->
three four
101
eq func(), 101
# `new` shouldn't add extra parens
ok new Date().constructor is Date
# `new` works against bare function
eq Date, new ->
eq this, new => this
Date
# Implicit objects with number arguments.
func = (x, y) -> y
obj =
prop: func "a", 1
ok obj.prop is 1
# Non-spaced unary and binary operators should cause a function call.
func = (val) -> val + 1
ok (func +5) is 6
ok (func -5) is -4
# Prefix unary assignment operators are allowed in parenless calls.
val = 5
ok (func --val) is 5
eq ok, new ->
ok
### Should `return` implicitly ###
### even with trailing comments. ###
#855: execution context for `func arr...` should be `null`
(->
global = @
contextTest = -> ok global is @
array = []
contextTest array
contextTest.apply null, array
contextTest array...
)()
# #894: Splatting against constructor-chained functions.
x = null
class Foo
bar: (y) -> x = y
new Foo().bar([101]...)
eq x, 101
# Issue #904: Destructuring function arguments with same-named variables in scope.
key for key, value of a: 1
f = ([key, value]) ->
key + value
eq f([10, 11]), 21
eq key, 'a'
eq value, 1
# Bare single line functions are closed by commas.
fn = (count, fns...) -> eq count, fns.length
fn 2, ->, ->
fn 3, ->, ->, ->
fn 2, -> value, -> value
eq 2, [-> value, -> value].length
numbers = one: -> 1, two: -> 2
funcs = one: -> [-> 1, three: 3], two: -> 2