lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


2012/9/22 Thijs Schreijer <thijs@thijsschreijer.nl>:
> The best thing I could come up with is something like this at the end of a
> module;
>
> -- test aliases for local functions
> if _TEST then
>   cli.split = split
>   cli.wordwrap = wordwrap
> end
>
> return cli
>
> It’s a small burden on production code and relies on a global ‘_TEST’, works
> perfectly well, just wondering if there is a better approach, and if not, is
> there an accepted form of the ‘_TEST’  global by another name?

Not a better solution, but an alternative is to put the tests in the
file itself. That's what I tend to do, I put my tests either at the
end of the file, or interspersed with the actual module code, and I
detect whether the file is executed through require or not. For
example :

local _M = {}
local _NAME = ... or "__test__"

local function a()
end

if _NAME=="__test__" then
  -- tests for private a (upvalue)
end

function _M.b()
end

if _NAME=="__test__" then
  -- tests for public _M.b
end

return _M