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
|
# Array Literals
# --------------
# * Array Literals
# * Splats in Array Literals
# TODO: add indexing and method invocation tests: [1][0] is 1, [].toString()
test "trailing commas", ->
trailingComma = [1, 2, 3,]
ok (trailingComma[0] is 1) and (trailingComma[2] is 3) and (trailingComma.length is 3)
trailingComma = [
1, 2, 3,
4, 5, 6
7, 8, 9,
]
(sum = (sum or 0) + n) for n in trailingComma
a = [((x) -> x), ((x) -> x * x)]
ok a.length is 2
test "incorrect indentation without commas", ->
result = [['a']
{b: 'c'}]
ok result[0][0] is 'a'
ok result[1]['b'] is 'c'
# Elisions
test "array elisions", ->
eq [,1].length, 2
eq [,,1,2,,].length, 5
arr = [1,,2]
eq arr.length, 3
eq arr[1], undefined
eq [,,].length, 2
test "array elisions indentation and commas", ->
arr1 = [
, 1, 2, , , 3,
4, 5, 6
, , 8, 9,
]
eq arr1.length, 12
eq arr1[5], 3
eq arr1[9], undefined
arr2 = [, , 1,
2, , 3,
, 4, 5
6
, , ,
]
eq arr2.length, 12
eq arr2[8], 5
eq arr2[1], undefined
test "array elisions destructuring", ->
arr = [1,2,3,4,5,6,7,8,9]
[,a] = arr
[,,,b] = arr
arrayEq [a,b], [2,4]
[,a,,b,,c,,,d] = arr
arrayEq [a,b,c,d], [2,4,6,9]
[
,e,
,f,
,g,
,,h] = arr
arrayEq [e,f,g,h], [2,4,6,9]
test "array elisions destructuring with splats and expansions", ->
arr = [1,2,3,4,5,6,7,8,9]
[,a,,,b...] = arr
arrayEq [a,b], [2,[5,6,7,8,9]]
[,c,...,,d,,e] = arr
arrayEq [c,d,e], [2,7,9]
[...,f,,,g,,,] = arr
arrayEq [f,g], [4,7]
test "array elisions as function parameters", ->
arr = [1,2,3,4,5,6,7,8,9]
foo = ([,a]) -> a
a = foo arr
eq a, 2
foo = ([,,,a]) -> a
a = foo arr
eq a, 4
foo = ([,a,,b,,c,,,d]) -> [a,b,c,d]
[a,b,c,d] = foo arr
arrayEq [a,b,c,d], [2,4,6,9]
test "array elisions nested destructuring", ->
arr = [
1,
[2,3, [4,5,6, [7,8,9] ] ]
]
[,a] = arr
arrayEq a[2][3], [7,8,9]
[,[,,[,b,,[,,c]]]] = arr
eq b, 5
eq c, 9
aobj = [
{},
{x: 2},
{},
[
{},
{},
{z:1, w:[1,2,4], p:3, q:4}
{},
{}
]
]
[,d,,[,,{w}]] = aobj
deepEqual d, {x:2}
arrayEq w, [1,2,4]
test "#5112: array elisions not detected inside strings", ->
arr = [
str: ", #{3}"
]
eq arr[0].str, ', 3'
# Splats in Array Literals
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
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'
test "array splats with nested arrays", ->
nonce = {}
a = [nonce]
list = [1, 2, a...]
eq list[0], 1
eq list[2], nonce
a = [[nonce]]
list = [1, 2, a...]
arrayEq list, [1, 2, [nonce]]
test "#4260: splat after existential operator soak", ->
a = {b: [3]}
foo = (a) -> [a]
arrayEq [a?.b...], [3]
arrayEq [c?.b ? []...], []
arrayEq [...a?.b], [3]
arrayEq [...c?.b ? []], []
arrayEq foo(a?.b...), [3]
arrayEq foo(...a?.b), [3]
arrayEq foo(c?.b ? []...), [undefined]
arrayEq foo(...c?.b ? []), [undefined]
e = yes
f = null
arrayEq [(a if e)?.b...], [3]
arrayEq [(a if f)?.b ? []...], []
arrayEq [...(a if e)?.b], [3]
arrayEq [...(a if f)?.b ? []], []
arrayEq foo((a if e)?.b...), [3]
arrayEq foo(...(a if e)?.b), [3]
arrayEq foo((a if f)?.b ? []...), [undefined]
arrayEq foo(...(a if f)?.b ? []), [undefined]
# Should not trigger implicit call, e.g. rest ... => rest(...)
arrayEq [... a?.b], [3]
arrayEq [... c?.b ? []], []
arrayEq [a?.b ...], [3]
arrayEq [(a if e)?.b ...], [3]
arrayEq foo(a?.b ...), [3]
arrayEq foo(... a?.b), [3]
test "#1349: trailing if after splat", ->
a = [3]
b = yes
c = null
foo = (a) -> [a]
arrayEq [a if b...], [3]
arrayEq [(a if c) ? []...], []
arrayEq [...a if b], [3]
arrayEq [...(a if c) ? []], []
arrayEq foo((a if b)...), [3]
arrayEq foo(...(a if b)), [3]
arrayEq foo((a if c) ? []...), [undefined]
arrayEq foo(...(a if c) ? []), [undefined]
# Should not trigger implicit call, e.g. rest ... => rest(...)
arrayEq [... a if b], [3]
arrayEq [a if b ...], [3]
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
test "splat extraction from generators", ->
gen = ->
yield 1
yield 2
yield 3
arrayEq [ gen()... ], [ 1, 2, 3 ]
test "for-from loops over Array", ->
array1 = [50, 30, 70, 20]
array2 = []
for x from array1
array2.push(x)
arrayEq array1, array2
array1 = [[20, 30], [40, 50]]
array2 = []
for [a, b] from array1
array2.push(b)
array2.push(a)
arrayEq array2, [30, 20, 50, 40]
array1 = [{a: 10, b: 20, c: 30}, {a: 40, b: 50, c: 60}]
array2 = []
for {a: a, b, c: d} from array1
array2.push([a, b, d])
arrayEq array2, [[10, 20, 30], [40, 50, 60]]
array1 = [[10, 20, 30, 40, 50]]
for [a, b..., c] from array1
eq 10, a
arrayEq [20, 30, 40], b
eq 50, c
test "for-from comprehensions over Array", ->
array1 = (x + 10 for x from [10, 20, 30])
ok array1.join(' ') is '20 30 40'
array2 = (x for x from [30, 41, 57] when x %% 3 is 0)
ok array2.join(' ') is '30 57'
array1 = (b + 5 for [a, b] from [[20, 30], [40, 50]])
ok array1.join(' ') is '35 55'
array2 = (a + b for [a, b] from [[10, 20], [30, 40], [50, 60]] when a + b >= 70)
ok array2.join(' ') is '70 110'
test "#5201: simple indented elisions", ->
arr1 = [
,
1,
2,
,
,
3,
4,
5,
6
,
,
8,
9,
]
eq arr1.length, 12
eq arr1[5], 3
eq arr1[9], undefined
arr2 = [
,
,
1,
2,
,
3,
,
4,
5
6
,
,
,
]
eq arr2.length, 12
eq arr2[8], 5
eq arr2[1], undefined
arr3 = [
,
,
,
]
eq arr3.length, 3
arr4 = [, , ,]
eq arr4.length, 3
|