diff options
Diffstat (limited to 'spec/outputs/test/param_destructure_spec.lua')
| -rw-r--r-- | spec/outputs/test/param_destructure_spec.lua | 369 |
1 files changed, 369 insertions, 0 deletions
diff --git a/spec/outputs/test/param_destructure_spec.lua b/spec/outputs/test/param_destructure_spec.lua new file mode 100644 index 0000000..6aa9907 --- /dev/null +++ b/spec/outputs/test/param_destructure_spec.lua | |||
| @@ -0,0 +1,369 @@ | |||
| 1 | local _anon_func_0 = function(_arg_0) | ||
| 2 | local _accum_0 = { } | ||
| 3 | local _len_0 = 1 | ||
| 4 | local _max_0 = #_arg_0 | ||
| 5 | for _index_0 = 2, _max_0 do | ||
| 6 | local _item_0 = _arg_0[_index_0] | ||
| 7 | _accum_0[_len_0] = _item_0 | ||
| 8 | _len_0 = _len_0 + 1 | ||
| 9 | end | ||
| 10 | return _accum_0 | ||
| 11 | end | ||
| 12 | local _anon_func_1 = function(_arg_0) | ||
| 13 | local _accum_0 = { } | ||
| 14 | local _len_0 = 1 | ||
| 15 | local _max_0 = #_arg_0 | ||
| 16 | for _index_0 = 1, _max_0 do | ||
| 17 | local _item_0 = _arg_0[_index_0] | ||
| 18 | _accum_0[_len_0] = _item_0 | ||
| 19 | _len_0 = _len_0 + 1 | ||
| 20 | end | ||
| 21 | return _accum_0 | ||
| 22 | end | ||
| 23 | return describe("parameter destructuring", function() | ||
| 24 | it("should destructure simple object", function() | ||
| 25 | local f | ||
| 26 | f = function(_arg_0) | ||
| 27 | local a, b, c | ||
| 28 | a, b, c = _arg_0.a, _arg_0.b, _arg_0.c | ||
| 29 | return { | ||
| 30 | a, | ||
| 31 | b, | ||
| 32 | c | ||
| 33 | } | ||
| 34 | end | ||
| 35 | local result = f({ | ||
| 36 | a = 1, | ||
| 37 | b = "2", | ||
| 38 | c = { } | ||
| 39 | }) | ||
| 40 | return assert.same(result, { | ||
| 41 | 1, | ||
| 42 | "2", | ||
| 43 | { } | ||
| 44 | }) | ||
| 45 | end) | ||
| 46 | it("should work with default values", function() | ||
| 47 | local f | ||
| 48 | f = function(_arg_0, c) | ||
| 49 | local a1, b | ||
| 50 | a1, b = _arg_0.a, _arg_0.b | ||
| 51 | if a1 == nil then | ||
| 52 | a1 = 123 | ||
| 53 | end | ||
| 54 | if b == nil then | ||
| 55 | b = 'abc' | ||
| 56 | end | ||
| 57 | if c == nil then | ||
| 58 | c = { } | ||
| 59 | end | ||
| 60 | return { | ||
| 61 | a1, | ||
| 62 | b, | ||
| 63 | c | ||
| 64 | } | ||
| 65 | end | ||
| 66 | local result1 = f({ | ||
| 67 | a = 0 | ||
| 68 | }, "test") | ||
| 69 | assert.same(result1, { | ||
| 70 | 0, | ||
| 71 | 'abc', | ||
| 72 | 'test' | ||
| 73 | }) | ||
| 74 | local result2 = f({ }) | ||
| 75 | return assert.same(result2, { | ||
| 76 | 123, | ||
| 77 | 'abc', | ||
| 78 | { } | ||
| 79 | }) | ||
| 80 | end) | ||
| 81 | it("should destructure with mixed syntax", function() | ||
| 82 | local f | ||
| 83 | f = function(_arg_0) | ||
| 84 | local a, b1, c | ||
| 85 | a, b1, c = _arg_0.a, _arg_0.b, _arg_0.c | ||
| 86 | return { | ||
| 87 | a, | ||
| 88 | b1, | ||
| 89 | c | ||
| 90 | } | ||
| 91 | end | ||
| 92 | local result = f({ | ||
| 93 | a = 1, | ||
| 94 | b = 2, | ||
| 95 | c = 3 | ||
| 96 | }) | ||
| 97 | return assert.same(result, { | ||
| 98 | 1, | ||
| 99 | 2, | ||
| 100 | 3 | ||
| 101 | }) | ||
| 102 | end) | ||
| 103 | it("should work with nested destructuring", function() | ||
| 104 | local f | ||
| 105 | f = function(_arg_0) | ||
| 106 | local x, y | ||
| 107 | x, y = _arg_0.nested.x, _arg_0.nested.y | ||
| 108 | return { | ||
| 109 | x, | ||
| 110 | y | ||
| 111 | } | ||
| 112 | end | ||
| 113 | local result = f({ | ||
| 114 | nested = { | ||
| 115 | x = 10, | ||
| 116 | y = 20 | ||
| 117 | } | ||
| 118 | }) | ||
| 119 | return assert.same(result, { | ||
| 120 | 10, | ||
| 121 | 20 | ||
| 122 | }) | ||
| 123 | end) | ||
| 124 | it("should handle array parameters", function() | ||
| 125 | local f | ||
| 126 | f = function(_arg_0) | ||
| 127 | local a, b, c | ||
| 128 | a, b, c = _arg_0[1], _arg_0[2], _arg_0[3] | ||
| 129 | return { | ||
| 130 | a, | ||
| 131 | b, | ||
| 132 | c | ||
| 133 | } | ||
| 134 | end | ||
| 135 | local result = f({ | ||
| 136 | 1, | ||
| 137 | 2, | ||
| 138 | 3 | ||
| 139 | }) | ||
| 140 | return assert.same(result, { | ||
| 141 | 1, | ||
| 142 | 2, | ||
| 143 | 3 | ||
| 144 | }) | ||
| 145 | end) | ||
| 146 | it("should support mixed array and object", function() | ||
| 147 | local f | ||
| 148 | f = function(_arg_0, _arg_1) | ||
| 149 | local first | ||
| 150 | first = _arg_0[1] | ||
| 151 | local value | ||
| 152 | value = _arg_1.key.value | ||
| 153 | return { | ||
| 154 | first, | ||
| 155 | value | ||
| 156 | } | ||
| 157 | end | ||
| 158 | local result = f({ | ||
| 159 | 1 | ||
| 160 | }, { | ||
| 161 | key = { | ||
| 162 | value = "test" | ||
| 163 | } | ||
| 164 | }) | ||
| 165 | return assert.same(result, { | ||
| 166 | 1, | ||
| 167 | "test" | ||
| 168 | }) | ||
| 169 | end) | ||
| 170 | it("should work with fat arrow", function() | ||
| 171 | local obj = { | ||
| 172 | value = 100, | ||
| 173 | f = function(self, _arg_0) | ||
| 174 | local x, y | ||
| 175 | x, y = _arg_0.x, _arg_0.y | ||
| 176 | return self.value + x + y | ||
| 177 | end | ||
| 178 | } | ||
| 179 | local result = obj:f({ | ||
| 180 | x = 10, | ||
| 181 | y = 20 | ||
| 182 | }) | ||
| 183 | return assert.same(result, 130) | ||
| 184 | end) | ||
| 185 | it("should handle missing keys", function() | ||
| 186 | local f | ||
| 187 | f = function(_arg_0) | ||
| 188 | local a, b, c | ||
| 189 | a, b, c = _arg_0.a, _arg_0.b, _arg_0.c | ||
| 190 | if b == nil then | ||
| 191 | b = "default" | ||
| 192 | end | ||
| 193 | if c == nil then | ||
| 194 | c = "missing" | ||
| 195 | end | ||
| 196 | return { | ||
| 197 | a, | ||
| 198 | b, | ||
| 199 | c | ||
| 200 | } | ||
| 201 | end | ||
| 202 | local result = f({ | ||
| 203 | a = 1 | ||
| 204 | }) | ||
| 205 | return assert.same(result, { | ||
| 206 | 1, | ||
| 207 | 'default', | ||
| 208 | 'missing' | ||
| 209 | }) | ||
| 210 | end) | ||
| 211 | it("should work with complex defaults", function() | ||
| 212 | local f | ||
| 213 | f = function(_arg_0) | ||
| 214 | local a1, b1 | ||
| 215 | a1, b1 = _arg_0.a, _arg_0.b | ||
| 216 | if a1 == nil then | ||
| 217 | a1 = 100 | ||
| 218 | end | ||
| 219 | if b1 == nil then | ||
| 220 | b1 = a1 + 1000 | ||
| 221 | end | ||
| 222 | return a1 + b1 | ||
| 223 | end | ||
| 224 | local result = f({ }) | ||
| 225 | return assert.same(result, 1200) | ||
| 226 | end) | ||
| 227 | it("should support deep nesting", function() | ||
| 228 | local f | ||
| 229 | f = function(_arg_0) | ||
| 230 | local value | ||
| 231 | value = _arg_0.data.nested.value | ||
| 232 | return value | ||
| 233 | end | ||
| 234 | local result = f({ | ||
| 235 | data = { | ||
| 236 | nested = { | ||
| 237 | value = 42 | ||
| 238 | } | ||
| 239 | } | ||
| 240 | }) | ||
| 241 | return assert.same(result, 42) | ||
| 242 | end) | ||
| 243 | it("should work with multiple parameters", function() | ||
| 244 | local f | ||
| 245 | f = function(_arg_0, extra) | ||
| 246 | local x, y, z | ||
| 247 | x, y, z = _arg_0.x, _arg_0.y, _arg_0.z | ||
| 248 | if extra == nil then | ||
| 249 | extra = "default" | ||
| 250 | end | ||
| 251 | return { | ||
| 252 | x, | ||
| 253 | y, | ||
| 254 | z, | ||
| 255 | extra | ||
| 256 | } | ||
| 257 | end | ||
| 258 | local result = f({ | ||
| 259 | x = 1, | ||
| 260 | y = 2, | ||
| 261 | z = 3 | ||
| 262 | }) | ||
| 263 | return assert.same(result, { | ||
| 264 | 1, | ||
| 265 | 2, | ||
| 266 | 3, | ||
| 267 | 'default' | ||
| 268 | }) | ||
| 269 | end) | ||
| 270 | it("should handle array destructuring in parameters", function() | ||
| 271 | local f | ||
| 272 | f = function(_arg_0) | ||
| 273 | local first, rest | ||
| 274 | first, rest = _arg_0[1], _anon_func_0(_arg_0) | ||
| 275 | return { | ||
| 276 | first, | ||
| 277 | rest | ||
| 278 | } | ||
| 279 | end | ||
| 280 | local result = f({ | ||
| 281 | 1, | ||
| 282 | 2, | ||
| 283 | 3, | ||
| 284 | 4 | ||
| 285 | }) | ||
| 286 | return assert.same(result, { | ||
| 287 | 1, | ||
| 288 | { | ||
| 289 | 2, | ||
| 290 | 3, | ||
| 291 | 4 | ||
| 292 | } | ||
| 293 | }) | ||
| 294 | end) | ||
| 295 | it("should support spreading", function() | ||
| 296 | local f | ||
| 297 | f = function(_arg_0) | ||
| 298 | local rest, last | ||
| 299 | rest, last = _anon_func_1(_arg_0), _arg_0.last | ||
| 300 | return { | ||
| 301 | rest, | ||
| 302 | last | ||
| 303 | } | ||
| 304 | end | ||
| 305 | local result = f({ | ||
| 306 | 1, | ||
| 307 | 2, | ||
| 308 | 3, | ||
| 309 | last = "final" | ||
| 310 | }) | ||
| 311 | return assert.same(result, { | ||
| 312 | { | ||
| 313 | 1, | ||
| 314 | 2, | ||
| 315 | 3 | ||
| 316 | }, | ||
| 317 | 'final' | ||
| 318 | }) | ||
| 319 | end) | ||
| 320 | it("should work with table comprehensions", function() | ||
| 321 | local f | ||
| 322 | f = function(_arg_0) | ||
| 323 | local items | ||
| 324 | items = _arg_0.items | ||
| 325 | local _accum_0 = { } | ||
| 326 | local _len_0 = 1 | ||
| 327 | for _index_0 = 1, #items do | ||
| 328 | local item = items[_index_0] | ||
| 329 | _accum_0[_len_0] = item * 2 | ||
| 330 | _len_0 = _len_0 + 1 | ||
| 331 | end | ||
| 332 | return _accum_0 | ||
| 333 | end | ||
| 334 | local result = f({ | ||
| 335 | items = { | ||
| 336 | 1, | ||
| 337 | 2, | ||
| 338 | 3 | ||
| 339 | } | ||
| 340 | }) | ||
| 341 | return assert.same(result, { | ||
| 342 | 2, | ||
| 343 | 4, | ||
| 344 | 6 | ||
| 345 | }) | ||
| 346 | end) | ||
| 347 | return it("should handle nil arguments", function() | ||
| 348 | local f | ||
| 349 | f = function(_arg_0) | ||
| 350 | local a, b | ||
| 351 | a, b = _arg_0.a, _arg_0.b | ||
| 352 | if a == nil then | ||
| 353 | a = "nil_a" | ||
| 354 | end | ||
| 355 | if b == nil then | ||
| 356 | b = "nil_b" | ||
| 357 | end | ||
| 358 | return { | ||
| 359 | a, | ||
| 360 | b | ||
| 361 | } | ||
| 362 | end | ||
| 363 | local result = f({ }) | ||
| 364 | return assert.same(result, { | ||
| 365 | "nil_a", | ||
| 366 | "nil_b" | ||
| 367 | }) | ||
| 368 | end) | ||
| 369 | end) | ||
