Module:User:Mr. Stradivarius/sandbox
Appearance
![]() | This is the module sandbox page for Module:User:Mr. Stradivarius. |
-- Gambiarra.lua is a single file that can be just copied into your source
-- tree and included. Once included it defines a test() function, that's the
-- only function is defines.
local test = require('Module:User:Mr. Stradivarius/gambiarra')
-- That's how you declare a new test. You pass a test name, a function and
-- optionally an async flag. If no async flag is passed, or if it's false
-- the test will finish right after the test function returns.
-- Otherwise you will have to call next() after the test is actually
-- finished.
test('My first test', function(next)
-- Inside a test function you can use three more functions:
-- ok, eq and spy
-- ok(cond, [msg]) is the only assertion helper.
ok(2 + 2 == 5, 'two plus two equals five')
-- spy([f]) returns function that records its every call (arguments,
-- return values, errors) and optionally delegates the execution to
-- the wrapped function
local f = spy(mycallback)
myfunc(f)
-- arguments are stored in the f.called table. Table is nil if the spy
-- was never called
ok(f.called, 'callback was called')
ok(#f.called == 2,' callback was called twice')
-- eq(a, b) is a helper to deeply compare two variables. It works with most Lua data types.
-- It's a handy way to compare tables.
ok(eq(f.called[1], {'arg1', 'arg2'}), 'first call with two args: arg1 and arg2')
-- Finally, you can use asynchronous code in your library, just call
-- next() when you're done
someasyncfunc(function()
next() -- Go to the next test
end)
end, true)