diff options
| author | Li Jin <dragon-fly@qq.com> | 2026-01-26 17:45:26 +0800 |
|---|---|---|
| committer | Li Jin <dragon-fly@qq.com> | 2026-01-26 17:45:56 +0800 |
| commit | e02321107277a63e7dcb12ab163c9942ac101b87 (patch) | |
| tree | f38c6f2fbf4ee35df4938933dbc1e645733356f4 /spec/outputs/test/switch_spec.lua | |
| parent | 5d5b657f606b5939062983b1f90c3359d542672e (diff) | |
| download | yuescript-e02321107277a63e7dcb12ab163c9942ac101b87.tar.gz yuescript-e02321107277a63e7dcb12ab163c9942ac101b87.tar.bz2 yuescript-e02321107277a63e7dcb12ab163c9942ac101b87.zip | |
Updated tests.
Diffstat (limited to 'spec/outputs/test/switch_spec.lua')
| -rw-r--r-- | spec/outputs/test/switch_spec.lua | 742 |
1 files changed, 742 insertions, 0 deletions
diff --git a/spec/outputs/test/switch_spec.lua b/spec/outputs/test/switch_spec.lua new file mode 100644 index 0000000..59ffca5 --- /dev/null +++ b/spec/outputs/test/switch_spec.lua | |||
| @@ -0,0 +1,742 @@ | |||
| 1 | return describe("switch", function() | ||
| 2 | it("should match single value", function() | ||
| 3 | local value = "cool" | ||
| 4 | local result | ||
| 5 | if "cool" == value then | ||
| 6 | result = "matched" | ||
| 7 | else | ||
| 8 | result = "not matched" | ||
| 9 | end | ||
| 10 | return assert.same(result, "matched") | ||
| 11 | end) | ||
| 12 | it("should match multiple values with or", function() | ||
| 13 | local hi = "world" | ||
| 14 | local matched = false | ||
| 15 | if "one" == hi or "two" == hi then | ||
| 16 | matched = true | ||
| 17 | end | ||
| 18 | assert.is_false(matched) | ||
| 19 | hi = "one" | ||
| 20 | if "one" == hi or "two" == hi then | ||
| 21 | matched = true | ||
| 22 | end | ||
| 23 | return assert.is_true(matched) | ||
| 24 | end) | ||
| 25 | it("should execute else branch when no match", function() | ||
| 26 | local value = "other" | ||
| 27 | local result | ||
| 28 | if "cool" == value then | ||
| 29 | result = "matched cool" | ||
| 30 | elseif "yeah" == value then | ||
| 31 | result = "matched yeah" | ||
| 32 | else | ||
| 33 | result = "else branch" | ||
| 34 | end | ||
| 35 | return assert.same(result, "else branch") | ||
| 36 | end) | ||
| 37 | it("should destructure table with single key", function() | ||
| 38 | local tb = { | ||
| 39 | x = 100 | ||
| 40 | } | ||
| 41 | local result | ||
| 42 | do | ||
| 43 | local _type_0 = type(tb) | ||
| 44 | local _tab_0 = "table" == _type_0 or "userdata" == _type_0 | ||
| 45 | local _match_0 = false | ||
| 46 | if _tab_0 then | ||
| 47 | local x = tb.x | ||
| 48 | if x ~= nil then | ||
| 49 | _match_0 = true | ||
| 50 | result = x | ||
| 51 | end | ||
| 52 | end | ||
| 53 | if not _match_0 then | ||
| 54 | result = "no match" | ||
| 55 | end | ||
| 56 | end | ||
| 57 | return assert.same(result, 100) | ||
| 58 | end) | ||
| 59 | it("should destructure table with multiple keys", function() | ||
| 60 | local tb = { | ||
| 61 | x = 100, | ||
| 62 | y = 200 | ||
| 63 | } | ||
| 64 | local result | ||
| 65 | do | ||
| 66 | local _type_0 = type(tb) | ||
| 67 | local _tab_0 = "table" == _type_0 or "userdata" == _type_0 | ||
| 68 | local _match_0 = false | ||
| 69 | if _tab_0 then | ||
| 70 | local x = tb.x | ||
| 71 | local y = tb.y | ||
| 72 | if x ~= nil and y ~= nil then | ||
| 73 | _match_0 = true | ||
| 74 | result = x + y | ||
| 75 | end | ||
| 76 | end | ||
| 77 | if not _match_0 then | ||
| 78 | result = "no match" | ||
| 79 | end | ||
| 80 | end | ||
| 81 | return assert.same(result, 300) | ||
| 82 | end) | ||
| 83 | it("should destructure table with default values", function() | ||
| 84 | local tb = { | ||
| 85 | a = 1 | ||
| 86 | } | ||
| 87 | local _type_0 = type(tb) | ||
| 88 | local _tab_0 = "table" == _type_0 or "userdata" == _type_0 | ||
| 89 | if _tab_0 then | ||
| 90 | local a = tb.a | ||
| 91 | local b = tb.b | ||
| 92 | if a == nil then | ||
| 93 | a = 1 | ||
| 94 | end | ||
| 95 | if b == nil then | ||
| 96 | b = 2 | ||
| 97 | end | ||
| 98 | assert.same(a, 1) | ||
| 99 | return assert.same(b, 2) | ||
| 100 | end | ||
| 101 | end) | ||
| 102 | it("should destructure nested tables", function() | ||
| 103 | local dict = { | ||
| 104 | { }, | ||
| 105 | { | ||
| 106 | 1, | ||
| 107 | 2, | ||
| 108 | 3 | ||
| 109 | }, | ||
| 110 | a = { | ||
| 111 | b = { | ||
| 112 | c = 1 | ||
| 113 | } | ||
| 114 | }, | ||
| 115 | x = { | ||
| 116 | y = { | ||
| 117 | z = 1 | ||
| 118 | } | ||
| 119 | } | ||
| 120 | } | ||
| 121 | local matched = false | ||
| 122 | do | ||
| 123 | local _type_0 = type(dict) | ||
| 124 | local _tab_0 = "table" == _type_0 or "userdata" == _type_0 | ||
| 125 | if _tab_0 then | ||
| 126 | local first = dict[1] | ||
| 127 | local one | ||
| 128 | do | ||
| 129 | local _obj_0 = dict[2] | ||
| 130 | local _type_1 = type(_obj_0) | ||
| 131 | if "table" == _type_1 or "userdata" == _type_1 then | ||
| 132 | one = _obj_0[1] | ||
| 133 | end | ||
| 134 | end | ||
| 135 | local two | ||
| 136 | do | ||
| 137 | local _obj_0 = dict[2] | ||
| 138 | local _type_1 = type(_obj_0) | ||
| 139 | if "table" == _type_1 or "userdata" == _type_1 then | ||
| 140 | two = _obj_0[2] | ||
| 141 | end | ||
| 142 | end | ||
| 143 | local three | ||
| 144 | do | ||
| 145 | local _obj_0 = dict[2] | ||
| 146 | local _type_1 = type(_obj_0) | ||
| 147 | if "table" == _type_1 or "userdata" == _type_1 then | ||
| 148 | three = _obj_0[3] | ||
| 149 | end | ||
| 150 | end | ||
| 151 | local c | ||
| 152 | do | ||
| 153 | local _obj_0 = dict.a | ||
| 154 | local _type_1 = type(_obj_0) | ||
| 155 | if "table" == _type_1 or "userdata" == _type_1 then | ||
| 156 | do | ||
| 157 | local _obj_1 = _obj_0.b | ||
| 158 | local _type_2 = type(_obj_1) | ||
| 159 | if "table" == _type_2 or "userdata" == _type_2 then | ||
| 160 | c = _obj_1.c | ||
| 161 | end | ||
| 162 | end | ||
| 163 | end | ||
| 164 | end | ||
| 165 | local z | ||
| 166 | do | ||
| 167 | local _obj_0 = dict.x | ||
| 168 | local _type_1 = type(_obj_0) | ||
| 169 | if "table" == _type_1 or "userdata" == _type_1 then | ||
| 170 | do | ||
| 171 | local _obj_1 = _obj_0.y | ||
| 172 | local _type_2 = type(_obj_1) | ||
| 173 | if "table" == _type_2 or "userdata" == _type_2 then | ||
| 174 | z = _obj_1.z | ||
| 175 | end | ||
| 176 | end | ||
| 177 | end | ||
| 178 | end | ||
| 179 | if first ~= nil and one ~= nil and two ~= nil and three ~= nil and c ~= nil and z ~= nil then | ||
| 180 | matched = type(first) == 'table' and one == 1 and two == 2 and three == 3 and c == 1 and z == 1 | ||
| 181 | end | ||
| 182 | end | ||
| 183 | end | ||
| 184 | return assert.is_true(matched) | ||
| 185 | end) | ||
| 186 | it("should destructure arrays with exact match", function() | ||
| 187 | local tb = { | ||
| 188 | 1, | ||
| 189 | 2, | ||
| 190 | 3 | ||
| 191 | } | ||
| 192 | local result | ||
| 193 | do | ||
| 194 | local _type_0 = type(tb) | ||
| 195 | local _tab_0 = "table" == _type_0 or "userdata" == _type_0 | ||
| 196 | local _match_0 = false | ||
| 197 | if _tab_0 then | ||
| 198 | if 1 == tb[1] and 2 == tb[2] and 3 == tb[3] then | ||
| 199 | _match_0 = true | ||
| 200 | result = "exact match" | ||
| 201 | end | ||
| 202 | end | ||
| 203 | if not _match_0 then | ||
| 204 | result = "no match" | ||
| 205 | end | ||
| 206 | end | ||
| 207 | return assert.same(result, "exact match") | ||
| 208 | end) | ||
| 209 | it("should destructure arrays with variables", function() | ||
| 210 | local tb = { | ||
| 211 | 1, | ||
| 212 | "b", | ||
| 213 | 3 | ||
| 214 | } | ||
| 215 | local result | ||
| 216 | do | ||
| 217 | local _type_0 = type(tb) | ||
| 218 | local _tab_0 = "table" == _type_0 or "userdata" == _type_0 | ||
| 219 | local _match_0 = false | ||
| 220 | if _tab_0 then | ||
| 221 | local b = tb[2] | ||
| 222 | if 1 == tb[1] and b ~= nil and 3 == tb[3] then | ||
| 223 | _match_0 = true | ||
| 224 | result = b | ||
| 225 | end | ||
| 226 | end | ||
| 227 | if not _match_0 then | ||
| 228 | result = "no match" | ||
| 229 | end | ||
| 230 | end | ||
| 231 | return assert.same(result, "b") | ||
| 232 | end) | ||
| 233 | it("should destructure arrays with defaults", function() | ||
| 234 | local tb = { | ||
| 235 | 1, | ||
| 236 | 2 | ||
| 237 | } | ||
| 238 | local result | ||
| 239 | do | ||
| 240 | local _type_0 = type(tb) | ||
| 241 | local _tab_0 = "table" == _type_0 or "userdata" == _type_0 | ||
| 242 | local _match_0 = false | ||
| 243 | if _tab_0 then | ||
| 244 | local b = tb[3] | ||
| 245 | if b == nil then | ||
| 246 | b = 3 | ||
| 247 | end | ||
| 248 | if 1 == tb[1] and 2 == tb[2] then | ||
| 249 | _match_0 = true | ||
| 250 | result = b | ||
| 251 | end | ||
| 252 | end | ||
| 253 | if not _match_0 then | ||
| 254 | result = "no match" | ||
| 255 | end | ||
| 256 | end | ||
| 257 | return assert.same(result, 3) | ||
| 258 | end) | ||
| 259 | it("should match pattern with __class", function() | ||
| 260 | local ClassA | ||
| 261 | do | ||
| 262 | local _class_0 | ||
| 263 | local _base_0 = { } | ||
| 264 | if _base_0.__index == nil then | ||
| 265 | _base_0.__index = _base_0 | ||
| 266 | end | ||
| 267 | _class_0 = setmetatable({ | ||
| 268 | __init = function() end, | ||
| 269 | __base = _base_0, | ||
| 270 | __name = "ClassA" | ||
| 271 | }, { | ||
| 272 | __index = _base_0, | ||
| 273 | __call = function(cls, ...) | ||
| 274 | local _self_0 = setmetatable({ }, _base_0) | ||
| 275 | cls.__init(_self_0, ...) | ||
| 276 | return _self_0 | ||
| 277 | end | ||
| 278 | }) | ||
| 279 | _base_0.__class = _class_0 | ||
| 280 | ClassA = _class_0 | ||
| 281 | end | ||
| 282 | local ClassB | ||
| 283 | do | ||
| 284 | local _class_0 | ||
| 285 | local _base_0 = { } | ||
| 286 | if _base_0.__index == nil then | ||
| 287 | _base_0.__index = _base_0 | ||
| 288 | end | ||
| 289 | _class_0 = setmetatable({ | ||
| 290 | __init = function() end, | ||
| 291 | __base = _base_0, | ||
| 292 | __name = "ClassB" | ||
| 293 | }, { | ||
| 294 | __index = _base_0, | ||
| 295 | __call = function(cls, ...) | ||
| 296 | local _self_0 = setmetatable({ }, _base_0) | ||
| 297 | cls.__init(_self_0, ...) | ||
| 298 | return _self_0 | ||
| 299 | end | ||
| 300 | }) | ||
| 301 | _base_0.__class = _class_0 | ||
| 302 | ClassB = _class_0 | ||
| 303 | end | ||
| 304 | local item = ClassA() | ||
| 305 | local result | ||
| 306 | do | ||
| 307 | local _type_0 = type(item) | ||
| 308 | local _tab_0 = "table" == _type_0 or "userdata" == _type_0 | ||
| 309 | local _match_0 = false | ||
| 310 | if _tab_0 then | ||
| 311 | ClassA = item.__class | ||
| 312 | if ClassA ~= nil then | ||
| 313 | _match_0 = true | ||
| 314 | result = "Object A" | ||
| 315 | end | ||
| 316 | end | ||
| 317 | if not _match_0 then | ||
| 318 | local _match_1 = false | ||
| 319 | if _tab_0 then | ||
| 320 | ClassB = item.__class | ||
| 321 | if ClassB ~= nil then | ||
| 322 | _match_1 = true | ||
| 323 | result = "Object B" | ||
| 324 | end | ||
| 325 | end | ||
| 326 | if not _match_1 then | ||
| 327 | result = "unknown" | ||
| 328 | end | ||
| 329 | end | ||
| 330 | end | ||
| 331 | return assert.same(result, "Object A") | ||
| 332 | end) | ||
| 333 | it("should match pattern with metatable", function() | ||
| 334 | local tb = setmetatable({ }, { | ||
| 335 | __mode = "v" | ||
| 336 | }) | ||
| 337 | local metatable_matched = false | ||
| 338 | do | ||
| 339 | local _type_0 = type(tb) | ||
| 340 | local _tab_0 = "table" == _type_0 or "userdata" == _type_0 | ||
| 341 | if _tab_0 then | ||
| 342 | local mt = getmetatable(tb) | ||
| 343 | if mt ~= nil then | ||
| 344 | metatable_matched = mt ~= nil | ||
| 345 | end | ||
| 346 | end | ||
| 347 | end | ||
| 348 | return assert.is_true(metatable_matched) | ||
| 349 | end) | ||
| 350 | it("should use switch as expression in assignment", function() | ||
| 351 | local tb = { | ||
| 352 | x = "abc" | ||
| 353 | } | ||
| 354 | local matched | ||
| 355 | if 1 == tb then | ||
| 356 | matched = "1" | ||
| 357 | else | ||
| 358 | do | ||
| 359 | local _type_0 = type(tb) | ||
| 360 | local _tab_0 = "table" == _type_0 or "userdata" == _type_0 | ||
| 361 | local _match_0 = false | ||
| 362 | if _tab_0 then | ||
| 363 | local x = tb.x | ||
| 364 | if x ~= nil then | ||
| 365 | _match_0 = true | ||
| 366 | matched = x | ||
| 367 | end | ||
| 368 | end | ||
| 369 | if not _match_0 then | ||
| 370 | if false == tb then | ||
| 371 | matched = "false" | ||
| 372 | else | ||
| 373 | matched = nil | ||
| 374 | end | ||
| 375 | end | ||
| 376 | end | ||
| 377 | end | ||
| 378 | return assert.same(matched, "abc") | ||
| 379 | end) | ||
| 380 | it("should use switch in return statement", function() | ||
| 381 | local fn | ||
| 382 | fn = function(tb) | ||
| 383 | if nil == tb then | ||
| 384 | return "invalid" | ||
| 385 | else | ||
| 386 | local _type_0 = type(tb) | ||
| 387 | local _tab_0 = "table" == _type_0 or "userdata" == _type_0 | ||
| 388 | local _match_0 = false | ||
| 389 | if _tab_0 then | ||
| 390 | local a = tb.a | ||
| 391 | local b = tb.b | ||
| 392 | if a ~= nil and b ~= nil then | ||
| 393 | _match_0 = true | ||
| 394 | return tostring(a + b) | ||
| 395 | end | ||
| 396 | end | ||
| 397 | if not _match_0 then | ||
| 398 | if 1 == tb or 2 == tb or 3 == tb or 4 == tb or 5 == tb then | ||
| 399 | return "number 1 - 5" | ||
| 400 | else | ||
| 401 | return "should not reach here" | ||
| 402 | end | ||
| 403 | end | ||
| 404 | end | ||
| 405 | end | ||
| 406 | assert.same(fn({ | ||
| 407 | a = 1, | ||
| 408 | b = 2 | ||
| 409 | }), "3") | ||
| 410 | assert.same(fn(3), "number 1 - 5") | ||
| 411 | return assert.same(fn(nil), "invalid") | ||
| 412 | end) | ||
| 413 | it("should support pattern matching assignment with :=", function() | ||
| 414 | local v = "hello" | ||
| 415 | local matched = false | ||
| 416 | do | ||
| 417 | v = "hello" | ||
| 418 | if "hello" == v then | ||
| 419 | matched = true | ||
| 420 | else | ||
| 421 | matched = false | ||
| 422 | end | ||
| 423 | end | ||
| 424 | assert.is_true(matched) | ||
| 425 | return assert.same(v, "hello") | ||
| 426 | end) | ||
| 427 | it("should match with computed expressions", function() | ||
| 428 | local hi = 4 | ||
| 429 | local matched = false | ||
| 430 | if (3 + 1) == hi or (function() | ||
| 431 | return 4 | ||
| 432 | end)() == hi or (5 - 1) == hi then | ||
| 433 | matched = true | ||
| 434 | end | ||
| 435 | return assert.is_true(matched) | ||
| 436 | end) | ||
| 437 | it("should handle nested array destructuring", function() | ||
| 438 | local tb = { | ||
| 439 | { | ||
| 440 | a = 1, | ||
| 441 | b = 2 | ||
| 442 | }, | ||
| 443 | { | ||
| 444 | a = 3, | ||
| 445 | b = 4 | ||
| 446 | }, | ||
| 447 | { | ||
| 448 | a = 5, | ||
| 449 | b = 6 | ||
| 450 | }, | ||
| 451 | "fourth" | ||
| 452 | } | ||
| 453 | local result | ||
| 454 | do | ||
| 455 | local _type_0 = type(tb) | ||
| 456 | local _tab_0 = "table" == _type_0 or "userdata" == _type_0 | ||
| 457 | local _match_0 = false | ||
| 458 | if _tab_0 then | ||
| 459 | local fourth = tb[4] | ||
| 460 | local _val_0 | ||
| 461 | do | ||
| 462 | local _obj_0 = tb[1] | ||
| 463 | if _obj_0 ~= nil then | ||
| 464 | _val_0 = _obj_0.a | ||
| 465 | end | ||
| 466 | end | ||
| 467 | local _val_1 | ||
| 468 | do | ||
| 469 | local _obj_0 = tb[1] | ||
| 470 | if _obj_0 ~= nil then | ||
| 471 | _val_1 = _obj_0.b | ||
| 472 | end | ||
| 473 | end | ||
| 474 | local _val_2 | ||
| 475 | do | ||
| 476 | local _obj_0 = tb[2] | ||
| 477 | if _obj_0 ~= nil then | ||
| 478 | _val_2 = _obj_0.a | ||
| 479 | end | ||
| 480 | end | ||
| 481 | local _val_3 | ||
| 482 | do | ||
| 483 | local _obj_0 = tb[2] | ||
| 484 | if _obj_0 ~= nil then | ||
| 485 | _val_3 = _obj_0.b | ||
| 486 | end | ||
| 487 | end | ||
| 488 | local _val_4 | ||
| 489 | do | ||
| 490 | local _obj_0 = tb[3] | ||
| 491 | if _obj_0 ~= nil then | ||
| 492 | _val_4 = _obj_0.a | ||
| 493 | end | ||
| 494 | end | ||
| 495 | local _val_5 | ||
| 496 | do | ||
| 497 | local _obj_0 = tb[3] | ||
| 498 | if _obj_0 ~= nil then | ||
| 499 | _val_5 = _obj_0.b | ||
| 500 | end | ||
| 501 | end | ||
| 502 | if 1 == _val_0 and 2 == _val_1 and 3 == _val_2 and 4 == _val_3 and 5 == _val_4 and 6 == _val_5 and fourth ~= nil then | ||
| 503 | _match_0 = true | ||
| 504 | result = fourth | ||
| 505 | end | ||
| 506 | end | ||
| 507 | if not _match_0 then | ||
| 508 | result = "no match" | ||
| 509 | end | ||
| 510 | end | ||
| 511 | return assert.same(result, "fourth") | ||
| 512 | end) | ||
| 513 | it("should match combined patterns", function() | ||
| 514 | local tb = { | ||
| 515 | success = true, | ||
| 516 | result = "data" | ||
| 517 | } | ||
| 518 | local result | ||
| 519 | do | ||
| 520 | local _type_0 = type(tb) | ||
| 521 | local _tab_0 = "table" == _type_0 or "userdata" == _type_0 | ||
| 522 | local _match_0 = false | ||
| 523 | if _tab_0 then | ||
| 524 | result = tb.result | ||
| 525 | if true == tb.success and result ~= nil then | ||
| 526 | _match_0 = true | ||
| 527 | result = { | ||
| 528 | "success", | ||
| 529 | result | ||
| 530 | } | ||
| 531 | end | ||
| 532 | end | ||
| 533 | if not _match_0 then | ||
| 534 | local _match_1 = false | ||
| 535 | if _tab_0 then | ||
| 536 | if false == tb.success then | ||
| 537 | _match_1 = true | ||
| 538 | result = { | ||
| 539 | "failed", | ||
| 540 | result | ||
| 541 | } | ||
| 542 | end | ||
| 543 | end | ||
| 544 | if not _match_1 then | ||
| 545 | result = { | ||
| 546 | "invalid" | ||
| 547 | } | ||
| 548 | end | ||
| 549 | end | ||
| 550 | end | ||
| 551 | return assert.same(result, { | ||
| 552 | "success", | ||
| 553 | "data" | ||
| 554 | }) | ||
| 555 | end) | ||
| 556 | it("should match type discriminated patterns", function() | ||
| 557 | local tb = { | ||
| 558 | type = "success", | ||
| 559 | content = "data" | ||
| 560 | } | ||
| 561 | local result | ||
| 562 | do | ||
| 563 | local _type_0 = type(tb) | ||
| 564 | local _tab_0 = "table" == _type_0 or "userdata" == _type_0 | ||
| 565 | local _match_0 = false | ||
| 566 | if _tab_0 then | ||
| 567 | local content = tb.content | ||
| 568 | if "success" == tb.type and content ~= nil then | ||
| 569 | _match_0 = true | ||
| 570 | result = { | ||
| 571 | "success", | ||
| 572 | content | ||
| 573 | } | ||
| 574 | end | ||
| 575 | end | ||
| 576 | if not _match_0 then | ||
| 577 | local _match_1 = false | ||
| 578 | if _tab_0 then | ||
| 579 | local content = tb.content | ||
| 580 | if "error" == tb.type and content ~= nil then | ||
| 581 | _match_1 = true | ||
| 582 | result = { | ||
| 583 | "error", | ||
| 584 | content | ||
| 585 | } | ||
| 586 | end | ||
| 587 | end | ||
| 588 | if not _match_1 then | ||
| 589 | result = { | ||
| 590 | "invalid" | ||
| 591 | } | ||
| 592 | end | ||
| 593 | end | ||
| 594 | end | ||
| 595 | return assert.same(result, { | ||
| 596 | "success", | ||
| 597 | "data" | ||
| 598 | }) | ||
| 599 | end) | ||
| 600 | it("should match with wildcard array capture", function() | ||
| 601 | local clientData = { | ||
| 602 | "Meta", | ||
| 603 | "CUST_1001", | ||
| 604 | "CHK123" | ||
| 605 | } | ||
| 606 | local metadata = nil | ||
| 607 | local customerId = nil | ||
| 608 | local checksum = nil | ||
| 609 | do | ||
| 610 | local _type_0 = type(clientData) | ||
| 611 | local _tab_0 = "table" == _type_0 or "userdata" == _type_0 | ||
| 612 | if _tab_0 then | ||
| 613 | local capturedMetadata | ||
| 614 | do | ||
| 615 | local _accum_0 = { } | ||
| 616 | local _len_0 = 1 | ||
| 617 | local _max_0 = #clientData + -3 + 1 | ||
| 618 | for _index_0 = 1, _max_0 do | ||
| 619 | local _item_0 = clientData[_index_0] | ||
| 620 | _accum_0[_len_0] = _item_0 | ||
| 621 | _len_0 = _len_0 + 1 | ||
| 622 | end | ||
| 623 | capturedMetadata = _accum_0 | ||
| 624 | end | ||
| 625 | customerId = clientData[#clientData - 1] | ||
| 626 | checksum = clientData[#clientData] | ||
| 627 | if customerId ~= nil and checksum ~= nil then | ||
| 628 | metadata = capturedMetadata | ||
| 629 | end | ||
| 630 | end | ||
| 631 | end | ||
| 632 | assert.same(metadata, { | ||
| 633 | "Meta" | ||
| 634 | }) | ||
| 635 | assert.same(customerId, "CUST_1001") | ||
| 636 | return assert.same(checksum, "CHK123") | ||
| 637 | end) | ||
| 638 | it("should work with complex tuple patterns", function() | ||
| 639 | local handlePath | ||
| 640 | handlePath = function(segments) | ||
| 641 | local _type_0 = type(segments) | ||
| 642 | local _tab_0 = "table" == _type_0 or "userdata" == _type_0 | ||
| 643 | local _match_0 = false | ||
| 644 | if _tab_0 then | ||
| 645 | local resource = segments[#segments - 1] | ||
| 646 | local action = segments[#segments] | ||
| 647 | if resource ~= nil and action ~= nil then | ||
| 648 | _match_0 = true | ||
| 649 | return { | ||
| 650 | "Resource: " .. tostring(resource), | ||
| 651 | "Action: " .. tostring(action) | ||
| 652 | } | ||
| 653 | end | ||
| 654 | end | ||
| 655 | if not _match_0 then | ||
| 656 | return { | ||
| 657 | "no match" | ||
| 658 | } | ||
| 659 | end | ||
| 660 | end | ||
| 661 | local result = handlePath({ | ||
| 662 | "admin", | ||
| 663 | "logs", | ||
| 664 | "view" | ||
| 665 | }) | ||
| 666 | return assert.same(result, { | ||
| 667 | "Resource: logs", | ||
| 668 | "Action: view" | ||
| 669 | }) | ||
| 670 | end) | ||
| 671 | it("should match boolean false correctly", function() | ||
| 672 | local items = { | ||
| 673 | { | ||
| 674 | x = 100, | ||
| 675 | y = 200 | ||
| 676 | }, | ||
| 677 | { | ||
| 678 | width = 300, | ||
| 679 | height = 400 | ||
| 680 | }, | ||
| 681 | false | ||
| 682 | } | ||
| 683 | local results = { } | ||
| 684 | for _index_0 = 1, #items do | ||
| 685 | local item = items[_index_0] | ||
| 686 | local _type_0 = type(item) | ||
| 687 | local _tab_0 = "table" == _type_0 or "userdata" == _type_0 | ||
| 688 | local _match_0 = false | ||
| 689 | if _tab_0 then | ||
| 690 | local x = item.x | ||
| 691 | local y = item.y | ||
| 692 | if x ~= nil and y ~= nil then | ||
| 693 | _match_0 = true | ||
| 694 | table.insert(results, "Vec2") | ||
| 695 | end | ||
| 696 | end | ||
| 697 | if not _match_0 then | ||
| 698 | local _match_1 = false | ||
| 699 | if _tab_0 then | ||
| 700 | local width = item.width | ||
| 701 | local height = item.height | ||
| 702 | if width ~= nil and height ~= nil then | ||
| 703 | _match_1 = true | ||
| 704 | table.insert(results, "Size") | ||
| 705 | end | ||
| 706 | end | ||
| 707 | if not _match_1 then | ||
| 708 | if false == item then | ||
| 709 | table.insert(results, "None") | ||
| 710 | end | ||
| 711 | end | ||
| 712 | end | ||
| 713 | end | ||
| 714 | return assert.same(results, { | ||
| 715 | "Vec2", | ||
| 716 | "Size", | ||
| 717 | "None" | ||
| 718 | }) | ||
| 719 | end) | ||
| 720 | it("should handle switch with then syntax", function() | ||
| 721 | local value = "cool" | ||
| 722 | local result | ||
| 723 | if "cool" == value then | ||
| 724 | result = "matched cool" | ||
| 725 | else | ||
| 726 | result = "else branch" | ||
| 727 | end | ||
| 728 | return assert.same(result, "matched cool") | ||
| 729 | end) | ||
| 730 | return it("should handle switch in function call", function() | ||
| 731 | local something = 1 | ||
| 732 | local getValue | ||
| 733 | getValue = function() | ||
| 734 | if 1 == something then | ||
| 735 | return "yes" | ||
| 736 | else | ||
| 737 | return "no" | ||
| 738 | end | ||
| 739 | end | ||
| 740 | return assert.same(getValue(), "yes") | ||
| 741 | end) | ||
| 742 | end) | ||
