UNPKG

6.13 kBtext/coffeescriptView Raw
1suite 'Object Literals', ->
2
3# TODO: refactor object literal tests
4# TODO: add indexing and method invocation tests: {a}['a'] is a, {a}.a()
5
6 suite 'Basic Objects', ->
7
8 test 'basic literals', ->
9 nonce = {}
10 eq nonce, {a:nonce}.a
11 eq nonce, {a: nonce}.a
12 eq nonce, { a : nonce }.a
13 eq nonce, {a: nonce,}.a
14 eq nonce, {0: nonce}[0]
15 eq nonce, {0x0: nonce}[0]
16 eq nonce, {'0x0': nonce}['0x0']
17 eq nonce, {1e3: nonce}[1e3]
18 eq nonce, {a:0,b:nonce,c:0}.b
19 eq nonce, {a: 0, b: nonce, c: 0}.b
20 eq nonce, {a: 0, b: nonce, c: 0, }.b
21 eq nonce, { a : 0 , b : nonce, c : 0 }.b
22 eq nonce, {'a': nonce}.a
23 eq nonce, {'s p a c e s': nonce}['s p a c e s']
24
25 test 'reserved words as keys', ->
26 nonce = {}
27
28 # CS reserved words
29 obj = {not: nonce}
30 eq nonce, obj.not
31
32 # JS reserved words
33 obj = {default: nonce}
34 eq nonce, obj.default
35
36 test 'listed functions', ->
37 nonce = {}
38 ok nonce, { 0: -> nonce }[0]()
39 ok nonce, { 0: -> 0, 1: -> nonce, 2: -> 0 }[1]()
40
41 test 'function context', ->
42 nonce = {}
43 eq nonce, { nonce: nonce, fn: -> @nonce }.fn()
44 eq nonce, { nonce: nonce, fn: -> @nonce }['fn']()
45
46 test 'implicit member shorthand', ->
47 nonce = {}
48 eq nonce, { nonce }.nonce
49 (-> eq nonce, { @nonce }.nonce).call { nonce }
50
51 test 'function calls in object literals', ->
52 fn = (a, b, c) -> c
53 nonce = {}
54 eq nonce, { a: fn 0, 1, nonce, 2 }.a
55 eq nonce, { a: -> fn 0, 1, nonce, 2 }.a()
56
57 test 'jashkenas/coffee-script#542: leading objects need parentheses', ->
58 a = false
59 {f: -> a = true}.f() + 1
60 ok a
61
62 #test 'jashkenas/coffee-script#1274: `{} = a()` should not optimise away a()', ->
63 # a = false
64 # fn = -> a = true
65 # {} = fn()
66 # ok a
67
68 test 'jashkenas/coffee-script#1436: `for` etc. work as normal property names', ->
69 obj = {}
70 ok 'for' not of obj
71 obj.for = 'for' of obj
72 ok 'for' of obj
73
74 #test 'jashkenas/coffee-script#1513: Top level bare objects need to be wrapped in parens for unary and existence ops', ->
75 # doesNotThrow -> CoffeeScript.run '{}?', bare: true
76 # doesNotThrow -> CoffeeScript.run '{}.a++', bare: true
77
78 suite 'Implicit Objects', ->
79
80 #test 'implicit object literals', ->
81 #
82 # obj =
83 # a: 1,
84 # b: 2,
85 # ok obj.a is 1
86 # ok obj.b is 2
87 #
88 # config =
89 # development:
90 # server: 'localhost'
91 # timeout: 10
92 # production:
93 # server: 'dreamboat'
94 # timeout: 1000
95 # ok config.development.server is 'localhost'
96 # ok config.production.server is 'dreamboat'
97 # ok config.development.timeout is 10
98 # ok config.production.timeout is 1000
99
100 #test 'implicit objects as part of chained calls', ->
101 # pluck = (x) -> x.a
102 # eq 100, pluck pluck pluck a: a: a: 100
103
104 #test 'explicit objects nested under implicit objects', ->
105
106 #test 'invoking functions with implicit object literals', ->
107 # generateGetter = (prop) -> (obj) -> obj[prop]
108 # getA = generateGetter 'a'
109 # getArgs = -> arguments
110 # a = b = 30
111 #
112 # result = getA
113 # a: 10
114 # eq 10, result
115 #
116 # result = getA
117 # 'a': 20
118 # eq 20, result
119 #
120 # result = getA a,
121 # b:1
122 # eq undefined, result
123 #
124 # result = getA b:1,
125 # a:43
126 # eq 43, result
127 #
128 # result = getA b:1,
129 # a:62
130 # eq undefined, result
131 #
132 # result = getA
133 # b:1
134 # a
135 # eq undefined, result
136 #
137 # result = getA
138 # a:
139 # b:2
140 # b:1
141 # eq 2, result.b
142 #
143 # result = getArgs
144 # a:1
145 # b
146 # c:1
147 # ok result.length is 3
148 # ok result[2].c is 1
149 #
150 # result = getA b: 13, a: 42, 2
151 # eq 42, result
152 #
153 # result = getArgs a:1, (1 + 1)
154 # ok result[1] is 2
155 #
156 # result = getArgs a:1, b
157 # ok result.length is 2
158 # ok result[1] is 30
159 #
160 # result = getArgs a:1, b, b:1, a
161 # ok result.length is 4
162 # ok result[2].b is 1
163 #
164 # throws -> CoffeeScript.compile 'a = b:1, c'
165
166 #test 'multiple dedentations in implicit object literals', ->
167 # nonce0 = {}
168 # nonce1 = {}
169 # obj =
170 # a:
171 # b: ->
172 # c: nonce0
173 # d: nonce1
174 # eq nonce0, obj.a.b().c
175 # eq nonce1, obj.d
176
177 #test 'jashkenas/coffee-script#1871: Special case for IMPLICIT_END in the middle of an implicit object', ->
178 # result = 'result'
179 # ident = (x) -> x
180 #
181 # result = ident one: 1 if false
182 #
183 # eq result, 'result'
184 #
185 # result = ident
186 # one: 1
187 # two: 2 for i in [1..3]
188 #
189 # eq result.two.join(' '), '2 2 2'
190
191 #test 'jashkenas/coffee-script#1961, jashkenas/coffee-script#1974, regression with compound assigning to an implicit object', ->
192 #
193 # obj = null
194 #
195 # obj ?=
196 # one: 1
197 # two: 2
198 #
199 # eq obj.two, 2
200 #
201 # obj = null
202 #
203 # obj or=
204 # three: 3
205 # four: 4
206 #
207 # eq obj.four, 4
208
209 #test 'jashkenas/coffee-script#2207: Immediate implicit closes don't close implicit objects', ->
210 # func = ->
211 # key: for i in [1, 2, 3] then i
212 #
213 # eq func().key.join(' '), '1 2 3'
214
215 test '#122 implicit object literal in conditional body', ->
216 a = yes
217
218 b = switch a
219 when yes
220 result: yes
221 when no, 10
222 result: no
223
224 ok b.result
225
226 c = if a
227 result: yes
228
229 ok c.result
230
231 d = 42
232 e = if 2 + 40 is d
233 result: yes
234
235 ok e.result
236
237 f = unless a
238 result: no
239 else
240 result: yes
241
242 ok f.result
243
244 g = 0
245 h = 1
246 while g < h
247 result: yes
248 g += 1
249
250 eq g, 1
251
252 i = 0
253 j = 1
254 unless i > j
255 result: yes
256 i += 1
257
258 eq i, 1
259
260 k = [0..3]
261 for l in k
262 result: yes
263
264 eq l, 3
265
266 m = [0..3]
267 for n of m
268 result: yes
269
270 eq n, '3'
271