Spring til indhold

Modul:Sandbox/Jhertel/Formattal/testcases

Fra Wikipedia, den frie encyklopædi
Dette er siden til testcases for modulet Modul:Sandbox/Jhertel/Formattal. Vis resultatet af testen.
-- Enhedstests for [[Modul:Sandbox/Jhertel/Formattal]]. Klik på diskussionsside for at køre tests.

local p = require('Module:UnitTests')
local Formattal = require('Module:Sandbox/Jhertel/Formattal')
 
function p:test_opsplitIFortegnOgRest()
	self:equals_deep("", {Formattal.opsplitIFortegnOgRest("")}, {"", ""})
	self:equals_deep("1", {Formattal.opsplitIFortegnOgRest("1")}, {"", "1"})
	self:equals_deep("-1", {Formattal.opsplitIFortegnOgRest("-1")}, {"-", "1"})
	self:equals_deep("-12.34", {Formattal.opsplitIFortegnOgRest("-12.34")}, {"-", "12.34"})
	self:equals_deep("+12,304.34", {Formattal.opsplitIFortegnOgRest("+12,304.34")}, {"+", "12,304.34"})
	self:equals_deep("12,304.34", {Formattal.opsplitIFortegnOgRest("12,304.34")}, {"", "12,304.34"})
end
 
function p:test_removeAll()
	self:equals("Remove all , from 123,332,332", Formattal.removeAll("123,332,332", ","), "123332332")
	self:equals("Remove all %. from 123,332,332", Formattal.removeAll("123,332,332", "%."), "123,332,332")
	self:equals("Remove all %. from 123.332.332", Formattal.removeAll("123.332.332", "%."), "123332332")
	self:equals("Remove all l from hello", Formattal.removeAll("hello", "l"), "heo")
end

 
function p:test_formattal()
	local medtagTestsSomViVedFejler = true
	
	-- 0 decimaler, ingen tusindadskiller
	self:preprocess_equals("{{#invoke:Sandbox/Jhertel/Formattal|formattal|1}}", "1")
	self:preprocess_equals("{{#invoke:Sandbox/Jhertel/Formattal|formattal|12}}", "12")
	self:preprocess_equals("{{#invoke:Sandbox/Jhertel/Formattal|formattal|123}}", "123")
	self:preprocess_equals("{{#invoke:Sandbox/Jhertel/Formattal|formattal|1234}}", "1.234")
	self:preprocess_equals("{{#invoke:Sandbox/Jhertel/Formattal|formattal|12345}}", "12.345")
	self:preprocess_equals("{{#invoke:Sandbox/Jhertel/Formattal|formattal|123456}}", "123.456")

	-- 0 decimaler, komma som tusindadskiller
	self:preprocess_equals("{{#invoke:Sandbox/Jhertel/Formattal|formattal|1}}", "1")
	self:preprocess_equals("{{#invoke:Sandbox/Jhertel/Formattal|formattal|12}}", "12")
	self:preprocess_equals("{{#invoke:Sandbox/Jhertel/Formattal|formattal|123}}", "123")
	self:preprocess_equals("{{#invoke:Sandbox/Jhertel/Formattal|formattal|1,234}}", "1.234")
	self:preprocess_equals("{{#invoke:Sandbox/Jhertel/Formattal|formattal|12,345}}", "12.345")
	self:preprocess_equals("{{#invoke:Sandbox/Jhertel/Formattal|formattal|123,456}}", "123.456")

	-- 0 decimaler, punktum som tusindadskiller
	self:preprocess_equals("{{#invoke:Sandbox/Jhertel/Formattal|formattal|1}}", "1")
	self:preprocess_equals("{{#invoke:Sandbox/Jhertel/Formattal|formattal|12}}", "12")
	self:preprocess_equals("{{#invoke:Sandbox/Jhertel/Formattal|formattal|123}}", "123")
	self:preprocess_equals("{{#invoke:Sandbox/Jhertel/Formattal|formattal|1.234}}", "1.234")
	self:preprocess_equals("{{#invoke:Sandbox/Jhertel/Formattal|formattal|12.345}}", "12.345")
	self:preprocess_equals("{{#invoke:Sandbox/Jhertel/Formattal|formattal|123.456}}", "123.456")

	-- 1 decimal, decimaladskiller komma, punktum som tusindadskiller
	self:preprocess_equals("{{#invoke:Sandbox/Jhertel/Formattal|formattal|1,0}}", "1,0")
	self:preprocess_equals("{{#invoke:Sandbox/Jhertel/Formattal|formattal|12,0}}", "12,0")
	self:preprocess_equals("{{#invoke:Sandbox/Jhertel/Formattal|formattal|123,0}}", "123,0")
	self:preprocess_equals("{{#invoke:Sandbox/Jhertel/Formattal|formattal|1.234,0}}", "1.234,0")
	self:preprocess_equals("{{#invoke:Sandbox/Jhertel/Formattal|formattal|12.345,0}}", "12.345,0")
	self:preprocess_equals("{{#invoke:Sandbox/Jhertel/Formattal|formattal|123.456,0}}", "123.456,0")

	-- 1 decimal, decimaladskiller punktum, komma som tusindadskiller
	self:preprocess_equals("{{#invoke:Sandbox/Jhertel/Formattal|formattal|1.0}}", "1,0")
	self:preprocess_equals("{{#invoke:Sandbox/Jhertel/Formattal|formattal|12.0}}", "12,0")
	self:preprocess_equals("{{#invoke:Sandbox/Jhertel/Formattal|formattal|123.0}}", "123,0")
	self:preprocess_equals("{{#invoke:Sandbox/Jhertel/Formattal|formattal|1,234.0}}", "1.234,0")
	self:preprocess_equals("{{#invoke:Sandbox/Jhertel/Formattal|formattal|12,345.0}}", "12.345,0")
	self:preprocess_equals("{{#invoke:Sandbox/Jhertel/Formattal|formattal|123,456.0}}", "123.456,0")

	-- 2 decimaler, decimaladskiller punktum, tusindadskiller punktum
	self:preprocess_equals("{{#invoke:Sandbox/Jhertel/Formattal|formattal|1.00}}", "1,00")
	self:preprocess_equals("{{#invoke:Sandbox/Jhertel/Formattal|formattal|12.00}}", "12,00")
	self:preprocess_equals("{{#invoke:Sandbox/Jhertel/Formattal|formattal|123.00}}", "123,00")
	self:preprocess_equals("{{#invoke:Sandbox/Jhertel/Formattal|formattal|1.234.00}}", "1.234.00[[Kategori:Sider med tal hvis format ikke kendes af formattal]]")
	self:preprocess_equals("{{#invoke:Sandbox/Jhertel/Formattal|formattal|12.345.00}}", "12.345.00[[Kategori:Sider med tal hvis format ikke kendes af formattal]]")
	self:preprocess_equals("{{#invoke:Sandbox/Jhertel/Formattal|formattal|123.456.00}}", "123.456.00[[Kategori:Sider med tal hvis format ikke kendes af formattal]]")

	-- 2 decimaler, decimaladskiller punktum, tusindadskiller komma
	self:preprocess_equals("{{#invoke:Sandbox/Jhertel/Formattal|formattal|1.00}}", "1,00")
	self:preprocess_equals("{{#invoke:Sandbox/Jhertel/Formattal|formattal|12.00}}", "12,00")
	self:preprocess_equals("{{#invoke:Sandbox/Jhertel/Formattal|formattal|123.00}}", "123,00")
	self:preprocess_equals("{{#invoke:Sandbox/Jhertel/Formattal|formattal|1,234.00}}", "1.234,00")
	self:preprocess_equals("{{#invoke:Sandbox/Jhertel/Formattal|formattal|12,345.00}}", "12.345,00")
	self:preprocess_equals("{{#invoke:Sandbox/Jhertel/Formattal|formattal|123,456.00}}", "123.456,00")

	-- Negative tal er blevet gyldige pr. 2017-11-21.
	self:preprocess_equals("{{#invoke:Sandbox/Jhertel/Formattal|formattal|-123}}", "-123")

	-- Nonsens er ugyldigt
	self:preprocess_equals("{{#invoke:Sandbox/Jhertel/Formattal|formattal|nonsens}}", "nonsens[[Kategori:Sider med tal hvis format ikke kendes af formattal]]")

	if medtagTestsSomViVedFejler then
		-- 2017-10-25: Disse fejler p.t.
		
		-- 3 eller flere decimaler, hvor tallet ikke kan fortolkes som et heltal.
		self:preprocess_equals("{{#invoke:Sandbox/Jhertel/Formattal|formattal|.123}}", ".123[[Kategori:Sider med tal hvis format ikke kendes af formattal]]")
		self:preprocess_equals("{{#invoke:Sandbox/Jhertel/Formattal|formattal|.1234}}", ".1234[[Kategori:Sider med tal hvis format ikke kendes af formattal]]")
		self:preprocess_equals("{{#invoke:Sandbox/Jhertel/Formattal|formattal|0.1234}}", "0.1234[[Kategori:Sider med tal hvis format ikke kendes af formattal]]")
		self:preprocess_equals("{{#invoke:Sandbox/Jhertel/Formattal|formattal|,123}}", ",123[[Kategori:Sider med tal hvis format ikke kendes af formattal]]")
		self:preprocess_equals("{{#invoke:Sandbox/Jhertel/Formattal|formattal|0,1234}}", "0,1234[[Kategori:Sider med tal hvis format ikke kendes af formattal]]")
	
		-- Tomt input er ugyldigt
		self:preprocess_equals("{{#invoke:Sandbox/Jhertel/Formattal|formattal|}}", "[[Kategori:Sider med tal hvis format ikke kendes af formattal]]")
	end
end

return p