Jump to content

Module:User:Mr. Stradivarius/sandbox

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Mr. Stradivarius (talk | contribs) at 01:05, 27 January 2017 (test sample gambiarra code). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
-- 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)