Module:TemplateDataDoc/testcases

From Northumberland Climbing
< Module:TemplateDataDoc
Revision as of 05:52, 31 July 2020 by Ianbirtwistle (talk | contribs) (1 revision imported)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Module:TemplateDataDoc[edit]

N1 test failed.

Name Expected Actual
Yes testConvertFormatString
Yes testGetFormatParts
N testProcessJson Lua error -- Module:TemplateDataDoc/testcases:75: attempt to index local 'data' (a nil value)


Module:TemplateDataDoc/sandbox[edit]

N1 test failed.

Name Expected Actual
Yes testConvertFormatString
Yes testGetFormatParts
N testProcessJson Lua error -- Module:TemplateDataDoc/testcases:75: attempt to index local 'data' (a nil value)

-- Unit tests for [[Module:TemplateDataDoc]]. Click talk page to run tests.
require( 'Module:No globals' )

local ScribuntoUnit = require( 'Module:ScribuntoUnit' )
local suite = ScribuntoUnit:new()
local libName = 'TemplateDataDoc'


-- Helper to run all tests using sandbox version of the library from the debug console. To run against main lib, use  =p.run()
function suite.runSandbox()
    local frame = mw.getCurrentFrame():newChild{ title='testcases', args={ module=libName .. '/sandbox', displayMode='log' } }
    return suite.run( frame )
end

-- Allow test runner to use the sandbox and the primary versions of the library with the same testcases
function suite:module()
    return require( 'Module:' .. ( self.frame and self.frame.args.module or libName ) )
end

------------------------------------------------------------------------
-- Test convertFormatString
------------------------------------------------------------------------
function suite:testConvertFormatString()
    local tt = self:module()

	local formatInline, flagInline = tt.convertFormatString( 'inline' )
    self:assertEquals( '{{_|_=_}}', formatInline )
    self:assertEquals( false, flagInline )

	local formatBlock, flagBlock = tt.convertFormatString( 'block' )
    self:assertEquals( '{{_\n| _ = _\n}}', formatBlock )
    self:assertEquals( true, flagBlock )

	local formatCustom, flagCustom = tt.convertFormatString( '{{ _ | _ = _ }}' )
    self:assertEquals( '{{ _ | _ = _ }}', formatCustom )
    self:assertEquals( false, flagCustom )
end

------------------------------------------------------------------------
-- Test getFormatParts
------------------------------------------------------------------------
function suite:testGetFormatParts()
    local tt = self:module()

	local flag1, start1, end1, paramKey1, paramValue1 = tt.getFormatParts( 'inline', 'Test' )
    self:assertEquals( false, flag1 )
    self:assertEquals( '{{Test', start1 )
    self:assertEquals( '}}', end1 )
    self:assertEquals( '|_=', paramKey1 )
    self:assertEquals( '_', paramValue1 )

	local flag2, start2, end2, paramKey2, paramValue2 = tt.getFormatParts( 'block', 'Test_2' )
    self:assertEquals( true, flag2 )
    self:assertEquals( '{{Test_2\n', start2 )
    self:assertEquals( '}}', end2 )
    self:assertEquals( '| _ =', paramKey2 )
    self:assertEquals( ' _\n', paramValue2 )

	local flag3, start3, end3, paramKey3, paramValue3 = tt.getFormatParts( '{{\n_\n|\n_\n=\n_\n}}\n', 'Test 3' )
    self:assertEquals( false, flag3 )
    self:assertEquals( '{{\nTest 3\n', start3 )
    self:assertEquals( '}}\n', end3 )
    self:assertEquals( '|\n_\n=', paramKey3 )
    self:assertEquals( '\n_\n', paramValue3 )
end

------------------------------------------------------------------------
-- Test processJson
------------------------------------------------------------------------
function suite:testProcessJson()
    local tt = self:module()

	local json = '{ "params": { "1": { "label": "1", }, "2": { "label": "2", } } }'
    local data = tt.processJson( json )
    self:assertEquals( 2, #data.params )
end

return suite