Skip to content

Commit

Permalink
Traceback test sample code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
singul4ri7y committed Jun 7, 2024
1 parent 07b975f commit 752686a
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 44 deletions.
13 changes: 8 additions & 5 deletions spec/traceback/depth_recursion/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@

local pallene = require 'spec.traceback.depth_recursion.depth_recursion'

function _G.lua_fn(depth)
-- luacheck: globals lua_fn
function lua_fn(depth)
if depth == 0 then
error "Depth reached 0!"
end

pallene.pallene_fn(_G.lua_fn, depth - 1)
pallene.pallene_fn(lua_fn, depth - 1)
end

-- Should be local.
-- Making it global so that it is visible in the traceback.
function _G.wrapper()
_G.lua_fn(10)
-- luacheck: globals wrapper
function wrapper()
lua_fn(10)
end

xpcall(_G.wrapper, _G.pallene_tracer_debug_traceback)
-- luacheck: globals pallene_tracer_debug_traceback
xpcall(wrapper, pallene_tracer_debug_traceback)
23 changes: 15 additions & 8 deletions spec/traceback/module_lua/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,31 @@
local another_module = require 'spec.traceback.module_lua.another_module'
local pallene = require 'spec.traceback.module_lua.module_lua'

function _G.lua_1()
pallene.pallene_1(_G.lua_2)
-- luacheck: globals lua_1
function lua_1()
-- luacheck: globals lua_2
pallene.pallene_1(lua_2)
end

function _G.lua_2()
pallene.pallene_2(_G.lua_3, 33, 79)
-- luacheck: globals lua_2
function lua_2()
-- luacheck: globals lua_3
pallene.pallene_2(lua_3, 33, 79)
end

function _G.lua_3(sum)
-- luacheck: globals lua_3
function lua_3(sum)
print("The summation is: ", sum)

error "Any normal error from Lua!"
end

-- Should be local.
-- Making it global so that it is visible in the traceback.
function _G.wrapper()
another_module.call_lua_callback(_G.lua_1)
-- luacheck: globals wrapper
function wrapper()
another_module.call_lua_callback(lua_1)
end

xpcall(_G.wrapper, _G.pallene_tracer_debug_traceback)
-- luacheck: globals pallene_tracer_debug_traceback
xpcall(wrapper, pallene_tracer_debug_traceback)
16 changes: 10 additions & 6 deletions spec/traceback/module_pallene/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,22 @@
local pallene = require 'spec.traceback.module_pallene.module_pallene'
local pallene_alt = require 'spec.traceback.module_pallene.module_pallene_alt'

function _G.lua_2()
-- luacheck: globals lua_2
function lua_2()
error "There's an error in everyday life. Shame!"
end

function _G.lua_1()
pallene_alt.alternate_everyday_fn(_G.lua_2)
-- luacheck: globals lua_1
function lua_1()
pallene_alt.alternate_everyday_fn(lua_2)
end

-- Should be local.
-- Making it global so that it is visible in the traceback.
function _G.wrapper()
pallene.normal_everyday_fn(_G.lua_1)
-- luacheck: globals wrapper
function wrapper()
pallene.normal_everyday_fn(lua_1)
end

xpcall(_G.wrapper, _G.pallene_tracer_debug_traceback)
-- luacheck: globals pallene_tracer_debug_traceback
xpcall(wrapper, pallene_tracer_debug_traceback)
6 changes: 4 additions & 2 deletions spec/traceback/rect/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ local rect = require 'spec.traceback.rect.rect'

-- Should be local.
-- Making it global so that it is visible in the traceback.
function _G.wrapper()
-- luacheck: globals wrapper
function wrapper()
print(rect.area { width = "Huh, gotcha!", height = 16.0 })
end

xpcall(_G.wrapper, _G.pallene_tracer_debug_traceback)
-- luacheck: globals pallene_tracer_debug_traceback
xpcall(wrapper, pallene_tracer_debug_traceback)
46 changes: 23 additions & 23 deletions spec/traceback_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,70 +26,70 @@ end

it("Rectangle", function()
assert_test("rect", [[
Runtime error: spec/traceback/rect/main.lua:11: file spec/traceback/rect/rect.pln: line 10: wrong type for downcasted value, expected float but found string
Runtime error: spec/traceback/rect/main.lua:12: file spec/traceback/rect/rect.pln: line 10: wrong type for downcasted value, expected float but found string
Stack traceback:
spec/traceback/rect/rect.pln:10: in function 'universal_calc_area'
spec/traceback/rect/rect.pln:13: in function 'area'
spec/traceback/rect/main.lua:11: in function 'wrapper'
spec/traceback/rect/main.lua:12: in function 'wrapper'
C: in function 'xpcall'
spec/traceback/rect/main.lua:14: in <main>
spec/traceback/rect/main.lua:16: in <main>
C: in function '<?>'
]])
end)

it("Multi-module Lua", function()
assert_test("module_lua", [[
Runtime error: spec/traceback/module_lua/main.lua:20: Any normal error from Lua!
Runtime error: spec/traceback/module_lua/main.lua:25: Any normal error from Lua!
Stack traceback:
C: in function 'error'
spec/traceback/module_lua/main.lua:20: in function 'lua_3'
spec/traceback/module_lua/main.lua:25: in function 'lua_3'
spec/traceback/module_lua/module_lua.pln:12: in function 'pallene_2'
spec/traceback/module_lua/main.lua:14: in function 'lua_2'
spec/traceback/module_lua/main.lua:18: in function 'lua_2'
spec/traceback/module_lua/module_lua.pln:8: in function 'pallene_1'
spec/traceback/module_lua/main.lua:10: in function 'callback'
spec/traceback/module_lua/main.lua:12: in function 'callback'
./spec/traceback/module_lua/another_module.lua:7: in function 'call_lua_callback'
spec/traceback/module_lua/main.lua:26: in function 'wrapper'
spec/traceback/module_lua/main.lua:32: in function 'wrapper'
C: in function 'xpcall'
spec/traceback/module_lua/main.lua:29: in <main>
spec/traceback/module_lua/main.lua:36: in <main>
C: in function '<?>'
]])
end)

it("Multi-module Pallene", function()
assert_test("module_pallene", [[
Runtime error: spec/traceback/module_pallene/main.lua:10: There's an error in everyday life. Shame!
Runtime error: spec/traceback/module_pallene/main.lua:11: There's an error in everyday life. Shame!
Stack traceback:
C: in function 'error'
spec/traceback/module_pallene/main.lua:10: in function 'lua_2'
spec/traceback/module_pallene/main.lua:11: in function 'lua_2'
spec/traceback/module_pallene/module_pallene_alt.pln:8: in function 'alternate_everyday_fn'
spec/traceback/module_pallene/main.lua:14: in function 'lua_1'
spec/traceback/module_pallene/main.lua:16: in function 'lua_1'
spec/traceback/module_pallene/module_pallene.pln:8: in function 'normal_everyday_fn'
spec/traceback/module_pallene/main.lua:20: in function 'wrapper'
spec/traceback/module_pallene/main.lua:23: in function 'wrapper'
C: in function 'xpcall'
spec/traceback/module_pallene/main.lua:23: in <main>
spec/traceback/module_pallene/main.lua:27: in <main>
C: in function '<?>'
]])
end)

it("Depth recursion", function()
assert_test("depth_recursion", [[
Runtime error: spec/traceback/depth_recursion/main.lua:10: Depth reached 0!
Runtime error: spec/traceback/depth_recursion/main.lua:11: Depth reached 0!
Stack traceback:
C: in function 'error'
spec/traceback/depth_recursion/main.lua:10: in function 'lua_fn'
spec/traceback/depth_recursion/main.lua:11: in function 'lua_fn'
spec/traceback/depth_recursion/depth_recursion.pln:8: in function 'pallene_fn'
spec/traceback/depth_recursion/main.lua:13: in function 'lua_fn'
spec/traceback/depth_recursion/main.lua:14: in function 'lua_fn'
spec/traceback/depth_recursion/depth_recursion.pln:8: in function 'pallene_fn'
spec/traceback/depth_recursion/main.lua:13: in function 'lua_fn'
spec/traceback/depth_recursion/main.lua:14: in function 'lua_fn'
spec/traceback/depth_recursion/depth_recursion.pln:8: in function 'pallene_fn'
spec/traceback/depth_recursion/main.lua:13: in function 'lua_fn'
spec/traceback/depth_recursion/main.lua:14: in function 'lua_fn'
spec/traceback/depth_recursion/depth_recursion.pln:8: in function 'pallene_fn'
spec/traceback/depth_recursion/main.lua:13: in function 'lua_fn'
spec/traceback/depth_recursion/main.lua:14: in function 'lua_fn'
spec/traceback/depth_recursion/depth_recursion.pln:8: in function 'pallene_fn'
spec/traceback/depth_recursion/main.lua:13: in function 'lua_fn'
spec/traceback/depth_recursion/main.lua:19: in function 'wrapper'
spec/traceback/depth_recursion/main.lua:14: in function 'lua_fn'
spec/traceback/depth_recursion/main.lua:21: in function 'wrapper'
C: in function 'xpcall'
spec/traceback/depth_recursion/main.lua:22: in <main>
spec/traceback/depth_recursion/main.lua:25: in <main>
C: in function '<?>'
]])
end)
Expand Down

0 comments on commit 752686a

Please sign in to comment.