Gaan na inhoud

Module:String/toetsgevalle

in Wikipedia, die vrye ensiklopedie

Dokumentasie vir hierdie module kan geskep word by: Module:String/toetsgevalle/doc

-- Unit tests for [[Module:String]]. Click talk page to run tests.
local p = {}
 
local frame, tick, cross
local result_table_header = "{|class=\"wikitable\"\n! !! Text !! Expected !! Actual"
local result_table = ''
local num_failures = 0

--[[
function first_difference(s1, s2)
    if s1 == s2 then return '' end
    local max = math.min(#s1, #s2)
    for i = 1, max do
        if s1:sub(i,i) ~= s2:sub(i,i) then return i end
    end
    return max + 1
end

function p:preprocess_equals_many(prefix, suffix, cases, options)
    for _, case in ipairs(cases) do
        self:preprocess_equals(prefix .. case[1] .. suffix, case[2], options)
    end
end

function p:preprocess_equals(text, expected, options)
    local actual = frame:preprocess(text)
    if actual == expected then
        result_table = result_table .. '| ' .. tick
    else
        result_table = result_table .. '| ' .. cross
        num_failures = num_failures + 1
    end
    local nowiki_open = (options and options.nowiki) and '<nowiki>' or ''
    local nowiki_close = (options and options.nowiki) and '</nowiki>' or ''
    local differs_at = self.differs_at and (' || ' .. first_difference(expected, actual)) or ''
    result_table = result_table .. ' || <nowiki>' .. text:gsub('%|', '&#124;') .. '</nowiki> || ' .. nowiki_open .. expected .. nowiki_close .. ' || ' .. nowiki_open .. actual .. nowiki_close .. differs_at .. "\n|-\n"
end

function p:test_len()
    self:preprocess_equals_many('{{#invoke:String/sandput|len|', '}}', {
        {' foo ', '5'},
        {'s= foo ', '3'},
        {'', '0'},
        {'s=', '0'},
        {'   ', '3'},
        {'最近の更新', '5'},
    })
end

function p:test_replace()
    self:preprocess_equals_many('{{#invoke:String/sandput|vervang|', '}}', {
        {'1.1.1.1|.|/', '1/1/1/1'},
        {'alpha (beta)| (| gamma (', 'alpha gamma (beta)'},
        {'Dog (cat)|%s%(.*%)||plain=false', 'Dog'},
    })
end

function p:test_match()
    self:preprocess_equals_many('{{#invoke:String/sandput|match|', '}}', {
        {'dog (cat)|%((%a-)%)', 'cat'},
        {'dog (cat) (canary) (fish)|%((%a-)%)||-1', 'fish'},
        {'dog (cat) (canary) (fish)|%((%a-)%)||2', 'canary'},
        {'dog (cat) (canary) (fish)|%((%a-)%)|6|1', 'canary'},
        {'dog (cat) (canary) (fish)|%((%a-)%)|6|2', 'fish'},
        {'dog (cat)|%((%a-)%)||2|no_category=true', '<strong class="error">String Module Error: Match not found</strong>'},
        {'dog (cat)|%((%a-)%)||2|ignore_errors=true', ''},
    })
end
function p:test_match()
    self:preprocess_equals_many('{{#invoke:String/sandput|match|', '}}', {
        {'dog (cat)|%((%a-)%)', 'cat'},
        {'dog (cat) (canary) (fish)|%((%a-)%)||-1', 'fish'},
        {'dog (cat) (canary) (fish)|%((%a-)%)||2', 'canary'},
        {'dog (cat) (canary) (fish)|%((%a-)%)|6|1', 'canary'},
        {'dog (cat) (canary) (fish)|%((%a-)%)|6|2', 'fish'},
        {'dog (cat)|%((%a-)%)||2|no_category=true', '<strong class="error">String Module Error: Match not found</strong>'},
        {'dog (cat)|%((%a-)%)||2|ignore_errors=true', ''},
    })
end

function p:test_find()
    self:preprocess_equals_many('{{#invoke:String/sandput|find|', '}}', {
        {'', '0'},
        {'source=', '0'},
        {'source=|target=', '0'},
        {'source=|target=Bob', '0'},
        {'Bob|Joe', '0'},
        {'Bob|b', '3'},
        {'Bob Joe|oe', '6'},
        {'source= Bob |o', '2'},
        {' Bob |o', '3'},
        {'높지만 급격히|만', '3'},
        {'source=BAAAB|target=%w|plain=false', '1'},
        {'source=BAAAB|target=%w|start=3|plain=false', '3'},
        {'source=BAAAB|target=(%w)%1|plain=false', '2'},
    })
end

function p:test_rep()
    self:preprocess_equals_many('{{#invoke:String/sandput|rep|', '}}', {
        {'abc|1', 'abc'},
        {'a b|7', 'a ba ba ba ba ba ba b'},
        {'bla|glug|no_category=true', '<strong class="error">String Module Error: function rep expects a number as second parameter, received "glug"</strong>'},
        {'bla|glug|ignore_errors=true', ''},
    })
end
]]
return p