<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://mywikibiz.com/index.php?action=history&amp;feed=atom&amp;title=Module%3ALua-mock%2FValueMatcher</id>
	<title>Module:Lua-mock/ValueMatcher - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://mywikibiz.com/index.php?action=history&amp;feed=atom&amp;title=Module%3ALua-mock%2FValueMatcher"/>
	<link rel="alternate" type="text/html" href="https://mywikibiz.com/index.php?title=Module:Lua-mock/ValueMatcher&amp;action=history"/>
	<updated>2026-06-14T22:59:43Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.35.3</generator>
	<entry>
		<id>https://mywikibiz.com/index.php?title=Module:Lua-mock/ValueMatcher&amp;diff=478652&amp;oldid=prev</id>
		<title>Zoran: Pywikibot 6.4.0</title>
		<link rel="alternate" type="text/html" href="https://mywikibiz.com/index.php?title=Module:Lua-mock/ValueMatcher&amp;diff=478652&amp;oldid=prev"/>
		<updated>2021-07-16T04:44:29Z</updated>

		<summary type="html">&lt;p&gt;Pywikibot 6.4.0&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;--- @module ValueMatcher&lt;br /&gt;
&lt;br /&gt;
local valueMismatchMessage =&lt;br /&gt;
    'did not match:\n'..&lt;br /&gt;
    '     was: %s\n'..&lt;br /&gt;
    'expected: %s'&lt;br /&gt;
&lt;br /&gt;
local valueCountMismatchMessage =&lt;br /&gt;
    'mismatch:\n'..&lt;br /&gt;
    '     was: %d\n'..&lt;br /&gt;
    'expected: %d'&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
--- A matcher is used to determine if a value statisfies some condition.&lt;br /&gt;
-- The test function is called with the value that shall be tested.&lt;br /&gt;
-- The function returns `true` if the condition is statisfied.&lt;br /&gt;
-- Otherwise the function shall return `false` and an error message that&lt;br /&gt;
-- describes the problem.&lt;br /&gt;
local function createMatcher( testFn )&lt;br /&gt;
    return {&lt;br /&gt;
        isMatcher = true,&lt;br /&gt;
        match = testFn&lt;br /&gt;
    }&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
local function createEqualityMatcher( matchedValue )&lt;br /&gt;
    return createMatcher(function( value )&lt;br /&gt;
        if value == matchedValue then&lt;br /&gt;
            return true&lt;br /&gt;
        else&lt;br /&gt;
            return false, valueMismatchMessage:format(tostring(value),&lt;br /&gt;
                                                      tostring(matchedValue))&lt;br /&gt;
        end&lt;br /&gt;
    end)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
local function createTableMatcher( matchedTable )&lt;br /&gt;
    return createMatcher(function( value )&lt;br /&gt;
        if value == matchedTable then&lt;br /&gt;
            return true&lt;br /&gt;
        elseif type(value) == 'table' then&lt;br /&gt;
            -- TODO&lt;br /&gt;
            return false, 'Can\'t recursively match tables at the moment.'&lt;br /&gt;
        else&lt;br /&gt;
            return false, valueMismatchMessage:format(tostring(value),&lt;br /&gt;
                                                      tostring(matchedTable))&lt;br /&gt;
        end&lt;br /&gt;
    end)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
--- Test a single value.&lt;br /&gt;
-- Will automatically create a matcher if `matchedValue` is not one.&lt;br /&gt;
-- Like a matcher it returns `true` if the condition is statisfied or `false`&lt;br /&gt;
-- with an error message if the condition is not statisfied.&lt;br /&gt;
local function matchValue( value, matchedValue )&lt;br /&gt;
    local matcher&lt;br /&gt;
&lt;br /&gt;
    if type(matchedValue) == 'table' then&lt;br /&gt;
        if matchedValue.isMatcher then&lt;br /&gt;
            matcher = matchedValue&lt;br /&gt;
        else&lt;br /&gt;
            matcher = createTableMatcher(matchedValue)&lt;br /&gt;
        end&lt;br /&gt;
    else&lt;br /&gt;
        matcher = createEqualityMatcher(matchedValue)&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    return matcher.match(value)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
--- Tests multiple values.&lt;br /&gt;
-- Like a #matchValue it returns `true` if all values matched or `false` with&lt;br /&gt;
-- the according index and an error message if a value did not match.&lt;br /&gt;
local function matchValues( values, matchedValues )&lt;br /&gt;
    if #values ~= #matchedValues then&lt;br /&gt;
        return false, valueCountMismatchMessage:format(#values,&lt;br /&gt;
                                                       #matchedValues)&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    for i,matchedValue in ipairs(matchedValues) do&lt;br /&gt;
        local value = values[i]&lt;br /&gt;
        local matched, message = matchValue(value, matchedValue)&lt;br /&gt;
        if not matched then&lt;br /&gt;
            return false, i, message&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    return true&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
local ValueMatcher = {}&lt;br /&gt;
&lt;br /&gt;
--- Tests if the values match `matchedValues`.&lt;br /&gt;
--&lt;br /&gt;
-- @param value&lt;br /&gt;
--&lt;br /&gt;
-- @param matchedValues&lt;br /&gt;
-- A list that consists of regular values or matchers.&lt;br /&gt;
--&lt;br /&gt;
-- @return&lt;br /&gt;
-- `true` if all values match or `false` if at least one don't.&lt;br /&gt;
-- Also returns the value index and a reason when failing.&lt;br /&gt;
function ValueMatcher.matches( value, matchedValues )&lt;br /&gt;
    return matchValues(value, matchedValues)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
--- Matches any value.&lt;br /&gt;
ValueMatcher.any = createMatcher(function( value )&lt;br /&gt;
    return true&lt;br /&gt;
end)&lt;br /&gt;
&lt;br /&gt;
--- Matches any value but nil.&lt;br /&gt;
ValueMatcher.notNil = createMatcher(function( value )&lt;br /&gt;
    if value == nil then&lt;br /&gt;
        return false, 'was nil.'&lt;br /&gt;
    else&lt;br /&gt;
        return true&lt;br /&gt;
    end&lt;br /&gt;
end)&lt;br /&gt;
&lt;br /&gt;
--- Matches a specific value type.&lt;br /&gt;
ValueMatcher.matchType = function( typeName )&lt;br /&gt;
    return createMatcher(function( value )&lt;br /&gt;
        if type(value) == typeName then&lt;br /&gt;
            return true&lt;br /&gt;
        else&lt;br /&gt;
            return false, ('was not a %s, but a %s.'):format(typeName, type(value))&lt;br /&gt;
        end&lt;br /&gt;
    end)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
--- Matches a boolean value.&lt;br /&gt;
ValueMatcher.anyBoolean  = ValueMatcher.matchType('boolean')&lt;br /&gt;
&lt;br /&gt;
--- Matches a number.&lt;br /&gt;
ValueMatcher.anyNumber   = ValueMatcher.matchType('number')&lt;br /&gt;
&lt;br /&gt;
--- Matches a string.&lt;br /&gt;
ValueMatcher.anyString   = ValueMatcher.matchType('string')&lt;br /&gt;
&lt;br /&gt;
--- Matches a table.&lt;br /&gt;
ValueMatcher.anyTable    = ValueMatcher.matchType('table')&lt;br /&gt;
&lt;br /&gt;
--- Matches a function.&lt;br /&gt;
ValueMatcher.anyFunction = ValueMatcher.matchType('function')&lt;br /&gt;
&lt;br /&gt;
--- Matches a thread.&lt;br /&gt;
ValueMatcher.anyThread   = ValueMatcher.matchType('thread')&lt;br /&gt;
&lt;br /&gt;
--- Matches a user data.&lt;br /&gt;
ValueMatcher.anyUserData = ValueMatcher.matchType('userdata')&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
return ValueMatcher&lt;/div&gt;</summary>
		<author><name>Zoran</name></author>
	</entry>
</feed>