Module:Current events monthly archive/testcases

-- Unit tests for [[Module:Current events monthly archive]].
-- View [[Module talk:Current events monthly archive/testcases]] to run tests.
-- While editing, preview this against [[Module talk:Current events monthly archive/testcases]]

-- We are just going to test the date info, and assume that all our argument-
-- checking code is correct, so make that clear here.
local getDateInfo = require('Module:Current events monthly archive/sandbox').getDateInfo
local suite = require('Module:ScribuntoUnit'):new()

-- Test data
local testData = {
	-- Normal month and year
	{
		date = {
			year  = 2011,
			month = 9,
		},
		results = {
			year                  = 2011,
			monthNumber           = 9,
			date                  = '2011-09-01',
			monthNumberZeroPadded = '09',
			monthName             = 'September',
			monthOrdinal          = 'ninth',
			daysInMonth           = 30,
			firstDayOfMonth       = 'Thursday',
			lastDayOfMonth        = 'Friday',
			beVerb                = 'was',
			leapDesc              = 'common',
			decadeNote            = '',
			moonNote              = '',
		},
	},

	-- Last month of year
	{
		date = {
			year  = 2011,
			month = 12,
		},
		results = {
			year                  = 2011,
			monthNumber           = 12,
			date                  = '2011-12-01',
			monthNumberZeroPadded = '12',
			monthName             = 'December',
			monthOrdinal          = 'twelfth and final',
			daysInMonth           = 31,
		},
	},

	-- Unusual month (February)
	{
		date = {
			year  = 2011,
			month = 2,
		},
		results = {
			year                  = 2011,
			monthNumber           = 2,
			date                  = '2011-02-01',
			monthNumberZeroPadded = '02',
			monthName             = 'February',
			monthOrdinal          = 'second',
			daysInMonth           = 28,
			firstDayOfMonth       = 'Tuesday',
			lastDayOfMonth        = 'Monday',
			beVerb                = 'was',
			leapDesc              = 'common',
			decadeNote            = '',
			moonNote              = '',
		},
	},

	-- Leap year
	{
		date = {
			year  = 1996,
			month = 2,
		},
		results = {
			year                  = 1996,
			monthNumber           = 2,
			date                  = '1996-02-01',
			monthNumberZeroPadded = '02',
			monthName             = 'February',
			monthOrdinal          = 'second',
			daysInMonth           = 29,
			leapDesc              = 'leap',
			decadeNote            = '',
			moonNote              = '',
		},
	},

	-- Another leap year
	{
		date = {
			year  = 2004,
			month = 1,
		},
		results = {
			year                  = 2004,
			monthNumber           = 1,
			date                  = '2004-01-01',
			monthNumberZeroPadded = '01',
			monthName             = 'January',
			monthOrdinal          = 'first',
			daysInMonth           = 31,
			leapDesc              = 'leap',
			decadeNote            = '',
			moonNote              = '',
		},
	},

	-- Century leap year, start of decade
	{
		date = {
			year  = 2000,
			month = 1,
		},
		results = {
			year                  = 2000,
			monthNumber           = 1,
			leapDesc              = 'century leap',
			decadeNote            = 'It was the first month of the [[2000s]] decade.',
			moonNote              = '',
		},
	},

	-- Start of decade
	{
		date = {
			year  = 2010,
			month = 1,
		},
		results = {
			year                  = 2010,
			monthNumber           = 1,
			decadeNote            = 'It was the first month of the [[2010s]] decade.',
		},
	},

	-- End of decade
	{
		date = {
			year  = 2009,
			month = 12,
		},
		results = {
			year                  = 2009,
			monthNumber           = 12,
			decadeNote            = 'It was the last month of the [[2000s]] decade.',
		},
	},

	-- Start of century
	{
		date = {
			year  = 1901,
			month = 1,
		},
		results = {
			year                  = 1901,
			monthNumber           = 1,
			decadeNote            = 'It was the first month of the [[20th century]].',
		},
	},

	-- End of century
	{
		date = {
			year  = 1900,
			month = 12,
		},
		results = {
			year                  = 1900,
			monthNumber           = 12,
			decadeNote            = 'It was the last month of the [[19th century]].',
		},
	},

	-- Unusual ordinal (11th), and start of millennium
	{
		date = {
			year  = 1001,
			month = 1,
		},
		results = {
			year                  = 1001,
			monthNumber           = 1,
			decadeNote            = 'It was the first month of the [[2nd millennium]] and the [[11th century]].',
		},
	},

	-- Unusual ordinal (12th)
	{
		date = {
			year  = 1101,
			month = 1,
		},
		results = {
			year                  = 1101,
			monthNumber           = 1,
			decadeNote            = 'It was the first month of the [[12th century]].',
		},
	},

	-- Unusual ordinal (13th)
	{
		date = {
			year  = 1201,
			month = 1,
		},
		results = {
			year                  = 1201,
			monthNumber           = 1,
			decadeNote            = 'It was the first month of the [[13th century]].',
		},
	},

	-- Start of millennium
	{
		date = {
			year  = 2001,
			month = 1,
		},
		results = {
			year                  = 2001,
			monthNumber           = 1,
			decadeNote            = 'It was the first month of the [[3rd millennium]] and the [[21st century]].',
		},
	},

	-- End of millennium
	{
		date = {
			year  = 2000,
			month = 12,
		},
		results = {
			year                  = 2000,
			monthNumber           = 12,
			decadeNote            = 'It was the last month of the [[2nd millennium]] and the [[20th century]].',
		},
	},

	-- Month with no full moon
	{
		date = {
			year  = 1999,
			month = 2,
		},
		results = {
			year                  = 1999,
			monthNumber           = 2,
			moonNote              = 'This month had no full moon.',
		},
	},

	-- Another month with no full moon
	{
		date = {
			year  = 1961,
			month = 2,
		},
		results = {
			year                  = 1961,
			monthNumber           = 2,
			moonNote              = 'This month had no full moon.',
		},
	},
}

for _, dateData in ipairs(testData) do
	local year = dateData.date.year
	local month = dateData.date.month
	for field, value in pairs(dateData.results) do
		local funcName = string.format(
			'test_%04d_%02d_%s',
			year, month, field
		)
		suite[funcName] = function (self)
			-- We could have got the date info once for each date,
			-- but doing it once for each field ensures that our
			-- tests don't depend on any state changes made by
			-- previous tests. This makes the tests less efficient,
			-- but probably not enough to make much actual
			-- difference.
			local dateInfo = getDateInfo(year, month)
			self:assertEquals(value, dateInfo[field])
		end
	end
end

function suite:test_current_date()
	local year = tonumber(os.date('%Y'))
	local month = tonumber(os.date('%m'))
	local dateInfo = getDateInfo(year, month)
	self:assertEquals(year, dateInfo.year)
	self:assertEquals(month, dateInfo.monthNumber)
	self:assertEquals('is', dateInfo.beVerb)
end

function suite:test_next_month()
	local year = tonumber(os.date('%Y'))
	local month = tonumber(os.date('%m')) + 1
	if month == 13 then
		month = 1
		year = year + 1
	end
	local dateInfo = getDateInfo(year, month)
	self:assertEquals(year, dateInfo.year)
	self:assertEquals(month, dateInfo.monthNumber)
	self:assertEquals('is', dateInfo.beVerb)
end

function suite:test_last_month()
	local year = tonumber(os.date('%Y'))
	local month = tonumber(os.date('%m')) - 1
	if month == 0 then
		month = 12
		year = year - 1
	end
	local dateInfo = getDateInfo(year, month)
	self:assertEquals(year, dateInfo.year)
	self:assertEquals(month, dateInfo.monthNumber)
	self:assertEquals('was', dateInfo.beVerb)
end

function suite:test_december_this_year()
	local year = tonumber(os.date('%Y'))
	local month = 12
	local dateInfo = getDateInfo(year, month)
	self:assertEquals(year, dateInfo.year)
	self:assertEquals(month, dateInfo.monthNumber)
	self:assertEquals('is', dateInfo.beVerb)
end

function suite:test_january_next_year()
	local year = tonumber(os.date('%Y')) + 1
	local month = 1
	local dateInfo = getDateInfo(year, month)
	self:assertEquals(year, dateInfo.year)
	self:assertEquals(month, dateInfo.monthNumber)
	self:assertEquals('is', dateInfo.beVerb)
end

return suite