<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-GB">
	<id>https://stockhub.co/index.php?action=history&amp;feed=atom&amp;title=Module%3ALua-mock%2FSpy</id>
	<title>Module:Lua-mock/Spy - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://stockhub.co/index.php?action=history&amp;feed=atom&amp;title=Module%3ALua-mock%2FSpy"/>
	<link rel="alternate" type="text/html" href="https://stockhub.co/index.php?title=Module:Lua-mock/Spy&amp;action=history"/>
	<updated>2026-05-26T04:31:51Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.5</generator>
	<entry>
		<id>https://stockhub.co/index.php?title=Module:Lua-mock/Spy&amp;diff=143898&amp;oldid=prev</id>
		<title>imported&gt;Mr. Stradivarius: Undid revision 1025765883 by Mr. Stradivarius (talk) definitely needs to be plain unpack in Scribunto, not table.unpack</title>
		<link rel="alternate" type="text/html" href="https://stockhub.co/index.php?title=Module:Lua-mock/Spy&amp;diff=143898&amp;oldid=prev"/>
		<updated>2021-05-29T12:44:19Z</updated>

		<summary type="html">&lt;p&gt;Undid revision 1025765883 by &lt;a href=&quot;/research/Special:Contributions/Mr._Stradivarius&quot; title=&quot;Special:Contributions/Mr. Stradivarius&quot;&gt;Mr. Stradivarius&lt;/a&gt; (&lt;a href=&quot;/index.php?title=User_talk:Mr._Stradivarius&amp;amp;action=edit&amp;amp;redlink=1&quot; class=&quot;new&quot; title=&quot;User talk:Mr. Stradivarius (page does not exist)&quot;&gt;talk&lt;/a&gt;) definitely needs to be plain unpack in Scribunto, not table.unpack&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;--- @classmod Spy&lt;br /&gt;
--- Wraps a function and records the calls.&lt;br /&gt;
-- For each call the arguments and return values are saved.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
local ValueMatcher = require &amp;#039;Module:Lua-mock/ValueMatcher&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
local Spy = {}&lt;br /&gt;
Spy.__index = Spy&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
function Spy:__call( ... )&lt;br /&gt;
    local returnValues = { self.wrappedFn(...) }&lt;br /&gt;
    local call = {&lt;br /&gt;
        arguments = {...},&lt;br /&gt;
        returnValues = returnValues&lt;br /&gt;
    }&lt;br /&gt;
    table.insert(self.calls, call)&lt;br /&gt;
    return unpack(returnValues)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function Spy:reset()&lt;br /&gt;
    self.calls = {}&lt;br /&gt;
    return self&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
--- Test if the spy was called exactly `count` times.&lt;br /&gt;
function Spy:assertCallCount( count )&lt;br /&gt;
    if #self.calls ~= count then&lt;br /&gt;
        error(&amp;#039;Should be called &amp;#039;..count..&amp;#039; times, but was called &amp;#039;..#self.calls..&amp;#039; times.&amp;#039;, 2)&lt;br /&gt;
    end&lt;br /&gt;
    return self&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function Spy:_getSelectedCalls( query, level )&lt;br /&gt;
    level = level or 1&lt;br /&gt;
    local selectedCalls = {}&lt;br /&gt;
&lt;br /&gt;
    if query.atIndex then&lt;br /&gt;
        local call = self.calls[query.atIndex]&lt;br /&gt;
        if call then&lt;br /&gt;
            table.insert(selectedCalls, call)&lt;br /&gt;
        else&lt;br /&gt;
            error(&amp;#039;No call at index &amp;#039;..query.atIndex..&amp;#039;.  Recorded only &amp;#039;..#self.calls..&amp;#039; calls.&amp;#039;, level+1)&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    -- Use wildcard as default:&lt;br /&gt;
    if not query.atIndex then&lt;br /&gt;
        selectedCalls = self.calls&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if not #selectedCalls then&lt;br /&gt;
        error(&amp;#039;No calls selected.&amp;#039;, level+1)&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    return selectedCalls&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
local argumentMismatchedMessage = &amp;#039;Argument %d of call %d mismatched:  %s&amp;#039;&lt;br /&gt;
local returnValueMismatchedMessage = &amp;#039;Return value %d of call %d mismatched:  %s&amp;#039;&lt;br /&gt;
&lt;br /&gt;
--- Test if some calls match specific properties.&lt;br /&gt;
--&lt;br /&gt;
-- Specify a set of calls to test, by adding:&lt;br /&gt;
-- - &amp;#039;atIndex&amp;#039;: Test only the call at the given index.&lt;br /&gt;
-- - nothing: Acts as a wildcard and will select all calls.&lt;br /&gt;
--&lt;br /&gt;
-- Specify a set of call attributes to test, by adding:&lt;br /&gt;
-- - &amp;#039;arguments&amp;#039;: A list of value matchers, that is compared to the actual arguments.&lt;br /&gt;
-- - &amp;#039;returnValues&amp;#039;: A list of value matchers, that is compared to the actual return values.&lt;br /&gt;
function Spy:assertCallMatches( query, level )&lt;br /&gt;
    level = level or 1&lt;br /&gt;
&lt;br /&gt;
    local selectedCalls = self:_getSelectedCalls(query, level+1)&lt;br /&gt;
&lt;br /&gt;
    for callIndex, call in ipairs(selectedCalls) do&lt;br /&gt;
        if query.arguments then&lt;br /&gt;
            local matches, mismatchedIndex, mismatchReason =&lt;br /&gt;
                ValueMatcher.matches(call.arguments, query.arguments)&lt;br /&gt;
            if not matches then&lt;br /&gt;
                error(argumentMismatchedMessage:format(mismatchedIndex,&lt;br /&gt;
                                                       callIndex,&lt;br /&gt;
                                                       mismatchReason),&lt;br /&gt;
                      level+1)&lt;br /&gt;
            end&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if query.returnValues then&lt;br /&gt;
            local matches, mismatchedIndex, mismatchReason =&lt;br /&gt;
                ValueMatcher.matches(call.returnValues, query.returnValues)&lt;br /&gt;
            if not matches then&lt;br /&gt;
                error(returnValueMismatchedMessage:format(mismatchedIndex,&lt;br /&gt;
                                                          callIndex,&lt;br /&gt;
                                                          mismatchReason),&lt;br /&gt;
                      level+1)&lt;br /&gt;
            end&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    return self&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
--- Test if at least one call match specific properties.&lt;br /&gt;
-- Acts like #Spy:assertCallMatches, but succeeds if at least one&lt;br /&gt;
-- call matches.&lt;br /&gt;
function Spy:assertAnyCallMatches( query, level )&lt;br /&gt;
    level = level or 1&lt;br /&gt;
&lt;br /&gt;
    local selectedCalls = self:_getSelectedCalls(query, level+1)&lt;br /&gt;
&lt;br /&gt;
    local oneMatched = false&lt;br /&gt;
    for _, call in ipairs(selectedCalls) do&lt;br /&gt;
        local argumentsMatch = true&lt;br /&gt;
        if query.arguments then&lt;br /&gt;
            argumentsMatch = ValueMatcher.matches(call.arguments, query.arguments)&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        local returnValuesMatch = true&lt;br /&gt;
        if query.returnValues then&lt;br /&gt;
            returnValuesMatch = ValueMatcher.matches(call.returnValues, query.returnValues)&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        if argumentsMatch and returnValuesMatch then&lt;br /&gt;
            oneMatched = true&lt;br /&gt;
            break&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if not oneMatched then&lt;br /&gt;
        error(&amp;#039;No call matched.&amp;#039;, level+1)&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    return self&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
return function( wrappedFn )&lt;br /&gt;
    local self = setmetatable({ wrappedFn = wrappedFn }, Spy)&lt;br /&gt;
    self:reset()&lt;br /&gt;
    return self&lt;br /&gt;
end&lt;/div&gt;</summary>
		<author><name>imported&gt;Mr. Stradivarius</name></author>
	</entry>
</feed>