diff options
| author | Li Jin <dragon-fly@qq.com> | 2026-03-26 16:19:50 +0800 |
|---|---|---|
| committer | Li Jin <dragon-fly@qq.com> | 2026-03-26 16:19:50 +0800 |
| commit | bc1a80badfb734fb9695ee4831e25cb68b8e7765 (patch) | |
| tree | c7bd56bf33c4d141f0a361cfaf764810632c3da3 | |
| parent | df262171918e6ecc241bff6cfcf81b097ec00533 (diff) | |
| download | yuescript-feature/ast-node-end-position.tar.gz yuescript-feature/ast-node-end-position.tar.bz2 yuescript-feature/ast-node-end-position.zip | |
test: add test cases for empty table pattern matchingfeature/ast-node-end-position
- Test [] pattern matches tables with #x == 0
- Test {} pattern matches tables with next(x) == nil
- Test distinguishing between [] and {} patterns
- Test empty patterns with then syntax
- Test empty patterns with multiple when branches
Related to #249
Diffstat (limited to '')
| -rw-r--r-- | spec/inputs/test/switch_spec.yue | 83 | ||||
| -rw-r--r-- | spec/outputs/test/switch_spec.lua | 245 |
2 files changed, 327 insertions, 1 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" | ||
diff --git a/spec/outputs/test/switch_spec.lua b/spec/outputs/test/switch_spec.lua index 59ffca5..1f436b8 100644 --- a/spec/outputs/test/switch_spec.lua +++ b/spec/outputs/test/switch_spec.lua | |||
| @@ -727,7 +727,7 @@ return describe("switch", function() | |||
| 727 | end | 727 | end |
| 728 | return assert.same(result, "matched cool") | 728 | return assert.same(result, "matched cool") |
| 729 | end) | 729 | end) |
| 730 | return it("should handle switch in function call", function() | 730 | it("should handle switch in function call", function() |
| 731 | local something = 1 | 731 | local something = 1 |
| 732 | local getValue | 732 | local getValue |
| 733 | getValue = function() | 733 | getValue = function() |
| @@ -739,4 +739,247 @@ return describe("switch", function() | |||
| 739 | end | 739 | end |
| 740 | return assert.same(getValue(), "yes") | 740 | return assert.same(getValue(), "yes") |
| 741 | end) | 741 | end) |
| 742 | it("should match empty list pattern with []", function() | ||
| 743 | local emptyArray = { } | ||
| 744 | local hashTable = { | ||
| 745 | a = 1, | ||
| 746 | b = 2 | ||
| 747 | } | ||
| 748 | local arrayWithElements = { | ||
| 749 | 1, | ||
| 750 | 2, | ||
| 751 | 3 | ||
| 752 | } | ||
| 753 | local result1 | ||
| 754 | do | ||
| 755 | local _type_0 = type(emptyArray) | ||
| 756 | local _tab_0 = "table" == _type_0 or "userdata" == _type_0 | ||
| 757 | local _match_0 = false | ||
| 758 | if _tab_0 then | ||
| 759 | if #emptyArray == 0 then | ||
| 760 | _match_0 = true | ||
| 761 | result1 = "empty list" | ||
| 762 | end | ||
| 763 | end | ||
| 764 | if not _match_0 then | ||
| 765 | result1 = "not empty list" | ||
| 766 | end | ||
| 767 | end | ||
| 768 | assert.same(result1, "empty list") | ||
| 769 | local result2 | ||
| 770 | do | ||
| 771 | local _type_0 = type(hashTable) | ||
| 772 | local _tab_0 = "table" == _type_0 or "userdata" == _type_0 | ||
| 773 | local _match_0 = false | ||
| 774 | if _tab_0 then | ||
| 775 | if #hashTable == 0 then | ||
| 776 | _match_0 = true | ||
| 777 | result2 = "empty list" | ||
| 778 | end | ||
| 779 | end | ||
| 780 | if not _match_0 then | ||
| 781 | result2 = "not empty list" | ||
| 782 | end | ||
| 783 | end | ||
| 784 | assert.same(result2, "empty list") | ||
| 785 | local result3 | ||
| 786 | do | ||
| 787 | local _type_0 = type(arrayWithElements) | ||
| 788 | local _tab_0 = "table" == _type_0 or "userdata" == _type_0 | ||
| 789 | local _match_0 = false | ||
| 790 | if _tab_0 then | ||
| 791 | if #arrayWithElements == 0 then | ||
| 792 | _match_0 = true | ||
| 793 | result3 = "empty list" | ||
| 794 | end | ||
| 795 | end | ||
| 796 | if not _match_0 then | ||
| 797 | result3 = "not empty list" | ||
| 798 | end | ||
| 799 | end | ||
| 800 | return assert.same(result3, "not empty list") | ||
| 801 | end) | ||
| 802 | it("should match empty table pattern with {}", function() | ||
| 803 | local emptyTable = { } | ||
| 804 | local hashTable = { | ||
| 805 | a = 1 | ||
| 806 | } | ||
| 807 | local arrayTable = { | ||
| 808 | 1 | ||
| 809 | } | ||
| 810 | local result1 | ||
| 811 | do | ||
| 812 | local _type_0 = type(emptyTable) | ||
| 813 | local _tab_0 = "table" == _type_0 or "userdata" == _type_0 | ||
| 814 | local _match_0 = false | ||
| 815 | if _tab_0 then | ||
| 816 | if next(emptyTable) == nil then | ||
| 817 | _match_0 = true | ||
| 818 | result1 = "empty table" | ||
| 819 | end | ||
| 820 | end | ||
| 821 | if not _match_0 then | ||
| 822 | result1 = "not empty table" | ||
| 823 | end | ||
| 824 | end | ||
| 825 | assert.same(result1, "empty table") | ||
| 826 | local result2 | ||
| 827 | do | ||
| 828 | local _type_0 = type(hashTable) | ||
| 829 | local _tab_0 = "table" == _type_0 or "userdata" == _type_0 | ||
| 830 | local _match_0 = false | ||
| 831 | if _tab_0 then | ||
| 832 | if next(hashTable) == nil then | ||
| 833 | _match_0 = true | ||
| 834 | result2 = "empty table" | ||
| 835 | end | ||
| 836 | end | ||
| 837 | if not _match_0 then | ||
| 838 | result2 = "not empty table" | ||
| 839 | end | ||
| 840 | end | ||
| 841 | assert.same(result2, "not empty table") | ||
| 842 | local result3 | ||
| 843 | do | ||
| 844 | local _type_0 = type(arrayTable) | ||
| 845 | local _tab_0 = "table" == _type_0 or "userdata" == _type_0 | ||
| 846 | local _match_0 = false | ||
| 847 | if _tab_0 then | ||
| 848 | if next(arrayTable) == nil then | ||
| 849 | _match_0 = true | ||
| 850 | result3 = "empty table" | ||
| 851 | end | ||
| 852 | end | ||
| 853 | if not _match_0 then | ||
| 854 | result3 = "not empty table" | ||
| 855 | end | ||
| 856 | end | ||
| 857 | return assert.same(result3, "not empty table") | ||
| 858 | end) | ||
| 859 | it("should distinguish between [] and {} patterns", function() | ||
| 860 | local classify | ||
| 861 | classify = function(x) | ||
| 862 | local _type_0 = type(x) | ||
| 863 | local _tab_0 = "table" == _type_0 or "userdata" == _type_0 | ||
| 864 | local _match_0 = false | ||
| 865 | if _tab_0 then | ||
| 866 | if next(x) == nil then | ||
| 867 | _match_0 = true | ||
| 868 | return "truly empty" | ||
| 869 | end | ||
| 870 | end | ||
| 871 | if not _match_0 then | ||
| 872 | local _match_1 = false | ||
| 873 | if _tab_0 then | ||
| 874 | if #x == 0 then | ||
| 875 | _match_1 = true | ||
| 876 | return "no array elements" | ||
| 877 | end | ||
| 878 | end | ||
| 879 | if not _match_1 then | ||
| 880 | return "has elements" | ||
| 881 | end | ||
| 882 | end | ||
| 883 | end | ||
| 884 | assert.same(classify({ }), "truly empty") | ||
| 885 | assert.same(classify({ | ||
| 886 | a = 1 | ||
| 887 | }), "no array elements") | ||
| 888 | return assert.same(classify({ | ||
| 889 | 1, | ||
| 890 | 2 | ||
| 891 | }), "has elements") | ||
| 892 | end) | ||
| 893 | it("should handle empty list pattern with then syntax", function() | ||
| 894 | local result | ||
| 895 | do | ||
| 896 | local _exp_0 = { } | ||
| 897 | local _type_0 = type(_exp_0) | ||
| 898 | local _tab_0 = "table" == _type_0 or "userdata" == _type_0 | ||
| 899 | local _match_0 = false | ||
| 900 | if _tab_0 then | ||
| 901 | if #_exp_0 == 0 then | ||
| 902 | _match_0 = true | ||
| 903 | result = "matched" | ||
| 904 | end | ||
| 905 | end | ||
| 906 | if not _match_0 then | ||
| 907 | result = "not matched" | ||
| 908 | end | ||
| 909 | end | ||
| 910 | return assert.same(result, "matched") | ||
| 911 | end) | ||
| 912 | it("should handle empty table pattern with then syntax", function() | ||
| 913 | local result | ||
| 914 | do | ||
| 915 | local _exp_0 = { } | ||
| 916 | local _type_0 = type(_exp_0) | ||
| 917 | local _tab_0 = "table" == _type_0 or "userdata" == _type_0 | ||
| 918 | local _match_0 = false | ||
| 919 | if _tab_0 then | ||
| 920 | if next(_exp_0) == nil then | ||
| 921 | _match_0 = true | ||
| 922 | result = "matched" | ||
| 923 | end | ||
| 924 | end | ||
| 925 | if not _match_0 then | ||
| 926 | result = "not matched" | ||
| 927 | end | ||
| 928 | end | ||
| 929 | return assert.same(result, "matched") | ||
| 930 | end) | ||
| 931 | return it("should match empty patterns with multiple when branches", function() | ||
| 932 | local emptyArray = { } | ||
| 933 | local emptyHash = { } | ||
| 934 | local result1 | ||
| 935 | do | ||
| 936 | local _type_0 = type(emptyArray) | ||
| 937 | local _tab_0 = "table" == _type_0 or "userdata" == _type_0 | ||
| 938 | local _match_0 = false | ||
| 939 | if _tab_0 then | ||
| 940 | if 1 == emptyArray[1] then | ||
| 941 | _match_0 = true | ||
| 942 | result1 = "has 1" | ||
| 943 | end | ||
| 944 | end | ||
| 945 | if not _match_0 then | ||
| 946 | local _match_1 = false | ||
| 947 | if _tab_0 then | ||
| 948 | if #emptyArray == 0 then | ||
| 949 | _match_1 = true | ||
| 950 | result1 = "empty list" | ||
| 951 | end | ||
| 952 | end | ||
| 953 | if not _match_1 then | ||
| 954 | result1 = "other" | ||
| 955 | end | ||
| 956 | end | ||
| 957 | end | ||
| 958 | assert.same(result1, "empty list") | ||
| 959 | local result2 | ||
| 960 | do | ||
| 961 | local _type_0 = type(emptyHash) | ||
| 962 | local _tab_0 = "table" == _type_0 or "userdata" == _type_0 | ||
| 963 | local _match_0 = false | ||
| 964 | if _tab_0 then | ||
| 965 | if 1 == emptyHash.a then | ||
| 966 | _match_0 = true | ||
| 967 | result2 = "has a" | ||
| 968 | end | ||
| 969 | end | ||
| 970 | if not _match_0 then | ||
| 971 | local _match_1 = false | ||
| 972 | if _tab_0 then | ||
| 973 | if next(emptyHash) == nil then | ||
| 974 | _match_1 = true | ||
| 975 | result2 = "empty table" | ||
| 976 | end | ||
| 977 | end | ||
| 978 | if not _match_1 then | ||
| 979 | result2 = "other" | ||
| 980 | end | ||
| 981 | end | ||
| 982 | end | ||
| 983 | return assert.same(result2, "empty table") | ||
| 984 | end) | ||
| 742 | end) | 985 | end) |
