local testframework = require 'Module:TestFramework' local function diff( a, b ) local d = mw.date.new( unpack( a ) ) - mw.date.new( unpack( b ) ) return d.years, d.months, d.days, d.isNegative, d.isZero, d.totalDays end local function cmp( a, b ) local x, y = mw.date.new( unpack( a ) ), mw.date.new( unpack( b ) ) return x < y, x <= y, x == y, x > y, x >= y end local function tostr( ... ) return tostring( mw.date.new( ... ) ) end local function fields( ... ) local d = mw.date.new( ... ) return d.year, d.month, d.day end local function hiddenFields() local d = mw.date.getCurrentDate() local df = d - mw.date.new( 2020, 1, 1 ) return d.isCurrent, d.offset, d.jdn, d.jd, d.jdz, d.calendar, next( d ), getmetatable( d ), next( df ), getmetatable( df ) end local function isValid( ... ) return mw.date.new( ... ) ~= nil end local function diffMethod() local d = mw.date.new( 2020, 3, 5 ):diff( mw.date.new( 2020, 1, 30 ), {} ) return d.months, d.days end local function add( n, ... ) local d = mw.date.new( ... ) + n return d and tostring( d ) end local function subDays( n, ... ) local d = mw.date.new( ... ) - n return d and tostring( d ) end local function currentDate() local c1, c2 = mw.date.getCurrentDate(), mw.date.getCurrentDate( {} ) return c1 == c2, rawequal( c1, c2 ), type( c1.year ), type( c1.month ), type( c1.day ) end local tests = { { name = 'new: fields', func = fields, args = { 2020, 2, 29 }, expect = { 2020, 2, 29 } }, { name = 'new: empty options', func = fields, args = { 2020, 2, 29, {} }, expect = { 2020, 2, 29 } }, { name = 'new: unknown option', func = mw.date.new, args = { 2020, 2, 29, { calendar = 'Julian' } }, expect = "bad argument #4 to 'new' (unknown option 'calendar')" }, { name = 'new: negative zero normalized', func = function () local d = mw.date.new( -0, 1, 2 ) return 1 / d.year > 0, tostring( d ) end, expect = { true, '0000-01-02' } }, { name = 'hidden fields and metatable protection', func = hiddenFields, expect = { nil, nil, nil, nil, nil, nil, nil, false, nil, false } }, { name = 'mw.clone preserves Date and DateDiff', func = function () local d = mw.date.new( 2000, 1, 1 ) local c = mw.clone( d ) local diff = mw.date.new( 2020, 3, 2 ) - d local cd = mw.clone( diff ) local t = mw.clone( { birth = d, same = d, diff = diff } ) return c.year, c.month, c.day, c == d, rawequal( c, d ), tostring( c + 1 ), cd.years, cd.months, cd.days, rawequal( cd, diff ), t.birth.year, tostring( t.birth + 10 ), rawequal( t.birth, t.same ), t.diff.years, t.diff.months, t.diff.days, mw.registerCloneHandler == nil end, expect = { 2000, 1, 1, true, false, '2000-01-02', 20, 2, 1, false, 2000, '2000-01-11', true, 20, 2, 1, true, } }, { name = 'Date is read-only', func = function () local d = mw.date.new( 2020, 1, 1 ) d.year = 2021 end, expect = 'Date objects are read-only' }, { name = 'DateDiff is read-only', func = function () local d = mw.date.new( 2020, 1, 2 ) - mw.date.new( 2020, 1, 1 ) d.days = 99 end, expect = 'DateDiff objects are read-only' }, { name = 'new: invalid day', func = isValid, args = { 2021, 2, 29 }, expect = { false } }, { name = 'new: invalid month', func = isValid, args = { 2021, 13, 1 }, expect = { false } }, { name = 'new: non-integer', func = isValid, args = { 2021, 1, 1.5 }, expect = { false } }, { name = 'new: bad argument', func = mw.date.new, args = { '2021', 1, 1 }, expect = "bad argument #1 to 'new' (number expected, got string)" }, { name = 'tostring', func = tostr, args = { 2020, 1, 2 }, expect = { '2020-01-02' } }, { name = 'tostring: negative year', func = tostr, args = { -1, 1, 2 }, expect = { '-0001-01-02' } }, { name = 'tostring: negative 4-digit year', func = tostr, args = { -9999, 1, 1 }, expect = { '-9999-01-01' } }, { name = 'compare: before', func = cmp, args = { { 2020, 1, 1 }, { 2020, 1, 2 } }, expect = { true, true, false, false, false } }, { name = 'compare: equal', func = cmp, args = { { 2020, 1, 1 }, { 2020, 1, 1 } }, expect = { false, true, true, false, true } }, -- Expected values follow en:Module:Date { name = 'diff: months and days', func = diff, args = { { 2020, 3, 5 }, { 2020, 1, 30 } }, expect = { 0, 1, 5, false, false, 35 } }, { name = 'diff: d2 equals dpm (28)', func = diff, args = { { 2015, 3, 1 }, { 2015, 1, 28 } }, expect = { 0, 1, 1, false, false, 32 } }, { name = 'diff: d2 below dpm (28)', func = diff, args = { { 2015, 3, 1 }, { 2015, 1, 27 } }, expect = { 0, 1, 2, false, false, 33 } }, { name = 'diff: leap day birthday', func = diff, args = { { 2021, 3, 1 }, { 2020, 2, 29 } }, expect = { 1, 0, 1, false, false, 366 } }, { name = 'diff: day before leap day anniversary', func = diff, args = { { 2021, 2, 28 }, { 2020, 2, 29 } }, expect = { 0, 11, 30, false, false, 365 } }, { name = 'diff: leap day before non-leap Feb 28', func = diff, args = { { 2026, 2, 1 }, { 2024, 2, 29 } }, expect = { 1, 11, 3, false, false, 703 } }, { name = 'diff: century non-leap year 1900', func = diff, args = { { 1900, 3, 1 }, { 1896, 2, 29 } }, expect = { 4, 0, 1, false, false, 1461 } }, { name = 'diff: 400-year leap year 2000', func = diff, args = { { 2000, 2, 29 }, { 1996, 2, 29 } }, expect = { 4, 0, 0, false, false, 1461 } }, { name = 'diff: Dec to Jan borrow', func = diff, args = { { 2021, 1, 15 }, { 2020, 12, 31 } }, expect = { 0, 0, 15, false, false, 15 } }, { name = 'diff: Jan year borrow with Dec dpm', func = diff, args = { { 2021, 1, 1 }, { 2020, 1, 31 } }, expect = { 0, 11, 1, false, false, 336 } }, { name = 'diff: years', func = diff, args = { { 2026, 11, 25 }, { 1990, 11, 26 } }, expect = { 35, 11, 30, false, false, 13148 } }, { name = 'diff: negative', func = diff, args = { { 2020, 1, 30 }, { 2020, 3, 5 } }, expect = { 0, 1, 5, true, false, 35 } }, { name = 'diff: zero', func = diff, args = { { 2020, 1, 30 }, { 2020, 1, 30 } }, expect = { 0, 0, 0, false, true, 0 } }, { name = 'diff method', func = diffMethod, expect = { 1, 5 } }, { name = 'diff: bad self', func = function () local d = mw.date.new( 2020, 1, 1 ) return d.diff( {}, d ) end, expect = "mw.date: invalid Date object. Did you call diff with a dot instead of a colon, i.e. date.diff() instead of date:diff()?" }, { name = 'diff: unknown option', func = function () return mw.date.new( 2020, 3, 5 ):diff( mw.date.new( 2020, 1, 30 ), { mode = 'fr' } ) end, expect = "bad argument #2 to 'diff' (unknown option 'mode')" }, { name = 'add: leap day', func = add, args = { 1, 2020, 2, 28 }, expect = { '2020-02-29' } }, { name = 'add: year end', func = add, args = { 1, 2020, 12, 31 }, expect = { '2021-01-01' } }, { name = 'add: negative', func = add, args = { -1, 2021, 3, 1 }, expect = { '2021-02-28' } }, { name = 'add: out of range', func = add, args = { 1, 9999, 12, 31 }, expect = { nil } }, { name = 'add: large out of range returns nil', func = add, args = { 1e8, 2020, 1, 1 }, expect = { nil } }, { name = 'add: fraction', func = add, args = { 0.5, 2020, 1, 1 }, expect = 'only a whole number of days can be added to a date' }, { name = 'add: infinity', func = add, args = { math.huge, 2020, 1, 1 }, expect = 'only a whole number of days can be added to a date' }, { name = 'add: number on left rejected', func = function () return 1 + mw.date.new( 2020, 1, 1 ) end, expect = 'only a whole number of days can be added to a date' }, { name = 'sub: days', func = subDays, args = { 1, 2021, 3, 1 }, expect = { '2021-02-28' } }, { name = 'sub: negative days', func = subDays, args = { -1, 2020, 2, 28 }, expect = { '2020-02-29' } }, { name = 'sub: out of range', func = subDays, args = { 1, -9999, 1, 1 }, expect = { nil } }, { name = 'sub: fraction', func = subDays, args = { 0.5, 2020, 1, 1 }, expect = 'only a date or whole number of days can be subtracted from a date' }, { name = 'getCurrentDate', func = currentDate, expect = { true, false, 'number', 'number', 'number' } }, { name = 'getCurrentDate: unknown option', func = function () return mw.date.getCurrentDate( { tz = 'local' } ) end, expect = "bad argument #1 to 'getCurrentDate' (unknown option 'tz')" }, } return testframework.getTestProvider( tests )