Module:Time ago/testcases
Appearance
![]() | This is the test cases page for the module Module:Time ago. Results of the test cases. |
-- Unit tests for [[Module:TimeAgo]]. Click talk page to run tests.
local p = require('Module:UnitTests')
local Date = require('Module:Date')._Date
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 monthssincesep13 = math.floor( ( currentTime - lang:formatDate( 'U', '15 September 2013' ) ) / 2678400 )
local monthsuntiljul49 = math.floor( ( lang:formatDate ( 'U', '1 July 2049' ) - currentTime ) / 2678400 )
local yearsuntilaug57 = math.floor( ( lang:formatDate ( 'U', '4 August 2057' ) - currentTime ) / 31557600 )
local nowDateObj = Date('currentdate')
local oldDateObj = nowDateObj - '18y'
local oldDateDiff = nowDateObj - oldDateObj
local oldDateText = oldDateObj:text()
self:preprocess_equals('{{#invoke:TimeAgo/sandbox|main|15 September 2013}}', monthssincesep13 .. ' months ago')
self:preprocess_equals('{{#invoke:TimeAgo/sandbox|main|{{CURRENTTIMESTAMP}}}}', '0 seconds ago')
self:preprocess_equals('{{#invoke:TimeAgo/sandbox|main|15 September 2013|purge=yes}}', monthssincesep13 .. ' months ago <span class="plainlinks">([//en.wikipedia.org/w/index.php?title=Module_talk:TimeAgo/testcases&action=purge purge])</span>')
self:preprocess_equals('{{#invoke:TimeAgo/sandbox|main|{{CURRENTTIMESTAMP}}|purge=yes}}', '0 seconds ago <span class="plainlinks">([//en.wikipedia.org/w/index.php?title=Module_talk:TimeAgo/testcases&action=purge purge])</span>')
self:preprocess_equals('{{#invoke:TimeAgo/sandbox|main|July 1 2009}}', '{{age in years|July 1 2009|format=raw}}' .. ' years ago')
self:preprocess_equals('{{#invoke:TimeAgo/sandbox|main|July 1 2009|magnitude=minutes}}', minutessincejul09 .. ' minutes ago')
self:preprocess_equals('{{#invoke:TimeAgo/sandbox|main|July 1 2009|magnitude=days}}', '{{age in days|July 1 2009|format=raw}}' .. ' days ago')
self:preprocess_equals('{{#invoke:TimeAgo/sandbox|main|July 1 2009|magnitude=weeks}}', '{{age in weeks|July 1 2009|format=raw}}' .. ' weeks ago')
self:preprocess_equals('{{#invoke:TimeAgo/sandbox|main|July 1 2009|magnitude=months}}', '{{age in months|July 1 2009|format=raw}}' .. ' months ago')
self:preprocess_equals('{{#invoke:TimeAgo/sandbox|main|July 1 2049|magnitude=months}}', monthsuntiljul49 .. ' months\' time')
self:preprocess_equals('{{#invoke:TimeAgo/sandbox|main|Julio 1}}', '<strong class="error">Error: first parameter cannot be parsed as a date or time.</strong>')
self:preprocess_equals('{{#invoke:TimeAgo/sandbox|main|July 1 2009|magnitude=fruits}}', secondssincejul09 .. ' seconds ago')
self:preprocess_equals('{{#invoke:TimeAgo/sandbox|main}}', '0 seconds ago')
self:preprocess_equals('{{#invoke:TimeAgo/sandbox|main|July 1 2009|magnitude=HoUrS}}', secondssincejul09 .. ' seconds ago')
self:preprocess_equals('{{#invoke:TimeAgo/sandbox|main|4 August 2057}}', yearsuntilaug57 .. ' years\' time')
self:preprocess_equals('{{#invoke:TimeAgo/sandbox|main|15 September 2013|ago=in the past}}', monthssincesep13 .. ' months in the past')
self:preprocess_equals('{{#invoke:TimeAgo/sandbox|main|{{CURRENTTIMESTAMP}}|min_magnitude=weeks}}', '0 weeks ago')
self:preprocess_equals('{{#invoke:TimeAgo/sandbox|main|15 September 2013|ago=}}', monthssincesep13 .. ' months')
self:preprocess_equals('{{#invoke:TimeAgo/sandbox|main|{{#time: r | -12 months -14 days}}|magnitude=months|spellout=yes}}', 'twelve months ago') -- #time always gives one month too few
self:preprocess_equals('{{#invoke:TimeAgo/sandbox|main|{{#time: r | -12 months -14 days}}|magnitude=months|spellout=y}}', 'twelve months ago')
self:preprocess_equals('{{#invoke:TimeAgo/sandbox|main|{{#time: r | -12 months -14 days}}|magnitude=months|spellout=y|spelloutmax=11}}', '12 months ago')
self:preprocess_equals('{{#invoke:TimeAgo/sandbox|main|{{#time: r | -12 months -14 days}}|magnitude=months|spellout=auto}}', '12 months ago')
self:preprocess_equals('{{#invoke:TimeAgo/sandbox|main|{{#time: r | -4 years -14 days}}|magnitude=years|spellout=auto}}', 'four years ago')
self:preprocess_equals('{{#invoke:TimeAgo/sandbox|main|July 1 2009|magnitude=weeks|spellout=yes}}', '{{age in days|July 1 2009|format=raw}}' .. ' weeks ago') -- [[Module:NumberSpell]] can only spell numbers up to 100.
self:preprocess_equals_preprocess('{{#invoke:TimeAgo/sandbox|main|'..oldDateText..'|magnitude=days}}' , oldDateDiff:age('d')..' days ago')
self:preprocess_equals_preprocess('{{#invoke:TimeAgo/sandbox|main|'..oldDateText..'|magnitude=weeks}}' , oldDateDiff:age('w')..' weeks ago')
self:preprocess_equals_preprocess('{{#invoke:TimeAgo/sandbox|main|'..oldDateText..'|magnitude=months}}', oldDateDiff:age('m')..' months ago')
self:preprocess_equals_preprocess('{{#invoke:TimeAgo/sandbox|main|'..oldDateText..'|magnitude=years}}' , oldDateDiff:age('y')..' years ago')
-- Testing whether the module is accurate on the day
self:preprocess_equals('{{#invoke:TimeAgo/sandbox|main|{{#time: r | -10 years }}}}', '10 years ago')
self:preprocess_equals('{{#invoke:TimeAgo/sandbox|main|{{#time: r | -10 years -1 days}}}}', '10 years ago')
self:preprocess_equals('{{#invoke:TimeAgo/sandbox|main|{{#time: r | -1 years }}}}', '12 months ago')
self:preprocess_equals('{{#invoke:TimeAgo/sandbox|main|{{#time: r | -1 years -1 days}}}}', '12 months ago')
self:preprocess_equals('{{#invoke:TimeAgo/sandbox|main|{{#time: r | -1 years -2 days}}}}', '12 months ago')
self:preprocess_equals('{{#invoke:TimeAgo/sandbox|main|{{#time: r | -1 years -3 days}}}}', '12 months ago')
self:preprocess_equals('{{#invoke:TimeAgo/sandbox|main|{{#time: r | -1 years -4 days}}}}', '12 months ago')
self:preprocess_equals('{{#invoke:TimeAgo/sandbox|main|{{#time: r | -1 years -5 days}}}}', '12 months ago')
self:preprocess_equals('{{#invoke:TimeAgo/sandbox|main|{{#time: r | -1 years -6 days}}}}', '12 months ago')
self:preprocess_equals('{{#invoke:TimeAgo/sandbox|main|{{#time: r | -1 years -7 days}}}}', '12 months ago')
self:preprocess_equals('{{#invoke:TimeAgo/sandbox|main|{{#time: r | -1 years -8 days}}}}', '12 months ago')
end
return p