Wir laden dich herzlich auf den Star Citizen Wiki Discord Server ein! Du kannst uns auch auf unserem neuen YouTube-Chanel finden!

Modul:Common/testcases

From Star Citizen Wiki

-- 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.extractDataFromPrefix tests
]]--
function suite:testExtractPrefixNumerical()
    local args = {
        prefix1 = 'Abc1',
        prefix2 = 'Abc2',
        prefix3 = 'Abc3',
        prefix4 = 'Abc4',
        prefix5 = 'Abc5',
    }

    local result = {
        'Abc1',
        'Abc2',
        'Abc3',
        'Abc4',
        'Abc5',
    }

    -- Test for equal length for now...
    self:assertDeepEquals(#result, #common.extractDataFromPrefix( 'prefix', args ))
end

function suite:testExtractPrefixNumericalEmpty()
    local args = {
        prefix = 'Abc1',
        prefix = 'Abc2',
        prefix = 'Abc3',
        prefix = 'Abc4',
        prefix = 'Abc5',
    }

    self:assertDeepEquals(0, #common.extractDataFromPrefix( 'prefix', args, '%d' ))
end


--[[
    common.removeTypeSuffix tests
]]--
function suite:testRemoveTypeSuffix()
    self:assertEquals('Raumschiff', common.removeTypeSuffix( 'Raumschiff (Raumschiff)', 'Raumschiff' ))
    self:assertEquals('Test', common.removeTypeSuffix( 'Test (Test)', 'Test' ))
    self:assertEquals('Beispielseite', common.removeTypeSuffix( 'Beispielseite (Bla)', 'Bla' ))
end

function suite:testRemoveTypeSuffixNoMatch()
    self:assertEquals('Seitenname', common.removeTypeSuffix( 'Seitenname', 'Raumschiff' ))
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


--[[
    common.booleanToText tests
]]--
function suite:testBooleanToTextTrueish()
    self:assertEquals("Ja", common.booleanToText(1))
    self:assertEquals("Ja", common.booleanToText(true))
end

function suite:testBooleanToTextFalseish()
    self:assertEquals("Nein", common.booleanToText(nil))
    self:assertEquals("Nein", common.booleanToText(0))
    self:assertEquals("Nein", common.booleanToText(false))
    self:assertEquals("Nein", common.booleanToText("false"))
    self:assertEquals("Nein", common.booleanToText("0"))
    self:assertEquals("Nein", common.booleanToText("Nein"))
end

function suite:testBooleanToTextFalseishCustomText()
    self:assertEquals("Falsch", common.booleanToText(0, nil, "Falsch"))
    self:assertEquals("Falsch", common.booleanToText(false, nil, "Falsch"))
    self:assertEquals("Falsch", common.booleanToText("false", nil, "Falsch"))
    self:assertEquals("Falsch", common.booleanToText("0", nil, "Falsch"))
    self:assertEquals("Falsch", common.booleanToText("Nein", nil, "Falsch"))
end

return suite
Cookies help us deliver our services. By using our services, you agree to our use of cookies.