1
0

Found and fixed issue occuring when function was loaded from a module pretty couldn't find.

This commit is contained in:
Jon Michael Aanes 2017-07-15 23:29:51 +02:00
parent a9b2799e6c
commit 6a677a65b2
2 changed files with 33 additions and 4 deletions

View File

@ -181,10 +181,16 @@ local function get_function_body_info (info)
if info.defined_how == 'file' then if info.defined_how == 'file' then
-- Read file -- Read file
local file = io.open(info.short_src, 'r') local file = io.open(info.short_src, 'r')
str = file:read '*all' if file then
file:close() str = file:read '*all'
file:close()
else
str = nil
end
end end
if not str then return info end
-- Calculate indices of the lines the function should be defined at. -- Calculate indices of the lines the function should be defined at.
local start_line_index = get_line_index(str, info.linedefined) local start_line_index = get_line_index(str, info.linedefined)
local end_line_index = get_line_index(str, info.lastlinedefined + 1) local end_line_index = get_line_index(str, info.lastlinedefined + 1)
@ -255,10 +261,10 @@ return function (value, depth, l, format_value)
local function_params, function_body = nil, '...' local function_params, function_body = nil, '...'
if not info.docs then if not info.docs and info.defined_how ~= 'C' then
info = get_function_body_info(info) info = get_function_body_info(info)
if #info.body <= NR_CHARS_IN_LONG_FUNCTION_BODY and not info.body:find '\n' and not info.body:find(FUNCTION_KEYWORD_MATCH) then if info.body and #info.body <= NR_CHARS_IN_LONG_FUNCTION_BODY and not info.body:find '\n' and not info.body:find(FUNCTION_KEYWORD_MATCH) then
if info.defined_how == 'string' then function_body = info.body end if info.defined_how == 'string' then function_body = info.body end
end end
end end

View File

@ -377,6 +377,29 @@ if HAS_JIT_LIBRARY then
} }
end end
--------------------------------------------------------------------------------
-- General
SUITE:addTest('UseCase: Can print _G with max_depth = 1', function ()
format(_G, {max_depth = 1})
assert(true)
end)
SUITE:addTest('UseCase: Can load function from file that is shortly deleted', function ()
local module_name = 'tmp_'..os.time()
-- Create module
local file = io.open('./'..module_name..'.lua', 'w')
file:write '\nlocal function yo ()\n -- Hello World\n return math.random()\nend\n\nreturn yo\n'
file:close()
-- Load module
local yo = require(module_name)
-- Remove module
os.remove('./'..module_name..'.lua')
-- Format the function, even though the module it came from is gone.
format(yo)
assert(true)
end)
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
return SUITE return SUITE