Modul:Time/tests

Time modul tesztek[mi ez?]

Név Várt Tényleges
Igen testParseTimestamp
Igen testToIso8601
Igen testAge
Igen testLessOrEqual
Igen testFormatDate
Igen testNew
local Time = require"Modul:Time"
local ScribuntoUnit = require"Modul:ScribuntoUnit"
local suite = ScribuntoUnit:new()

function suite:testNew()
	self:assertEquals('2013-10-23', Time.newFromIso8601('2013-10-23'):toString())
	local status, value = pcall(function () return mw.wikibase.getEntityObject().claims.P569[1].mainsnak.datavalue.value end)
	if status then
		self:assertEquals('2012-10-23', Time.newFromWikidataValue(value):toString())
	end
end

function suite:testAge()
	local time1 = Time.newFromIso8601'2003-11-09'
	local time2 = Time.newFromIso8601'2013-12-09'
	local time3 = Time.newFromIso8601'2013-10-09'
	self:assertEquals(10, Time.age(time1, time2))
	self:assertEquals(9, Time.age(time1, time3))
	self:assertTrue(Time.age(time1) >= 10)
end

function suite:testFormatDate()
	self:assertEquals('[[i. e. 323]]. [[június 10.|június 10.]]', Time.newFromIso8601('-00000000323-06-10T00:00:00Z'):formatDate())
	self:assertEquals('2013. október 23.', Time.newFromIso8601('2013-10-23'):formatDate{link = 'nem'})
	self:assertEquals('1923. augusztus', Time.new{year = 1923, month = 8, precision = Time.PRECISION.MONTH}:formatDate{link = 'nem'})
	self:assertEquals('1655', Time.newFromIso8601('1655'):formatDate{link = 'nem'})
	self:assertEquals('[[1. század]]', Time.new{year = 1, precision = Time.PRECISION.YEAR100}:formatDate())
	self:assertEquals('i. e. 2. évezred', Time.new{year = -1001, precision = Time.PRECISION.KY}:formatDate{link = 'nem'})
	self:assertEquals('1920-as évek', Time.new{year = 1922, precision = Time.PRECISION.YEAR10}:formatDate{link = 'nem'})
	self:assertEquals('1950-es évek', Time.new{year = 1950, precision = Time.PRECISION.YEAR10}:formatDate{link = 'nem'})
	self:assertEquals('1700-as évek', Time.new{year = 1705, precision = Time.PRECISION.YEAR10}:formatDate{link = 'nem'})
	self:assertEquals('2000-es évek', Time.new{year = 2006, precision = Time.PRECISION.YEAR10}:formatDate{link = 'nem'})
	self:assertEquals('[[i. e. 740-es évek]]', Time.new{year = -741, precision = Time.PRECISION.YEAR10}:formatDate())
end

-- Test cases from https://github.com/wmde/DataValuesJavascript/blob/master/tests/src/values/TimeValue.tests.js
function suite:testParseTimestamp()
	local t
	t = Time.newFromIso8601('-9000000000000000-12-31T23:59:59Z') self:assertDeepEquals({-9000000000000000, 12, 31}, {t.year, t.month, t.day})
	t = Time.newFromIso8601('-123456789012-00-00T00:00:00Z') self:assertDeepEquals({-123456789012, 0, 0}, {t.year, t.month, t.day})
	t = Time.newFromIso8601('-12345678901-00-00T00:00:00Z') self:assertDeepEquals({-12345678901, 0, 0}, {t.year, t.month, t.day})
	t = Time.newFromIso8601('-1234-01-01T01:01:01Z') self:assertDeepEquals({-1234, 1, 1}, {t.year, t.month, t.day})
	t = Time.newFromIso8601('-0123-01-01T01:01:01Z') self:assertDeepEquals({-123, 1, 1}, {t.year, t.month, t.day})

	t = Time.newFromIso8601('-0012-01-01T01:01:01Z') self:assertDeepEquals({-12, 1, 1}, {t.year, t.month, t.day})
	t = Time.newFromIso8601('-0001-01-01T01:01:01Z') self:assertDeepEquals({-1, 1, 1}, {t.year, t.month, t.day})
	t = Time.newFromIso8601('+0000-01-01T01:01:01Z') self:assertDeepEquals({0, 1, 1}, {t.year, t.month, t.day})
	t = Time.newFromIso8601('+0001-01-01T01:01:01Z') self:assertDeepEquals({1, 1, 1}, {t.year, t.month, t.day})
	t = Time.newFromIso8601('+0012-00-00T00:00:00Z') self:assertDeepEquals({12, 0, 0}, {t.year, t.month, t.day})

	t = Time.newFromIso8601('+0123-00-00T00:00:00Z') self:assertDeepEquals({123, 0, 0}, {t.year, t.month, t.day})
	t = Time.newFromIso8601('+1234-00-00T00:00:00Z') self:assertDeepEquals({1234, 0, 0}, {t.year, t.month, t.day})
	t = Time.newFromIso8601('+1234567890-00-00T00:00:00Z') self:assertDeepEquals({1234567890, 0, 0}, {t.year, t.month, t.day})
	t = Time.newFromIso8601('+12345678901-00-00T00:00:00Z') self:assertDeepEquals({12345678901, 0, 0}, {t.year, t.month, t.day})
	t = Time.newFromIso8601('+123456789012-00-00T00:00:00Z') self:assertDeepEquals({123456789012, 0, 0}, {t.year, t.month, t.day})

	t = Time.newFromIso8601('+1234567890123456-00-00T00:00:00Z') self:assertDeepEquals({1234567890123456, 0, 0}, {t.year, t.month, t.day})
	t = Time.newFromIso8601('+9000000000000000-12-31T23:59:59Z') self:assertDeepEquals({9000000000000000, 12, 31}, {t.year, t.month, t.day})
	t = Time.newFromIso8601('-900000000000000-12-31T23:59:59Z') self:assertDeepEquals({-900000000000000, 12, 31}, {t.year, t.month, t.day})
	t = Time.newFromIso8601('-0123-01-01T01:01:01Z') self:assertDeepEquals({-123, 1, 1}, {t.year, t.month, t.day})
	t = Time.newFromIso8601('+0000-01-01T01:01:01Z') self:assertDeepEquals({0, 1, 1}, {t.year, t.month, t.day})

	t = Time.newFromIso8601('+0001-01-01T01:01:01Z') self:assertDeepEquals({1, 1, 1}, {t.year, t.month, t.day})
	t = Time.newFromIso8601('+900000000000000-12-31T23:59:59Z') self:assertDeepEquals({900000000000000, 12, 31}, {t.year, t.month, t.day})
	t = Time.newFromIso8601('-9999999999999999-12-31T23:59:59Z') self:assertDeepEquals({-9999999999999999, 12, 31}, {t.year, t.month, t.day})
	t = Time.newFromIso8601('+9999999999999999-12-31T23:59:59Z') self:assertDeepEquals({9999999999999999, 12, 31}, {t.year, t.month, t.day})
end

function suite:testToIso8601()
	self:assertEquals('-9000000000000000-12-31', Time.new{year = -9000000000000000, month = 12, day = 31}:toIso8601())
	self:assertEquals('-123456789012', Time.new{year = -123456789012, month = 0, day = 0}:toIso8601())
	self:assertEquals('-12345678901', Time.new{year = -12345678901, month = 0, day = 0}:toIso8601())
	self:assertEquals('-1234-01-01', Time.new{year = -1234, month = 1, day = 1}:toIso8601())
	self:assertEquals('-0123-01-01', Time.new{year = -123, month = 1, day = 1}:toIso8601())

	self:assertEquals('-0012-01-01', Time.new{year = -12, month = 1, day = 1}:toIso8601())
	self:assertEquals('-0001-01-01', Time.new{year = -1, month = 1, day = 1}:toIso8601())
	self:assertEquals('0000-01-01', Time.new{year = 0, month = 1, day = 1}:toIso8601())
	self:assertEquals('0001-01-01', Time.new{year = 1, month = 1, day = 1}:toIso8601())
	self:assertEquals('0012', Time.new{year = 12, month = 0, day = 0}:toIso8601())

	self:assertEquals('0123', Time.new{year = 123, month = 0, day = 0}:toIso8601())
	self:assertEquals('1234', Time.new{year = 1234, month = 0, day = 0}:toIso8601())
	self:assertEquals('1234567890', Time.new{year = 1234567890, month = 0, day = 0}:toIso8601())
	self:assertEquals('12345678901', Time.new{year = 12345678901, month = 0, day = 0}:toIso8601())
	self:assertEquals('123456789012', Time.new{year = 123456789012, month = 0, day = 0}:toIso8601())

	self:assertEquals('1234567890123456', Time.new{year = 1234567890123456, month = 0, day = 0}:toIso8601())
	self:assertEquals('9000000000000000-12-31', Time.new{year = 9000000000000000, month = 12, day = 31}:toIso8601())
	self:assertEquals('-900000000000000-12-31', Time.new{year = -900000000000000, month = 12, day = 31}:toIso8601())
	self:assertEquals('-0123-01-01', Time.new{year = -123, month = 1, day = 1}:toIso8601())
	self:assertEquals('0000-01-01', Time.new{year = 0, month = 1, day = 1}:toIso8601())

	self:assertEquals('0001-01-01', Time.new{year = 1, month = 1, day = 1}:toIso8601())
	self:assertEquals('900000000000000-12-31', Time.new{year = 900000000000000, month = 12, day = 31}:toIso8601())
	self:assertEquals('-10000000000000000-12-31', Time.new{year = -9999999999999999, month = 12, day = 31}:toIso8601())
	self:assertEquals('10000000000000000-12-31', Time.new{year = 9999999999999999, month = 12, day = 31}:toIso8601())
end

function suite:testLessOrEqual()
	self:assertTrue(Time.newFromIso8601"1950-10-23" <= Time.newFromIso8601"2013-01-01")
	self:assertTrue(Time.newFromIso8601"2013-01-31" <= Time.newFromIso8601"2013-05-01")
	self:assertTrue(Time.newFromIso8601"2013-02-28" <= Time.newFromIso8601"2013-02-28")

	self:assertFalse(Time.newFromIso8601"2013-01-01" <= Time.newFromIso8601"1950-10-23")
	self:assertFalse(Time.newFromIso8601"2013-05-01" <= Time.newFromIso8601"2013-01-31")

	self:assertFalse(Time.newFromIso8601"1950-10-23" >= Time.newFromIso8601"2013-01-01")
	self:assertFalse(Time.newFromIso8601"2013-01-31" >= Time.newFromIso8601"2013-05-01")
	self:assertFalse(Time.newFromIso8601"2013-02-27" >= Time.newFromIso8601"2013-02-28")
	self:assertTrue (Time.newFromIso8601"2013-02-28" >= Time.newFromIso8601"2013-02-28")

	self:assertTrue(Time.newFromIso8601"2013-01-01" >= Time.newFromIso8601"1950-10-23")
	self:assertTrue(Time.newFromIso8601"2013-05-01" >= Time.newFromIso8601"2013-01-31")

	self:assertFalse(pcall(function () return Time.newFromIso8601"2013-01-01" <= nil end))
	self:assertFalse(pcall(function () return nil <= Time.newFromIso8601"2013-01-01" end))
	self:assertTrue(Time.newFromIso8601"1250-10-23" <= Time.newFromIso8601"2013-01-01")  -- different calendar
end

return suite