How can I test functions inside a single file Lua script?

Viewed 2952

I would like to unit test functions from a single file Lua script, say script.lua. The script looks something like follows:

-- 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

Let's say I do not want to separate the functions from the script file into a separate module file (which would probably make things easier). How can I define tests (preferably with luaunit because easy to integrate, but other tool would be ok as well) for the functions defined in script.lua inside the script.lua file or in a separate test_script.lua file?

2 Answers

To enable separate script and unit test execution one needs at least 3 files (in this example 4 because the unit test framework luaunit which consists of a single file is integrated into the project directory). For this example all files reside in the same directory. The script script.lua may not define any functions in it but must import all functions it needs from its module module.lua.

-- script imports module functions
module = require('module')

-- ... and uses it to print the result of the addition function
result = module.addtwo(1,1)
print(result)

module.lua is implemented accoring to the Lua module skeleton that its functions are automatically registered for import through other script files or modules.

-- capture the name searched for by require
local NAME=...

-- table for our functions
local M = { }

-- A typical local function that is also published in the
-- module table.
local function addtwo(a,b) return a+b end
M.addtwo = addtwo

-- Shorthand form is less typing and doesn't use a local variable
function M.subtwo(x) return x-2 end

return M

test_module.lua contains the unit tests for the module functions and imports luaunit.lua (unit test framework) for its execution. test_module.lua has the following content.

luaunit = require('luaunit')
script = require('module')

function testAddPositive()
    luaunit.assertEquals(module.addtwo(1,1),2)
end

os.exit( luaunit.LuaUnit.run() )

If you run the tests by executing lua test_module.lua the tests are executed separately from the script functionality.

.
Ran 1 tests in 0.000 seconds, 1 success, 0 failures
OK

The script is executed as usual with lua script.lua with output 2.

Simple answer: You can't!

I've asked the Lua team about this myself a few years ago as there is no obvious way for a script to know if it is the main script running or included (e.g., 'require'd).

There does not seem to be interest for adding such capability in the foreseeable future, either!

Related