Modul:ConvertNumeric/testcases

-- Unit tests for [[Module:ConvertNumeric/sandbox]]. Click talk page to run tests.
local p = require('Module:UnitTests')

local frame = mw.getCurrentFrame()
local function get_error_text(wikitext)
	local message = frame:preprocess(wikitext):match('<strong class="error"><[^>]+>(.-)<[^>]+></strong>')
	message = message:match('at line %d+: (.+)%.$') or message
	return message
end

function p:test_numeral_to_english()
	self:preprocess_equals_many('{{#invoke:ConvertNumeric/sandbox | numeral_to_english |', '}}', {
		{'0', 'nol'},
		{'1', 'satu'},
		{' 1 ', 'satu'}, -- test whitespace around number
		{'1 | case=u', 'Satu'},
		{'1 | case=U', 'Satu'},
		{'-1', 'negatif satu'},
		{'-1 | negative=minus', 'minus satu'},
		{'70', 'tujuh puluh'},
		{'42', 'empat puluh dua'},
		{'123', 'seratus dua puluh tiga'},
		{'723', 'tujuh ratus dua puluh tiga'},
		{'1050', 'seribu lima puluh'},
		{'5050', 'lima ribu lima puluh'},
		{'0.25', 'nol koma dua lima'},
		{'.25', 'nol koma dua lima'},
		{'0 | ord=on', 'kenol'},
		{'33 | ord=on', 'ketiga puluh tiga'},
		{'40 | ord=on', 'keempat puluh'},
		{'5000000000 | ord=on', 'kelima miliar'},
		{'0 | pl=on', 'nolan'},
		{'76 | pl=on', 'tujuh puluh enaman'},
		{'1000000000000000000000000000001', 'satu noniliun satu'},
		{'1.23E3', 'seribu dua ratus tiga puluh'},
		{'123E-4', 'nol koma nol satu dua tiga'},
		{'1001001001001000 | lk=on', 'satu [[Urutan_besaran_(bilangan)#1015|kuadriliun]] satu [[Urutan_besaran_(bilangan)#1012|triliun]] satu [[Urutan_besaran_(bilangan)#109|miliar]] satu juta seribu'},
		{'1001001001001000 | lk=billion,quadrillion', 'satu [[Urutan_besaran_(bilangan)#1015|kuadriliun]] satu triliun satu [[Urutan_besaran_(bilangan)#109|miliar]] satu juta seribu'},
		{'5E30', 'lima noniliun'},
		{'-123456789.25', 'negatif seratus dua puluh tiga juta empat ratus lima puluh enam ribu tujuh ratus delapan puluh sembilan koma dua lima'},
		{'0 | zero=foo', 'foo'},
		{'0 | ord=on | zero=none', 'kosong'},
		{'1 | numerator=2 | denominator=3', 'satu dua per tiga'},
		{'| numerator=2 | denominator=3', 'dua per tiga'},
		{'1 | denominator=2', 'satu setengah'},
		{'1 | numerator=3 | denominator=2', 'satu tiga per dua'},
		{'1 | numerator=1 | denominator=4', 'satu seperempat'},
		{'1 | numerator=1 | denominator=4 | one=one', 'satu satu per empat'},
		{'9 | numerator=3 | denominator=4', 'sembilan tiga per empat'},
		{'23 | numerator=12 | denominator=16', 'dua puluh tiga dua belas per enam belas'},
	})
end

function p:test_numeral_to_english_error()
	local cases = {'', 'a', '.', 'e', 'E', 'E1', '.E1', '+', '-', '123a', 'a123', '0.1a', '-a123' }
	local nowiki = mw.text.nowiki
	for _, case in ipairs(cases) do
		local wikitext = '{{#invoke:ConvertNumeric/sandbox | numeral_to_english |' .. case .. '}}'
		self:equals(nowiki(wikitext), get_error_text(wikitext), 'Invalid decimal numeral')
	end
end

function p:test_numeral_to_english_round()
	self:preprocess_equals_many('{{#invoke:ConvertNumeric/sandbox | numeral_to_english |', '}}', {
		{'1 | round=on', 'satu'},
		{'37 | round=on', 'tiga puluh tujuh'},
		{'190 | round=on', 'dua ratus'},
		{'110 | round=up', 'dua ratus'},
		{'190 | round=down', 'seratus'},
		{'87654321 | round=on', 'sembilan puluh juta'},
		{'12345678 | round=on', 'dua belas juta'},
		{'-190 | round=on', 'negatif dua ratus'},
		{'-190 | round=up', 'negatif seratus'},
		{'-110 | round=down', 'negatif dua ratus'},
		{'0.25 | round=on', 'nol'},
		{'0.5 | round=on', 'satu'},
		{'-0.25 | round=on', 'nol'},
		{'-0.5 | round=on', 'negatif satu'},
		{'0.01 | round=up', 'satu'},
		{'0.99 | round=down', 'nol'},
		{'-0.01 | round=down', 'negatif satu'},
		{'-0.99 | round=up', 'nol'},
	})
end

function p:test_decToHex()
	self:preprocess_equals(
		'{{#invoke:ConvertNumeric/sandbox|decToHex|10 20 40 1024 78912345|minlength=4}}',
		'000A 0014 0028 0400 4B41B59'
	)
end

return p