Module:Variables/testcases

local variables = require( 'Module:Variables' ) local ScribuntoUnit = require( 'Module:ScribuntoUnit' ) local suite = ScribuntoUnit:new

-- Test Variables.vardefine

function suite:test_vardefine_string local variable_name = 'vardefine_string_test' local value = 'foo' variables.vardefine( variable_name, value ) self:assertEquals( value, mw.getCurrentFrame:callParserFunction( '#var', variable_name ) ) end

function suite:test_vardefine_number local variable_name = 'vardefine_number_test' variables.vardefine( variable_name, 1 ) self:assertEquals( '1', mw.getCurrentFrame:callParserFunction( '#var', variable_name ) ) end

function suite:test_vardefine_boolean local variable_name = 'vardefine_boolean_test' variables.vardefine( variable_name, true ) self:assertEquals( 'true', mw.getCurrentFrame:callParserFunction( '#var', variable_name ) ) end

function suite:test_vardefine_nil local variable_name = 'vardefine_nil_test' variables.vardefine( variable_name, nil ) self:assertEquals( '', mw.getCurrentFrame:callParserFunction( '#var', variable_name ) ) end

function suite:test_vardefine_function_throws self:assertFalse( pcall( variables.vardefine, 'vardefine_function_throws_test', function end ) ) end

function suite:test_vardefine_table_throws self:assertFalse( pcall( variables.vardefine, 'vardefine_table_throws_test', {} ) ) end

-- Test Variables.vardefine

function suite:test_vardefineecho_returns local values = { ['test'] = 'test', ['1'] = 1,   ['true'] = true, [''] = nil }

for k,v in pairs( values ) do   self:assertEquals( k, variables.vardefineecho( 'vardefineecho_return_test', v ), 'Wrong return for ' .. type(v) .. '.' ) end end

function suite:test_vardefineecho_string local variable_name = 'vardefineecho_string_test' local value = 'foo' variables.vardefineecho( variable_name, value ) self:assertEquals( value, mw.getCurrentFrame:callParserFunction( '#var', variable_name ) ) end

function suite:test_vardefineecho_number local variable_name = 'vardefineecho_number_test' variables.vardefineecho( variable_name, 1 ) self:assertEquals( '1', mw.getCurrentFrame:callParserFunction( '#var', variable_name ) ) end

function suite:test_vardefineecho_boolean local variable_name = 'vardefineecho_boolean_test' variables.vardefineecho( variable_name, true ) self:assertEquals( 'true', mw.getCurrentFrame:callParserFunction( '#var', variable_name ) ) end

function suite:test_vardefineecho_nil local variable_name = 'vardefineecho_nil_test' variables.vardefineecho( variable_name, nil ) self:assertEquals( '', mw.getCurrentFrame:callParserFunction( '#var', variable_name ) ) end

function suite:test_vardefineecho_function_throws self:assertFalse( pcall( variables.vardefineecho, 'vardefineecho_function_throws_test', function end ) ) end

function suite:test_vardefineecho_table_throws self:assertFalse( pcall( variables.vardefineecho, 'vardefineecho_table_throws_test', {} ) ) end

-- Test Variables.var

function suite:test_var_defined local variable_name = 'var_test' local value = 'foo' mw.getCurrentFrame:callParserFunction( '#vardefine', { variable_name, value } ) self:assertEquals( value, variables.var( variable_name ) ) end

function suite:test_var_defined_empty local variable_name = 'var_test' local value = '' mw.getCurrentFrame:callParserFunction( '#vardefine', { variable_name, value } ) self:assertEquals( nil, variables.var( 'non_existent_var_test' ) ) end

function suite:test_var_defined_empty_default local variable_name = 'var_test' local value = '' mw.getCurrentFrame:callParserFunction( '#vardefine', { variable_name, value } ) self:assertEquals( 5, variables.var( 'non_existent_var_test', 5 ) ) end

function suite:test_var_not_defined self:assertEquals( nil, variables.var( 'non_existent_var_test' ) ) end

function suite:test_var_not_defined_default self:assertEquals( 5, variables.var( 'non_existent_var_test', 5 ) ) end

-- Test Variables.varexists

function suite:test_varexists_true local variable_name = 'varexists_true_test' local value = 'bar' mw.getCurrentFrame:callParserFunction( '#vardefine', { variable_name, value } ) self:assertTrue( variables.varexists( variable_name ) ) end

function suite:test_varexists_false self:assertFalse( variables.varexists( 'varexists_false_test' ) ) end

return suite