Module:Template test case: Difference between revisions

    (when doing Template class memoization, return wrapper functions for the values rather than the values themselves)
    (show the template invocation if the showcode option is set)
    Line 1: Line 1:
    -- This module provides several methods to generate test cases.
    -- This module provides several methods to generate test cases.


    local yesno = require('Module:Yesno')
    local mTableTools = require('Module:TableTools')
    local mTableTools = require('Module:TableTools')
    local libraryUtil = require('libraryUtil')
    local libraryUtil = require('libraryUtil')
    Line 106: Line 107:
    if format == 'code' then
    if format == 'code' then
    invocation = '<code>' .. invocation .. '</code>'
    invocation = '<code>' .. invocation .. '</code>'
    elseif format == 'pre' then
    elseif format ~= 'plain' then
    -- Default is pre tags
    invocation = '<pre style="white-space: pre-wrap;">' .. invocation .. '</pre>'
    invocation = '<pre style="white-space: pre-wrap;">' .. invocation .. '</pre>'
    invocation = mw.getCurrentFrame():preprocess(invocation)
    invocation = mw.getCurrentFrame():preprocess(invocation)
    Line 153: Line 155:
    local templateOptions = mTableTools.numData(options, true)
    local templateOptions = mTableTools.numData(options, true)
    obj.options = templateOptions.other or {}
    obj.options = templateOptions.other or {}
    -- Normalize showcode option
    obj.options.showcode = yesno(obj.options.showcode)


    -- Add default template options
    -- Add default template options
    Line 185: Line 190:


    function TestCase:renderColumns()
    function TestCase:renderColumns()
    local root = mw.html.create('table')
    local root = mw.html.create()
    root
    if self.options.showcode then
    root
    :wikitext(self.templates[1]:getInvocation())
    :newline()
    end
     
    local tableroot = root:tag('table')
    tableroot
    :addClass(self.options.class)
    :addClass(self.options.class)
    :cssText(self.options.style)
    :cssText(self.options.style)
    Line 193: Line 205:


    -- Headings
    -- Headings
    local headingRow = root:tag('tr')
    local headingRow = tableroot:tag('tr')
    if self.options.rowheader then
    if self.options.rowheader then
    -- rowheader is correct here. We need to add another th cell if
    -- rowheader is correct here. We need to add another th cell if
    Line 213: Line 225:


    -- Row header
    -- Row header
    local dataRow = root:tag('tr'):css('vertical-align', 'top')
    local dataRow = tableroot:tag('tr'):css('vertical-align', 'top')
    if self.options.rowheader then
    if self.options.rowheader then
    dataRow:tag('th')
    dataRow:tag('th')
    Line 232: Line 244:


    function TestCase:renderRows()
    function TestCase:renderRows()
    local root = mw.html.create('table')
    local root = mw.html.create()
    root
    if self.options.showcode then
    root
    :wikitext(self.templates[1]:getInvocation())
    :newline()
    end
     
    local tableroot = root:tag('table')
    tableroot
    :addClass(self.options.class)
    :addClass(self.options.class)
    :cssText(self.options.style)
    :cssText(self.options.style)


    if self.options.caption then
    if self.options.caption then
    root
    tableroot
    :tag('caption')
    :tag('caption')
    :wikitext(self.options.caption)
    :wikitext(self.options.caption)
    Line 245: Line 264:
    for _, obj in ipairs(self.templates) do
    for _, obj in ipairs(self.templates) do
    -- Build the row HTML
    -- Build the row HTML
    root
    tableroot
    :tag('tr')
    :tag('tr')
    :tag('td')
    :tag('td')
    Line 264: Line 283:
    function TestCase:renderDefault()
    function TestCase:renderDefault()
    local ret = {}
    local ret = {}
    ret[#ret + 1] = self.templates[1]:getInvocation('code')
    if self.options.showcode then
    ret[#ret + 1] = self.templates[1]:getInvocation()
    end
    for i, obj in ipairs(self.templates) do
    for i, obj in ipairs(self.templates) do
    ret[#ret + 1] = '<div style="clear: both;"></div>'
    ret[#ret + 1] = '<div style="clear: both;"></div>'
    Line 399: Line 420:


    function p._nowiki(args)
    function p._nowiki(args)
    local invocationObj = NowikiInvocation.new(args.invocation)
    local invocationObj = NowikiInvocation.new(args.code)
    args.invocation = nil
    args.code = nil
    local options = args
    -- Assume we want to see the code as we already passed it in.
    local testCaseObj = TestCase.new(invocationObj, options)
    args.showcode = args.showcode or true
    local testCaseObj = TestCase.new(invocationObj, args)
    return tostring(testCaseObj)
    return tostring(testCaseObj)
    end
    end