Documentation for this module may be created at Module:Sandbox/Tom.Reding/sandbox/doc
--use "local arg1 = frame.args[1]" for sandboxing via #invoke
--use "local args = frame:getParent().args" for use via template
local p = {}
function p.string_match(frame)
local r = "2008–09 in Scottish women's football"
local regex_hyph = '%d%d%d%d[–-]%d+'
-- if string.match(r, regex_hyph) then --returns 'false'!
-- if mw.ustring.match(r, regex_hyph) then --returns 'true'
if string.match(r, 'ball$') then --returns 'true'
return 'true'
else
return 'false'
end
end
function p.avoidself(frame)
local currtitle = mw.title.getCurrentTitle()
local testcases = string.match(currtitle.subpageText, '^testcases')
local avoidself = (currtitle.text ~= 'Navseasoncats' and
not string.match(currtitle.text, 'Navseasoncats with') and
not string.match(currtitle.text, 'Navseasoncats.*/doc') and
not string.match(currtitle.text, 'Navseasoncats.*/sandbox') and
currtitle.nsText ~= 'User_talk' and
(currtitle.nsText ~= 'Template' or testcases)) --avoid nested transclusion errors (i.e. {{Infilmdecade}})
return 'text: '..currtitle.text..
'<br/>nsText: "'..currtitle.nsText..'"'..
'<br/>testcases: '..(testcases or 'nil')..
'<br/>avoidself: '..tostring(avoidself)
end
function p.ttest(frame) --#t
local t0 = {} --0
local t1 = { [0] = 1 } --0
local t2 = { [1] = 1 } --1
local t31 = { [0] = 1, [1] = 1, [2] = 1 } --2
local t32 = { [2] = 1, [3] = 1, [4] = 1 } --0
local t33 = { [3] = 1, [4] = 1, [5] = 1 } --0
local tneg = { [-1]=1, [0] = 1, [1] = 1 } --1
--output
return '#t0: ' ..#t0.. --0
'<br/>#t1: ' ..#t1.. --0
'<br/>#t2: ' ..#t2.. --1
'<br/>#t31: '..#t31.. --2
'<br/>#t32: '..#t32.. --0
'<br/>#t33: '..#t33.. --0
'<br/>#tneg: '..#tneg.. --1
'<br/>mw.log'..(mw.log( #t31 ) or '')
end
function p.indexcalctest(frame)
local t = {}
t[1+3] = 5
return t[4] or 'nil' --returns 5 as expected
end
function p.fort(frame)
local i = 0
for i=1, 10 do
i = i + 1
end
return i --returns '0' as expected
end
function p.whilet(frame)
local i = 0
while i < 10 do
i = i + 1
end
return i --returns '10' as expected
end
function p.ifevaluationtest(frame)
local cat1 = '2010s awards'
local cat2 = '2020s awards'
if mw.title.new(cat1, 'Category').exists and
mw.title.new(cat2, 'Category').exists
then
return 1
end
return 0
end
function p.zerotest(frame)
local neg = -0 --displays '-0' as expected
local nneg = tonumber('-0') --displays '-0' as expected
local pos = 0 --displays '-0' NOT as expected
local ppos = tonumber('0') --displays '0'
local pppos = tonumber('+0') --displays '0' as expected
local s = tostring(pos) --displays '-0' NOT as expected
local ss = tostring(ppos) --displays '0'
local t = (neg < 0) --false
local tt = (s == '-0') --true
local u = (neg == 0) --true
local v = (neg == -0) --true
return tostring(nneg)..' '..pppos..' '..s..' '..tostring(tt)..' '..tostring(v) --returns '-0 0 -0 true true'
end
function p.test(frame)
local conf = require( "Module:Authority control" ).conf
local out = ''
for _, c in pairs( conf ) do
out = out..'<br/>'..c[1]
end
return out
end
function p.iffalsetest(frame)
local f = false
if f then
return 'f'
else
return 'endd' --debugger thinks "'end'" == "end"...
end --returns endd, as expected
end
function p.fortest(frame)
local out = ''
for gs = 0, 5 do
out = out..gs
end
return out --returns 012345
end
function p.regex_group_test(frame)
local R = 'asdfasdf 2020–'
local y, hyph, ending = mw.ustring.match(R, '^.-(%d+)([–-])(.*)$')
return (y or 'nil')..', '..(hyph or 'nil')..', '..(ending or 'nil') --returns '2020, –,' as expected
end
function p.empty_regex_group_test(frame)
local text = '2020–'
-- return (mw.ustring.match(text, '%d+([–-])$') or 'nil') --returns –
-- return (mw.ustring.match(text, '%d+[–-]()$') or 'nil') --returns 6
-- return (mw.ustring.match(text, '%d+[–-]($)') or 'nil') --returns nil
return (mw.ustring.match(text, '%d+[–-](.*)$') or 'nil') --returns '' as expected
end
function p.regex_gsub_test(frame)
local cat1 = '2006 Super 14 season'
local base1 = '"'..mw.ustring.gsub(cat1, '%d+[–-]?%d?%d?%d?%d?', '')..'"' --returns " Super season"
local base1_1 = '"'..mw.ustring.gsub(cat1, '%d+[–-]?%d?%d?%d?%d?', '', 1)..'"' --returns " Super 14 season"
local cat2 = '14 Super 2006 season'
local base2 = '"'..mw.ustring.gsub(cat2, '%d+[–-]?%d?%d?%d?%d?', '')..'"' --returns " Super season"
local base2_2 = '"'..mw.ustring.gsub(cat2, '%d+[–-]?%d?%d?%d?%d?', '', 1)..'"' --returns " Super 2006 season"
local base2_3 = '"'..mw.ustring.gsub(cat2, '%d%d?%d?%d?[–-]?%d?%d?%d?%d?', '')..'"' --returns " Super season"
local base2_4 = '"'..mw.ustring.gsub(cat2, '%d%d?%d?%d?[–-]?%d?%d?%d?%d?', '', 1)..'"' --returns " Super 2006 season"
return base2_4
end
function p.match_or_match_test(frame)
local text = '2020–'
-- local y, hyph = mw.ustring.match(text, '(%d+)([–-])present$') or mw.ustring.match(text, '(%d+)([–-])$')
-- return (y or 'nil')..' & '..(hyph or 'nil') --returns 2020 & nil (NOT as expected)
-- local y, hyph = mw.ustring.match(text, '(%d+)([–-])$') or mw.ustring.match(text, '(%d+)([–-])present$')
-- return (y or 'nil')..' & '..(hyph or 'nil') --returns 2020 & nil (NOT as expected)
-- local y, hyph = mw.ustring.match(text, '(%d+)([–-])$')
-- return (y or 'nil')..' & '..(hyph or 'nil') --returns 2020 & – (as expected)
local y, hyph = mw.ustring.match(text, '(%d+)([–-])present$')
return (y or 'nil')..' & '..(hyph or 'nil') --returns nil & nil (as expected)
end
function p.mw_title_new_exists_wstest(frame)
local cat = 'UK MPs 2015–2017 '..' '
return tostring(mw.title.new( cat, 'Category' ).exists) --returns true (as expected)
end
function p.tonumber_test(frame)
local cat = 'Wikipedia articles with VIAF identifiers' --800,000 something
local lang = mw.getContentLanguage()
local count = mw.site.stats.pagesInCategory(cat, 'pages') --800000
local count_lang = lang:formatNum( count ) --800,000
-- return (count or 'nil')..', '..(tonumber(count or '-1') or '-2') --returns 880843, 880843 as expected
-- return (count_lang or 'nil')..', '..(tonumber(count_lang or '-1') or '-2') --returns 881,195, -2 --use lang:parseFormattedNumber() instead
return (count_lang or 'nil')..', '..(lang:parseFormattedNumber(count_lang or '-1') or '-2') --returns 880,358, 880358
end
function p.lastedit_test(frame)
return mw.title.new( 'Special:Contributions/Tom.Reding', 'Special' ):getContent() or 'nil'
end
function p.split_test(frame)
-- return table.concat( mw.text.split('test-1 two-', ' '), ',' ) or 'nil' --returns test-1,two-
-- return table.concat( mw.text.split('test-1 two-', '%s'), ',' ) or 'nil' --returns test-1,two-
-- return table.concat( mw.text.split('test-1 two-', '-'), ',' ) or 'nil' --returns test,1 two,
-- return table.concat( mw.text.split('test-1 two-', ' %-'), ',' ) or 'nil' --returns test-1 two-
-- return table.concat( mw.text.split('test-1 two-', ' -'), ',' ) or 'nil' --returns t,e,s,t,-,1, ,t,w,o,-
-- return table.concat( mw.text.split('test-1 two-', '- '), ',' ) or 'nil' --returns test-1 two-
-- return table.concat( mw.text.split('test-1 two-', '%s%-'), ',' ) or 'nil' --returns test-1 two-
return table.concat( mw.text.split('test-1 two-', '[%s%-]'), ',' ) or 'nil' --returns test,1,two,
end
function p.regex_greed_and_nested_groups_test(frame)
local t = '{{category redirect | {{title year}}s in New Spain }}'
local r = '{{category redirect%s*|%s*([^{}]*{{([^{|}]+)}}[^{}]-)%s*}}'
local g1, g2 = mw.ustring.match(t, r)
return '"'..g1..'" "'..g2..'"' --returns "{{title year}}s in New Spain" "title year" as expected
end
function p.redirectTarget_test(frame)
-- local title = mw.title.new( '1670s in Mexico', 'Category' ) --#R --returns false
-- local title2 = mw.title.new( '1670s in New Spain', 'Category' ) --#R target --returns false
-- local title = mw.title.makeTitle( 'Category', '1670s in Mexico' ) --#R --returns false
-- local title2 = mw.title.makeTitle( 'Category', '1670s in New Spain' ) --#R target --returns false
local title = mw.title.new( 'EL 61' ) --#R --returns "Haumea"
local title2 = mw.title.new( 'Haumea' ) --#R target --returns "false"
local rtitle = title.redirectTarget --only works in mainspace; ignorant of {{category redirect}}s
local rtitle2 = title2.redirectTarget --only works in mainspace; ignorant of {{category redirect}}s
if type(rtitle) == 'boolean' then
return tostring(rtitle)..' '..tostring(rtitle)
else
return '"'..title.redirectTarget.baseText..'" "'..tostring(rtitle2)..'"'
end
end
function p.frame_newChild_test(frame)
local t = 'Category:1670s in Mexico'
local child = frame:newChild{ title = t, args = {} }
-- return '"'..child:getTitle()..'"' --returns "Category:1670s in Mexico" as expected
-- return '"'..child:expandTemplate{ title = 'title year', args = {} }..'"' --returns "" NOT as expected...
-- return '"'..child:expandTemplate{ title = 'title year', args = { page = t } }..'"' --returns "1670" as expected
return '"'..frame:expandTemplate{ title = 'title year', args = { page = t } }..'"' --returns "1670" as expected
end
function p.ns_test(frame)
local title = mw.title.getCurrentTitle()
local namespace = title.namespace
return namespace
end
return p