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

110 lines
1.9 KiB
CoffeeScript
Raw Normal View History

2010-12-29 14:06:57 -05:00
# Array Literals
# --------------
# * Array Literals
# * Splats in Array Literals
2010-12-30 22:48:31 -05:00
2011-01-03 04:17:00 -05:00
# TODO: add indexing and method invocation tests: [1][0] is 1, [].toString()
2010-12-30 22:48:31 -05:00
2011-03-11 21:55:26 -05:00
test "trailing commas", ->
trailingComma = [1, 2, 3,]
ok (trailingComma[0] is 1) and (trailingComma[2] is 3) and (trailingComma.length is 3)
2010-12-30 22:48:31 -05:00
2011-03-11 21:55:26 -05:00
trailingComma = [
1, 2, 3,
4, 5, 6
7, 8, 9,
]
(sum = (sum or 0) + n) for n in trailingComma
2010-12-30 22:48:31 -05:00
2011-03-11 21:55:26 -05:00
a = [((x) -> x), ((x) -> x * x)]
ok a.length is 2
2010-12-30 22:48:31 -05:00
2011-03-11 21:55:26 -05:00
test "incorrect indentation without commas", ->
result = [['a']
{b: 'c'}]
ok result[0][0] is 'a'
ok result[1]['b'] is 'c'
2010-12-30 22:48:31 -05:00
2011-03-11 21:41:12 -05:00
# Splats in Array Literals
2010-12-30 22:48:31 -05:00
test "array splat expansions with assignments", ->
nums = [1, 2, 3]
list = [a = 0, nums..., b = 4]
eq 0, a
eq 4, b
arrayEq [0,1,2,3,4], list
2011-03-11 22:44:18 -05:00
test "mixed shorthand objects in array lists", ->
arr = [
a:1
'b'
c:1
]
ok arr.length is 3
ok arr[2].c is 1
arr = [b: 1, a: 2, 100]
eq arr[1], 100
arr = [a:0, b:1, (1 + 1)]
eq arr[1], 2
arr = [a:1, 'a', b:1, 'b']
eq arr.length, 4
eq arr[2].b, 1
eq arr[3], 'b'
2011-03-12 08:41:58 -05:00
2011-02-17 21:56:28 -05:00
test "array splats with nested arrays", ->
2011-02-18 07:15:40 -05:00
nonce = {}
a = [nonce]
2011-02-17 21:56:28 -05:00
list = [1, 2, a...]
eq list[0], 1
2011-02-18 07:15:40 -05:00
eq list[2], nonce
2011-02-17 21:56:28 -05:00
2011-02-18 07:15:40 -05:00
a = [[nonce]]
2011-02-17 21:56:28 -05:00
list = [1, 2, a...]
2011-02-18 07:15:40 -05:00
arrayEq list, [1, 2, [nonce]]
2011-02-17 21:56:28 -05:00
2011-04-09 09:53:04 -04:00
test "#1274: `[] = a()` compiles to `false` instead of `a()`", ->
a = false
fn = -> a = true
[] = fn()
ok a
test "#3194: string interpolation in array", ->
arr = [ "a"
key: 'value'
]
eq 2, arr.length
eq 'a', arr[0]
eq 'value', arr[1].key
b = 'b'
arr = [ "a#{b}"
key: 'value'
]
eq 2, arr.length
eq 'ab', arr[0]
eq 'value', arr[1].key
test "regex interpolation in array", ->
arr = [ /a/
key: 'value'
]
eq 2, arr.length
eq 'a', arr[0].source
eq 'value', arr[1].key
b = 'b'
arr = [ ///a#{b}///
key: 'value'
]
eq 2, arr.length
eq 'ab', arr[0].source
eq 'value', arr[1].key