# MediaWiki 'Arrays' extension Parser test cases # Author: Daniel Werner # Generate required templates. # These are the defaults set in $egArraysExpansionEscapeTemplates !!article Template:(( !!text {{ !!endarticle !!article Template:)) !!text }} !!endarticle !!article Template:= !!text = !!endarticle !!article Template:! !!text | !!endarticle ## ## 0. Misc ## !! test Inclusion of special page does not reset arrays. Did not work before 'Arrays 2.0' rewrite !! input "{{#arraydefine:a|test}}{{#arraydefine:foo|{{Special:AllPages}} }}", "{{#arraysize:a}}" !!result
"", "1"
!! end ## ## 1. Construction ## #arraydefine ## !! functionhooks arraydefine !! endfunctionhooks !! test 'arraydefine' with default parameters. !! input {{#arraydefine: a | a, b, , a, a, }} "{{#arrayprint: a}}" !!result"a, b, , a, a,"
!! end !! test ';' as separator, unique option !! input {{#arraydefine: a | a; ; b; a; a ; ; | ; | unique }} {{#arrayprint: a}} !!resulta, b
!! end !! test 'arraydefine' using an regular expression !! input {{#arraydefine: a | a b c d | /\s+/ }} {{#arrayprint: a}} !!resulta, b, c, d
!! end !! test 'arraydefine' with empty definition should create empty array and one with 'singleempty' option !! input {{#arraydefine: a | }}{{#arraydefine: b | |, | singleempty }} "{{#arraysize: a}}", "{{#arraysize: b}}" !!result"0", "1"
!! end ## ## 2. Extracting information ## #arrayprint ## !! test 'arrayprint' with non-existent array. !! input "{{#arrayprint: non-existent-array }}" !!result""
!! end !! test 'arrayprint' with array key output. !! input {{#arraydefine: a | a, bb, ccc}} "{{#arrayprint: a |"a
bb
ccc"
"123"
!! end !! test Test whether escaping special chars in 'arrayprint' works (this is related to $egArraysExpansionEscapeTemplates ) !! input {{#arraydefine: a | a{{))}}, {{!}}bb=bb, cc{{))}}c}} "{{#arrayprint: a | , | @ | @ }}" !!result"a}},|bb=bb,cc}}c"
!! end !!article Template:ArrayTestHelper !!text {{ #arrayprint:{{{name|}}} |{{{sep|}}} |@@@@ | {{#if:{{{1|}}}|"{{{1|}}}",}} "@@@@" {{#len:@@@@}} }} !!endarticle !! test 'arrayprint' within a template with parameter in print statement !! input {{#arraydefine: a | a, bb, ccc}} {{ArrayTestHelper |name=a |sep="fo}}o", "a" 1
"fo}}o", "bb" 2
"fo}}o", "ccc" 3
"", ""
!! end !! test 'arrayindex' with existing and non-existing keys. !! input {{#arraydefine: a | 1 }} "{{#arrayindex: a | 0 }}", "{{#arrayindex: a | 100 }}", "{{#arrayindex: a | xxx }}" !!result"1", "", ""
!! end !! test 'arrayindex' with negative index. !! input {{#arraydefine: a | a,b,c,d,e }}"{{ #arrayindex: a | -1 }}", "{{ #arrayindex: a | -0 }}", "{{ #arrayindex: a | -111 }}" !!result"e", "a", ""
!! end !! test the need for the 'default=' sucks, should be removed some day. !! input "{{ #arrayindex: undefined-array | }}", "{{ #arrayindex: undefined-array | 0| dflt }}" !!result"", "dflt"
!! end !! test existing index, empty value "", default should jump in !! input "{{ #arraydefine: a | A }}{{ #arrayindex: a | A | dflt }}" !!result"dflt"
!! end !! test default expansion test !! input "{{ #arraydefine: a | A }}{{ #vardefine: var }}{{ #arrayindex: a | 0 | {{#vardefine: var | 1 }} }}{{ #var: var }}", "{{ #arrayindex: a | 1 | {{#vardefine: var | 2 }} }}{{ #var: var }}" !!result"A", "2"
!! end ## ## #arraysize ## !! test 'arraysize' with non-defined array. !! input "{{#arraysize: undefined-array}}" !!result""
!! end !! test 'arraysize' with defined, empty array. !! input {{#arraydefine: a}} "{{#arraysize: a}}" !!result"0"
!! end !! test 'arraysize' non-empty array. !! input {{#arraydefine: a | a, 2 }} "{{#arraysize: a}}" !!result"2"
!! end ## ## #arraysearch ## !! test 'arraysearch' without data !! input "{{#arraysearch:}}" !!result""
!! end !! test 'arraysearch' returning '-1' on not found sucks returns empty string since 2.0 rewrite !! input "{{#arraysearch:}}" !!result""
!! end !! test 'arraysearch' without data !! input "{{ #arraydefine:a|a}}{{ #arraysearch:a| a |0|yes|no}}", "{{ #arraysearch:a|foo|0|yes|no }}" !!result"yes", "no"
!! end !!article Template:ArrayTests:arraysearch !!text {{#arraydefine: arrSearch | red, blue, 1, 5, green, 3, 1, 1, 2}} !!endarticle !! test Some basic 'arraysearch' !! input {{ArrayTests:arraysearch}} "{{#arraysearch: arrSearch | 1 }}", "{{#arraysearch: arrSearch | red }}", "{{#arraysearch: arrSearch | 1 | 3 }}", "{{#arraysearch: arrSearch | /\d+/isx }}" !!result"2", "0", "6", "2"
!! end !! test With negative start index !! input {{ArrayTests:arraysearch}} "{{#arraysearch: arrSearch | 1 | -1 }}", "{{#arraysearch: arrSearch | 1 | -2 }}", "{{#arraysearch: arrSearch | 1 | -20 }}" !!result"", "7", "2"
!! end !! test 'arraysearch' parameter 'yes' and 'no' expansion test. !! input {{ArrayTests:arraysearch}}{{ #arraydefine: arrSearch | red, blue, 1, 5, green, 3, 1, 1, 2}}{{ #arraysearch: arrSearch | 1 | 0 | {{#vardefine:yes|1}} | {{#vardefine:no|1}} }}"{{#var:yes}}_{{#var:no}}", {{#arraysearch: arrSearch | xxx | 0 | {{#vardefine:yes2|1}} | {{#vardefine:no2|1}} }}"{{#var:yes2}}_{{#var:no2}}" !!result"1_", "_1"
!! end ## ## #arraysearcharray ## !!article Template:ArrayTests:arraysearcharray !!text {{ #arrayreset: }}{{ #arraydefine: arrSearch | red, green 1, yellow 1 , 33 , 21, , , 9, 0, , 0 , 2, 1 , 1, red, blue }} !!endarticle !! test Only parameter one !! input "{{ ArrayTests:arraysearcharray }}{{ #arrayreset: a }}{{ #arraysearcharray: a }}{{ #arraysize: a }}" !!result"0"
!! end !! test Empty, this will find the three empty elements !! input "{{ ArrayTests:arraysearcharray }}{{ #arraysearcharray: a | arrSearch }}{{ #arrayprint: a | , }}" !!result",,"
!! end !! test Search for '1' !! input "{{ ArrayTests:arraysearcharray }}{{ #arraysearcharray: a | arrSearch | 1 }}{{ #arrayprint: a }}" !!result"1, 1"
!! end !! test Regex search expression, index and offset omitted !! input "{{ ArrayTests:arraysearcharray }}{{ #arraysearcharray: a | arrSearch | /\d+/ | | }}{{ #arrayprint: a }}" !!result"green 1, yellow 1, 33, 21, 9, 0, 0, 2, 1, 1"
!! end !! test Regex search with limit !! input "{{ ArrayTests:arraysearcharray }}{{ #arraysearcharray: a | arrSearch | /\d+/ | 0 | 4 }}{{ #arrayprint: a }}" !!result"green 1, yellow 1, 33, 21"
!! end !! test Regex search with limit '0' !! input "{{ ArrayTests:arraysearcharray }}{{ #arraysearcharray: a | arrSearch | /\d+/ | 0 | 0 }}{{ #arrayprint: a }}" !!result""
!! end !! test Regex search with limit and start index !! input "{{ ArrayTests:arraysearcharray }}{{ #arraysearcharray: a | arrSearch | /\d+/ | 2 | 4 }}{{ #arrayprint: a }}" !!result"yellow 1, 33, 21, 9"
!! end !! test Regex search and transform !! input "{{ ArrayTests:arraysearcharray }}{{ #arraysearcharray: a | arrSearch | /(\d+)/ | 2 | 3 | $0 + $1 }}{{ #arrayprint: a }}" !!result"yellow 1 + 1, 33 + 33, 21 + 21"
!! end !! test Regex search and transform with limit and negative start index !! input "{{ ArrayTests:arraysearcharray }}{{ #arraysearcharray: a | arrSearch | /(\d+)/ | -6 | 4 | +$1 }}{{ #arrayprint: a }}" !!result"+0, +2, +1, +1"
!! end !! test Using Subpage Fun extension with 'e' modifier !! input "{{ ArrayTests:arraysearcharray }}{{ #arraydefine: b | 55555, 333, 4444, 1, 22, 999999999, 88888888, 4444 }}{{ #arraysearcharray: b | b | /\d+/e | 0 | -1 | {{#len:$0}} }}{{ #arrayprint: b }}", "{{ #arrayindex: b | 0 }}" !!result"5, 3, 4, 1, 2, 9, 8, 4", "5"
!! end ## ## #arrayslice ## !! test empty 'arrayslice' !! input "{{ #arrayreset: a }}{{ #arrayslice: a }}{{ #arraysize: a }}" !!result"0"
!! end !! test 'arrayslice' without offset using for cloning an array !! input "{{ #arraydefine: a | a, b, c, d, e, f }}{{ #arrayslice: a | a }}{{ #arrayprint: a }}" !!result"a, b, c, d, e, f"
!! end !! test 'arrayslice' with offset and limit !! input "{{ #arraydefine: a | a, b, c, d, e, f }}{{ #arrayslice: a | a | 1 | 2 }}{{ #arrayprint: a }}" !!result"b, c"
!! end !! test 'arrayslice' with negative offset !! input "{{ #arraydefine: a | a, b, c, d, e, f }}{{ #arrayslice: a | a | -2 | 100 }}{{ #arrayprint: a }}" !!result"e, f"
!! end !! test 'arrayslice' with negative offset and limit !! input "{{ #arraydefine: a | a, b, c, d, e, f }}{{ #arrayslice: a | a | -3 | -1 }}{{ #arrayprint: a }}" !!result"d, e"
!! end !! test 'arrayslice' with negative offset and too hight negative limit !! input "{{ #arraydefine: a | a, b, c, d, e, f }}{{ #arrayslice: a | a | -3 | -99 }}{{ #arrayprint: a }}" !!result""
!! end !! test 'arrayslice' with negative offset below '0' !! input "{{ #arraydefine: a | a, b, c, d, e, f }}{{ #arrayslice: a | a | -99 }}{{ #arrayprint: a }}" !!result"a, b, c, d, e, f"
!! end ## ## 3. Alteration ## #arrayreset ## !! test 'arrayreset' certain arrayes !! input "{{ #arraydefine: a }}{{#arraydefine: b }}{{ #arrayreset: a | xxx }}{{ #arraysize: a }}{{#arraysize: b }}" !!result"0"
!! end !! test 'arrayreset' all arrayes !! input "{{ #arraydefine: a }}{{#arraydefine: b }}{{ #arrayreset: }}{{ #arraysize: a }}{{#arraysize: b }}" !!result""
!! end !! test 'arrayreset' compatbility-mode !! input "{{ #arraydefine: a }}{{#arraydefine: b }}{{ #arrayreset: a, xxx }}{{ #arraysize: a }}{{#arraysize: b }}" !!result"00"
!! end ## ## #arrayunique ## !! test 'arrayunique' also removes empty elements! !! input "{{ #arraydefine: a }}{{#arraydefine: a | a,b,a,b, , a, ,a,b }}{{ #arrayunique: a }}{{ #arrayprint: a }}" !!result"a, b"
!! end ## ## #arraysort ## !!article Template:ArrayTests:arraysort !!text {{#arraydefine: arrSort | c, g, b, e, a, b, f, e, d }} !!endarticle !! test 'arraysort' ascending order !! input "{{ ArrayTests:arraysort }}{{ #arraysort: arrSort | asce }}{{ #arrayprint: arrSort }}" !!result"a, b, b, c, d, e, e, f, g"
!! end !! test 'arraysort' descending order !! input "{{ ArrayTests:arraysort }}{{ #arraysort: arrSort | desc }}{{ #arrayprint: arrSort }}" !!result"g, f, e, e, d, c, b, b, a"
!! end !! test 'arraysort' ascending order and then revert !! input "{{ ArrayTests:arraysort }}{{ #arraysort: arrSort | asc }}{{ #arraysort: arrSort | reverse }}{{ #arrayprint: arrSort }}" !!result"g, f, e, e, d, c, b, b, a"
!! end ## ## 4. Interaction between arrays ## #arraymerge ## !!article Template:ArrayTests:arrayinteractions !!text {{ #arrayreset: }}{{ #arraydefine: 1 | a, c, d, e, f, g }}{{ #arraydefine: 2 | a, b, d, x, y, z }}{{ #arraydefine: 3 | a, b, e, x, 1, 2 }} !!endarticle !! test 'arraymerge' with only one parameter !! input "{{ ArrayTests:arrayinteractions }}{{ #arrayreset: new1 }}{{ #arraymerge: new1 }}{{ #arraysize: new1 }}" !!result"0"
!! end !! test 'arraymerge' with only one array This already worked before 2.0! !! input "{{ ArrayTests:arrayinteractions }}{{ #arrayreset: new1 }}{{ #arraymerge: new1 | 1 }}{{ #arraysize: new1 }}" !!result"6"
!! end !! test 'arraymerge' of two tables !! input "{{ ArrayTests:arrayinteractions }}{{ #arraymerge: new1 | 1 | 2 }}{{ #arrayprint: new1 }}" !!result"a, c, d, e, f, g, a, b, d, x, y, z"
!! end !! test 'arraymerge' of three tables !! input "{{ ArrayTests:arrayinteractions }}{{ #arraymerge: new1 | 1 | 2 | 3 }}{{ #arrayprint: new1 }}" !!result"a, c, d, e, f, g, a, b, d, x, y, z, a, b, e, x, 1, 2"
!! end ## ## #arrayunion ## !! test 'arrayunion' with only one parameter !! input "{{ ArrayTests:arrayinteractions }}{{ #arrayreset: new1 }}{{ #arrayunion: new1 }}{{ #arraysize: new1 }}" !!result"0"
!! end !! test 'arrayunion' with only one array !! input "{{ ArrayTests:arrayinteractions }}{{ #arrayreset: new1 }}{{ #arrayunion: new1 | 1 }}{{ #arraysize: new1 }}" !!result"6"
!! end !! test 'arrayunion' merging two tables !! input "{{ ArrayTests:arrayinteractions }}{{ #arrayunion: new1 | 1 | 2 }}{{ #arrayprint: new1 }}" !!result"a, c, d, e, f, g, b, x, y, z"
!! end !! test 'arrayunion' of three tables !! input "{{ ArrayTests:arrayinteractions }}{{ #arrayunion: new1 | 1 | 2 | 3 }}{{ #arrayprint: new1 }}" !!result"a, c, d, e, f, g, b, x, y, z, 1, 2"
!! end ## ## #arraydiff ## !! test 'arraydiff' with only one parameter !! input "{{ ArrayTests:arrayinteractions }}{{ #arrayreset: new1 }}{{ #arraydiff: new1 }}{{ #arraysize: new1 }}" !!result"0"
!! end !! test 'arraydiff' with only one array !! input "{{ ArrayTests:arrayinteractions }}{{ #arrayreset: new1 }}{{ #arraydiff: new1 | 1 }}{{ #arraysize: new1 }}" !!result"6"
!! end !! test 'arraydiff' comparing two tables !! input "{{ ArrayTests:arrayinteractions }}{{ #arraydiff: new1 | 1 | 2 }}{{ #arrayprint: new1 }}" !!result"c, e, f, g"
!! end !! test 'arraydiff' of three tables !! input "{{ ArrayTests:arrayinteractions }}{{ #arraydiff: new1 | 1 | 2 | 3 }}{{ #arrayprint: new1 }}" !!result"c, f, g"
!! end ## ## #arrayintersect ## !! test 'arrayintersect' with only one parameter !! input "{{ ArrayTests:arrayinteractions }}{{ #arrayreset: new1 }}{{ #arrayintersect: new1 }}{{ #arraysize: new1 }}" !!result"0"
!! end !! test 'arrayintersect' with only one array !! input "{{ ArrayTests:arrayinteractions }}{{ #arrayreset: new1 }}{{ #arrayintersect: new1 | 1 }}{{ #arraysize: new1 }}" !!result"6"
!! end !! test 'arrayintersect' comparing two tables !! input "{{ ArrayTests:arrayinteractions }}{{ #arrayintersect: new1 | 1 | 2 }}{{ #arrayprint: new1 }}" !!result"a, d"
!! end !! test 'arrayintersect' of three tables !! input "{{ ArrayTests:arrayinteractions }}{{ #arrayintersect: new1 | 1 | 2 | 3 }}{{ #arrayprint: new1 }}" !!result"a"
!! end