local mDts = require('Module:Date table sorting/sandbox') -- the module to be testedlocal Dts = mDts._exportClasses.Dts -- the Dts classlocal ScribuntoUnit = require('Module:ScribuntoUnit')local suite = ScribuntoUnit:new
--------------------------------------------------------------------------------- Helper functions and values-------------------------------------------------------------------------------
suite.currentYear = os.date('*t').yearsuite.nowrapPattern = '^
]-" style="white%-space:nowrap">[^<>]-$'suite.offValues = suite.onValues = local function makeOnOffAsserts(fragment) suite['assert' .. fragment .. 'WhenOn'] = function (self, first, func, ...) for i, on in ipairs(suite.onValues) do self['assert' .. fragment](self, first, func(on), ...) end end suite['assert' .. fragment .. 'WhenOff'] = function (self, first, func, ...) for i, off in ipairs(suite.offValues) do self['assert' .. fragment](self, first, func(off), ...) end endendmakeOnOffAsserts('Equals')makeOnOffAsserts('StringContains')makeOnOffAsserts('NotStringContains')
function suite:assertErrorContains(pattern, func, plain) local success, msg = pcall(func) self:assertFalse(success) self:assertStringContains(pattern, msg, plain)end
function suite:assertDateEquals(year, month, day, args) local dtsObj = Dts.new(args) self:assertEquals(year, dtsObj.year) self:assertEquals(month, dtsObj.month) self:assertEquals(day, dtsObj.day)end
function suite:assertSortKeyEquals(expected, args) local dtsObj = Dts.new(args) self:assertEquals(expected, dtsObj:makeSortKey)end
function suite:assertDisplayEquals(expected, args) local dtsObj = Dts.new(args) self:assertEquals(expected, dtsObj:makeDisplay)end
--------------------------------------------------------------------------------- Date parameter tests-------------------------------------------------------------------------------
function suite:testDateYMD self:assertDateEquals(2000, 1, 27,)end
function suite:testDateYMonthD self:assertDateEquals(2000, 1, 27,) self:assertDateEquals(2000, 1, 27,)end
function suite:testDateYMonD self:assertDateEquals(2000, 1, 27,) self:assertDateEquals(2000, 1, 27,)end
function suite:testDateYM self:assertDateEquals(2000, 1, nil,)end
function suite:testDateYD self:assertDateEquals(2000, nil, 27,)end
function suite:testDateMD self:assertDateEquals(nil, 1, 27,)end
function suite:testDateY self:assertDateEquals(2000, nil, nil,)end
function suite:testDateM self:assertDateEquals(nil, 1, nil,)end
function suite:testDateD self:assertDateEquals(nil, nil, 27,)end
function suite:testDateNegativeYear self:assertDateEquals(-2000, 1, 27,)end
function suite:testDateString self:assertDateEquals(2000, 1, 27,)end
function suite:testDateAllMonths self:assertDateEquals(nil, 1, nil,) self:assertDateEquals(nil, 2, nil,) self:assertDateEquals(nil, 3, nil,) self:assertDateEquals(nil, 4, nil,) self:assertDateEquals(nil, 5, nil,) self:assertDateEquals(nil, 6, nil,) self:assertDateEquals(nil, 7, nil,) self:assertDateEquals(nil, 8, nil,) self:assertDateEquals(nil, 9, nil,) self:assertDateEquals(nil, 10, nil,) self:assertDateEquals(nil, 11, nil,) self:assertDateEquals(nil, 12, nil,)end
function suite:testDateAllMons self:assertDateEquals(nil, 1, nil,) self:assertDateEquals(nil, 2, nil,) self:assertDateEquals(nil, 3, nil,) self:assertDateEquals(nil, 4, nil,) self:assertDateEquals(nil, 5, nil,) self:assertDateEquals(nil, 6, nil,) self:assertDateEquals(nil, 7, nil,) self:assertDateEquals(nil, 8, nil,) self:assertDateEquals(nil, 9, nil,) self:assertDateEquals(nil, 10, nil,) self:assertDateEquals(nil, 11, nil,) self:assertDateEquals(nil, 12, nil,)end
--------------------------------------------------------------------------------- parseDate tests-------------------------------------------------------------------------------
function suite:testParseYear self:assertDateEquals(2000, nil, nil,)end
function suite:testParseNegativeYear self:assertDateEquals(-2000, nil, nil,)end
function suite:testParseSmallYear self:assertDateEquals(12, nil, nil,)end
function suite:testParseYYYYMMDD self:assertDateEquals(2000, 1, 27,) self:assertDateEquals(2000, 1, 27,) self:assertDateEquals(2000, 1, 6,) self:assertDateEquals(2000, 1, 6,)end
function suite:testParseDDMMYYYY self:assertDateEquals(2000, 1, 27,) self:assertDateEquals(2000, 1, 27,) self:assertDateEquals(2000, 1, 6,)end
function suite:testParseYYYYMM self:assertDateEquals(2000, 1, nil,) self:assertDateEquals(2000, 1, nil,)end
function suite:testParseMonthYYYY self:assertDateEquals(1418, 2, nil,)end
function suite:testParseMonYYYY self:assertDateEquals(1418, 2, nil,)end
function suite:testParseDDMonthYYYY self:assertDateEquals(1975, 4, 24,)end
function suite:testParseDDMonYYYY self:assertDateEquals(1975, 4, 24,)end
function suite:testParseMonthDDYYYY self:assertDateEquals(1975, 4, 24,) self:assertDateEquals(1975, 4, 24,)end
function suite:testParseMonDDYYYY self:assertDateEquals(1975, 4, 24,) self:assertDateEquals(1975, 4, 24,)end
function suite:testParseMonth self:assertDateEquals(nil, 4, nil,) self:assertDateEquals(nil, 4, nil,)end
function suite:testParseMon self:assertDateEquals(nil, 4, nil,) self:assertDateEquals(nil, 4, nil,)end
function suite:testParseDDMonth self:assertDateEquals(nil, 11, 12,)end
function suite:testParseDDMon self:assertDateEquals(nil, 11, 12,)end
function suite:testParseMonthDD self:assertDateEquals(nil, 11, 12,)end
function suite:testParseMonDD self:assertDateEquals(nil, 11, 12,)end
function suite:testParseDDMonthYYY self:assertDateEquals(100, 1, 27,)end
function suite:testParseDDMonth0YYY self:assertDateEquals(100, 1, 27,)end
function suite:testParseDDMonth000Y self:assertDateEquals(3, 1, 27,)end
function suite:testParseAllMonths self:assertDateEquals(nil, 1, nil,) self:assertDateEquals(nil, 2, nil,) self:assertDateEquals(nil, 3, nil,) self:assertDateEquals(nil, 4, nil,) self:assertDateEquals(nil, 5, nil,) self:assertDateEquals(nil, 6, nil,) self:assertDateEquals(nil, 7, nil,) self:assertDateEquals(nil, 8, nil,) self:assertDateEquals(nil, 9, nil,) self:assertDateEquals(nil, 10, nil,) self:assertDateEquals(nil, 11, nil,) self:assertDateEquals(nil, 12, nil,)end
function suite:testParseAllMons self:assertDateEquals(nil, 1, nil,) self:assertDateEquals(nil, 2, nil,) self:assertDateEquals(nil, 3, nil,) self:assertDateEquals(nil, 4, nil,) self:assertDateEquals(nil, 5, nil,) self:assertDateEquals(nil, 6, nil,) self:assertDateEquals(nil, 7, nil,) self:assertDateEquals(nil, 8, nil,) self:assertDateEquals(nil, 9, nil,) self:assertDateEquals(nil, 10, nil,) self:assertDateEquals(nil, 11, nil,) self:assertDateEquals(nil, 12, nil,)end
function suite:testParseSept self:assertDateEquals(nil, 9, nil,) self:assertDateEquals(nil, 9, nil,) self:assertDateEquals(2012, 9, 1,) self:assertDateEquals(2012, 9, 1,)end
--------------------------------------------------------------------------------- Date error tests-------------------------------------------------------------------------------
function suite:testInvalidDateError self:assertErrorContains("'foo' is an invalid date", function Dts.new end)end
function suite:testZeroYearError self:assertErrorContains('years cannot be zero', function Dts.new end)end
function suite:testYearNonIntegerError self:assertErrorContains('years must be an integer', function Dts.new end)end
function suite:testYearLowRangeError self:assertErrorContains('years cannot be less than −999,999,999,999', function Dts.new end, true -- plain match )end
function suite:testYearHighRangeError self:assertErrorContains('years cannot be greater than 999,999,999,999', function Dts.new end)end
function suite:testMonthError self:assertErrorContains('months must be an integer between 1 and 12', function Dts.new end) self:assertErrorContains('months must be an integer between 1 and 12', function Dts.new end)end
function suite:testDayError self:assertErrorContains('days must be an integer between 1 and 31', function Dts.new end) self:assertErrorContains('days must be an integer between 1 and 31', function Dts.new end)end
function suite:testBCError self:assertErrorContains("'foo' is not a valid era code (expected 'BC', 'BCE', 'AD' or 'CE')", function Dts.new end, true -- plain match )end
--------------------------------------------------------------------------------- Sort key tests-------------------------------------------------------------------------------
function suite:testSortKeyYMD suite:assertSortKeyEquals('000000002000-05-27-0000',) suite:assertSortKeyEquals('000000002000-01-27-0000',) suite:assertSortKeyEquals('000000002000-01-01-0000',) suite:assertSortKeyEquals('000000000001-01-01-0000',)end
function suite:testSortKeyYM suite:assertSortKeyEquals('000000002000-05-01-0000',)end
function suite:testSortKeyYD suite:assertSortKeyEquals('000000002000-01-27-0000',)end
function suite:testSortKeyMD suite:assertSortKeyEquals('00000000' .. suite.currentYear .. '-05-27-0000',)end
function suite:testSortKeyY suite:assertSortKeyEquals('000000002000-01-01-0000',)end
function suite:testSortKeyM suite:assertSortKeyEquals('00000000' .. suite.currentYear .. '-05-01-0000',)end
function suite:testSortKeyD suite:assertSortKeyEquals('00000000' .. suite.currentYear .. '-01-27-0000',)end
function suite:testSortKeyNegative suite:assertSortKeyEquals('-999999999955-05-27-0000',)end
function suite:testSortKeyMaxYear suite:assertSortKeyEquals('999999999999-01-01-0000',)end
function suite:testSortKeyMinYear suite:assertSortKeyEquals('-000000000001-01-01-0000',)end
function suite:testSortKeyBlank suite:assertSortKeyEquals('999999999999-99-99-0000',)end
--------------------------------------------------------------------------------- addkey tests-------------------------------------------------------------------------------
function suite:testAddkey suite:assertSortKeyEquals('000000002000-05-27-0003',) suite:assertSortKeyEquals('000000002000-05-27-0003',)end
function suite:testAddkeyError local msg = "the 'addkey' parameter must be an integer between 0 and 9999" self:assertErrorContains(msg, function Dts.new end) self:assertErrorContains(msg, function Dts.new end) self:assertErrorContains(msg, function Dts.new end)end
--------------------------------------------------------------------------------- Display tests-------------------------------------------------------------------------------
function suite:testFormatDefault suite:assertDisplayEquals('January 27, 2000',)end
function suite:testFormatDMY suite:assertDisplayEquals('27 January 2000',)end
function suite:testFormatMDY suite:assertDisplayEquals('January 27, 2000',)end
function suite:testFormatDM suite:assertDisplayEquals('27 January',)end
function suite:testFormatMD suite:assertDisplayEquals('January 27',)end
function suite:testFormatMY suite:assertDisplayEquals('January 2000',)end
function suite:testFormatY suite:assertDisplayEquals('2000',)end
function suite:testFormatM suite:assertDisplayEquals('January',)end
function suite:testFormatD suite:assertDisplayEquals('27',)end
function suite:testFormatHide suite:assertDisplayEquals(,)end
function suite:testBCParam local result = 'January 27, 2000 BC' suite:assertDisplayEquals(result,) suite:assertDisplayEquals(result,) suite:assertDisplayEquals(result,) suite:assertDisplayEquals(result,)end
function suite:testBCNegativeYear suite:assertDisplayEquals('January 27, 2000 BC',)end
function suite:testLargeYearDisplay suite:assertDisplayEquals('1,000,000',)end
function suite:testLargeNegativeYearDisplay suite:assertDisplayEquals('1,000,000 BC',)end
--------------------------------------------------------------------------------- Blank tests-------------------------------------------------------------------------------
function suite:testBlank self:assertStringContains('^
]-">$', mDts._main )end--------------------------------------------------------------------------------- Nowrap tests-------------------------------------------------------------------------------
function suite:testNoWrapDefault self:assertStringContains(self.nowrapPattern, mDts._main )end
function suite:testNoWrapOff self:assertStringContainsWhenOff('^
]-">[^>]-$', function (off) return mDts._main end ) self:assertNotStringContainsWhenOff('white%-space%s*:%s*nowrap', function (off) return mDts._main end )endfunction suite:testNoWrapOn self:assertStringContainsWhenOn(self.nowrapPattern, function (on) return mDts._main end )end
--------------------------------------------------------------------------------- Abbr tests-------------------------------------------------------------------------------
function suite:testAbbrDefault self:assertStringContains('January 1, 2000', mDts._main )end
function suite:testAbbrOn self:assertStringContainsWhenOn('Jan 1, 2000', function (on) return mDts._main end )end
function suite:testAbbrOff self:assertStringContainsWhenOff('January 1, 2000', function (off) return mDts._main end )end
--------------------------------------------------------------------------------- Main tests-------------------------------------------------------------------------------
function suite:testMain local errorPattern = '^Error in %[%[Template:Date table sorting%]%]: .-' self:assertStringContains(errorPattern, mDts.main) self:assertNotStringContains(errorPattern, mDts.main) self:assertStringContains(errorPattern, mDts._main) self:assertNotStringContains(errorPattern, mDts._main)end
return suite