Jump to content

Module:CallAssert

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Petr Matas (talk | contribs) at 09:16, 20 August 2018 (Creating Module:CallAssert). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

local function pack(...)
	return {...}, select('#', ...)
end

local function mapArray(func, array, count)
	local result = {}
	for i = 1, count or #array do
		result[i] = func(array[i])
	end
	return result
end

local function callAssert(func, funcName, ...)
	local result, resultCount = pack(func(...))
	if not result[1] then
		local args, argsCount = pack(...)
		args = mapArray(tostring, args, argsCount)
		local message = mw.ustring.format(
			'%s(%s) failed',
			funcName,
			table.concat(args, ', ')
		)
		error(message, 2)
	end
	return unpack(result, 1, resultCount)
end

return callAssert