Difference between revisions of "Module:Time ago/testcases"

From blackwiki
Jump to navigation Jump to search
blackwiki>FunPika
(Try calculating the number of months in this function for one of the tests.)
blackwiki>FunPika
(Calculate most of the dates in the test cases.)
Line 4: Line 4:
 
function p:test_timeAgo()
 
function p:test_timeAgo()
 
local lang = mw.language.getContentLanguage()
 
local lang = mw.language.getContentLanguage()
monthssincejan11 = ( lang:formatDate( 'U' ) - lang:formatDate( 'U', '15 January 2011' ) ) / 2678400
+
self:preprocess_equals('{{#invoke:TimeAgo|timeAgo|15 January 2011}}', monthssincejan11 .. 'months ago')
+
-- Calculate the time since/until the test dates here, so that the test cases do not fail in the future.
 +
minutessincejul09 = math.floor( ( lang:formatDate( 'U' ) - lang:formatDate( 'U', '1 July 2009' ) ) / 60 )
 +
dayssincejul09 = math.floor( ( lang:formatDate( 'U' ) - lang:formatDate( 'U', '1 July 2009' ) ) / 86400 )
 +
weekssincejul09 = math.floor( ( lang:formatDate( 'U' ) - lang:formatDate( 'U', '1 July 2009' ) ) / 604800 )
 +
monthssincejul09 = math.floor( ( lang:formatDate( 'U' ) - lang:formatDate( 'U', '1 July 2009' ) ) / 2678400 )
 +
yearssincejul09 = math.floor( ( lang:formatDate( 'U' ) - lang:formatDate( 'U', '1 July 2009' ) ) / 31557600 )
 +
monthssincesep13 = math.floor( ( lang:formatDate( 'U' ) - lang:formatDate( 'U', '15 September 2013' ) ) / 2678400 )
 +
monthsuntiljul49 = math.floor( ( lang:formatDate ( 'U', '1 July 2049' ) - lang:formatDate( 'U' ) ) / 2678400 )
 +
yearsuntilaug57 = math.floor( ( lang:formatDate ( 'U', '4 August 2057' ) - lang:formatDate( 'U' ) ) / 31557600 )
 +
 +
self:preprocess_equals('{{#invoke:TimeAgo|timeAgo|15 September 2013}}', monthssincesep13 .. ' months ago')
 
self:preprocess_equals('{{#invoke:TimeAgo|timeAgo|{{CURRENTTIMESTAMP}}}}', '0 seconds ago')
 
self:preprocess_equals('{{#invoke:TimeAgo|timeAgo|{{CURRENTTIMESTAMP}}}}', '0 seconds ago')
self:preprocess_equals('{{#invoke:TimeAgo|timeAgo|15 January 2011|purge=yes}}', '{{Time ago|1 January 2012|purge=yes}}')
+
self:preprocess_equals('{{#invoke:TimeAgo|timeAgo|15 September 2013|purge=yes}}', '{{Time ago|1 January 2012|purge=yes}}')
 
self:preprocess_equals('{{#invoke:TimeAgo|timeAgo|{{CURRENTTIMESTAMP}}}}|purge=yes}}', '{{Time ago|{{CURRENTTIMESTAMP}}|purge=yes}}')
 
self:preprocess_equals('{{#invoke:TimeAgo|timeAgo|{{CURRENTTIMESTAMP}}}}|purge=yes}}', '{{Time ago|{{CURRENTTIMESTAMP}}|purge=yes}}')
self:preprocess_equals('{{#invoke:TimeAgo|timeAgo|July 1 2009}}', '{{Time ago|July 1 2009}}')
+
self:preprocess_equals('{{#invoke:TimeAgo|timeAgo|July 1 2009}}', yearssincejul09 .. ' years ago')
self:preprocess_equals('{{#invoke:TimeAgo|timeAgo|July 1 2009|magnitude=minutes}}', '{{Time ago|July 1 2009|magnitude=minutes}}')
+
self:preprocess_equals('{{#invoke:TimeAgo|timeAgo|July 1 2009|magnitude=minutes}}', minutessincejul09 .. ' minutes ago')
self:preprocess_equals('{{#invoke:TimeAgo|timeAgo|July 1 2009|magnitude=days}}', '{{Time ago|July 1 2009|magnitude=days}}')
+
self:preprocess_equals('{{#invoke:TimeAgo|timeAgo|July 1 2009|magnitude=days}}', dayssincejul09 .. ' days ago')
self:preprocess_equals('{{#invoke:TimeAgo|timeAgo|July 1 2009|magnitude=weeks}}', '{{Time ago|July 1 2009|magnitude=weeks}}')
+
self:preprocess_equals('{{#invoke:TimeAgo|timeAgo|July 1 2009|magnitude=weeks}}', weekssincejul09 .. ' weeks ago')
self:preprocess_equals('{{#invoke:TimeAgo|timeAgo|July 1 2009|magnitude=months}}', '{{Time ago|July 1 2009|magnitude=months}}')
+
self:preprocess_equals('{{#invoke:TimeAgo|timeAgo|July 1 2009|magnitude=months}}', monthssincejul09 .. ' months ago')
self:preprocess_equals('{{#invoke:TimeAgo|timeAgo|July 1 2049|magnitude=months}}', '{{Time ago|July 1 2049|magnitude=months}}')
+
self:preprocess_equals('{{#invoke:TimeAgo|timeAgo|July 1 2049|magnitude=months}}', monthsuntiljul49 .. ' months\' time')
 
self:preprocess_equals('{{#invoke:TimeAgo|timeAgo|Julio 1}}', '<strong class="error">Error: first parameter cannot be parsed as a date or time.</strong>')
 
self:preprocess_equals('{{#invoke:TimeAgo|timeAgo|Julio 1}}', '<strong class="error">Error: first parameter cannot be parsed as a date or time.</strong>')
 
self:preprocess_equals('{{#invoke:TimeAgo|timeAgo|July 1 2009|magnitude=fruits}}', '{{Time ago|July 1 2009|July 1 2009|magnitude=fruits}}')
 
self:preprocess_equals('{{#invoke:TimeAgo|timeAgo|July 1 2009|magnitude=fruits}}', '{{Time ago|July 1 2009|July 1 2009|magnitude=fruits}}')
 
self:preprocess_equals('{{#invoke:TimeAgo|timeAgo}}', '0 seconds ago')
 
self:preprocess_equals('{{#invoke:TimeAgo|timeAgo}}', '0 seconds ago')
 
self:preprocess_equals('{{#invoke:TimeAgo|timeAgo|July 1 2009|magnitude=HoUrS}}', '{{Time ago|July 1 2009|magnitude=HoUrS}}')
 
self:preprocess_equals('{{#invoke:TimeAgo|timeAgo|July 1 2009|magnitude=HoUrS}}', '{{Time ago|July 1 2009|magnitude=HoUrS}}')
self:preprocess_equals('{{#invoke:TimeAgo|timeAgo|4 August 2057}}', '{{time ago|4 August 2057}}')
+
self:preprocess_equals('{{#invoke:TimeAgo|timeAgo|4 August 2057}}', yearsuntilaug57 .. ' years\' time')
 
end
 
end
  
 
return p
 
return p

Revision as of 20:34, 20 December 2013

Documentation for this module may be created at Module:Time ago/testcases/doc

-- Unit tests for [[Module:TimeAgo]]. Click talk page to run tests.
local p = require('Module:UnitTests')

function p:test_timeAgo()
	local lang = mw.language.getContentLanguage()
	
	-- Calculate the time since/until the test dates here, so that the test cases do not fail in the future.
	minutessincejul09 = math.floor( ( lang:formatDate( 'U' ) - lang:formatDate( 'U', '1 July 2009' ) ) / 60 )
	dayssincejul09 = math.floor( ( lang:formatDate( 'U' ) - lang:formatDate( 'U', '1 July 2009' ) ) / 86400 )
	weekssincejul09 = math.floor( ( lang:formatDate( 'U' ) - lang:formatDate( 'U', '1 July 2009' ) ) / 604800 )
	monthssincejul09 = math.floor( ( lang:formatDate( 'U' ) - lang:formatDate( 'U', '1 July 2009' ) ) / 2678400 )
	yearssincejul09 = math.floor( ( lang:formatDate( 'U' ) - lang:formatDate( 'U', '1 July 2009' ) ) / 31557600 )
	monthssincesep13 = math.floor( ( lang:formatDate( 'U' ) - lang:formatDate( 'U', '15 September 2013' ) ) / 2678400 )
	monthsuntiljul49 = math.floor( ( lang:formatDate ( 'U', '1 July 2049' ) - lang:formatDate( 'U' ) ) / 2678400 )
	yearsuntilaug57 = math.floor( ( lang:formatDate ( 'U', '4 August 2057' ) - lang:formatDate( 'U' ) ) / 31557600 )
	
	self:preprocess_equals('{{#invoke:TimeAgo|timeAgo|15 September 2013}}', monthssincesep13 .. ' months ago')
	self:preprocess_equals('{{#invoke:TimeAgo|timeAgo|{{CURRENTTIMESTAMP}}}}', '0 seconds ago')
	self:preprocess_equals('{{#invoke:TimeAgo|timeAgo|15 September 2013|purge=yes}}', '{{Time ago|1 January 2012|purge=yes}}')
	self:preprocess_equals('{{#invoke:TimeAgo|timeAgo|{{CURRENTTIMESTAMP}}}}|purge=yes}}', '{{Time ago|{{CURRENTTIMESTAMP}}|purge=yes}}')
	self:preprocess_equals('{{#invoke:TimeAgo|timeAgo|July 1 2009}}', yearssincejul09 .. ' years ago')
	self:preprocess_equals('{{#invoke:TimeAgo|timeAgo|July 1 2009|magnitude=minutes}}', minutessincejul09 .. ' minutes ago')
	self:preprocess_equals('{{#invoke:TimeAgo|timeAgo|July 1 2009|magnitude=days}}', dayssincejul09 .. ' days ago')
	self:preprocess_equals('{{#invoke:TimeAgo|timeAgo|July 1 2009|magnitude=weeks}}', weekssincejul09 .. ' weeks ago')
	self:preprocess_equals('{{#invoke:TimeAgo|timeAgo|July 1 2009|magnitude=months}}', monthssincejul09 .. ' months ago')
	self:preprocess_equals('{{#invoke:TimeAgo|timeAgo|July 1 2049|magnitude=months}}', monthsuntiljul49 .. ' months\' time')
	self:preprocess_equals('{{#invoke:TimeAgo|timeAgo|Julio 1}}', '<strong class="error">Error: first parameter cannot be parsed as a date or time.</strong>')
	self:preprocess_equals('{{#invoke:TimeAgo|timeAgo|July 1 2009|magnitude=fruits}}', '{{Time ago|July 1 2009|July 1 2009|magnitude=fruits}}')
	self:preprocess_equals('{{#invoke:TimeAgo|timeAgo}}', '0 seconds ago')
	self:preprocess_equals('{{#invoke:TimeAgo|timeAgo|July 1 2009|magnitude=HoUrS}}', '{{Time ago|July 1 2009|magnitude=HoUrS}}')
	self:preprocess_equals('{{#invoke:TimeAgo|timeAgo|4 August 2057}}', yearsuntilaug57 .. ' years\' time')
end

return p