Skip to content

Commit 640dcd8

Browse files
committed
Added more tests.
1 parent 4e7edb2 commit 640dcd8

File tree

5 files changed

+80
-9
lines changed

5 files changed

+80
-9
lines changed

spec/inputs/test/break_multiple_values_spec.yue

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -838,3 +838,22 @@ describe "break with multiple values", ->
838838
break
839839
assert.same x, 5
840840
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

spec/inputs/test/format_spec.yue

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ files = [
113113
"spec/inputs/test/loops_spec.yue"
114114
"spec/inputs/test/if_assignment_spec.yue"
115115
"spec/inputs/test/tables_advanced_spec.yue"
116+
"spec/inputs/test/break_multiple_values_spec.yue"
116117
"spec/inputs/unicode/macro_export.yue"
117118
"spec/inputs/unicode/attrib.yue"
118119
"spec/inputs/unicode/macro.yue"
@@ -179,11 +180,13 @@ for file in *files
179180
code = f\read "a*"
180181
f\close!
181182

182-
original_ast = yue.to_ast code
183+
original_ast, err = yue.to_ast code
184+
assert.is_nil err
183185
assert.is_not_nil original_ast
184186
rewriteLineCol original_ast
185187
formated = yue.format code, 0, true
186-
ast = yue.to_ast formated
188+
ast, err = yue.to_ast formated
189+
assert.is_nil err
187190
assert.is_not_nil ast
188191
rewriteLineCol ast
189192
assert.same original_ast, ast

spec/outputs/test/break_multiple_values_spec.lua

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1676,7 +1676,7 @@ return describe("break with multiple values", function()
16761676
assert.is_nil(x)
16771677
return assert.is_nil(y)
16781678
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()
16801680
local x, y
16811681
for i = 1, 9 do
16821682
if i % 2 == 0 then
@@ -1694,4 +1694,53 @@ return describe("break with multiple values", function()
16941694
assert.same(x, 5)
16951695
return assert.same(y, 15)
16961696
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)
16971746
end)

spec/outputs/test/format_spec.lua

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ local files = {
113113
"spec/inputs/test/loops_spec.yue",
114114
"spec/inputs/test/if_assignment_spec.yue",
115115
"spec/inputs/test/tables_advanced_spec.yue",
116+
"spec/inputs/test/break_multiple_values_spec.yue",
116117
"spec/inputs/unicode/macro_export.yue",
117118
"spec/inputs/unicode/attrib.yue",
118119
"spec/inputs/unicode/macro.yue",
@@ -182,11 +183,14 @@ return describe("format", function()
182183
local f = io.open(file)
183184
local code = f:read("a*")
184185
f:close()
185-
local original_ast = yue.to_ast(code)
186+
local original_ast, err = yue.to_ast(code)
187+
assert.is_nil(err)
186188
assert.is_not_nil(original_ast)
187189
rewriteLineCol(original_ast)
188190
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)
190194
assert.is_not_nil(ast)
191195
rewriteLineCol(ast)
192196
return assert.same(original_ast, ast)

src/yuescript/yue_compiler.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8948,10 +8948,6 @@ class YueCompilerImpl {
89488948
addToScope(var);
89498949
}
89508950
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-
}
89558951
}
89568952
breakLoop->vars = breakWithValues;
89578953
} else {

0 commit comments

Comments
 (0)