Module:Common/testcases

-- Unit tests for Module:Common. Click talk page to run tests. local common = require('Module:Common') local ScribuntoUnit = require('Module:ScribuntoUnit') local suite = ScribuntoUnit:new

--   common.removeTypeSuffix tests -- function suite:testRemoveTypeSuffix self:assertEquals('Ship', common.removeTypeSuffix( 'Ship (ship)', 'ship' )) self:assertEquals('Test', common.removeTypeSuffix( 'Test (test)', 'test' )) self:assertEquals('Sample page', common.removeTypeSuffix( 'Sample page (sample)', 'sample' )) end

function suite:testRemoveTypeSuffixNoMatch self:assertEquals('Page name', common.removeTypeSuffix( 'Page name', 'ship' )) end

--   common.toNumber tests -- function suite:testToNumberNum self:assertEquals(10, common.toNumber(10)) self:assertEquals(15, common.toNumber(15)) self:assertEquals(0, common.toNumber(0)) self:assertEquals(-123123123, common.toNumber(-123123123)) self:assertEquals(0.001, common.toNumber(0.001)) self:assertEquals(202.5, common.toNumber(202.5)) end

function suite:testToNumberString self:assertEquals(125, common.toNumber("125")) self:assertEquals(12.5, common.toNumber("12.5")) self:assertEquals(12.5, common.toNumber("12.5")) self:assertEquals(202.5, common.toNumber("202.5")) end

function suite:testToNumberStringFail self:assertEquals(0, common.toNumber("NotANumber", 0)) self:assertEquals(nil, common.toNumber("NotANumber")) end

--   common.formatNum tests -- function suite:testFormatNum self:assertEquals("10", common.formatNum(10)) self:assertEquals("15", common.formatNum(15)) self:assertEquals("0", common.formatNum(0)) self:assertEquals("-123,123,123", common.formatNum(-123123123)) self:assertEquals("0.001", common.formatNum(0.001)) self:assertEquals("0.001", common.formatNum(.001)) self:assertEquals("202.5", common.formatNum(202.5)) end

function suite:testFormatNumberString self:assertEquals("125", common.formatNum("125")) self:assertEquals("12.5", common.formatNum("12.5")) self:assertEquals("12.5", common.formatNum("12,5")) self:assertEquals("202.5", common.formatNum("202.5")) end

function suite:testFormatNumberStringFail self:assertEquals(false, common.formatNum("NotANumber")) end

return suite