Modul:Wikidata/Formatters/time/testcases

Z Wikipedie, otevřené encyklopedie
Skočit na navigaci Skočit na vyhledávání
local myModule = require 'Modul:Wikidata/Formatters/time'
local Time = require 'Modul:Time'
local ScribuntoUnit = require 'Modul:ScribuntoUnit'
local suite = ScribuntoUnit:new()

function suite:testYearDifference()
	local provider = {
		{ '1890-01-09', '1938-12-25',  48 },
		{ '1890-01-09', '1990-01-09', 100 },
		{ '1890-01-09', '1938-12',     48 },
		{ '1890-01-09', '1938-01',     47, '47–48' },
		{ '1890-01-09', '1938',        47, '47–48' },
		{ '1890-01',    '1938-12-25',  48 },
		{ '1890-12',    '1938-12-25',  47, '47–48' },
		{ '1890-01',    '1938-12',     48 },
		{ '1890-12',    '1938-12',     47, '47–48' },
		{ '1890-01',    '1938',        47, '47–48' },
		{ '1890',       '1938-12-25',  47, '47–48' },
		{ '1890',       '1938-12',     47, '47–48' },
		{ '1890',       '1938',        47, '47–48' },
	}
	for _, dates in ipairs(provider) do
		local sooner = Time.newFromIso8601(dates[1])
		local later = Time.newFromIso8601(dates[2])
		local num_age, display_age = myModule.yearDifference(sooner, later)
		self:assertEquals(dates[3], num_age)
		self:assertEquals(dates[4] or dates[3], display_age)
	end
end

function suite:testJulianToGregorian()
	local provider = {
		{
			gregorian = '1582-10-15',
			julian = '1582-10-05'
		},
		{
			gregorian = '1582-11-10',
			julian = '1582-10-31'
		},
		{
			gregorian = '1583-01-09',
			julian = '1582-12-30'
		},
		{
			gregorian = '1583-01-09',
			julian = '1582-12-30'
		},
		{
			gregorian = '1583-03-09',
			julian = '1583-02-27'
		},
		{
			gregorian = '1584-03-08',
			julian = '1584-02-27'
		},
		{
			gregorian = '1600-03-08',
			julian = '1600-02-27'
		},
		{
			gregorian = '1700-02-28',
			julian = '1700-02-18'
		},
		{
			gregorian = '1700-03-01',
			julian = '1700-02-19'
		},
		{
			gregorian = '1700-03-10',
			julian = '1700-02-28'
		},
		{
			gregorian = '1700-03-11',
			julian = '1700-02-29'
		},
	}
	for _, data in ipairs(provider) do
		local julian = Time.newFromIso8601(data.julian)
		local gregorian = Time.newFromIso8601(data.gregorian)
		julian.calendar = julian.CALENDAR.JULIAN
		gregorian.calendar = julian.CALENDAR.GREGORIAN
		self:assertEquals(gregorian, myModule.julianToGregorian(julian))
	end
end

function suite:testFormatRawValue()
	local provider = {
		{
			iso = '1890-01-09',
			formatted = '[[9. leden|9. ledna]] [[1890]]'
		},
		{
			iso = '1890-01-09',
			formatted = '9. ledna 1890',
			options = { nolink = true }
		},
		{
			iso = '123',
			formatted = '[[123]]'
		},
		{
			iso = '123',
			formatted = '123',
			options = { nolink = true }
		},
		{
			iso = '-123',
			formatted = '[[123 př. n. l.|123 př. n. l.]]'
		},
		{
			iso = '-123',
			formatted = '123 př. n. l.',
			options = { nolink = true }
		},
		{
			iso = '-0601',
			formatted = '7. století př. n. l.',
			options = { nolink = true, precision = 7 }
		},
		{
			iso = '-0600',
			formatted = '6. století př. n. l.',
			options = { nolink = true, precision = 7 }
		},
		{
			iso = '-0599',
			formatted = '6. století př. n. l.',
			options = { nolink = true, precision = 7 }
		},
		{
			iso = '1890-01-09',
			formatted = '[[1890]]',
			options = { precision = 9 }
		},
		{
			iso = '1890',
			formatted = '[[1890]]',
			options = { precision = 10 }
		},
		{
			iso = '1890',
			precision = 8,
			formatted = 'Desetiletí od 1890'
		},
		{
			iso = '1890',
			formatted = '[[19. století|19. století]]',
			options = { precision = 7 }
		},
		{
			iso = '1900',
			precision = 7,
			formatted = '[[19. století|19. století]]'
		},
	}
	for _, data in ipairs(provider) do
		local timevalue = Time.newFromIso8601(data.iso)
		if data.precision then
			timevalue.precision = data.precision
		end
		local options = data.options or {}
		self:assertEquals(data.formatted, myModule.formatRawValue(timevalue, options))
	end
end

return suite