From 752686a111aef90fb77d1983a71086ec7a58058e Mon Sep 17 00:00:00 2001 From: SD Asif Hossein Date: Fri, 7 Jun 2024 22:47:17 +0600 Subject: [PATCH] Traceback test sample code refactoring --- spec/traceback/depth_recursion/main.lua | 13 ++++--- spec/traceback/module_lua/main.lua | 23 ++++++++----- spec/traceback/module_pallene/main.lua | 16 +++++---- spec/traceback/rect/main.lua | 6 ++-- spec/traceback_spec.lua | 46 ++++++++++++------------- 5 files changed, 60 insertions(+), 44 deletions(-) diff --git a/spec/traceback/depth_recursion/main.lua b/spec/traceback/depth_recursion/main.lua index 54959966..1688ee67 100644 --- a/spec/traceback/depth_recursion/main.lua +++ b/spec/traceback/depth_recursion/main.lua @@ -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) diff --git a/spec/traceback/module_lua/main.lua b/spec/traceback/module_lua/main.lua index 2a69241a..ef764119 100644 --- a/spec/traceback/module_lua/main.lua +++ b/spec/traceback/module_lua/main.lua @@ -6,15 +6,20 @@ 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!" @@ -22,8 +27,10 @@ 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) diff --git a/spec/traceback/module_pallene/main.lua b/spec/traceback/module_pallene/main.lua index 8ff36a0c..62f7902c 100644 --- a/spec/traceback/module_pallene/main.lua +++ b/spec/traceback/module_pallene/main.lua @@ -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) diff --git a/spec/traceback/rect/main.lua b/spec/traceback/rect/main.lua index 037e2d2b..3a4da419 100644 --- a/spec/traceback/rect/main.lua +++ b/spec/traceback/rect/main.lua @@ -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) diff --git a/spec/traceback_spec.lua b/spec/traceback_spec.lua index 8d01b99f..af7666fe 100644 --- a/spec/traceback_spec.lua +++ b/spec/traceback_spec.lua @@ -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
+ spec/traceback/rect/main.lua:16: in
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
+ spec/traceback/module_lua/main.lua:36: in
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
+ spec/traceback/module_pallene/main.lua:27: in
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
+ spec/traceback/depth_recursion/main.lua:25: in
C: in function '' ]]) end)