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:41, 27 January 2017 (get a working gambiarra example). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
local setupTests = require('Module:User:Mr. Stradivarius/gambiarra')

local p = {}

function p.run()
	setupTests()

	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 == 4, 'two plus two equals four')

        -- 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(function ()
        -- 	return 3
        -- end)
        local function return3()
        	return 3
    	end
        local f = spy(return3)
        local function myfunc(foo)
        	foo('arg1', 'arg2')
    	end
        myfunc(f)
        ok(f() == 3, 'function return3 returns 3')

        -- 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')
	end)
end

return p