Module:Time ago/testcases: Difference between revisions

Content deleted Content added
Calculate most of the dates in the test cases.
No edit summary
 
(24 intermediate revisions by 8 users not shown)
Line 1:
-- Unit tests for [[Module:TimeAgoTime ago]]. Click talk page to run tests.
local p = require('Module:UnitTests')
local Date = require('Module:Date')._Date
 
local nowDateObj = Date('currentdate')
function p:test_timeAgo()
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_timeAgotest_main()
local lang = mw.language.getContentLanguage()
 
-- Calculate the time since/until the test dates here, so thatsince the testexpected casesoutput dois notdependent failon the current time in themost futurecases.
minutessincejul09local currentTime = math.floor( ( lang:formatDate( 'U' ) - lang:formatDate( 'U', '1 July 2009' ) ) / 60 )
dayssincejul09local jul09 = math.floor( ( lang:formatDate( 'U' )currentTime - lang:formatDate( 'U', '1 July 2009' ) ) / 86400 )
local secondssincejul09 = math.floor( jul09 )
weekssincejul09 = math.floor( ( lang:formatDate( 'U' ) - lang:formatDate( 'U', '1 July 2009' ) ) / 604800 )
local minutessincejul09 = math.floor( jul09 / 60 )
monthssincejul09 = math.floor( ( lang:formatDate( 'U' ) - lang:formatDate( 'U', '1 July 2009' ) ) / 2678400 )
yearssincejul09local yearsuntilaug57 = math.floor( ( lang:formatDate( 'U' ) - lang:formatDate( 'U', '14 JulyAugust 20092057' ) - currentTime ) / 31557600 )
 
monthssincesep13 = math.floor( ( lang:formatDate( 'U' ) - lang:formatDate( 'U', '15 September 2013' ) ) / 2678400 )
self:preprocess_equals('{{#invoke:Time ago/sandbox|main|'..checker(3, 'date')..'}}', checker(3, 'm') .. ' months ago')
monthsuntiljul49 = math.floor( ( lang:formatDate ( 'U', '1 July 2049' ) - lang:formatDate( 'U' ) ) / 2678400 )
self:preprocess_equals('{{#invoke:Time ago/sandbox|main|{{CURRENTTIMESTAMP}}}}', '0 seconds ago')
yearsuntilaug57 = math.floor( ( lang:formatDate ( 'U', '4 August 2057' ) - lang:formatDate( 'U' ) ) / 31557600 )
self:preprocess_equals('{{#invoke:Time ago/sandbox|main|'..checker(3, 'date')..'|purge=yes}}', checker(3, 'm') .. ' months ago <span class="plainlinks">([//en.wikipedia.org/w/index.php?title=Module_talk:Time ago/testcases&action=purge purge])</span>')
self:preprocess_equals('{{#invoke:TimeAgoTime ago/sandbox|timeAgomain|15 September 2013{{CURRENTTIMESTAMP}}|purge=yes}}', monthssincesep13'0 ..seconds 'ago months<span class="plainlinks">([//en.wikipedia.org/w/index.php?title=Module_talk:Time ago/testcases&action=purge purge])</span>')
self:preprocess_equals('{{#invoke:TimeAgoTime ago/sandbox|timeAgomain|{{CURRENTTIMESTAMP}}July 1 2009}}', checker(2, 'y') .. '0 secondsyears ago')
self:preprocess_equals('{{#invoke:TimeAgoTime ago/sandbox|timeAgomain|15July September1 20132009|purgemagnitude=yesminutes}}', minutessincejul09 .. '{{Time minutes ago|1 January 2012|purge=yes}}')
self:preprocess_equals('{{#invoke:TimeAgoTime ago/sandbox|timeAgomain|{{CURRENTTIMESTAMP}}}}July 1 2009|purgemagnitude=yesdays}}', checker(2, '{{Timed') .. ' days ago|{{CURRENTTIMESTAMP}}|purge=yes}}')
self:preprocess_equals('{{#invoke:TimeAgoTime ago/sandbox|timeAgomain|July 1 2009|magnitude=weeks}}', yearssincejul09checker(2, 'w') .. ' yearsweeks ago')
self:preprocess_equals('{{#invoke:TimeAgoTime ago/sandbox|timeAgomain|July 1 2009|magnitude=minutesmonths}}', minutessincejul09checker(2, 'm') .. ' minutesmonths ago')
self:preprocess_equals('{{#invoke:TimeAgoTime ago/sandbox|timeAgomain|July 1 20092049|magnitude=daysmonths}}', dayssincejul09checker(4, 'm') .. ' daysmonths\' agotime')
self:preprocess_equals('{{#invoke:TimeAgoTime ago/sandbox|timeAgomain|JulyJulio 1 2009|magnitude=weeks}}', weekssincejul09'<strong ..class="error">Error: 'first weeksparameter agocannot be parsed as a date or time.</strong>')
self:preprocess_equals('{{#invoke:TimeAgoTime ago/sandbox|timeAgomain|July 1 2009|magnitude=monthsfruits}}', monthssincejul09secondssincejul09 .. ' monthsseconds ago')
self:preprocess_equals('{{#invoke:TimeAgo|timeAgo|July 1Time 2049ago/sandbox|magnitude=monthsmain}}', monthsuntiljul49 .. '0 months\'seconds timeago')
self:preprocess_equals('{{#invoke:TimeAgoTime ago/sandbox|timeAgomain|JulioJuly 1 2009|magnitude=HoUrS}}', '<strongsecondssincejul09 class="error">Error:.. first' parameterseconds cannot be parsed as a date or time.</strong>ago')
self:preprocess_equals('{{#invoke:TimeAgoTime ago/sandbox|timeAgomain|July4 1August 2009|magnitude=fruits2057}}', '{{Timeyearsuntilaug57 ago|July.. 1' 2009|Julyyears\' 1 2009|magnitude=fruits}}time')
self:preprocess_equals('{{#invoke:TimeAgoTime ago/sandbox|timeAgomain|'..checker(3, 'date')..'|ago=in the past}}', checker(3, '0m') seconds.. ago' months in the past')
self:preprocess_equals('{{#invoke:TimeAgoTime ago/sandbox|timeAgomain|July 1 2009{{CURRENTTIMESTAMP}}|magnitudemin_magnitude=HoUrSweeks}}', '{{Time0 weeks ago|July 1 2009|magnitude=HoUrS}}')
self:preprocess_equals('{{#invoke:TimeAgoTime ago/sandbox|timeAgomain|4 August'..checker(3, 2057'date')..'|ago=}}', yearsuntilaug57checker(3, 'm') .. ' years\' timemonths')
self:preprocess_equals('{{#invoke:Time ago/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:Time ago/sandbox|main|{{#time: r | -12 months -14 days}}|magnitude=months|spellout=y}}', 'twelve months ago')
self:preprocess_equals('{{#invoke:Time ago/sandbox|main|{{#time: r | -12 months -14 days}}|magnitude=months|spellout=y|spelloutmax=11}}', '12 months ago')
self:preprocess_equals('{{#invoke:Time ago/sandbox|main|{{#time: r | -12 months -14 days}}|magnitude=months|spellout=auto}}', '12 months ago')
self:preprocess_equals('{{#invoke:Time ago/sandbox|main|{{#time: r | -4 years -14 days}}|magnitude=years|spellout=auto}}', 'four years ago')
self:preprocess_equals('{{#invoke:Time ago/sandbox|main|July 1 2009|magnitude=weeks|spellout=yes}}', checker(2, 'w') .. ' weeks ago') -- [[Module:NumberSpell]] can only spell numbers up to 100.
self:preprocess_equals_preprocess('{{#invoke:Time ago/sandbox|main|'..checker(1, 'date')..'|magnitude=days}}' , checker(1, 'd')..' days ago')
self:preprocess_equals_preprocess('{{#invoke:Time ago/sandbox|main|'..checker(1, 'date')..'|magnitude=weeks}}' , checker(1, 'w')..' weeks ago')
self:preprocess_equals_preprocess('{{#invoke:Time ago/sandbox|main|'..checker(1, 'date')..'|magnitude=months}}', checker(1, 'm')..' months ago')
self:preprocess_equals_preprocess('{{#invoke:Time ago/sandbox|main|'..checker(1, 'date')..'|magnitude=years}}' , checker(1, 'y')..' years ago')
 
-- Testing whether the module is accurate on the day
self:preprocess_equals('{{#invoke:Time ago/sandbox|main|{{#time: r | -10 years }}}}', '10 years ago')
self:preprocess_equals('{{#invoke:Time ago/sandbox|main|{{#time: r | -10 years -1 days}}}}', '10 years ago')
self:preprocess_equals('{{#invoke:Time ago/sandbox|main|{{#time: r | -1 years }}}}', '12 months ago')
self:preprocess_equals('{{#invoke:Time ago/sandbox|main|{{#time: r | -1 years -1 days}}}}', '12 months ago')
self:preprocess_equals('{{#invoke:Time ago/sandbox|main|{{#time: r | -1 years -2 days}}}}', '12 months ago')
self:preprocess_equals('{{#invoke:Time ago/sandbox|main|{{#time: r | -1 years -3 days}}}}', '12 months ago')
self:preprocess_equals('{{#invoke:Time ago/sandbox|main|{{#time: r | -1 years -4 days}}}}', '12 months ago')
self:preprocess_equals('{{#invoke:Time ago/sandbox|main|{{#time: r | -1 years -5 days}}}}', '12 months ago')
self:preprocess_equals('{{#invoke:Time ago/sandbox|main|{{#time: r | -1 years -6 days}}}}', '12 months ago')
self:preprocess_equals('{{#invoke:Time ago/sandbox|main|{{#time: r | -1 years -7 days}}}}', '12 months ago')
self:preprocess_equals('{{#invoke:Time ago/sandbox|main|{{#time: r | -1 years -8 days}}}}', '12 months ago')
self:preprocess_equals('{{#invoke:Time ago/sandbox|main|{{#time: r | -1 years -8 days}}|numeric=y}}', '12')
end