Module:Cache/testcases

local cache = require('Module:Cache') -- the module to be tested local ScribuntoUnit = require('Module:ScribuntoUnit') local suite = ScribuntoUnit:new

-- Test Cache.set

function suite:test_set_string local parts = { 'some', 'test', 'parts', 1, 2, 3, true, false } local key = cache.key( parts ) self:assertEquals( 'test', cache.set( parts, parts[2] ) ) self:assertEquals( 'test', mw.getCurrentFrame:callParserFunction( '#var', key ) ) self:assertEquals( 'string', mw.getCurrentFrame:callParserFunction( '#var', key .. '__type' ) ) end

function suite:test_set_number local parts = { 'some', 'test', 'parts', 1, 2, 3, true, false } local key = cache.key( parts ) self:assertEquals( 1, cache.set( parts, parts[4] ) ) self:assertEquals( '1', mw.getCurrentFrame:callParserFunction( '#var', key ) ) self:assertEquals( 'number', mw.getCurrentFrame:callParserFunction( '#var', key .. '__type' ) ) end

function suite:test_set_boolean local parts = { 'some', 'test', 'parts', 1, 2, 3, true, false } local key = cache.key( parts ) self:assertEquals( true, cache.set( parts, parts[7] ) ) self:assertEquals( 'true', mw.getCurrentFrame:callParserFunction( '#var', key ) ) self:assertEquals( 'boolean', mw.getCurrentFrame:callParserFunction( '#var', key .. '__type' ) ) end

function suite:test_set_table local parts = { 'some', 'test', 'parts', 1, 2, 3, true, false } local key = cache.key( parts ) self:assertEquals( parts, cache.set( parts, parts ) ) self:assertEquals( mw.text.jsonEncode( parts ), mw.getCurrentFrame:callParserFunction( '#var', key ) ) self:assertEquals( 'table', mw.getCurrentFrame:callParserFunction( '#var', key .. '__type' ) ) end

-- Test Cache.get

function suite:test_get_no_set local parts = { 'some', 'non', 'cached', 'parts' } self:assertEquals( nil, cache.get( parts ) ) end

function suite:test_get_string local parts = { 'some', 'test', 'parts', 1, 2, 3, true, false } local key = cache.key( parts ) mw.getCurrentFrame:callParserFunction( '#vardefine', { key, 'test' } ) mw.getCurrentFrame:callParserFunction( '#vardefine', { key .. '__type', 'string' } )

self:assertEquals( 'test', cache.get( parts ) ) end

function suite:test_get_number local parts = { 'some', 'test', 'parts', 1, 2, 3, true, false } local key = cache.key( parts ) mw.getCurrentFrame:callParserFunction( '#vardefine', { key, '1' } ) mw.getCurrentFrame:callParserFunction( '#vardefine', { key .. '__type', 'number' } )

self:assertEquals( 1, cache.get( parts ) ) end

function suite:test_get_boolean local parts = { 'some', 'test', 'parts', 1, 2, 3, true, false } local key = cache.key( parts ) mw.getCurrentFrame:callParserFunction( '#vardefine', { key, 'true' } ) mw.getCurrentFrame:callParserFunction( '#vardefine', { key .. '__type', 'boolean' } )

self:assertEquals( true, cache.get( parts ) ) end

function suite:test_get_table local parts = { 'some', 'test', 'parts', 1, 2, 3, true, false } local key = cache.key( parts ) mw.getCurrentFrame:callParserFunction( '#vardefine', { key, mw.text.jsonEncode( parts ) } ) mw.getCurrentFrame:callParserFunction( '#vardefine', { key .. '__type', 'table' } )

self:assertDeepEquals( parts, cache.get( parts ) ) end

-- Test Cache.auto

function suite:test_auto_string local function test_function( args ) return args[1] .. args[2] end self:assertEquals( 'foobar', cache.auto( test_function, { 'foo', 'bar' } ), 'Error during storage.' ) self:assertEquals( 'foobar', cache.auto( test_function, { 'foo', 'bar' } ), 'Error during retrieval.' ) end

function suite:test_auto_number local function test_function( args ) return args[1] + args[2] end self:assertEquals( 3, cache.auto( test_function, { 1, 2 } ), 'Error during storage.' ) self:assertEquals( 3, cache.auto( test_function, { 1, 2 } ), 'Error during retrieval.' ) end

function suite:test_auto_boolean local function test_function( args ) return args[1] and args[2] end self:assertEquals( true, cache.auto( test_function, { true, true } ), 'Error during storage.' ) self:assertEquals( true, cache.auto( test_function, { true, true } ), 'Error during retrieval.' ) end

function suite:test_auto_table local function test_function( args ) return { args[1], args[2] } end self:assertDeepEquals( { 'test', 2 }, cache.auto( test_function, { 'test', 2 } ), 'Error during storage.' ) self:assertDeepEquals( { 'test', 2 }, cache.auto( test_function, { 'test', 2 } ), 'Error during retrieval.' ) end

return suite