Skip to content

Commit

Permalink
Modernise goto test.
Browse files Browse the repository at this point in the history
  • Loading branch information
corsix committed Apr 9, 2016
1 parent 74d256f commit b8bbda3
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 44 deletions.
81 changes: 37 additions & 44 deletions test/misc/goto.lua → test/lang/goto.lua
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
local loadstring = loadstring or load

-- Basic goto and label semantics.
do
local function expect(src, msg)
local ok, err = loadstring(src)
if msg then
assert(not ok and string.find(err, msg))
else
assert(ok)
end
local function expect(src, msg)
local ok, err = loadstring(src)
if msg then
assert(not ok and string.find(err, msg), err)
else
assert(ok, err)
end

end

do --- Basic goto and label semantics.
-- Error: duplicate label.
expect("::a:: ::a::", "'a'")
expect("::a:: ::b:: do ::b:: end ::a::", "'a'")
Expand All @@ -27,17 +27,18 @@ do
expect("do local v,w; goto a; end; local x; ::a:: local y", "'x'")
expect("repeat goto a; local x; ::a:: until x", "'x'")

if os.getenv("LUA52") then
expect("goto = 1", "<name>")
else
expect("goto = 1")
end

::a:: do goto a; ::a:: end -- Forward jump, not an infinite loop.
end

-- Trailing label is considered to be out of scope.
do
do --- Goto is not a keyword. -compat5.2 !lex !private_G
goto = 1
end

do --- Goto is a keyword. +compat5.2
expect("goto = 1", "<name>")
end

do --- Trailing label is considered to be out of scope.
local x = 11
do
goto a
Expand All @@ -48,24 +49,22 @@ do
::b::
end
assert(x == 11)
if os.getenv("LUA52") then
assert(loadstring([[
local x = 11
do
goto a
goto a
local y = 22
x = y
::a:: ;;
::b:: ;;
end
return x
]])() == 11)
end

do --- Trailing labels and empty statements are considered to be out of scope. +compat5.2 !lex
local x = 11
do
goto a
goto a
local y = 22
x = y
::a:: ;;
::b:: ;;
end
assert(x == 11)
end

-- Simple loop with cross-jumping.
do
do --- Simple loop with cross-jumping.
local x = 1
while true do
goto b
Expand All @@ -78,8 +77,7 @@ do
assert(x == 100)
end

-- Backwards goto must close upval.
do
do --- Backwards goto must close upval.
local t = {}
local i = 1
::a::
Expand All @@ -92,8 +90,7 @@ do
assert(t[2]() == 2)
end

-- Break must close upval, even if closure is parsed after break.
do
do --- Break must close upval, even if closure is parsed after break.
local foo
repeat
local x
Expand All @@ -106,8 +103,7 @@ do
assert(foo() == true)
end

-- Label prevents joining to KNIL.
do
do --- Label prevents joining to KNIL. -lua==5.2
local k = 0
local x
::foo::
Expand All @@ -118,8 +114,7 @@ do
if k < 2 then goto foo end
end

-- Break resolved from the right scope.
do
do --- Break resolved from the right scope.
local function p(lvl)
lvl = lvl or 1
while true do
Expand All @@ -134,8 +129,7 @@ do
end
end

-- Do not join twice with UCLO.
do
do --- Do not join twice with UCLO.
while true do
do
local x
Expand All @@ -153,4 +147,3 @@ do
end
::foo::
end

1 change: 1 addition & 0 deletions test/lang/index
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ upvalue
coroutine.lua
tail_recursion.lua
gc.lua
goto.lua +goto
3 changes: 3 additions & 0 deletions test/test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ local function default_tags()
if loadstring"return 0xep+9" then
tags.hexfloat = true
end
if loadstring"goto x ::x::" then
tags["goto"] = true
end

-- Libraries
for _, lib in ipairs{"bit", "ffi", "jit.profile", "table.new"} do
Expand Down

0 comments on commit b8bbda3

Please sign in to comment.