Module:Time ago/testcases: Difference between revisions

Content deleted Content added
m Fix missing brackets.
No edit summary
 
(28 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 = {
self:preprocess_equals('{{#invoke:TimeAgo|timeAgo|1 January 2012}}', '{{Time ago|1 January 2012}}')
nowDateObj - '18y',
self:preprocess_equals('{{#invoke:TimeAgo|timeAgo|{{CURRENTTIMESTAMP}}}}', '{{Time ago|{{CURRENTTIMESTAMP}}')
Date('July 1 2009'),
self:preprocess_equals('{{#invoke:TimeAgo|timeAgo|1 January 2012|purge=yes}}', '{{Time ago|1 January 2012|purge=yes}}')
nowDateObj - '4m' + '1d',
self:preprocess_equals('{{#invoke:TimeAgo|timeAgo|{{CURRENTTIMESTAMP}}}}|purge=yes}}', '{{Time ago|{{CURRENTTIMESTAMP}}|purge=yes}}')
Date('1 July 2049'),
self:preprocess_equals('{{#invoke:TimeAgo|timeAgo|July 1 2009}}', '{{Time ago|July 1 2009}}')
}
self:preprocess_equals('{{#invoke:TimeAgo|timeAgo|July 1 2009|magnitude=minutes}}', '{{Time ago|July 1 2009|magnitude=minutes}}')
local function checker(id, unit)
self:preprocess_equals('{{#invoke:TimeAgo|timeAgo|July 1 2009|magnitude=days}}', '{{Time ago|July 1 2009|magnitude=days}}')
-- id = 1, 2, ... : dates[id] is the wanted date
self:preprocess_equals('{{#invoke:TimeAgo|timeAgo|July 1 2009|magnitude=weeks}}', '{{Time ago|July 1 2009|magnitude=minutes}}')
-- unit = 'date', 'y', 'm', 'w', 'd', 'h'
self:preprocess_equals('{{#invoke:TimeAgo|timeAgo|July 1 2009|magnitude=months}}', '{{Time ago|July 1 2009|magnitude=minutes}}')
local dateObj = dates[id] or error('invalid id: ' .. tostring(id))
self:preprocess_equals('{{#invoke:TimeAgo|timeAgo|July 1 2049|magnitude=months}}', '{{Time ago|July 1 2049|magnitude=months}}')
if unit == 'date' then
self:preprocess_equals('{{#invoke:TimeAgo|timeAgo|Julio 1}}', '{{Time ago|Julio 1}}')
return dateObj:text()
self:preprocess_equals('{{#invoke:TimeAgo|timeAgo|July 1 2009|magnitude=fruits}}', '{{Time ago|July 1 2009|July 1 2009|magnitude=fruits}}')
end
self:preprocess_equals('{{#invoke:TimeAgo|timeAgo}}', '{{Time ago}}')
local diff = nowDateObj - dateObj
self:preprocess_equals('{{#invoke:TimeAgo|timeAgo|July 1 2009|magnitude=HoUrS}}', '{{Time ago|July 1 2009|magnitude=HoUrS}}')
if unit == 'h' then
self:preprocess_equals('{{#invoke:TimeAgo|timeAgo|4 August 2057}}', '{{time ago|4 August 2057}}')
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, 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('{{#invoke:Time ago/sandbox|main|'..checker(3, 'date')..'}}', checker(3, 'm') .. ' months ago')
self:preprocess_equals('{{#invoke:TimeAgoTime ago/sandbox|timeAgomain|{{CURRENTTIMESTAMP}}}}', '{{Time0 seconds ago|{{CURRENTTIMESTAMP}}')
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:Time ago/sandbox|main|{{CURRENTTIMESTAMP}}|purge=yes}}', '0 seconds 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|July 1 2009}}', checker(2, '{{Timey') ago|July.. 1' 2009}}years ago')
self:preprocess_equals('{{#invoke:TimeAgoTime ago/sandbox|timeAgomain|July 1 2009|magnitude=minutes}}', '{{Timeminutessincejul09 ago|July.. 1' 2009|magnitude=minutes}} ago')
self:preprocess_equals('{{#invoke:TimeAgoTime ago/sandbox|timeAgomain|July 1 2009|magnitude=days}}', checker(2, 'd'{{Time) ago|July.. 1' 2009|magnitude=days}} ago')
self:preprocess_equals('{{#invoke:TimeAgoTime ago/sandbox|timeAgomain|July 1 2009|magnitude=weeks}}', checker(2, 'w'{{Time) ago|July.. 1' 2009|magnitude=minutes}}weeks ago')
self:preprocess_equals('{{#invoke:TimeAgoTime ago/sandbox|timeAgomain|July 1 2009|magnitude=months}}', checker(2, 'm'{{Time) ago|July.. 1' 2009|magnitude=minutes}}months ago')
self:preprocess_equals('{{#invoke:TimeAgoTime ago/sandbox|timeAgomain|July 1 2049|magnitude=months}}', checker(4, 'm'{{Time) ago|July.. 1' 2049|magnitude=months}}\' time')
self:preprocess_equals('{{#invoke:Time ago/sandbox|main|Julio 1}}', '<strong class="error">Error: first parameter cannot be parsed as a date or time.</strong>')
self:preprocess_equals('{{#invoke:TimeAgoTime ago/sandbox|timeAgomain|July 1 2009|magnitude=fruits}}', '{{Timesecondssincejul09 ago|July.. 1' 2009|Julyseconds 1 2009|magnitude=fruits}}ago')
self:preprocess_equals('{{#invoke:TimeAgoTime ago/sandbox|timeAgo|1 January 2012main}}', '{{Time0 seconds ago|1 January 2012}}')
self:preprocess_equals('{{#invoke:TimeAgoTime ago/sandbox|timeAgomain|July 1 2009|magnitude=HoUrS}}', secondssincejul09 .. '{{Time seconds ago|July 1 2009|magnitude=HoUrS}}')
self:preprocess_equals('{{#invoke:TimeAgoTime ago/sandbox|timeAgomain|4 August 2057}}', yearsuntilaug57 .. '{{time ago|4years\' August 2057}}time')
self:preprocess_equals('{{#invoke:Time ago/sandbox|main|'..checker(3, 'date')..'|ago=in the past}}', checker(3, 'm') .. ' months in the past')
self:preprocess_equals('{{#invoke:TimeAgoTime ago/sandbox|timeAgomain|{{CURRENTTIMESTAMP}}}}|purgemin_magnitude=yesweeks}}', '{{Time0 weeks ago|{{CURRENTTIMESTAMP}}|purge=yes}}')
self:preprocess_equals('{{#invoke:Time ago/sandbox|main|'..checker(3, 'date')..'|ago=}}', checker(3, 'm') .. ' months')
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:TimeAgoTime ago/sandbox|timeAgomain|{{#time: r | -10 years }}}}', '{{Time10 years ago}}')
self:preprocess_equals('{{#invoke:Time ago/sandbox|main|{{#time: r | -10 years -1 days}}}}', '10 years ago')
self:preprocess_equals('{{#invoke:TimeAgoTime ago/sandbox|timeAgomain|Julio{{#time: r | -1 years }}}}', '{{Time12 ago|Juliomonths 1}}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