diff options
| author | Li Jin <dragon-fly@qq.com> | 2026-02-12 09:08:12 +0800 |
|---|---|---|
| committer | Li Jin <dragon-fly@qq.com> | 2026-02-12 09:08:12 +0800 |
| commit | 640dcd82b30629f54596820c09144dfff10cb2c0 (patch) | |
| tree | e6c1822f459b8afc64d06a86459cf676ea7c74c2 | |
| parent | 4e7edb2bd615e0a86b593c56c3392fab2110b1a1 (diff) | |
| download | yuescript-640dcd82b30629f54596820c09144dfff10cb2c0.tar.gz yuescript-640dcd82b30629f54596820c09144dfff10cb2c0.tar.bz2 yuescript-640dcd82b30629f54596820c09144dfff10cb2c0.zip | |
Added more tests.
| -rw-r--r-- | spec/inputs/test/break_multiple_values_spec.yue | 19 | ||||
| -rw-r--r-- | spec/inputs/test/format_spec.yue | 7 | ||||
| -rw-r--r-- | spec/outputs/test/break_multiple_values_spec.lua | 51 | ||||
| -rw-r--r-- | spec/outputs/test/format_spec.lua | 8 | ||||
| -rw-r--r-- | src/yuescript/yue_compiler.cpp | 4 |
5 files changed, 80 insertions, 9 deletions
diff --git a/spec/inputs/test/break_multiple_values_spec.yue b/spec/inputs/test/break_multiple_values_spec.yue index 846be00..053008d 100644 --- a/spec/inputs/test/break_multiple_values_spec.yue +++ b/spec/inputs/test/break_multiple_values_spec.yue | |||
| @@ -838,3 +838,22 @@ describe "break with multiple values", -> | |||
| 838 | break | 838 | break |
| 839 | assert.same x, 5 | 839 | assert.same x, 5 |
| 840 | assert.same y, 15 | 840 | assert.same y, 15 |
| 841 | |||
| 842 | it "should allow nesting do and for", -> | ||
| 843 | x, y = do | ||
| 844 | min, max = 1, 10 | ||
| 845 | if max > min | ||
| 846 | break for j = min, max | ||
| 847 | break j, j * 10 if j > 5 | ||
| 848 | break 0, 0 | ||
| 849 | assert.same x, 6 | ||
| 850 | assert.same y, 60 | ||
| 851 | |||
| 852 | it "should allow nesting do and with", -> | ||
| 853 | x = with a: 123, b: true | ||
| 854 | do | ||
| 855 | if .b | ||
| 856 | break with a: .a, b: .b, c: "ok" | ||
| 857 | if .b and .c == "ok" | ||
| 858 | break .a | ||
| 859 | assert.same x, 123 | ||
diff --git a/spec/inputs/test/format_spec.yue b/spec/inputs/test/format_spec.yue index afe9331..8c6096a 100644 --- a/spec/inputs/test/format_spec.yue +++ b/spec/inputs/test/format_spec.yue | |||
| @@ -113,6 +113,7 @@ files = [ | |||
| 113 | "spec/inputs/test/loops_spec.yue" | 113 | "spec/inputs/test/loops_spec.yue" |
| 114 | "spec/inputs/test/if_assignment_spec.yue" | 114 | "spec/inputs/test/if_assignment_spec.yue" |
| 115 | "spec/inputs/test/tables_advanced_spec.yue" | 115 | "spec/inputs/test/tables_advanced_spec.yue" |
| 116 | "spec/inputs/test/break_multiple_values_spec.yue" | ||
| 116 | "spec/inputs/unicode/macro_export.yue" | 117 | "spec/inputs/unicode/macro_export.yue" |
| 117 | "spec/inputs/unicode/attrib.yue" | 118 | "spec/inputs/unicode/attrib.yue" |
| 118 | "spec/inputs/unicode/macro.yue" | 119 | "spec/inputs/unicode/macro.yue" |
| @@ -179,11 +180,13 @@ for file in *files | |||
| 179 | code = f\read "a*" | 180 | code = f\read "a*" |
| 180 | f\close! | 181 | f\close! |
| 181 | 182 | ||
| 182 | original_ast = yue.to_ast code | 183 | original_ast, err = yue.to_ast code |
| 184 | assert.is_nil err | ||
| 183 | assert.is_not_nil original_ast | 185 | assert.is_not_nil original_ast |
| 184 | rewriteLineCol original_ast | 186 | rewriteLineCol original_ast |
| 185 | formated = yue.format code, 0, true | 187 | formated = yue.format code, 0, true |
| 186 | ast = yue.to_ast formated | 188 | ast, err = yue.to_ast formated |
| 189 | assert.is_nil err | ||
| 187 | assert.is_not_nil ast | 190 | assert.is_not_nil ast |
| 188 | rewriteLineCol ast | 191 | rewriteLineCol ast |
| 189 | assert.same original_ast, ast | 192 | assert.same original_ast, ast |
diff --git a/spec/outputs/test/break_multiple_values_spec.lua b/spec/outputs/test/break_multiple_values_spec.lua index ba14a64..7a8bc87 100644 --- a/spec/outputs/test/break_multiple_values_spec.lua +++ b/spec/outputs/test/break_multiple_values_spec.lua | |||
| @@ -1676,7 +1676,7 @@ return describe("break with multiple values", function() | |||
| 1676 | assert.is_nil(x) | 1676 | assert.is_nil(x) |
| 1677 | return assert.is_nil(y) | 1677 | return assert.is_nil(y) |
| 1678 | end) | 1678 | end) |
| 1679 | return it("should mix break continue and value break with value break winning", function() | 1679 | it("should mix break continue and value break with value break winning", function() |
| 1680 | local x, y | 1680 | local x, y |
| 1681 | for i = 1, 9 do | 1681 | for i = 1, 9 do |
| 1682 | if i % 2 == 0 then | 1682 | if i % 2 == 0 then |
| @@ -1694,4 +1694,53 @@ return describe("break with multiple values", function() | |||
| 1694 | assert.same(x, 5) | 1694 | assert.same(x, 5) |
| 1695 | return assert.same(y, 15) | 1695 | return assert.same(y, 15) |
| 1696 | end) | 1696 | end) |
| 1697 | it("should allow nesting do and for", function() | ||
| 1698 | local x, y | ||
| 1699 | do | ||
| 1700 | repeat | ||
| 1701 | local min, max = 1, 10 | ||
| 1702 | if max > min then | ||
| 1703 | for j = min, max do | ||
| 1704 | if j > 5 then | ||
| 1705 | x, y = j, j * 10 | ||
| 1706 | break | ||
| 1707 | end | ||
| 1708 | end | ||
| 1709 | break | ||
| 1710 | end | ||
| 1711 | x, y = 0, 0 | ||
| 1712 | break | ||
| 1713 | until true | ||
| 1714 | end | ||
| 1715 | assert.same(x, 6) | ||
| 1716 | return assert.same(y, 60) | ||
| 1717 | end) | ||
| 1718 | return it("should allow nesting do and with", function() | ||
| 1719 | local x | ||
| 1720 | do | ||
| 1721 | local _with_0 = { | ||
| 1722 | a = 123, | ||
| 1723 | b = true | ||
| 1724 | } | ||
| 1725 | repeat | ||
| 1726 | do | ||
| 1727 | if _with_0.b then | ||
| 1728 | local _with_1 = { | ||
| 1729 | a = _with_0.a, | ||
| 1730 | b = _with_0.b, | ||
| 1731 | c = "ok" | ||
| 1732 | } | ||
| 1733 | repeat | ||
| 1734 | if _with_1.b and _with_1.c == "ok" then | ||
| 1735 | x = _with_1.a | ||
| 1736 | break | ||
| 1737 | end | ||
| 1738 | until true | ||
| 1739 | break | ||
| 1740 | end | ||
| 1741 | end | ||
| 1742 | until true | ||
| 1743 | end | ||
| 1744 | return assert.same(x, 123) | ||
| 1745 | end) | ||
| 1697 | end) | 1746 | end) |
diff --git a/spec/outputs/test/format_spec.lua b/spec/outputs/test/format_spec.lua index 3e98e4a..c9ea3c2 100644 --- a/spec/outputs/test/format_spec.lua +++ b/spec/outputs/test/format_spec.lua | |||
| @@ -113,6 +113,7 @@ local files = { | |||
| 113 | "spec/inputs/test/loops_spec.yue", | 113 | "spec/inputs/test/loops_spec.yue", |
| 114 | "spec/inputs/test/if_assignment_spec.yue", | 114 | "spec/inputs/test/if_assignment_spec.yue", |
| 115 | "spec/inputs/test/tables_advanced_spec.yue", | 115 | "spec/inputs/test/tables_advanced_spec.yue", |
| 116 | "spec/inputs/test/break_multiple_values_spec.yue", | ||
| 116 | "spec/inputs/unicode/macro_export.yue", | 117 | "spec/inputs/unicode/macro_export.yue", |
| 117 | "spec/inputs/unicode/attrib.yue", | 118 | "spec/inputs/unicode/attrib.yue", |
| 118 | "spec/inputs/unicode/macro.yue", | 119 | "spec/inputs/unicode/macro.yue", |
| @@ -182,11 +183,14 @@ return describe("format", function() | |||
| 182 | local f = io.open(file) | 183 | local f = io.open(file) |
| 183 | local code = f:read("a*") | 184 | local code = f:read("a*") |
| 184 | f:close() | 185 | f:close() |
| 185 | local original_ast = yue.to_ast(code) | 186 | local original_ast, err = yue.to_ast(code) |
| 187 | assert.is_nil(err) | ||
| 186 | assert.is_not_nil(original_ast) | 188 | assert.is_not_nil(original_ast) |
| 187 | rewriteLineCol(original_ast) | 189 | rewriteLineCol(original_ast) |
| 188 | local formated = yue.format(code, 0, true) | 190 | local formated = yue.format(code, 0, true) |
| 189 | local ast = yue.to_ast(formated) | 191 | local ast |
| 192 | ast, err = yue.to_ast(formated) | ||
| 193 | assert.is_nil(err) | ||
| 190 | assert.is_not_nil(ast) | 194 | assert.is_not_nil(ast) |
| 191 | rewriteLineCol(ast) | 195 | rewriteLineCol(ast) |
| 192 | return assert.same(original_ast, ast) | 196 | return assert.same(original_ast, ast) |
diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp index caeee53..2615a2b 100644 --- a/src/yuescript/yue_compiler.cpp +++ b/src/yuescript/yue_compiler.cpp | |||
| @@ -8948,10 +8948,6 @@ private: | |||
| 8948 | addToScope(var); | 8948 | addToScope(var); |
| 8949 | } | 8949 | } |
| 8950 | popScope(); | 8950 | popScope(); |
| 8951 | } else { | ||
| 8952 | if (breakLoop->valueList->exprs.size() != breakWithValues.size()) { | ||
| 8953 | throw CompileError("expecting "s + std::to_string(breakWithValues.size()) + " break values, got "s + std::to_string(breakLoop->valueList->exprs.size()), breakLoop->valueList->exprs.front()); | ||
| 8954 | } | ||
| 8955 | } | 8951 | } |
| 8956 | breakLoop->vars = breakWithValues; | 8952 | breakLoop->vars = breakWithValues; |
| 8957 | } else { | 8953 | } else { |
