<?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%2FSpy</id>
	<title>Module:Lua-mock/Spy - 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%2FSpy"/>
	<link rel="alternate" type="text/html" href="https://mywikibiz.com/index.php?title=Module:Lua-mock/Spy&amp;action=history"/>
	<updated>2026-06-15T14:23:46Z</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/Spy&amp;diff=478651&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/Spy&amp;diff=478651&amp;oldid=prev"/>
		<updated>2021-07-16T04:44:27Z</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;--- @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 'Module:Lua-mock/ValueMatcher'&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('Should be called '..count..' times, but was called '..#self.calls..' times.', 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('No call at index '..query.atIndex..'.  Recorded only '..#self.calls..' calls.', 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('No calls selected.', 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 = 'Argument %d of call %d mismatched:  %s'&lt;br /&gt;
local returnValueMismatchedMessage = 'Return value %d of call %d mismatched:  %s'&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;
-- - 'atIndex': 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;
-- - 'arguments': A list of value matchers, that is compared to the actual arguments.&lt;br /&gt;
-- - 'returnValues': 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('No call matched.', 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>Zoran</name></author>
	</entry>
</feed>