Module:Time ago/testcases

-- Unit tests for Module:Time ago. Click talk page to run tests. local p = require('Module:UnitTests') local Date = require('Module:Date')._Date

local nowDateObj = Date('currentdate') local dates = { nowDateObj - '18y', Date('July 1 2009'), nowDateObj - '4m' + '1d', Date('1 July 2049'), } local function checker(id, unit) -- id = 1, 2, ... : dates[id] is the wanted date -- unit = 'date', 'y', 'm', 'w', 'd', 'h' local dateObj = dates[id] or error('invalid id: ' .. tostring(id)) if unit == 'date' then return dateObj:text end local diff = nowDateObj - dateObj if unit == 'h' then return math.floor(diff.days_ago * 24) end return tostring(math.abs(diff:age(unit))) end

function p:test_main local lang = mw.language.getContentLanguage

-- Calculate the time since/until the test dates here, since the expected output is dependent on the current time in most cases. local currentTime = lang:formatDate( 'U' ) local jul09 = currentTime - lang:formatDate( 'U', '1 July 2009' ) local secondssincejul09 = math.floor( jul09 ) local minutessincejul09 = math.floor( jul09 / 60 ) local yearsuntilaug57 = math.floor( ( lang:formatDate ( 'U', '4 August 2057' ) - currentTime ) / 31557600 )

self:preprocess_equals('', checker(3, 'm') .. ' months ago') self:preprocess_equals('', '0 seconds ago') self:preprocess_equals('', checker(3, 'm') .. ' months ago ([//en.wikipedia.org/w/index.php?title=Module_talk:Time ago/testcases&action=purge purge]) ') self:preprocess_equals('', '0 seconds ago ([//en.wikipedia.org/w/index.php?title=Module_talk:Time ago/testcases&action=purge purge]) ') self:preprocess_equals('', checker(2, 'y') .. ' years ago') self:preprocess_equals('', minutessincejul09 .. ' minutes ago') self:preprocess_equals('', checker(2, 'd') .. ' days ago') self:preprocess_equals('', checker(2, 'w') .. ' weeks ago') self:preprocess_equals('', checker(2, 'm') .. ' months ago') self:preprocess_equals('', checker(4, 'm') .. ' months\' time') self:preprocess_equals('', ' Error: first parameter cannot be parsed as a date or time. ') self:preprocess_equals('', secondssincejul09 .. ' seconds ago') self:preprocess_equals('', '0 seconds ago') self:preprocess_equals('', secondssincejul09 .. ' seconds ago') self:preprocess_equals('', yearsuntilaug57 .. ' years\' time') self:preprocess_equals('', checker(3, 'm') .. ' months in the past') self:preprocess_equals('', '0 weeks ago') self:preprocess_equals('', checker(3, 'm') .. ' months') self:preprocess_equals('', 'twelve months ago') -- #time always gives one month too few self:preprocess_equals('', 'twelve months ago') self:preprocess_equals('', '12 months ago') self:preprocess_equals('', '12 months ago') self:preprocess_equals('', 'four years ago') self:preprocess_equals('', checker(2, 'w') .. ' weeks ago') -- Module:NumberSpell can only spell numbers up to 100. self:preprocess_equals_preprocess('', checker(1, 'd')..' days ago') self:preprocess_equals_preprocess('', checker(1, 'w')..' weeks ago') self:preprocess_equals_preprocess('', checker(1, 'm')..' months ago') self:preprocess_equals_preprocess('', checker(1, 'y')..' years ago')

-- Testing whether the module is accurate on the day self:preprocess_equals('', '10 years ago') self:preprocess_equals('', '10 years ago') self:preprocess_equals('', '12 months ago') self:preprocess_equals('', '12 months ago') self:preprocess_equals('', '12 months ago') self:preprocess_equals('', '12 months ago') self:preprocess_equals('', '12 months ago') self:preprocess_equals('', '12 months ago') self:preprocess_equals('', '12 months ago') self:preprocess_equals('', '12 months ago') self:preprocess_equals('', '12 months ago') self:preprocess_equals('', '12') end

return p