Functions defined within a string will now be shown fully.
This commit is contained in:
parent
b27d307da0
commit
038a88eca5
43
pretty.lua
43
pretty.lua
|
@ -192,9 +192,33 @@ local function get_function_info (f)
|
||||||
for i = 1, info.nparams do info.params[i] = debug.getlocal(f, i) end
|
for i = 1, info.nparams do info.params[i] = debug.getlocal(f, i) end
|
||||||
for i = 1, info.nups do local k, v = debug.getupvalue(f, i); info.ups[k] = v end
|
for i = 1, info.nups do local k, v = debug.getupvalue(f, i); info.ups[k] = v end
|
||||||
|
|
||||||
|
if info.source:sub(1,1) == '=' then info.defined_how = 'C'
|
||||||
|
elseif info.source:sub(1,1) == '@' then info.defined_how = 'file'
|
||||||
|
else info.defined_how = 'string'
|
||||||
|
end
|
||||||
|
|
||||||
return info
|
return info
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function get_line_index (str, line_nr)
|
||||||
|
local index = 0
|
||||||
|
for _ = 2, line_nr do
|
||||||
|
index = str:find('\n', index, true)
|
||||||
|
if not index then return #str end
|
||||||
|
index = index + 1
|
||||||
|
end
|
||||||
|
return index
|
||||||
|
end
|
||||||
|
|
||||||
|
local function get_full_function_str (str, start_line, end_line)
|
||||||
|
local start_line_index = get_line_index(str, start_line)
|
||||||
|
local end_line_index = get_line_index(str, end_line + 1)
|
||||||
|
|
||||||
|
local matched_function = str:sub(start_line_index, end_line_index):match('function.+end')
|
||||||
|
return matched_function
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
-- Identifyer stuff
|
-- Identifyer stuff
|
||||||
|
|
||||||
|
@ -447,9 +471,17 @@ local function format_primitive (value)
|
||||||
return tostring(value)
|
return tostring(value)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function format_string_defined_function (value, options, depth)
|
||||||
|
local info = get_function_info(value)
|
||||||
|
local function_str = get_full_function_str(info.source, info.linedefined, info.lastlinedefined)
|
||||||
|
return function_str
|
||||||
|
end
|
||||||
|
|
||||||
local function format_function (value, options, depth)
|
local function format_function (value, options, depth)
|
||||||
local info = get_function_info(value)
|
local info = get_function_info(value)
|
||||||
|
|
||||||
|
if info.defined_how == 'string' then return format_string_defined_function(value, options, depth) end
|
||||||
|
|
||||||
local l = {}
|
local l = {}
|
||||||
|
|
||||||
-- Build function signature
|
-- Build function signature
|
||||||
|
@ -464,7 +496,16 @@ local function format_function (value, options, depth)
|
||||||
if not options.more_function_info or depth ~= 0 then
|
if not options.more_function_info or depth ~= 0 then
|
||||||
l[#l+1] = ' ... end'
|
l[#l+1] = ' ... end'
|
||||||
elseif options._all_function_info then
|
elseif options._all_function_info then
|
||||||
-- NOTE: This is for testing/debugging purposes.
|
-- NOTE: This is for testing/debugging/experimentation purposes.
|
||||||
|
|
||||||
|
local file = io.open(info.short_src, 'r')
|
||||||
|
local function_str = get_full_function_str(file:read('*all'), info.linedefined, info.lastlinedefined)
|
||||||
|
file:close()
|
||||||
|
|
||||||
|
l[#l+1] = '\n\t--[[ Function Body\n\t'
|
||||||
|
l[#l+1] = function_str
|
||||||
|
l[#l+1] = '\n\t--]]'
|
||||||
|
|
||||||
l[#l+1] = '\n\t--[[\n\tNative repr:'
|
l[#l+1] = '\n\t--[[\n\tNative repr:'
|
||||||
l[#l+1] = tostring(value)
|
l[#l+1] = tostring(value)
|
||||||
l[#l+1] = '\n\t'
|
l[#l+1] = '\n\t'
|
||||||
|
|
|
@ -13,6 +13,7 @@ Approximate strings not similar enough:
|
||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
|
|
||||||
local function format_test (t)
|
local function format_test (t)
|
||||||
|
if t.longterm then return end
|
||||||
SUITE:addTest(t.expect, function ()
|
SUITE:addTest(t.expect, function ()
|
||||||
local input_value = t.input
|
local input_value = t.input
|
||||||
local input_options = t.options
|
local input_options = t.options
|
||||||
|
@ -222,14 +223,14 @@ do
|
||||||
input = function () l = SOME_RANDOM_UPVALUE end,
|
input = function () l = SOME_RANDOM_UPVALUE end,
|
||||||
options = { more_function_info = true },
|
options = { more_function_info = true },
|
||||||
-- TODO: Make this more general, such that it won't fail when adding more tests above it.
|
-- TODO: Make this more general, such that it won't fail when adding more tests above it.
|
||||||
expect = 'function ()\n\t-- source_file: \'./test/test_pretty.lua\' [Line: 222]\n\t-- up_values: { SOME_RANDOM_UPVALUE = false }\n\n\t...\nend'
|
expect = 'function ()\n\t-- source_file: \'./test/test_pretty.lua\' [Line: 223]\n\t-- up_values: { SOME_RANDOM_UPVALUE = false }\n\n\t...\nend'
|
||||||
}
|
}
|
||||||
|
|
||||||
format_test {
|
format_test {
|
||||||
input = function () SOME_RANDOM_UPVALUE = true end,
|
input = function () SOME_RANDOM_UPVALUE = true end,
|
||||||
options = { more_function_info = true },
|
options = { more_function_info = true },
|
||||||
-- TODO: Make this more general, such that it won't fail when adding more tests above it.
|
-- TODO: Make this more general, such that it won't fail when adding more tests above it.
|
||||||
expect = 'function ()\n\t-- source_file: \'./test/test_pretty.lua\' [Line: 229]\n\t-- up_values: { SOME_RANDOM_UPVALUE = false }\n\n\t...\nend'
|
expect = 'function ()\n\t-- source_file: \'./test/test_pretty.lua\' [Line: 230]\n\t-- up_values: { SOME_RANDOM_UPVALUE = false }\n\n\t...\nend'
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -237,7 +238,7 @@ format_test {
|
||||||
input = function () end,
|
input = function () end,
|
||||||
options = { more_function_info = true },
|
options = { more_function_info = true },
|
||||||
-- TODO: Make this more general, such that it won't fail when adding more tests above it.
|
-- TODO: Make this more general, such that it won't fail when adding more tests above it.
|
||||||
expect = 'function ()\n\t-- source_file: \'./test/test_pretty.lua\' [Line: 237]\n\n\t...\nend'
|
expect = 'function ()\n\t-- source_file: \'./test/test_pretty.lua\' [Line: 238]\n\n\t...\nend'
|
||||||
}
|
}
|
||||||
|
|
||||||
do
|
do
|
||||||
|
@ -252,20 +253,45 @@ do
|
||||||
input = function () index = index + 1; return index end,
|
input = function () index = index + 1; return index end,
|
||||||
options = { more_function_info = true },
|
options = { more_function_info = true },
|
||||||
-- TODO: Make this more general, such that it won't fail when adding more tests above it.
|
-- TODO: Make this more general, such that it won't fail when adding more tests above it.
|
||||||
expect = 'function ()\n\t-- source_file: \'./test/test_pretty.lua\' [Line: 252]\n\t-- up_values: { index = 0 }\n\n\t...\nend'
|
expect = 'function ()\n\t-- source_file: \'./test/test_pretty.lua\' [Line: 253]\n\t-- up_values: { index = 0 }\n\n\t...\nend'
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
format_test {
|
format_test {
|
||||||
input = loadstring('return function () end')(),
|
input = loadstring('return function () end')(),
|
||||||
expect = 'function () ... end',
|
expect = 'function () end',
|
||||||
|
}
|
||||||
|
|
||||||
|
format_test {
|
||||||
|
input = loadstring('return function () return function () end end')(),
|
||||||
|
expect = 'function () return function () end end',
|
||||||
|
}
|
||||||
|
|
||||||
|
format_test {
|
||||||
|
longterm = true,
|
||||||
|
input = loadstring('return function () return function () end\nend')()(),
|
||||||
|
expect = 'function () end',
|
||||||
|
}
|
||||||
|
|
||||||
|
format_test {
|
||||||
|
-- NOTE: This is HARD to fix. It's thus longerterm
|
||||||
|
longterm = true,
|
||||||
|
input = loadstring('return function () return function () end end')()(),
|
||||||
|
expect = 'function () end',
|
||||||
}
|
}
|
||||||
|
|
||||||
format_test {
|
format_test {
|
||||||
-- More function info allows one to even get the function whole, if it was defined in a string.
|
-- More function info allows one to even get the function whole, if it was defined in a string.
|
||||||
input = loadstring('return function (a, b) return a + b end')(),
|
input = loadstring('return function (a, b) return a + b end')(),
|
||||||
options = { more_function_info = true },
|
options = { more_function_info = true },
|
||||||
expect = 'function () return a + b end',
|
expect = 'function (a, b) return a + b end',
|
||||||
|
}
|
||||||
|
|
||||||
|
format_test {
|
||||||
|
-- More function info allows one to even get the function whole, if it was defined in a string.
|
||||||
|
input = loadstring('return function (a, b)\n\treturn a + b\nend')(),
|
||||||
|
options = { more_function_info = true },
|
||||||
|
expect = 'function (a, b)\n\treturn a + b\nend',
|
||||||
}
|
}
|
||||||
|
|
||||||
format_test {
|
format_test {
|
||||||
|
@ -274,20 +300,14 @@ format_test {
|
||||||
end,
|
end,
|
||||||
options = { more_function_info = true },
|
options = { more_function_info = true },
|
||||||
-- TODO: Make this more general, such that it won't fail when adding more tests above it.
|
-- TODO: Make this more general, such that it won't fail when adding more tests above it.
|
||||||
expect = 'function ()\n\t-- source_file: \'./test/test_pretty.lua\' [Lines: 272 - 274]\n\n\t...\nend',
|
expect = 'function ()\n\t-- source_file: \'./test/test_pretty.lua\' [Lines: 298 - 300]\n\n\t...\nend',
|
||||||
}
|
}
|
||||||
|
|
||||||
format_test {
|
format_test {
|
||||||
input = function () --[[ NOTE: This function must cover a single line of code! ]] end,
|
input = function () --[[ NOTE: This function must cover a single line of code! ]] end,
|
||||||
options = { more_function_info = true },
|
options = { more_function_info = true },
|
||||||
-- TODO: Make this more general, such that it won't fail when adding more tests above it.
|
-- TODO: Make this more general, such that it won't fail when adding more tests above it.
|
||||||
expect = 'function ()\n\t-- source_file: \'./test/test_pretty.lua\' [Line: 281]\n\n\t...\nend',
|
expect = 'function ()\n\t-- source_file: \'./test/test_pretty.lua\' [Line: 307]\n\n\t...\nend',
|
||||||
}
|
|
||||||
|
|
||||||
format_test {
|
|
||||||
input = function () end,
|
|
||||||
options = { more_function_info = true, _all_function_info = true },
|
|
||||||
expect = '',
|
|
||||||
}
|
}
|
||||||
|
|
||||||
format_test {
|
format_test {
|
||||||
|
@ -298,10 +318,7 @@ format_test {
|
||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
-- Userdata printing
|
-- Userdata printing
|
||||||
|
|
||||||
format_test {
|
-- TODO. First off figure out a way to test this stuff.
|
||||||
input = 'DUNNO MAYBE USE LUAJIT IN SOME WAY?',
|
|
||||||
expect = 'TODO',
|
|
||||||
}
|
|
||||||
|
|
||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
-- Thread printing
|
-- Thread printing
|
||||||
|
|
Loading…
Reference in New Issue
Block a user