@DeletedUser41170185 (talk | contribs) m (Testing) |
@DeletedUser41170185 (talk | contribs) mNo edit summary |
||
Line 12: | Line 12: | ||
function h.parser_function( func, args ) |
function h.parser_function( func, args ) |
||
+ | local frame = frame |
||
if not frame then |
if not frame then |
||
mw.log( 'Frame generated.' ) |
mw.log( 'Frame generated.' ) |
Revision as of 14:36, 27 May 2018
See also: Extension:Variables
This module provides a Lua interface for the parser functions provided by Extension:Variables.
Usage
Function names are identical to the parser function names.
Variables.vardefineecho
Returns the value as it was stored (as a string). Should only be used to verify the stored value, not for inline declarations.
Differences
- Returns
nil
instead of empty strings (usep.varexists()
to check for variables holding an empty string). varexists()
returns a boolean instead of1
ornil
.
Tests
4 tests failed.
Name | Expected | Actual | |
---|---|---|---|
test_var_defined | |||
test_var_defined_empty | |||
test_var_defined_empty_default | Module:Variables/testcases:126:Failed to assert that nil equals expected 5 | ||
test_var_not_defined | |||
test_var_not_defined_default | Module:Variables/testcases:134:Failed to assert that nil equals expected 5 | ||
test_vardefine_boolean | |||
test_vardefine_function_throws | |||
test_vardefine_nil | Lua error -- Module:Variables/testcases:35: bad argument #2 to 'vardefine' (boolean, string or number expected, got nil) | ||
test_vardefine_number | |||
test_vardefine_string | |||
test_vardefine_table_throws | |||
test_vardefineecho_boolean | |||
test_vardefineecho_function_throws | |||
test_vardefineecho_nil | Lua error -- Module:Variables/testcases:89: bad argument #2 to 'vardefineecho' (boolean, string or number expected, got nil) | ||
test_vardefineecho_number | |||
test_vardefineecho_returns | |||
test_vardefineecho_string | |||
test_vardefineecho_table_throws | |||
test_varexists_false | |||
test_varexists_true |
--------------------------------------------------------------------------------
-- Imports
--------------------------------------------------------------------------------
local libraryUtil = require( 'libraryUtil' )
--------------------------------------------------------------------------------
-- Helper functions
--------------------------------------------------------------------------------
local h = {}
function h.parser_function( func, args )
local frame = frame
if not frame then
mw.log( 'Frame generated.' )
frame = mw.getCurrentFrame()
end
return frame:callParserFunction( func, args )
end
--------------------------------------------------------------------------------
-- Functions
--------------------------------------------------------------------------------
local p = {}
function p.vardefine( name, value )
libraryUtil.checkType( 'vardefine', 1, name, 'string' )
libraryUtil.checkTypeMulti( 'vardefine', 2, value, { 'boolean', 'string', 'number' }, true )
h.parser_function( '#vardefine', { name, tostring(value) } )
end
function p.vardefineecho( name, value )
libraryUtil.checkType( 'vardefineecho', 1, name, 'string' )
libraryUtil.checkTypeMulti( 'vardefineecho', 2, value, { 'boolean', 'string', 'number' }, true )
return h.parser_function( '#vardefineecho', { name, tostring(value) } )
end
function p.varexists( name )
libraryUtil.checkType( 'varexists', 1, name, 'string' )
return ( h.parser_function( '#varexists', name ) == '1' )
end
function p.var( name )
libraryUtil.checkType( 'var', 1, name, 'string' )
if p.varexists( name ) then
return h.parser_function( '#var', name )
end
end
--------------------------------------------------------------------------------
-- Return
--------------------------------------------------------------------------------
return p