Jump to content

Module:Template test case: Difference between revisions

memoize expensive method calls in the Template objects
(add TestCase:renderRows)
(memoize expensive method calls in the Template objects)
Line 13: Line 13:


local Template = {}
local Template = {}
Template.__index = Template
 
Template.memoizedMethods = {
-- Names of methods to be memoized in each object. This table should only
-- hold methods with no parameters.
getFullPage = true,
getName = true,
makeHeading = true,
getOutput = true
}


function Template.new(invocationObj, options)
function Template.new(invocationObj, options)
local obj = setmetatable({}, Template)
local obj = {}


-- Set input
-- Set input
Line 24: Line 32:
end
end
end
end
obj.invocation = invocationObj
obj._invocation = invocationObj


-- Validate input
-- Validate input
Line 31: Line 39:
end
end


return obj
-- Memoize expensive method calls
local memoizedValues = {}
return setmetatable(obj, {
__index = function (t, key)
if Template.memoizedMethods[key] then
local val = memoizedValues[key]
if val then
return val
else
val = Template[key](t)
memoizedValues[key] = val
return val
end
else
return Template[key]
end
end
})
end
end


Line 78: Line 103:


function Template:getInvocation(format)
function Template:getInvocation(format)
local invocation = self.invocation:getInvocation(self:getName())
local invocation = self._invocation:getInvocation(self:getName())
invocation = mw.text.nowiki(invocation)
invocation = mw.text.nowiki(invocation)
if format == 'code' then
if format == 'code' then
Line 90: Line 115:


function Template:getOutput()
function Template:getOutput()
return self.invocation:getOutput(self:getName())
return self._invocation:getOutput(self:getName())
end
end


Cookies help us deliver our services. By using our services, you agree to our use of cookies.