1
0
Fork 0
mirror of https://github.com/jashkenas/coffeescript.git synced 2022-11-09 12:23:24 -05:00
jashkenas--coffeescript/test/test_splats.coffee

117 lines
2 KiB
CoffeeScript
Raw Normal View History

2010-07-25 01:23:37 -04:00
func = (first, second, rest...) ->
rest.join ' '
2009-12-31 17:50:12 -05:00
2010-07-25 01:23:37 -04:00
result = func 1, 2, 3, 4, 5
2009-12-31 17:50:12 -05:00
ok result is "3 4 5"
2010-07-25 01:23:37 -04:00
gold = silver = bronze = theField = last = null
2010-07-25 01:23:37 -04:00
medalists = (first, second, third, rest..., unlucky) ->
gold = first
silver = second
bronze = third
theField = rest.concat([last])
last = unlucky
2010-07-25 01:23:37 -04:00
contenders = [
"Michael Phelps"
"Liu Xiang"
"Yao Ming"
"Allyson Felix"
"Shawn Johnson"
"Roman Sebrle"
"Guo Jingjing"
"Tyson Gay"
"Asafa Powell"
"Usain Bolt"
]
medalists "Mighty Mouse", contenders...
ok gold is "Mighty Mouse"
ok silver is "Michael Phelps"
ok bronze is "Liu Xiang"
ok last is "Usain Bolt"
ok theField.length is 8
contenders.reverse()
medalists contenders[0...2]..., "Mighty Mouse", contenders[2...contenders.length]...
ok gold is "Usain Bolt"
ok silver is "Asafa Powell"
ok bronze is "Mighty Mouse"
ok last is "Michael Phelps"
ok theField.length is 8
medalists contenders..., 'Tim', 'Moe', 'Jim'
ok last is 'Jim'
obj =
name: 'moe'
accessor: (args...) ->
[@name].concat(args).join(' ')
getNames: ->
2010-07-25 01:23:37 -04:00
args = ['jane', 'ted']
@accessor(args...)
2010-09-27 01:32:26 -04:00
index: 0
0: {method: -> this is obj[0]}
ok obj.getNames() is 'moe jane ted'
2010-09-27 01:32:26 -04:00
ok obj[obj.index++].method([]...), 'should cache base value'
2010-07-25 01:23:37 -04:00
crowd = [
contenders...
"Mighty Mouse"
]
2010-07-25 01:23:37 -04:00
bests = [
"Mighty Mouse"
contenders[0..3]...
]
ok crowd[0] is contenders[0]
ok crowd[10] is "Mighty Mouse"
ok bests[1] is contenders[0]
ok bests[4] is contenders[3]
# Finally, splats with super() within classes.
class Parent
meth: (args...) ->
args
class Child extends Parent
meth: ->
2010-07-25 01:23:37 -04:00
nums = [3, 2, 1]
super nums...
ok (new Child).meth().join(' ') is '3 2 1'
# Functions with splats being called with too few arguments.
2010-07-25 01:23:37 -04:00
pen = null
method = (first, variable..., penultimate, ultimate) ->
pen = penultimate
method 1, 2, 3, 4, 5, 6, 7, 8, 9
ok pen is 8
method 1, 2, 3
ok pen is 2
method 1, 2
ok pen is 2
# Array splat expansions with assigns.
2010-07-25 01:23:37 -04:00
nums = [1, 2, 3]
list = [a = 0, nums..., b = 4]
ok a is 0
ok b is 4
ok list.join(' ') is '0 1 2 3 4'