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
|
return describe("table comprehension", function()
it("should create simple table copy", function()
local thing = {
color = "red",
name = "fast",
width = 123
}
local thing_copy
do
local _tbl_0 = { }
for k, v in pairs(thing) do
_tbl_0[k] = v
end
thing_copy = _tbl_0
end
assert.same(thing_copy.color, thing.color)
assert.same(thing_copy.name, thing.name)
return assert.same(thing_copy.width, thing.width)
end)
it("should filter with when clause", function()
local thing = {
color = "red",
name = "fast",
width = 123
}
local no_color
do
local _tbl_0 = { }
for k, v in pairs(thing) do
if k ~= "color" then
_tbl_0[k] = v
end
end
no_color = _tbl_0
end
assert.same(no_color.color, nil)
assert.same(no_color.name, "fast")
return assert.same(no_color.width, 123)
end)
it("should transform values", function()
local numbers = {
a = 1,
b = 2,
c = 3
}
local doubled
do
local _tbl_0 = { }
for k, v in pairs(numbers) do
_tbl_0[k] = v * 2
end
doubled = _tbl_0
end
assert.same(doubled.a, 2)
assert.same(doubled.b, 4)
return assert.same(doubled.c, 6)
end)
it("should transform keys", function()
local data = {
a = 1,
b = 2
}
local upper_keys
do
local _tbl_0 = { }
for k, v in pairs(data) do
_tbl_0[k:upper()] = v
end
upper_keys = _tbl_0
end
assert.same(upper_keys.A, 1)
return assert.same(upper_keys.B, 2)
end)
it("should work with ipairs", function()
local items = {
"a",
"b",
"c"
}
local reversed
do
local _tbl_0 = { }
for i, v in ipairs(items) do
_tbl_0[i] = v
end
reversed = _tbl_0
end
assert.same(reversed[1], "a")
assert.same(reversed[2], "b")
return assert.same(reversed[3], "c")
end)
it("should filter array items", function()
local items = {
1,
2,
3,
4,
5
}
local evens
do
local _tbl_0 = { }
for i, v in ipairs(items) do
if v % 2 == 0 then
_tbl_0[i] = v
end
end
evens = _tbl_0
end
assert.same(evens[2], 2)
assert.same(evens[4], 4)
return assert.same(evens[1], nil)
end)
it("should work with numeric for loop", function()
local squares
do
local _tbl_0 = { }
for i = 1, 5 do
_tbl_0[i] = i * i
end
squares = _tbl_0
end
assert.same(squares[1], 1)
assert.same(squares[2], 4)
assert.same(squares[3], 9)
assert.same(squares[4], 16)
return assert.same(squares[5], 25)
end)
it("should support nested comprehensions", function()
local matrix = {
{
1,
2
},
{
3,
4
},
{
5,
6
}
}
local flat = { }
for _index_0 = 1, #matrix do
local row = matrix[_index_0]
for i, v in ipairs(row) do
flat[#flat + 1] = v
end
end
return assert.same(flat, {
1,
2,
3,
4,
5,
6
})
end)
it("should combine pairs and when", function()
local data = {
a = 1,
b = 2,
c = 3,
d = 4
}
local greater_than_two
do
local _tbl_0 = { }
for k, v in pairs(data) do
if v > 2 then
_tbl_0[k] = v
end
end
greater_than_two = _tbl_0
end
assert.same(greater_than_two.a, nil)
assert.same(greater_than_two.b, nil)
assert.same(greater_than_two.c, 3)
return assert.same(greater_than_two.d, 4)
end)
it("should work with string keys", function()
local obj = {
["key-with-dash"] = "value1",
["key_with_underscore"] = "value2"
}
local result
do
local _tbl_0 = { }
for k, v in pairs(obj) do
_tbl_0[k] = v
end
result = _tbl_0
end
assert.same(result["key-with-dash"], "value1")
return assert.same(result["key_with_underscore"], "value2")
end)
it("should handle empty source", function()
local empty = { }
local result
do
local _tbl_0 = { }
for k, v in pairs(empty) do
_tbl_0[k] = v
end
result = _tbl_0
end
return assert.same(#result, 0)
end)
it("should work with computed keys", function()
local base = {
a = 1,
b = 2
}
local result
do
local _tbl_0 = { }
for k, v in pairs(base) do
_tbl_0[k .. "_suffix"] = v * 10
end
result = _tbl_0
end
assert.same(result.a_suffix, 10)
return assert.same(result.b_suffix, 20)
end)
it("should support nested table transformation", function()
local data = {
first = {
x = 1,
y = 2
},
second = {
x = 3,
y = 4
}
}
local transformed
do
local _tbl_0 = { }
for k, v in pairs(data) do
_tbl_0[k] = v.x + v.y
end
transformed = _tbl_0
end
assert.same(transformed.first, 3)
return assert.same(transformed.second, 7)
end)
it("should filter with multiple conditions", function()
local numbers = {
a = 1,
b = 2,
c = 3,
d = 4,
e = 5
}
local result
do
local _tbl_0 = { }
for k, v in pairs(numbers) do
if v > 1 and v < 5 then
_tbl_0[k] = v
end
end
result = _tbl_0
end
assert.same(result.a, nil)
assert.same(result.b, 2)
assert.same(result.c, 3)
assert.same(result.d, 4)
return assert.same(result.e, nil)
end)
return it("should work with custom iterator", function()
local custom_iter
custom_iter = function()
local state = 0
return function()
state = state + 1
if state <= 3 then
return state, state * 10
else
return nil
end
end
end
local result
do
local _tbl_0 = { }
for k, v in custom_iter() do
_tbl_0[k] = v
end
result = _tbl_0
end
assert.same(result[1], 10)
assert.same(result[2], 20)
return assert.same(result[3], 30)
end)
end)
|