我想从单个文件Lua脚本中测试函数,比如script.lua
。该脚本看起来像如下:如何测试单个文件Lua脚本中的函数?
-- some fields from gvsp dissector which shall be post processed in custom dissector
gvsp_field0_f = Field.new("gvsp.<field0-name>")
gvsp_field1_f = Field.new("gvsp.<field1-name>")
-- custom protocol declaration
custom_protocol = Proto("custom","Custom Postdissector")
-- custom protocol field declarations
field0_f = ProtoField.string("custom.<field0-name>","Custom Field 0")
field1_f = ProtoField.string("custom.<field1-name>","Custom Field 1")
-- register custom protocol as postdissector
register_postdissector(custom_protocol)
function custom_protocol.dissector(buffer,pinfo,tree)
-- local field values of "pre" dissector which are analyzed
local gvsp_field0_value = gvsp_field0_f()
local gvsp_field1_value = gvsp_field1_f()
-- functions which shell be unit tested
function0(...)
function1(...)
end
function0(...)
-- implementation
end
function1(...)
-- implementation
end
比方说,我不想从脚本文件中的函数分成单独的模块文件(这可能会使事情变得更容易)。如何定义script.lua
文件中的script.lua
或单独的test_script.lua
文件中定义的测试功能(最好是luaunit,因为容易集成,但其他工具也可以)?
这取决于_how_你定义的东西lot_。你使用'local'(这会让事情变得更难)还是你使用“local''ENV'ironments”(更容易)?你的代码结构如何?请添加一个如何定义函数的小代码示例(不一定是实际的代码,只是与您定义的东西相匹配的虚函数)。目前,没有足够的信息来给出有意义的信息答案。 – nobody
该脚本非常类似于https://wiki.wireshark.org/Lua/Dissectors#postdissectors中的解析程序,在文件末尾定义了用于协议'dissector'函数的'local'函数。但我不坚持这种结构。 – thinwybk