Jump to content
Wikipedia The Free Encyclopedia

Module:CallAssert

From Wikipedia, the free encyclopedia
Module documentation[view] [edit] [history] [purge]
This module is rated as ready for general use. It has reached a mature state, is considered relatively stable and bug-free, and may be used wherever appropriate. It can be mentioned on help pages and other Wikipedia resources as an option for new users. To minimise server load and avoid disruptive output, improvements should be developed through sandbox testing rather than repeated trial-and-error editing.
Page protected This module is currently protected from editing.
See the protection policy and protection log for more details. Please discuss any changes on the talk page; you may submit an edit request to ask an administrator to make an edit if it is uncontroversial or supported by consensus. You may also request that this page be unprotected.
[画像:Warning] This Lua module is used in system messages, and on approximately 118,000 pages.
Changes to it can cause immediate changes to the Wikipedia user interface.
To avoid major disruption and server load, any changes should be tested in the module's /sandbox or /testcases subpages, or in your own module sandbox. The tested changes can be added to this page in a single edit. Please discuss changes on the talk page before implementing them.

This metamodule provides function callAssert, which simplifies error checking by throwing an error if a given function returns nil or false. This is similar to the built-in function assert, but unlike it, callAssert itself calls the function, whose return value is to be checked. This allows it to include the function name and argument values in the error message.

Usage

local callAssert = require('Module:CallAssert')
local result1, result2, etc = callAssert(func, 'funcName', ...)

callAssert calls func(...) and checks if the first returned value evaluates to true. If it does, then it returns all the returned values. If not, it throws an error with a message in the form 'funcName(argument_values) failed'.

The above documentation is transcluded from Module:CallAssert/doc. (edit | history)
Editors can experiment in this module's sandbox (create | mirror) and testcases (create) pages.
Subpages of this module.

 localfunctionpack(...)
 return{...},select('#',...)
 end

 localfunctionmapArray(func,array,count)
 localresult={}
 fori=1,countor#arraydo
 result[i]=func(array[i])
 end
 returnresult
 end

 localfunctionquote(value)
 iftype(value)=='string'then
 return(string.gsub(string.format('%q',value),'\\\n','\\n'))-- Outer parentheses remove second value returned by gsub
 end
 localstr=tostring(value)
 iftype(value)=='table'andstr~='table'then
 return'{'..str..'}'
 end
 returnstr
 end

 localfunctioncallAssert(func,funcName,...)
 localresult,resultCount=pack(func(...))
 ifnotresult[1]then
 localargs,argsCount=pack(...)
 args=mapArray(quote,args,argsCount)
 localmessage=mw.ustring.format(
 '%s(%s) failed',
 funcName,
 table.concat(args,', ')
 )
 error(message,2)
 end
 returnunpack(result,1,resultCount)
 end

 returncallAssert

AltStyle によって変換されたページ (->オリジナル) /