https://en.wikipedia.org/w/index.php?action=history&feed=atom&title=Module%3ALua-mock%2FValueMatcher
Module:Lua-mock/ValueMatcher - Revision history
2025-06-05T17:26:15Z
Revision history for this page on the wiki
MediaWiki 1.45.0-wmf.3
https://en.wikipedia.org/w/index.php?title=Module:Lua-mock/ValueMatcher&diff=1025766378&oldid=prev
Mr. Stradivarius: looks like typeName doesn't do anything here (and is a global, so Module:No globals raises an error), so remove it
2021-05-29T12:46:19Z
<p>looks like typeName doesn't do anything here (and is a global, so <a href="/w/index.php?title=Module:No_globals&action=edit&redlink=1" class="new" title="Module:No globals (page does not exist)">Module:No globals</a> raises an error), so remove it</p>
<table style="background-color: #fff; color: #202122;" data-mw="interface">
<col class="diff-marker" />
<col class="diff-content" />
<col class="diff-marker" />
<col class="diff-content" />
<tr class="diff-title" lang="en">
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">← Previous revision</td>
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">Revision as of 12:46, 29 May 2021</td>
</tr><tr>
<td colspan="2" class="diff-lineno">Line 74:</td>
<td colspan="2" class="diff-lineno">Line 74:</td>
</tr>
<tr>
<td class="diff-marker"></td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>-- the according index and an error message if a value did not match.</div></td>
<td class="diff-marker"></td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>-- the according index and an error message if a value did not match.</div></td>
</tr>
<tr>
<td class="diff-marker"></td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>local function matchValues( values, matchedValues )</div></td>
<td class="diff-marker"></td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>local function matchValues( values, matchedValues )</div></td>
</tr>
<tr>
<td class="diff-marker" data-marker="−"></td>
<td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div> typeName = typeName or 'Value'</div></td>
<td colspan="2" class="diff-empty diff-side-added"></td>
</tr>
<tr>
<td class="diff-marker" data-marker="−"></td>
<td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><br /></td>
<td colspan="2" class="diff-empty diff-side-added"></td>
</tr>
<tr>
<td class="diff-marker"></td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> if #values ~= #matchedValues then</div></td>
<td class="diff-marker"></td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> if #values ~= #matchedValues then</div></td>
</tr>
<tr>
<td class="diff-marker"></td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> return false, valueCountMismatchMessage:format(#values,</div></td>
<td class="diff-marker"></td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> return false, valueCountMismatchMessage:format(#values,</div></td>
</tr>
</table>
Mr. Stradivarius
https://en.wikipedia.org/w/index.php?title=Module:Lua-mock/ValueMatcher&diff=895026068&oldid=prev
Mr. Stradivarius: copy ValueMatcher.lua from https://luarocks.org/modules/henry4k/lua-mock; the software is public domain
2019-05-01T14:46:29Z
<p>copy ValueMatcher.lua from https://luarocks.org/modules/henry4k/lua-mock; the software is public domain</p>
<p><b>New page</b></p><div>--- @module ValueMatcher<br />
<br />
local valueMismatchMessage =<br />
'did not match:\n'..<br />
' was: %s\n'..<br />
'expected: %s'<br />
<br />
local valueCountMismatchMessage =<br />
'mismatch:\n'..<br />
' was: %d\n'..<br />
'expected: %d'<br />
<br />
<br />
--- A matcher is used to determine if a value statisfies some condition.<br />
-- The test function is called with the value that shall be tested.<br />
-- The function returns `true` if the condition is statisfied.<br />
-- Otherwise the function shall return `false` and an error message that<br />
-- describes the problem.<br />
local function createMatcher( testFn )<br />
return {<br />
isMatcher = true,<br />
match = testFn<br />
}<br />
end<br />
<br />
local function createEqualityMatcher( matchedValue )<br />
return createMatcher(function( value )<br />
if value == matchedValue then<br />
return true<br />
else<br />
return false, valueMismatchMessage:format(tostring(value),<br />
tostring(matchedValue))<br />
end<br />
end)<br />
end<br />
<br />
local function createTableMatcher( matchedTable )<br />
return createMatcher(function( value )<br />
if value == matchedTable then<br />
return true<br />
elseif type(value) == 'table' then<br />
-- TODO<br />
return false, 'Can\'t recursively match tables at the moment.'<br />
else<br />
return false, valueMismatchMessage:format(tostring(value),<br />
tostring(matchedTable))<br />
end<br />
end)<br />
end<br />
<br />
--- Test a single value.<br />
-- Will automatically create a matcher if `matchedValue` is not one.<br />
-- Like a matcher it returns `true` if the condition is statisfied or `false`<br />
-- with an error message if the condition is not statisfied.<br />
local function matchValue( value, matchedValue )<br />
local matcher<br />
<br />
if type(matchedValue) == 'table' then<br />
if matchedValue.isMatcher then<br />
matcher = matchedValue<br />
else<br />
matcher = createTableMatcher(matchedValue)<br />
end<br />
else<br />
matcher = createEqualityMatcher(matchedValue)<br />
end<br />
<br />
return matcher.match(value)<br />
end<br />
<br />
<br />
--- Tests multiple values.<br />
-- Like a #matchValue it returns `true` if all values matched or `false` with<br />
-- the according index and an error message if a value did not match.<br />
local function matchValues( values, matchedValues )<br />
typeName = typeName or 'Value'<br />
<br />
if #values ~= #matchedValues then<br />
return false, valueCountMismatchMessage:format(#values,<br />
#matchedValues)<br />
end<br />
<br />
for i,matchedValue in ipairs(matchedValues) do<br />
local value = values[i]<br />
local matched, message = matchValue(value, matchedValue)<br />
if not matched then<br />
return false, i, message<br />
end<br />
end<br />
<br />
return true<br />
end<br />
<br />
<br />
<br />
local ValueMatcher = {}<br />
<br />
--- Tests if the values match `matchedValues`.<br />
--<br />
-- @param value<br />
--<br />
-- @param matchedValues<br />
-- A list that consists of regular values or matchers.<br />
--<br />
-- @return<br />
-- `true` if all values match or `false` if at least one don't.<br />
-- Also returns the value index and a reason when failing.<br />
function ValueMatcher.matches( value, matchedValues )<br />
return matchValues(value, matchedValues)<br />
end<br />
<br />
--- Matches any value.<br />
ValueMatcher.any = createMatcher(function( value )<br />
return true<br />
end)<br />
<br />
--- Matches any value but nil.<br />
ValueMatcher.notNil = createMatcher(function( value )<br />
if value == nil then<br />
return false, 'was nil.'<br />
else<br />
return true<br />
end<br />
end)<br />
<br />
--- Matches a specific value type.<br />
ValueMatcher.matchType = function( typeName )<br />
return createMatcher(function( value )<br />
if type(value) == typeName then<br />
return true<br />
else<br />
return false, ('was not a %s, but a %s.'):format(typeName, type(value))<br />
end<br />
end)<br />
end<br />
<br />
--- Matches a boolean value.<br />
ValueMatcher.anyBoolean = ValueMatcher.matchType('boolean')<br />
<br />
--- Matches a number.<br />
ValueMatcher.anyNumber = ValueMatcher.matchType('number')<br />
<br />
--- Matches a string.<br />
ValueMatcher.anyString = ValueMatcher.matchType('string')<br />
<br />
--- Matches a table.<br />
ValueMatcher.anyTable = ValueMatcher.matchType('table')<br />
<br />
--- Matches a function.<br />
ValueMatcher.anyFunction = ValueMatcher.matchType('function')<br />
<br />
--- Matches a thread.<br />
ValueMatcher.anyThread = ValueMatcher.matchType('thread')<br />
<br />
--- Matches a user data.<br />
ValueMatcher.anyUserData = ValueMatcher.matchType('userdata')<br />
<br />
<br />
return ValueMatcher</div>
Mr. Stradivarius