Module:Arguments/testcases: Difference between revisions
Appearance
Content deleted Content added
. |
Masoud.h1368 (talk | contribs) No edit summary Tag: Reverted |
||
Line 1: | Line 1: | ||
-- This module provides easy processing of arguments passed to Scribunto from |
|||
local getArgs = require('Module:Arguments/sandbox').getArgs |
|||
-- #invoke. It is intended for use by other Lua modules, and should not be |
|||
local ScribuntoUnit = require('Module:ScribuntoUnit') |
|||
-- called from #invoke directly. |
|||
local suite = ScribuntoUnit:new() |
|||
local libraryUtil = require('libraryUtil') |
|||
-------------------------------------------------------------------------- |
|||
local checkType = libraryUtil.checkType |
|||
-- Default values |
|||
-------------------------------------------------------------------------- |
|||
local |
local arguments = {} |
||
d.frameTitle = 'Frame title' |
|||
d.parentTitle = 'Parent title' |
|||
-- Generate four different tidyVal functions, so that we don't have to check the |
|||
-- Precedence-testing values |
|||
-- options every time we call it. |
|||
d.firstFrameArg = 'first frame argument' |
|||
d.firstParentArg = 'first parent argument' |
|||
d.secondParentArg = 'second parent argument' |
|||
d.uniqueFrameArg = 'unique frame argument' |
|||
d.uniqueFrameArgKey = 'uniqueFrameArgKey' |
|||
d.uniqueParentArg = 'unique parent argument' |
|||
d.uniqueParentArgKey = 'uniqueParentArgKey' |
|||
local function tidyValDefault(key, val) |
|||
-- Trimming and whitespace values. |
|||
if type(val) == 'string' then |
|||
-- Whitespace gets trimmed from named parameters, so keys for these need |
|||
val = val:match('^%s*(.-)%s*$') |
|||
-- to be numbers to make this a proper test. |
|||
if val == '' then |
|||
return nil |
|||
d.blankArgKey = 100 |
|||
else |
|||
d.spacesArg = '\n ' |
|||
return val |
|||
d.spacesArgKey = 101 |
|||
end |
|||
d.untrimmedArg = '\n foo bar ' |
|||
else |
|||
d.untrimmedArgKey = 102 |
|||
return val |
|||
d.trimmedArg = 'foo bar' |
|||
end |
|||
d.valueFuncValue = 'valueFuncValue' |
|||
d.defaultValueFunc = function() return d.valueFuncValue end |
|||
d.translate = { |
|||
foo = 'F00', |
|||
bar = '8@r', |
|||
baz = '8@z', |
|||
qux = 'qUx' |
|||
} |
|||
-------------------------------------------------------------------------- |
|||
-- Helper functions |
|||
-------------------------------------------------------------------------- |
|||
function suite.getFrames(frameTitle, frameArgs, parentTitle, parentArgs) |
|||
frameTitle = frameTitle or d.frameTitle |
|||
frameArgs = frameArgs or { |
|||
d.firstFrameArg, |
|||
[d.uniqueFrameArgKey] = d.uniqueFrameArg, |
|||
[d.blankArgKey] = d.blankArg, |
|||
[d.spacesArgKey] = d.spacesArg, |
|||
[d.untrimmedArgKey] = d.untrimmedArg |
|||
} |
|||
parentTitle = parentTitle or d.parentTitle |
|||
parentArgs = parentArgs or { |
|||
d.firstParentArg, |
|||
d.secondParentArg, |
|||
[d.uniqueParentArgKey] = d.uniqueParentArg |
|||
} |
|||
local currentFrame = mw.getCurrentFrame() |
|||
local parent = currentFrame:newChild{title = parentTitle, args = parentArgs} |
|||
local frame = parent:newChild{title = frameTitle, args = frameArgs} |
|||
return frame, parent |
|||
end |
end |
||
local function tidyValTrimOnly(key, val) |
|||
function suite.getDefaultArgs(options, frameTitle, frameArgs, parentTitle, parentArgs) |
|||
if type(val) == 'string' then |
|||
local frame, parent = suite.getFrames(frameTitle, frameArgs, parentTitle, parentArgs) |
|||
return val:match('^%s*(.-)%s*$') |
|||
local args = getArgs(frame, options) |
|||
else |
|||
return args |
|||
return val |
|||
end |
|||
end |
end |
||
function |
local function tidyValRemoveBlanksOnly(key, val) |
||
if type(val) == 'string' then |
|||
-- Asserts that executing the function func results in an error. |
|||
if val:find('%S') then |
|||
-- Parameters after func are func's arguments. |
|||
return val |
|||
local success, msg = pcall(func, ...) |
|||
else |
|||
self:assertFalse(success) |
|||
return nil |
|||
end |
|||
end |
|||
else |
|||
function suite:assertNumberOfIterations(expected, iterator, t) |
|||
return val |
|||
local noIterations = 0 |
|||
for k, v in iterator(t) do |
|||
noIterations = noIterations + 1 |
|||
end |
end |
||
self:assertEquals(expected, noIterations) |
|||
end |
end |
||
local function tidyValNoChange(key, val) |
|||
-------------------------------------------------------------------------- |
|||
return val |
|||
-- Test precedence |
|||
-------------------------------------------------------------------------- |
|||
function suite:assertDefaultPrecedence(args) |
|||
self:assertEquals(d.firstFrameArg, args[1]) |
|||
self:assertEquals(d.secondParentArg, args[2]) |
|||
self:assertEquals(d.uniqueFrameArg, args[d.uniqueFrameArgKey]) |
|||
self:assertEquals(d.uniqueParentArg, args[d.uniqueParentArgKey]) |
|||
end |
end |
||
local function matchesTitle(given, title) |
|||
function suite:testDefaultPrecedence() |
|||
local tp = type( given ) |
|||
self:assertDefaultPrecedence(suite.getDefaultArgs()) |
|||
return (tp == 'string' or tp == 'number') and mw.title.new( given ).prefixedText == title |
|||
end |
end |
||
local translate_mt = { __index = function(t, k) return k end } |
|||
function suite:testDefaultPrecedenceThroughWrapper() |
|||
self:assertDefaultPrecedence(suite.getDefaultArgs{wrappers = {d.parentTitle}, parentOnly = false}) |
|||
end |
|||
function arguments.getArgs(frame, options) |
|||
function suite:testDefaultPrecedenceThroughNonWrapper() |
|||
checkType('getArgs', 1, frame, 'table', true) |
|||
self:assertDefaultPrecedence(suite.getDefaultArgs({wrappers = d.parentTitle, frameOnly = false}, nil, nil, 'Not the parent title')) |
|||
checkType('getArgs', 2, options, 'table', true) |
|||
end |
|||
frame = frame or {} |
|||
options = options or {} |
|||
--[[ |
|||
function suite:assertParentFirst(args) |
|||
-- Set up argument translation. |
|||
self:assertEquals(d.firstParentArg, args[1]) |
|||
--]] |
|||
self:assertEquals(d.secondParentArg, args[2]) |
|||
options.translate = options.translate or {} |
|||
self:assertEquals(d.uniqueFrameArg, args[d.uniqueFrameArgKey]) |
|||
if getmetatable(options.translate) == nil then |
|||
self:assertEquals(d.uniqueParentArg, args[d.uniqueParentArgKey]) |
|||
setmetatable(options.translate, translate_mt) |
|||
end |
|||
function suite:testParentFirst() |
|||
self:assertParentFirst(suite.getDefaultArgs{parentFirst = true}) |
|||
end |
|||
function suite:testParentFirstThroughWrapper() |
|||
self:assertParentFirst(suite.getDefaultArgs{wrappers = {d.parentTitle}, parentOnly = false, parentFirst = true}) |
|||
end |
|||
function suite:testParentFirstThroughNonWrapper() |
|||
self:assertParentFirst(suite.getDefaultArgs({wrappers = d.parentTitle, frameOnly = false, parentFirst = true}, nil, nil, 'Not the parent title')) |
|||
end |
|||
function suite:assertParentOnly(args) |
|||
self:assertEquals(d.firstParentArg, args[1]) |
|||
self:assertEquals(d.secondParentArg, args[2]) |
|||
self:assertEquals(nil, args[d.uniqueFrameArgKey]) |
|||
self:assertEquals(d.uniqueParentArg, args[d.uniqueParentArgKey]) |
|||
end |
|||
function suite:testParentOnly() |
|||
self:assertParentOnly(suite.getDefaultArgs{parentOnly = true}) |
|||
end |
|||
function suite:testParentOnlyThroughWrapper() |
|||
self:assertParentOnly(suite.getDefaultArgs{wrappers = {d.parentTitle}}) |
|||
end |
|||
function suite:testParentOnlyThroughSandboxWrapper() |
|||
self:assertParentOnly(suite.getDefaultArgs({wrappers = d.parentTitle}, nil, nil, d.parentTitle .. '/sandbox')) |
|||
end |
|||
function suite:assertFrameOnly(args) |
|||
self:assertEquals(d.firstFrameArg, args[1]) |
|||
self:assertEquals(nil, args[2]) |
|||
self:assertEquals(d.uniqueFrameArg, args[d.uniqueFrameArgKey]) |
|||
self:assertEquals(nil, args[d.uniqueParentArgKey]) |
|||
end |
|||
function suite:testFrameOnly() |
|||
self:assertFrameOnly(suite.getDefaultArgs{frameOnly = true}) |
|||
end |
|||
function suite:testFrameOnlyThroughNonWrapper() |
|||
self:assertFrameOnly(suite.getDefaultArgs({wrappers = d.parentTitle}, nil, nil, 'Not the parent title')) |
|||
end |
|||
function suite:testDefaultPrecedenceWithWhitespace() |
|||
local frame, parent = suite.getFrames( |
|||
d.frameTitle, |
|||
{' '}, |
|||
d.parentTitle, |
|||
{d.firstParentArg} |
|||
) |
|||
local args = getArgs(frame) |
|||
self:assertEquals(d.firstParentArg, args[1]) |
|||
end |
|||
-------------------------------------------------------------------------- |
|||
-- Test trimming and blank removal |
|||
-------------------------------------------------------------------------- |
|||
function suite:testDefaultTrimmingAndBlankRemoval() |
|||
local args = suite.getDefaultArgs() |
|||
self:assertEquals(nil, args[d.blankArgKey]) |
|||
self:assertEquals(nil, args[d.spacesArgKey]) |
|||
self:assertEquals(d.trimmedArg, args[d.untrimmedArgKey]) |
|||
end |
|||
function suite:testRemoveBlanksButNoTrimming() |
|||
local args = suite.getDefaultArgs{trim = false} |
|||
self:assertEquals(nil, args[d.blankArgKey]) |
|||
self:assertEquals(nil, args[d.spacesArgKey]) |
|||
self:assertEquals(d.untrimmedArg, args[d.untrimmedArgKey]) |
|||
end |
|||
function suite:testTrimButNoBlankRemoval() |
|||
local args = suite.getDefaultArgs{removeBlanks = false} |
|||
self:assertEquals(d.blankArg, args[d.blankArgKey]) |
|||
self:assertEquals('', args[d.spacesArgKey]) |
|||
self:assertEquals(d.trimmedArg, args[d.untrimmedArgKey]) |
|||
end |
|||
function suite:testNoTrimOrBlankRemoval() |
|||
local args = suite.getDefaultArgs{trim = false, removeBlanks = false} |
|||
self:assertEquals(d.blankArg, args[d.blankArgKey]) |
|||
self:assertEquals(d.spacesArg, args[d.spacesArgKey]) |
|||
self:assertEquals(d.untrimmedArg, args[d.untrimmedArgKey]) |
|||
end |
|||
-------------------------------------------------------------------------- |
|||
-- Test valueFunc |
|||
-------------------------------------------------------------------------- |
|||
function suite:testValueFunc() |
|||
local args = suite.getDefaultArgs{valueFunc = d.defaultValueFunc} |
|||
self:assertEquals(d.valueFuncValue, args['some random key: sdfaliwyda']) |
|||
end |
|||
function suite:testValueFuncPrecedence() |
|||
local args = suite.getDefaultArgs{ |
|||
trim = false, |
|||
removeBlanks = false, |
|||
valueFunc = d.defaultValueFunc |
|||
} |
|||
self:assertEquals(d.valueFuncValue, args[1]) |
|||
self:assertEquals(d.valueFuncValue, args['some random key: gekjabawyvy']) |
|||
end |
|||
function suite:testValueFuncKey() |
|||
local args = suite.getDefaultArgs{valueFunc = function(key, value) |
|||
return 'valueFunc key: '.. key |
|||
end} |
|||
self:assertEquals('valueFunc key: foo', args.foo) |
|||
end |
|||
function suite:testValueFuncValue() |
|||
local args = suite.getDefaultArgs{valueFunc = function(key, value) |
|||
return 'valueFunc value: '.. value |
|||
end} |
|||
self:assertEquals( |
|||
'valueFunc value: ' .. d.uniqueFrameArg, |
|||
args[d.uniqueFrameArgKey] |
|||
) |
|||
end |
|||
-------------------------------------------------------------------------- |
|||
-- Test adding new arguments |
|||
-------------------------------------------------------------------------- |
|||
function suite:testAddingNewArgs() |
|||
local args = suite.getDefaultArgs() |
|||
self:assertEquals(nil, args.newKey) |
|||
args.newKey = 'some new key' |
|||
self:assertEquals('some new key', args.newKey) |
|||
end |
|||
function suite:testAddingNewBlankArgs() |
|||
local args = suite.getDefaultArgs() |
|||
self:assertEquals(nil, args.newKey) |
|||
args.newKey = '' |
|||
self:assertEquals('', args.newKey) |
|||
end |
|||
function suite:testAddingNewSpacesArgs() |
|||
local args = suite.getDefaultArgs() |
|||
self:assertEquals(nil, args.newKey) |
|||
args.newKey = ' ' |
|||
self:assertEquals(' ', args.newKey) |
|||
end |
|||
function suite:testOverwriting() |
|||
local args = suite.getDefaultArgs() |
|||
self:assertEquals(d.firstFrameArg, args[1]) |
|||
args[1] = 'a new first frame argument' |
|||
self:assertEquals('a new first frame argument', args[1]) |
|||
end |
|||
function suite:testOverwritingWithNil() |
|||
local args = suite.getDefaultArgs() |
|||
self:assertEquals(d.firstFrameArg, args[1]) |
|||
args[1] = nil |
|||
self:assertEquals(nil, args[1]) |
|||
end |
|||
function suite:testOverwritingWithBlank() |
|||
local args = suite.getDefaultArgs() |
|||
self:assertEquals(d.firstFrameArg, args[1]) |
|||
args[1] = '' |
|||
self:assertEquals('', args[1]) |
|||
end |
|||
function suite:testOverwritingWithSpaces() |
|||
local args = suite.getDefaultArgs() |
|||
self:assertEquals(d.firstFrameArg, args[1]) |
|||
args[1] = ' ' |
|||
self:assertEquals(' ', args[1]) |
|||
end |
|||
function suite:testReadOnly() |
|||
local args = suite.getDefaultArgs{readOnly = true} |
|||
local function testFunc() |
|||
args.newKey = 'some new value' |
|||
end |
end |
||
if options.backtranslate == nil then |
|||
self:assertError(testFunc) |
|||
options.backtranslate = {} |
|||
end |
|||
for k,v in pairs(options.translate) do |
|||
options.backtranslate[v] = k |
|||
function suite:testNoOverwriteExistingKey() |
|||
end |
|||
local args = suite.getDefaultArgs{noOverwrite = true} |
|||
end |
|||
self:assertEquals(d.firstFrameArg, args[1]) |
|||
if options.backtranslate and getmetatable(options.backtranslate) == nil then |
|||
local function testFunc() |
|||
setmetatable(options.backtranslate, { |
|||
args[1] = 'a new first frame argument' |
|||
__index = function(t, k) |
|||
if options.translate[k] ~= k then |
|||
return nil |
|||
else |
|||
return k |
|||
end |
|||
end |
|||
}) |
|||
end |
end |
||
self:assertError(testFunc) |
|||
end |
|||
--[[ |
|||
function suite:testNoOverwriteNewKey() |
|||
-- Get the argument tables. If we were passed a valid frame object, get the |
|||
local args = suite.getDefaultArgs{noOverwrite = true} |
|||
-- frame arguments (fargs) and the parent frame arguments (pargs), depending |
|||
self:assertEquals(nil, args.newKey) |
|||
-- on the options set and on the parent frame's availability. If we weren't |
|||
args.newKey = 'some new value' |
|||
-- passed a valid frame object, we are being called from another Lua module |
|||
self:assertEquals('some new value', args.newKey) |
|||
-- or from the debug console, so assume that we were passed a table of args |
|||
end |
|||
-- directly, and assign it to a new variable (luaArgs). |
|||
--]] |
|||
local fargs, pargs, luaArgs |
|||
if type(frame.args) == 'table' and type(frame.getParent) == 'function' then |
|||
if options.wrappers then |
|||
--[[ |
|||
-- The wrappers option makes Module:Arguments look up arguments in |
|||
-- either the frame argument table or the parent argument table, but |
|||
-- not both. This means that users can use either the #invoke syntax |
|||
-- or a wrapper template without the loss of performance associated |
|||
-- with looking arguments up in both the frame and the parent frame. |
|||
-- Module:Arguments will look up arguments in the parent frame |
|||
-- if it finds the parent frame's title in options.wrapper; |
|||
-- otherwise it will look up arguments in the frame object passed |
|||
-- to getArgs. |
|||
--]] |
|||
local parent = frame:getParent() |
|||
if not parent then |
|||
fargs = frame.args |
|||
else |
|||
local title = parent:getTitle():gsub('/proves$', '') |
|||
local found = false |
|||
if matchesTitle(options.wrappers, title) then |
|||
found = true |
|||
elseif type(options.wrappers) == 'table' then |
|||
for _,v in pairs(options.wrappers) do |
|||
if matchesTitle(v, title) then |
|||
found = true |
|||
break |
|||
end |
|||
end |
|||
end |
|||
-- We test for false specifically here so that nil (the default) acts like true. |
|||
-------------------------------------------------------------------------- |
|||
if found or options.frameOnly == false then |
|||
-- Test bad input |
|||
pargs = parent.args |
|||
-------------------------------------------------------------------------- |
|||
end |
|||
if not found or options.parentOnly == false then |
|||
fargs = frame.args |
|||
end |
|||
end |
|||
else |
|||
-- options.wrapper isn't set, so check the other options. |
|||
if not options.parentOnly then |
|||
fargs = frame.args |
|||
end |
|||
if not options.frameOnly then |
|||
local parent = frame:getParent() |
|||
pargs = parent and parent.args or nil |
|||
end |
|||
end |
|||
if options.parentFirst then |
|||
fargs, pargs = pargs, fargs |
|||
end |
|||
else |
|||
luaArgs = frame |
|||
end |
|||
-- Set the order of precedence of the argument tables. If the variables are |
|||
function suite:testBadFrameInput() |
|||
-- nil, nothing will be added to the table, which is how we avoid clashes |
|||
self:assertError(getArgs, 'foo') |
|||
-- between the frame/parent args and the Lua args. |
|||
self:assertError(getArgs, 9) |
|||
local argTables = {fargs} |
|||
self:assertError(getArgs, true) |
|||
argTables[#argTables + 1] = pargs |
|||
self:assertError(getArgs, function() return true end) |
|||
argTables[#argTables + 1] = luaArgs |
|||
end |
|||
--[[ |
|||
function suite:testBadOptionsInput() |
|||
-- Generate the tidyVal function. If it has been specified by the user, we |
|||
self:assertError(getArgs, {}, 'foo') |
|||
-- use that; if not, we choose one of four functions depending on the |
|||
self:assertError(getArgs, {}, 9) |
|||
-- options chosen. This is so that we don't have to call the options table |
|||
self:assertError(getArgs, {}, true) |
|||
-- every time the function is called. |
|||
self:assertError(getArgs, {}, function() return true end) |
|||
--]] |
|||
end |
|||
local tidyVal = options.valueFunc |
|||
if tidyVal then |
|||
if type(tidyVal) ~= 'function' then |
|||
error( |
|||
"valor incorrecte assignat a l'opció 'valueFunc'" |
|||
.. '(s\'esperava una funció i s\'ha trobat ' |
|||
.. type(tidyVal) |
|||
.. ')', |
|||
2 |
|||
) |
|||
end |
|||
elseif options.trim ~= false then |
|||
if options.removeBlanks ~= false then |
|||
tidyVal = tidyValDefault |
|||
else |
|||
tidyVal = tidyValTrimOnly |
|||
end |
|||
else |
|||
if options.removeBlanks ~= false then |
|||
tidyVal = tidyValRemoveBlanksOnly |
|||
else |
|||
tidyVal = tidyValNoChange |
|||
end |
|||
end |
|||
--[[ |
|||
function suite:testBadValueFuncInput() |
|||
-- Set up the args, metaArgs and nilArgs tables. args will be the one |
|||
self:assertError(getArgs, {}, {valueFunc = 'foo'}) |
|||
-- accessed from functions, and metaArgs will hold the actual arguments. Nil |
|||
self:assertError(getArgs, {}, {valueFunc = 9}) |
|||
-- arguments are memoized in nilArgs, and the metatable connects all of them |
|||
self:assertError(getArgs, {}, {valueFunc = true}) |
|||
-- together. |
|||
self:assertError(getArgs, {}, {valueFunc = {}}) |
|||
--]] |
|||
end |
|||
local args, metaArgs, nilArgs, metatable = {}, {}, {}, {} |
|||
setmetatable(args, metatable) |
|||
local function mergeArgs(tables) |
|||
-------------------------------------------------------------------------- |
|||
--[[ |
|||
-- Test iterator metamethods |
|||
-- Accepts multiple tables as input and merges their keys and values |
|||
-------------------------------------------------------------------------- |
|||
-- into one table. If a value is already present it is not overwritten; |
|||
-- tables listed earlier have precedence. We are also memoizing nil |
|||
function suite:testPairs() |
|||
-- values, which can be overwritten if they are 's' (soft). |
|||
local args = getArgs{'foo', 'bar', baz = 'qux'} |
|||
--]] |
|||
self:assertNumberOfIterations(3, pairs, args) |
|||
for _, t in ipairs(tables) do |
|||
end |
|||
for key, val in pairs(t) do |
|||
if metaArgs[key] == nil and nilArgs[key] ~= 'h' then |
|||
function suite:testIpairs() |
|||
local tidiedVal = tidyVal(key, val) |
|||
local args = getArgs{'foo', 'bar', baz = 'qux'} |
|||
if tidiedVal == nil then |
|||
self:assertNumberOfIterations(2, ipairs, args) |
|||
nilArgs[key] = 's' |
|||
end |
|||
else |
|||
metaArgs[key] = tidiedVal |
|||
function suite:testNoNilsinPairs() |
|||
end |
|||
-- Test that when we use pairs, we don't iterate over any nil values |
|||
end |
|||
-- that have been memoized. |
|||
end |
|||
local args = getArgs{''} |
|||
end |
|||
local temp = args[1] -- Memoizes the nil |
|||
self:assertNumberOfIterations(0, pairs, args) |
|||
end |
|||
function suite:testNoNilsinIpairs() |
|||
-- Test that when we use ipairs, we don't iterate over any nil values |
|||
-- that have been memoized. |
|||
local args = getArgs{''} |
|||
local temp = args[1] -- Memoizes the nil |
|||
self:assertNumberOfIterations(0, ipairs, args) |
|||
end |
|||
function suite:testDeletedArgsInPairs() |
|||
-- Test that when we use pairs, we don't iterate over any values that have |
|||
-- been explicitly set to nil. |
|||
local args = getArgs{'foo'} |
|||
args[1] = nil |
|||
self:assertNumberOfIterations(0, pairs, args) |
|||
end |
|||
function suite:testDeletedArgsInIpairs() |
|||
-- Test that when we use ipairs, we don't iterate over any values that have |
|||
-- been explicitly set to nil. |
|||
local args = getArgs{'foo'} |
|||
args[1] = nil |
|||
self:assertNumberOfIterations(0, ipairs, args) |
|||
end |
|||
function suite:testNoNilsInPairsAfterIndex() |
|||
-- Test that when we use pairs, we don't iterate over any nils that |
|||
-- might have been memoized after a value that is not present in the |
|||
-- original args table is indexed. |
|||
local args = getArgs{} |
|||
local temp = args.someRandomValue -- Memoizes the nil |
|||
self:assertNumberOfIterations(0, pairs, args) |
|||
end |
|||
function suite:testNoNilsInPairsAfterNewindex() |
|||
-- Test that when we use pairs, we don't iterate over any nils that |
|||
-- might have been memoized after a value that is not present in the |
|||
-- original args table is added to the args table. |
|||
local args = getArgs{} |
|||
args.newKey = nil -- The nil is memoized |
|||
self:assertNumberOfIterations(0, pairs, args) |
|||
end |
|||
function suite:testNoTableLengthChangeWhileIterating() |
|||
-- Test that the number of arguments doesn't change if we index the |
|||
-- args table while iterating. |
|||
-- (Note that the equivalent test is not needed for new arg table |
|||
-- indexes, as that would be a user error - doing so produces |
|||
-- undetermined behaviour in Lua's next() function.) |
|||
local args = getArgs{'foo', 'bar', baz = 'qux'} |
|||
self:assertNumberOfIterations(3, pairs, args) |
|||
for k, v in pairs(args) do |
|||
local temp = args[k .. 'foo'] |
|||
end |
end |
||
self:assertNumberOfIterations(3, pairs, args) |
|||
end |
|||
--[[ |
|||
function suite:testPairsPrecedenceWithWhitespace() |
|||
-- Define metatable behaviour. Arguments are memoized in the metaArgs table, |
|||
local frame, parent = suite.getFrames( |
|||
-- and are only fetched from the argument tables once. Fetching arguments |
|||
d.frameTitle, |
|||
-- from the argument tables is the most resource-intensive step in this |
|||
{' '}, |
|||
-- module, so we try and avoid it where possible. For this reason, nil |
|||
d.parentTitle, |
|||
-- arguments are also memoized, in the nilArgs table. Also, we keep a record |
|||
{d.firstParentArg} |
|||
-- in the metatable of when pairs and ipairs have been called, so we do not |
|||
) |
|||
-- run pairs and ipairs on the argument tables more than once. We also do |
|||
local args = getArgs(frame) |
|||
-- not run ipairs on fargs and pargs if pairs has already been run, as all |
|||
local actual |
|||
-- the arguments will already have been copied over. |
|||
for k, v in pairs(args) do |
|||
--]] |
|||
actual = v |
|||
end |
|||
self:assertEquals(d.firstParentArg, actual) |
|||
-- Check that we have actually iterated. |
|||
self:assertNumberOfIterations(1, pairs, args) |
|||
end |
|||
metatable.__index = function (t, key) |
|||
function suite:testPairsPrecedenceWithNil() |
|||
--[[ |
|||
local frame, parent = suite.getFrames( |
|||
-- Fetches an argument when the args table is indexed. First we check |
|||
d.frameTitle, |
|||
-- to see if the value is memoized, and if not we try and fetch it from |
|||
{}, |
|||
-- the argument tables. When we check memoization, we need to check |
|||
d.parentTitle, |
|||
-- metaArgs before nilArgs, as both can be non-nil at the same time. |
|||
{d.firstParentArg} |
|||
-- If the argument is not present in metaArgs, we also check whether |
|||
) |
|||
-- pairs has been run yet. If pairs has already been run, we return nil. |
|||
local args = getArgs(frame) |
|||
-- This is because all the arguments will have already been copied into |
|||
local actual |
|||
-- metaArgs by the mergeArgs function, meaning that any other arguments |
|||
for k, v in pairs(args) do |
|||
-- must be nil. |
|||
actual = v |
|||
--]] |
|||
if type(key) == 'string' then |
|||
key = options.translate[key] |
|||
end |
|||
local val = metaArgs[key] |
|||
if val ~= nil then |
|||
return val |
|||
elseif metatable.donePairs or nilArgs[key] then |
|||
return nil |
|||
end |
|||
for _, argTable in ipairs(argTables) do |
|||
local argTableVal = tidyVal(key, argTable[key]) |
|||
if argTableVal ~= nil then |
|||
metaArgs[key] = argTableVal |
|||
return argTableVal |
|||
end |
|||
end |
|||
nilArgs[key] = 'h' |
|||
return nil |
|||
end |
end |
||
self:assertEquals(d.firstParentArg, actual) |
|||
-- Check that we have actually iterated. |
|||
self:assertNumberOfIterations(1, pairs, args) |
|||
end |
|||
metatable.__newindex = function (t, key, val) |
|||
function suite:testIpairsEarlyExit() |
|||
-- This function is called when a module tries to add a new value to the |
|||
local mt = {} |
|||
-- args table, or tries to change an existing value. |
|||
function mt.__index(t, k) |
|||
if |
if type(key) == 'string' then |
||
key = options.translate[key] |
|||
return 'foo' |
|||
end |
|||
elseif k == 2 then |
|||
if options.readOnly then |
|||
return 'bar' |
|||
error( |
|||
elseif k == 3 then |
|||
'no es pot canviar la taula d\'arguments "' |
|||
error('Expanded argument 3 unnecessarily') |
|||
.. tostring(key) |
|||
.. '"; és només de lectura', |
|||
2 |
|||
) |
|||
elseif options.noOverwrite and args[key] ~= nil then |
|||
error( |
|||
'no es pot canviar la taula d\'arguments "' |
|||
.. tostring(key) |
|||
.. '"; no està permès modificar els arguments existents', |
|||
2 |
|||
) |
|||
elseif val == nil then |
|||
--[[ |
|||
-- If the argument is to be overwritten with nil, we need to erase |
|||
-- the value in metaArgs, so that __index, __pairs and __ipairs do |
|||
-- not use a previous existing value, if present; and we also need |
|||
-- to memoize the nil in nilArgs, so that the value isn't looked |
|||
-- up in the argument tables if it is accessed again. |
|||
--]] |
|||
metaArgs[key] = nil |
|||
nilArgs[key] = 'h' |
|||
else |
|||
metaArgs[key] = val |
|||
end |
end |
||
end |
end |
||
function mt.__pairs(t) |
|||
local function translatenext(invariant) |
|||
error('Called pairs unnecessarily') |
|||
local k, v = next(invariant.t, invariant.k) |
|||
end |
|||
invariant.k = k |
|||
function mt.__ipairs(t) |
|||
if k == nil then |
|||
-- Works just like the default ipairs, except respecting __index |
|||
return |
return nil |
||
elseif type(k) ~= 'string' or not options.backtranslate then |
|||
local v = t[i + 1] |
|||
return k, v |
|||
if v ~= nil then |
|||
else |
|||
return i + 1, v |
|||
local backtranslate = options.backtranslate[k] |
|||
if backtranslate == nil then |
|||
-- Skip this one. This is a tail call, so this won't cause stack overflow |
|||
return translatenext(invariant) |
|||
else |
|||
return backtranslate, v |
|||
end |
end |
||
end, t, 0 |
|||
end |
|||
local args = getArgs(setmetatable({}, mt)) |
|||
for k,v in ipairs(args) do |
|||
if v == 'bar' then |
|||
break |
|||
end |
end |
||
end |
end |
||
end |
|||
metatable.__pairs = function () |
|||
-------------------------------------------------------------------------- |
|||
-- Called when pairs is run on the args table. |
|||
-- Test argument translation |
|||
if not metatable.donePairs then |
|||
-------------------------------------------------------------------------- |
|||
mergeArgs(argTables) |
|||
metatable.donePairs = true |
|||
function suite:testTranslationIndex() |
|||
end |
|||
local args = getArgs({F00 = 'one', ['8@r'] = 'two', ['8@z'] = 'three', qUx = 'four', foo = 'nope', untranslated = 'yep'}, {translate = d.translate}) |
|||
return translatenext, { t = metaArgs } |
|||
self:assertEquals('one', args.foo) |
|||
self:assertEquals('two', args.bar) |
|||
self:assertEquals('three', args.baz) |
|||
self:assertEquals('four', args.qux) |
|||
self:assertEquals('yep', args.untranslated) |
|||
end |
|||
function suite:testTranslationPairsWithAutoBacktranslate() |
|||
local args = getArgs({F00 = 'one', ['8@r'] = 'two', ['8@z'] = 'three', qUx = 'four', foo = 'nope', untranslated = 'yep'}, {translate = d.translate}) |
|||
local cleanArgs = {} |
|||
for k,v in pairs(args) do |
|||
cleanArgs[k] = v |
|||
end |
end |
||
self:assertDeepEquals( |
|||
{ |
|||
foo = 'one', |
|||
bar = 'two', |
|||
baz = 'three', |
|||
qux = 'four', |
|||
untranslated = 'yep' |
|||
}, |
|||
cleanArgs |
|||
) |
|||
end |
|||
local function inext(t, i) |
|||
function suite:testTranslationPairsWithBacktranslate() |
|||
-- This uses our __index metamethod |
|||
local args = getArgs({F00 = 'one', ['8@r'] = 'two', ['8@z'] = 'three', qUx = 'four', foo = 'nope', untranslated = 'yep'}, {translate = d.translate, backtranslate = {F00 = 'foo'}}) |
|||
local v = t[i + 1] |
|||
if v ~= nil then |
|||
for k,v in pairs(args) do |
|||
return i + 1, v |
|||
end |
|||
end |
end |
||
self:assertDeepEquals( |
|||
{ |
|||
foo = 'one', |
|||
['8@r'] = 'two', |
|||
['8@z'] = 'three', |
|||
qUx = 'four', |
|||
untranslated = 'yep' |
|||
}, |
|||
cleanArgs |
|||
) |
|||
end |
|||
metatable.__ipairs = function (t) |
|||
function suite:testTranslationPairsWithoutBacktranslate() |
|||
-- Called when ipairs is run on the args table. |
|||
local args = getArgs({F00 = 'one', ['8@r'] = 'two', ['8@z'] = 'three', qUx = 'four', foo = 'nope', untranslated = 'yep'}, {translate = d.translate, backtranslate = false}) |
|||
return inext, t, 0 |
|||
local cleanArgs = {} |
|||
for k,v in pairs(args) do |
|||
cleanArgs[k] = v |
|||
end |
end |
||
self:assertDeepEquals( |
|||
{ |
|||
F00 = 'one', |
|||
['8@r'] = 'two', |
|||
['8@z'] = 'three', |
|||
qUx = 'four', |
|||
foo = 'nope', |
|||
untranslated = 'yep' |
|||
}, |
|||
cleanArgs |
|||
) |
|||
end |
|||
return args |
|||
function suite:testTranslationNewindex() |
|||
local args = getArgs({F00 = 'one', ['8@r'] = 'two', ['8@z'] = 'three', qUx = 'four', foo = 'nope', untranslated = 'yep'}, {translate = d.translate, backtranslate = false}) |
|||
args.foo = 'changed1' |
|||
args.untranslated = 'changed2' |
|||
local cleanArgs = {} |
|||
for k,v in pairs(args) do |
|||
cleanArgs[k] = v |
|||
end |
|||
self:assertDeepEquals( |
|||
{ |
|||
F00 = 'changed1', |
|||
['8@r'] = 'two', |
|||
['8@z'] = 'three', |
|||
qUx = 'four', |
|||
foo = 'nope', |
|||
untranslated = 'changed2' |
|||
}, |
|||
cleanArgs |
|||
) |
|||
end |
|||
function suite:test_argument() |
|||
local currentFrame = mw.getCurrentFrame() |
|||
currentFrame.args[5] = 555; |
|||
local args = getArgs(currentFrame) |
|||
self:assertEquals('nil', type(args.foo)) |
|||
end |
end |
||
return |
return arguments |
Revision as of 00:56, 30 March 2021
![]() | This is the test cases page for the module Module:Arguments. Results of the test cases. |
-- This module provides easy processing of arguments passed to Scribunto from
-- #invoke. It is intended for use by other Lua modules, and should not be
-- called from #invoke directly.
local libraryUtil = require('libraryUtil')
local checkType = libraryUtil.checkType
local arguments = {}
-- Generate four different tidyVal functions, so that we don't have to check the
-- options every time we call it.
local function tidyValDefault(key, val)
if type(val) == 'string' then
val = val:match('^%s*(.-)%s*$')
if val == '' then
return nil
else
return val
end
else
return val
end
end
local function tidyValTrimOnly(key, val)
if type(val) == 'string' then
return val:match('^%s*(.-)%s*$')
else
return val
end
end
local function tidyValRemoveBlanksOnly(key, val)
if type(val) == 'string' then
if val:find('%S') then
return val
else
return nil
end
else
return val
end
end
local function tidyValNoChange(key, val)
return val
end
local function matchesTitle(given, title)
local tp = type( given )
return (tp == 'string' or tp == 'number') and mw.title.new( given ).prefixedText == title
end
local translate_mt = { __index = function(t, k) return k end }
function arguments.getArgs(frame, options)
checkType('getArgs', 1, frame, 'table', true)
checkType('getArgs', 2, options, 'table', true)
frame = frame or {}
options = options or {}
--[[
-- Set up argument translation.
--]]
options.translate = options.translate or {}
if getmetatable(options.translate) == nil then
setmetatable(options.translate, translate_mt)
end
if options.backtranslate == nil then
options.backtranslate = {}
for k,v in pairs(options.translate) do
options.backtranslate[v] = k
end
end
if options.backtranslate and getmetatable(options.backtranslate) == nil then
setmetatable(options.backtranslate, {
__index = function(t, k)
if options.translate[k] ~= k then
return nil
else
return k
end
end
})
end
--[[
-- Get the argument tables. If we were passed a valid frame object, get the
-- frame arguments (fargs) and the parent frame arguments (pargs), depending
-- on the options set and on the parent frame's availability. If we weren't
-- passed a valid frame object, we are being called from another Lua module
-- or from the debug console, so assume that we were passed a table of args
-- directly, and assign it to a new variable (luaArgs).
--]]
local fargs, pargs, luaArgs
if type(frame.args) == 'table' and type(frame.getParent) == 'function' then
if options.wrappers then
--[[
-- The wrappers option makes Module:Arguments look up arguments in
-- either the frame argument table or the parent argument table, but
-- not both. This means that users can use either the #invoke syntax
-- or a wrapper template without the loss of performance associated
-- with looking arguments up in both the frame and the parent frame.
-- Module:Arguments will look up arguments in the parent frame
-- if it finds the parent frame's title in options.wrapper;
-- otherwise it will look up arguments in the frame object passed
-- to getArgs.
--]]
local parent = frame:getParent()
if not parent then
fargs = frame.args
else
local title = parent:getTitle():gsub('/proves$', '')
local found = false
if matchesTitle(options.wrappers, title) then
found = true
elseif type(options.wrappers) == 'table' then
for _,v in pairs(options.wrappers) do
if matchesTitle(v, title) then
found = true
break
end
end
end
-- We test for false specifically here so that nil (the default) acts like true.
if found or options.frameOnly == false then
pargs = parent.args
end
if not found or options.parentOnly == false then
fargs = frame.args
end
end
else
-- options.wrapper isn't set, so check the other options.
if not options.parentOnly then
fargs = frame.args
end
if not options.frameOnly then
local parent = frame:getParent()
pargs = parent and parent.args or nil
end
end
if options.parentFirst then
fargs, pargs = pargs, fargs
end
else
luaArgs = frame
end
-- Set the order of precedence of the argument tables. If the variables are
-- nil, nothing will be added to the table, which is how we avoid clashes
-- between the frame/parent args and the Lua args.
local argTables = {fargs}
argTables[#argTables + 1] = pargs
argTables[#argTables + 1] = luaArgs
--[[
-- Generate the tidyVal function. If it has been specified by the user, we
-- use that; if not, we choose one of four functions depending on the
-- options chosen. This is so that we don't have to call the options table
-- every time the function is called.
--]]
local tidyVal = options.valueFunc
if tidyVal then
if type(tidyVal) ~= 'function' then
error(
"valor incorrecte assignat a l'opció 'valueFunc'"
.. '(s\'esperava una funció i s\'ha trobat '
.. type(tidyVal)
.. ')',
2
)
end
elseif options.trim ~= false then
if options.removeBlanks ~= false then
tidyVal = tidyValDefault
else
tidyVal = tidyValTrimOnly
end
else
if options.removeBlanks ~= false then
tidyVal = tidyValRemoveBlanksOnly
else
tidyVal = tidyValNoChange
end
end
--[[
-- Set up the args, metaArgs and nilArgs tables. args will be the one
-- accessed from functions, and metaArgs will hold the actual arguments. Nil
-- arguments are memoized in nilArgs, and the metatable connects all of them
-- together.
--]]
local args, metaArgs, nilArgs, metatable = {}, {}, {}, {}
setmetatable(args, metatable)
local function mergeArgs(tables)
--[[
-- Accepts multiple tables as input and merges their keys and values
-- into one table. If a value is already present it is not overwritten;
-- tables listed earlier have precedence. We are also memoizing nil
-- values, which can be overwritten if they are 's' (soft).
--]]
for _, t in ipairs(tables) do
for key, val in pairs(t) do
if metaArgs[key] == nil and nilArgs[key] ~= 'h' then
local tidiedVal = tidyVal(key, val)
if tidiedVal == nil then
nilArgs[key] = 's'
else
metaArgs[key] = tidiedVal
end
end
end
end
end
--[[
-- Define metatable behaviour. Arguments are memoized in the metaArgs table,
-- and are only fetched from the argument tables once. Fetching arguments
-- from the argument tables is the most resource-intensive step in this
-- module, so we try and avoid it where possible. For this reason, nil
-- arguments are also memoized, in the nilArgs table. Also, we keep a record
-- in the metatable of when pairs and ipairs have been called, so we do not
-- run pairs and ipairs on the argument tables more than once. We also do
-- not run ipairs on fargs and pargs if pairs has already been run, as all
-- the arguments will already have been copied over.
--]]
metatable.__index = function (t, key)
--[[
-- Fetches an argument when the args table is indexed. First we check
-- to see if the value is memoized, and if not we try and fetch it from
-- the argument tables. When we check memoization, we need to check
-- metaArgs before nilArgs, as both can be non-nil at the same time.
-- If the argument is not present in metaArgs, we also check whether
-- pairs has been run yet. If pairs has already been run, we return nil.
-- This is because all the arguments will have already been copied into
-- metaArgs by the mergeArgs function, meaning that any other arguments
-- must be nil.
--]]
if type(key) == 'string' then
key = options.translate[key]
end
local val = metaArgs[key]
if val ~= nil then
return val
elseif metatable.donePairs or nilArgs[key] then
return nil
end
for _, argTable in ipairs(argTables) do
local argTableVal = tidyVal(key, argTable[key])
if argTableVal ~= nil then
metaArgs[key] = argTableVal
return argTableVal
end
end
nilArgs[key] = 'h'
return nil
end
metatable.__newindex = function (t, key, val)
-- This function is called when a module tries to add a new value to the
-- args table, or tries to change an existing value.
if type(key) == 'string' then
key = options.translate[key]
end
if options.readOnly then
error(
'no es pot canviar la taula d\'arguments "'
.. tostring(key)
.. '"; és només de lectura',
2
)
elseif options.noOverwrite and args[key] ~= nil then
error(
'no es pot canviar la taula d\'arguments "'
.. tostring(key)
.. '"; no està permès modificar els arguments existents',
2
)
elseif val == nil then
--[[
-- If the argument is to be overwritten with nil, we need to erase
-- the value in metaArgs, so that __index, __pairs and __ipairs do
-- not use a previous existing value, if present; and we also need
-- to memoize the nil in nilArgs, so that the value isn't looked
-- up in the argument tables if it is accessed again.
--]]
metaArgs[key] = nil
nilArgs[key] = 'h'
else
metaArgs[key] = val
end
end
local function translatenext(invariant)
local k, v = next(invariant.t, invariant.k)
invariant.k = k
if k == nil then
return nil
elseif type(k) ~= 'string' or not options.backtranslate then
return k, v
else
local backtranslate = options.backtranslate[k]
if backtranslate == nil then
-- Skip this one. This is a tail call, so this won't cause stack overflow
return translatenext(invariant)
else
return backtranslate, v
end
end
end
metatable.__pairs = function ()
-- Called when pairs is run on the args table.
if not metatable.donePairs then
mergeArgs(argTables)
metatable.donePairs = true
end
return translatenext, { t = metaArgs }
end
local function inext(t, i)
-- This uses our __index metamethod
local v = t[i + 1]
if v ~= nil then
return i + 1, v
end
end
metatable.__ipairs = function (t)
-- Called when ipairs is run on the args table.
return inext, t, 0
end
return args
end
return arguments