<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://moasspedia.org/w/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://moasspedia.org/w/index.php?action=history&amp;feed=atom&amp;title=Module%3ALua-mock%2FValueMatcher"/>
	<link rel="alternate" type="text/html" href="https://moasspedia.org/w/index.php?title=Module:Lua-mock/ValueMatcher&amp;action=history"/>
	<updated>2026-04-17T02:44:26Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.37.2</generator>
	<entry>
		<id>https://moasspedia.org/w/index.php?title=Module:Lua-mock/ValueMatcher&amp;diff=2626&amp;oldid=prev</id>
		<title>Wikipedia&gt;Mr. Stradivarius: looks like typeName doesn&#039;t do anything here (and is a global, so Module:No globals raises an error), so remove it</title>
		<link rel="alternate" type="text/html" href="https://moasspedia.org/w/index.php?title=Module:Lua-mock/ValueMatcher&amp;diff=2626&amp;oldid=prev"/>
		<updated>2021-05-29T12:46:19Z</updated>

		<summary type="html">&lt;p&gt;looks like typeName doesn&amp;#039;t do anything here (and is a global, so &lt;a href=&quot;/wiki/Module:No_globals&quot; title=&quot;Module:No globals&quot;&gt;Module:No globals&lt;/a&gt; raises an error), so remove it&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;
    &amp;#039;did not match:\n&amp;#039;..&lt;br /&gt;
    &amp;#039;     was: %s\n&amp;#039;..&lt;br /&gt;
    &amp;#039;expected: %s&amp;#039;&lt;br /&gt;
&lt;br /&gt;
local valueCountMismatchMessage =&lt;br /&gt;
    &amp;#039;mismatch:\n&amp;#039;..&lt;br /&gt;
    &amp;#039;     was: %d\n&amp;#039;..&lt;br /&gt;
    &amp;#039;expected: %d&amp;#039;&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) == &amp;#039;table&amp;#039; then&lt;br /&gt;
            -- TODO&lt;br /&gt;
            return false, &amp;#039;Can\&amp;#039;t recursively match tables at the moment.&amp;#039;&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) == &amp;#039;table&amp;#039; 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&amp;#039;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, &amp;#039;was nil.&amp;#039;&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, (&amp;#039;was not a %s, but a %s.&amp;#039;):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(&amp;#039;boolean&amp;#039;)&lt;br /&gt;
&lt;br /&gt;
--- Matches a number.&lt;br /&gt;
ValueMatcher.anyNumber   = ValueMatcher.matchType(&amp;#039;number&amp;#039;)&lt;br /&gt;
&lt;br /&gt;
--- Matches a string.&lt;br /&gt;
ValueMatcher.anyString   = ValueMatcher.matchType(&amp;#039;string&amp;#039;)&lt;br /&gt;
&lt;br /&gt;
--- Matches a table.&lt;br /&gt;
ValueMatcher.anyTable    = ValueMatcher.matchType(&amp;#039;table&amp;#039;)&lt;br /&gt;
&lt;br /&gt;
--- Matches a function.&lt;br /&gt;
ValueMatcher.anyFunction = ValueMatcher.matchType(&amp;#039;function&amp;#039;)&lt;br /&gt;
&lt;br /&gt;
--- Matches a thread.&lt;br /&gt;
ValueMatcher.anyThread   = ValueMatcher.matchType(&amp;#039;thread&amp;#039;)&lt;br /&gt;
&lt;br /&gt;
--- Matches a user data.&lt;br /&gt;
ValueMatcher.anyUserData = ValueMatcher.matchType(&amp;#039;userdata&amp;#039;)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
return ValueMatcher&lt;/div&gt;</summary>
		<author><name>Wikipedia&gt;Mr. Stradivarius</name></author>
	</entry>
</feed>