diff options
Diffstat (limited to 'spec/inputs/test')
| -rw-r--r-- | spec/inputs/test/switch_spec.yue | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/spec/inputs/test/switch_spec.yue b/spec/inputs/test/switch_spec.yue index 0865e5a..b7aca5b 100644 --- a/spec/inputs/test/switch_spec.yue +++ b/spec/inputs/test/switch_spec.yue | |||
| @@ -265,3 +265,86 @@ describe "switch", -> | |||
| 265 | when 1 then "yes" | 265 | when 1 then "yes" |
| 266 | else "no" | 266 | else "no" |
| 267 | assert.same getValue!, "yes" | 267 | assert.same getValue!, "yes" |
| 268 | |||
| 269 | it "should match empty list pattern with []", -> | ||
| 270 | -- [] uses #x == 0 check, matches tables with no array elements | ||
| 271 | emptyArray = {} | ||
| 272 | hashTable = {a: 1, b: 2} | ||
| 273 | arrayWithElements = {1, 2, 3} | ||
| 274 | |||
| 275 | result1 = switch emptyArray | ||
| 276 | when [] then "empty list" | ||
| 277 | else "not empty list" | ||
| 278 | assert.same result1, "empty list" | ||
| 279 | |||
| 280 | result2 = switch hashTable | ||
| 281 | when [] then "empty list" | ||
| 282 | else "not empty list" | ||
| 283 | assert.same result2, "empty list" | ||
| 284 | |||
| 285 | result3 = switch arrayWithElements | ||
| 286 | when [] then "empty list" | ||
| 287 | else "not empty list" | ||
| 288 | assert.same result3, "not empty list" | ||
| 289 | |||
| 290 | it "should match empty table pattern with {}", -> | ||
| 291 | -- {} uses next(x) == nil check, matches only truly empty tables | ||
| 292 | emptyTable = {} | ||
| 293 | hashTable = {a: 1} | ||
| 294 | arrayTable = {1} | ||
| 295 | |||
| 296 | result1 = switch emptyTable | ||
| 297 | when {} then "empty table" | ||
| 298 | else "not empty table" | ||
| 299 | assert.same result1, "empty table" | ||
| 300 | |||
| 301 | result2 = switch hashTable | ||
| 302 | when {} then "empty table" | ||
| 303 | else "not empty table" | ||
| 304 | assert.same result2, "not empty table" | ||
| 305 | |||
| 306 | result3 = switch arrayTable | ||
| 307 | when {} then "empty table" | ||
| 308 | else "not empty table" | ||
| 309 | assert.same result3, "not empty table" | ||
| 310 | |||
| 311 | it "should distinguish between [] and {} patterns", -> | ||
| 312 | -- [] matches tables with #x == 0 (no array part) | ||
| 313 | -- {} matches tables with next(x) == nil (completely empty) | ||
| 314 | classify = (x) -> | ||
| 315 | switch x | ||
| 316 | when {} then "truly empty" | ||
| 317 | when [] then "no array elements" | ||
| 318 | else "has elements" | ||
| 319 | |||
| 320 | assert.same classify({}), "truly empty" | ||
| 321 | assert.same classify({a: 1}), "no array elements" | ||
| 322 | assert.same classify({1, 2}), "has elements" | ||
| 323 | |||
| 324 | it "should handle empty list pattern with then syntax", -> | ||
| 325 | result = switch {} | ||
| 326 | when [] then "matched" | ||
| 327 | else "not matched" | ||
| 328 | assert.same result, "matched" | ||
| 329 | |||
| 330 | it "should handle empty table pattern with then syntax", -> | ||
| 331 | result = switch {} | ||
| 332 | when {} then "matched" | ||
| 333 | else "not matched" | ||
| 334 | assert.same result, "matched" | ||
| 335 | |||
| 336 | it "should match empty patterns with multiple when branches", -> | ||
| 337 | emptyArray = {} | ||
| 338 | emptyHash = {} | ||
| 339 | |||
| 340 | result1 = switch emptyArray | ||
| 341 | when {1} then "has 1" | ||
| 342 | when [] then "empty list" | ||
| 343 | else "other" | ||
| 344 | assert.same result1, "empty list" | ||
| 345 | |||
| 346 | result2 = switch emptyHash | ||
| 347 | when {a: 1} then "has a" | ||
| 348 | when {} then "empty table" | ||
| 349 | else "other" | ||
| 350 | assert.same result2, "empty table" | ||
