lua-users home
lua-l archive

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


On 09/28/2012 10:04 PM, Paul K wrote:
Hi George,

It seems that I cannot require my own package and use my methods from it...
what I am doing wrong any clues?? I tried reading the examples but there is
nothing which requires an external module? Looked also on luaforge at the
other unit testing lib luaunit and it also has very basic examples.

You didn't show what's inside your "log" module, so it's difficult to
say, but if you are not using "module" call (and you shouldn't), then
you probably need to assign the result of your require "log" call:

local log = require "log"

Paul.

I read this http://lua-users.org/wiki/LuaModuleFunctionCritiqued so I decided to go for a simple table module instead as I have no time to grasp what all that means, but the gist of it only -> that it is not so perfect to do just module("",) due to global scope polution etc. etc. lunit does use module BTW.

require "os"
log = {}
function log.debug(txt)
  print(os.date().." [DEBUG]: "..txt)
end
return log

I tried your suggestion at one point out of desperation :) ... prior to mailing the list but what the heck I gave it another shot same outcome:

gs@flipper:~/Lua/mystuff/tests$ lunit log_tests.lua
Starting testing of log lib ...
lua: log_tests.lua:6: attempt to call global 'require' (a nil value)
stack traceback:
	log_tests.lua:6: in function 'chunk'
	/home/gs/Lua/adopted/lunit/lunit.lua:628: in function 'loadtestcase'
	/home/gs/Lua/adopted/lunit/lunit.lua:657: in function 'main'
	stdin:11: in main chunk
	[C]: ?

I think it has to do with the way those functions are loaded by lunit (xpcall), but I find it hard to believe that a unit testing lib would let me test only stuff in the same file... so I tried setUp

function setUp()
  require "log"
end

same story:

gs@flipper:~/Lua/mystuff/tests$ lunit log_tests.lua
Starting testing of log lib ...
Loaded testsuite with 1 tests in 1 testcases.

    E

0 Assertions checked.

  1) Error! (log_tests.test_loglib:setUp):
log_tests.lua:7: attempt to call global 'require' (a nil value)
	log_tests.lua:7: in function <log_tests.lua:6>
	[C]: in function 'xpcall'
	(tail call): ?
	stdin:11: in main chunk
	[C]: ?


ZeroBrane Studio - slick Lua IDE and debugger for Windows, OSX, and
Linux - http://studio.zerobrane.com/

On Fri, Sep 28, 2012 at 12:50 PM, g.lister <g.lister@nodeunit.com> wrote:
Hi guys,

I have another newbie question this time on unit testing.

I am trying to use lunit and I am having a problem with this code:

require "lunit"
require "log"
print("Starting testing of log lib ...");

module( "log_tests", lunit.testcase )

function test_loglib()
   assert_true(log ~= nil, "Log object must be defined!")
   assert_true(type(log.debug) == "function", "log.debug method must exist!")
end

it blows up:

gs@flipper:~/Lua/mystuff/db/files$ lunit ../../tests/log_tests.lua
Starting testing of log lib ...
Loaded testsuite with 1 tests in 1 testcases.

     F

1 Assertions checked.

   1) Failure (log_tests.test_loglib):
../../tests/log_tests.lua:8: true expected but was false
../../tests/log_tests.lua:8: Log object must be defined!

It seems that I cannot require my own package and use my methods from it...
what I am doing wrong any clues?? I tried reading the examples but there is
nothing which requires an external module? Looked also on luaforge at the
other unit testing lib luaunit and it also has very basic examples.

TIA
/george