From: Tim Wood Date: Thu, 13 Oct 2011 21:53:51 +0000 (-0700) Subject: Changing tests to move all language related tests to language specific test file X-Git-Tag: 1.0.1~23 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=99b82394d61677f242ad6bd1c9953b6c2dd3bac8;p=thirdparty%2Fmoment.git Changing tests to move all language related tests to language specific test file --- diff --git a/build.js b/build.js index 5e05b08a6..20b372e6a 100755 --- a/build.js +++ b/build.js @@ -34,7 +34,9 @@ var JSHINT_CONFIG = { "strict": false, "white": true }; -var LANGS = "fr it pt".split(" "); +var LANG_MINIFY = "fr it pt".split(" "); +var LANG_TEST = "en".split(" "); +var LANG_PREFIX = "var _date;if (typeof window === 'undefined') {_date = require('../underscore.date.js');module = QUnit.module;}"; /********************************************* @@ -102,7 +104,7 @@ function hint(source, name) { /********************************************* - Lang + Lang Minify *********************************************/ @@ -111,10 +113,10 @@ function hint(source, name) { i, failures = 0, source; - for (i = 0; i < LANGS.length; i++) { - source = fs.readFileSync('./lang/' + LANGS[i] + '.js', 'utf8'); - if (hint(source, 'lang/' + LANGS[i])) { - minifyToFile(source, 'lang/' + LANGS[i]); + for (i = 0; i < LANG_MINIFY.length; i++) { + source = fs.readFileSync('./lang/' + LANG_MINIFY[i] + '.js', 'utf8'); + if (hint(source, 'lang/' + LANG_MINIFY[i])) { + minifyToFile(source, 'lang/' + LANG_MINIFY[i]); allSource += source; } else { failures ++; @@ -126,6 +128,20 @@ function hint(source, name) { })(); +/********************************************* + Lang Tests +*********************************************/ + + +(function(){ + var source = LANG_PREFIX; + for (i = 0; i < LANG_TEST.length; i++) { + source += fs.readFileSync('./test/lang/' + LANG_TEST[i] + '.js', 'utf8'); + } + makeFile('./test/lang.js', source); +})(); + + /********************************************* Main *********************************************/ diff --git a/lang/test.js b/lang/test.js deleted file mode 100644 index fc4210716..000000000 --- a/lang/test.js +++ /dev/null @@ -1,35 +0,0 @@ -(function () { - var data = { - months : ["1.test", "2.test", "3.test", "4.test", "5.test", "6.test", "7.test", "8.test", "9.test", "10.test", "11.test", "12.test"], - monthsShort : ["1.t", "2.t", "3.t", "4.t", "5.t", "6.t", "7.t", "8.t", "9.t", "10.t", "11.t", "12.t"], - weekdays : ["a.test", "b.test", "c.test", "d.test", "e.test", "f.test", "g.test"], - weekdaysShort : ["a.t", "b.t", "c.t", "d.t", "e.t", "f.t", "g.t"], - relativeTime : { - future: "%s future", - past: "%s past", - s: "s", - m: "m", - mm: "%d mm", - h: "h", - hh: "%d hh", - d: "d", - dd: "%d dd", - M: "M", - MM: "%d MM", - y: "y", - yy: "%d yy" - }, - ordinal : function (number) { - return 'ordinal'; - } - }; - - // Node - if (typeof module !== 'undefined') { - module.exports = data; - } - // Browser - if (typeof window !== 'undefined' && this._date && this._date.lang) { - this._date.lang('test', data); - } -}()); \ No newline at end of file diff --git a/test.js b/test.js index f67d0c1f8..aba6636a8 100755 --- a/test.js +++ b/test.js @@ -4,10 +4,10 @@ testrunner.options.errorsOnly = true; testrunner.run({ code: "./underscore.date.js", - tests: "./test/date.js" + tests: ["./test/date.js", "./test/lang.js"] }); testrunner.run({ code: "./underscore.date.min.js", - tests: "./test/date.js" + tests: ["./test/date.js", "./test/lang.js"] }); \ No newline at end of file diff --git a/test/date.js b/test/date.js index fb8c4ecc1..c1f6a5e0c 100755 --- a/test/date.js +++ b/test/date.js @@ -88,104 +88,33 @@ test("string with format - years", 2, function() { }); -module("format"); - - -test("format", 15, function() { - var a = [ - ['dddd, MMMM Do YYYY, h:mm:ss a', 'Sunday, February 14th 2010, 3:25:50 pm'], - ['ddd, hA', 'Sun, 3PM'], - ['M Mo MM MMMM MMM', '2 2nd 02 February Feb'], - ['YYYY YY', '2010 10'], - ['D Do DD', '14 14th 14'], - ['d do dddd ddd', '0 0th Sunday Sun'], - ['DDD DDDo DDDD', '45 45th 045'], - ['w wo ww', '8 8th 08'], - ['h hh', '3 03'], - ['H HH', '15 15'], - ['m mm', '25 25'], - ['s ss', '50 50'], - ['a A', 'pm PM'], - ['z zz', 'PST PST'], - ['t\\he DDDo \\d\\ay of t\\he ye\\ar', 'the 45th day of the year'] - ], - b = _date(new Date(2010, 1, 14, 15, 25, 50, 125)), - i; - for (i = 0; i < a.length; i++) { - equal(b.format(a[i][0]), a[i][1], a[i][0] + ' ---> ' + a[i][1]); - } -}); - - module("add and subtract"); -test("add and subtract", 3, function() { - equal( - _date([2010, 1, 14, 15, 25, 50, 125]).add({ms:200,s:10,m:10,h:2,d:3,M:2,y:3}).format("MMMM Do YYYY, h:mm:ss a"), - "April 17th 2013, 5:36:00 pm", - "[2010, 1, 14, 15, 25, 50, 125] + {ms:200,s:10,m:10,h:2,d:3,M:2,y:3} = April 17th 2013, 5:36:00 pm" - ); - equal( - _date([2010, 0, 31]).add({M:1}).format("MMMM Do YYYY"), - "February 28th 2010", - "[2010, 0, 31] + {M:1} = February 28th 2010" - ); - equal( - _date([2007, 1, 28]).subtract({M:1}).format("MMMM Do YYYY"), - "January 28th 2007", - "[2007, 1, 28] - {M:1} = January 28th 2010" - ); -}); - - -module("from"); - - -test("from", 30, function() { - var start = _date([2007, 1, 28]); - equal(start.from(_date([2007, 1, 28]).add({s:44}), true), "seconds", "44 seconds = seconds"); - equal(start.from(_date([2007, 1, 28]).add({s:45}), true), "a minute", "45 seconds = a minute"); - equal(start.from(_date([2007, 1, 28]).add({s:89}), true), "a minute", "89 seconds = a minute"); - equal(start.from(_date([2007, 1, 28]).add({s:90}), true), "2 minutes", "90 seconds = 2 minutes"); - equal(start.from(_date([2007, 1, 28]).add({m:44}), true), "44 minutes", "44 minutes = 44 minutes"); - equal(start.from(_date([2007, 1, 28]).add({m:45}), true), "an hour", "45 minutes = an hour"); - equal(start.from(_date([2007, 1, 28]).add({m:89}), true), "an hour", "89 minutes = an hour"); - equal(start.from(_date([2007, 1, 28]).add({m:90}), true), "2 hours", "90 minutes = 2 hours"); - equal(start.from(_date([2007, 1, 28]).add({h:5}), true), "5 hours", "5 hours = 5 hours"); - equal(start.from(_date([2007, 1, 28]).add({h:21}), true), "21 hours", "21 hours = 21 hours"); - equal(start.from(_date([2007, 1, 28]).add({h:22}), true), "a day", "22 hours = a day"); - equal(start.from(_date([2007, 1, 28]).add({h:35}), true), "a day", "35 hours = a day"); - equal(start.from(_date([2007, 1, 28]).add({h:36}), true), "2 days", "36 hours = 2 days"); - equal(start.from(_date([2007, 1, 28]).add({d:1}), true), "a day", "1 day = a day"); - equal(start.from(_date([2007, 1, 28]).add({d:5}), true), "5 days", "5 days = 5 days"); - equal(start.from(_date([2007, 1, 28]).add({d:25}), true), "25 days", "25 days = 25 days"); - equal(start.from(_date([2007, 1, 28]).add({d:26}), true), "a month", "26 days = a month"); - equal(start.from(_date([2007, 1, 28]).add({d:30}), true), "a month", "30 days = a month"); - equal(start.from(_date([2007, 1, 28]).add({d:45}), true), "a month", "45 days = a month"); - equal(start.from(_date([2007, 1, 28]).add({d:46}), true), "2 months", "46 days = 2 months"); - equal(start.from(_date([2007, 1, 28]).add({d:75}), true), "2 months", "75 days = 2 months"); - equal(start.from(_date([2007, 1, 28]).add({d:76}), true), "3 months", "76 days = 3 months"); - equal(start.from(_date([2007, 1, 28]).add({M:1}), true), "a month", "1 month = a month"); - equal(start.from(_date([2007, 1, 28]).add({M:5}), true), "5 months", "5 months = 5 months"); - equal(start.from(_date([2007, 1, 28]).add({d:344}), true), "11 months", "344 days = 11 months"); - equal(start.from(_date([2007, 1, 28]).add({d:345}), true), "a year", "345 days = a year"); - equal(start.from(_date([2007, 1, 28]).add({d:547}), true), "a year", "547 days = a year"); - equal(start.from(_date([2007, 1, 28]).add({d:548}), true), "2 years", "548 days = 2 years"); - equal(start.from(_date([2007, 1, 28]).add({y:1}), true), "a year", "1 year = a year"); - equal(start.from(_date([2007, 1, 28]).add({y:5}), true), "5 years", "5 years = 5 years"); -}); - +test("add and subtract", 11, function() { + var a = _date(); + a.year(2011); + a.month(9); + a.date(12); + a.hours(6); + a.minutes(7); + a.seconds(8); -test("suffix", 2, function() { - equal(_date(30000).from(0), "in seconds", "prefix"); - equal(_date(0).from(30000), "seconds ago", "suffix"); -}); + equal(a.add({ms:1000}).seconds(), 9, 'Add milliseconds'); + equal(a.add({s:1}).seconds(), 10, 'Add seconds'); + equal(a.add({m:1}).minutes(), 8, 'Add minutes'); + equal(a.add({h:1}).hours(), 7, 'Add hours'); + equal(a.add({d:1}).date(), 13, 'Add date'); + equal(a.add({M:1}).month(), 10, 'Add month'); + equal(a.add({y:1}).year(), 2012, 'Add year'); + var b = _date([2010, 0, 31]).add({M:1}); + var c = _date([2010, 1, 28]).subtract({M:1}); -test("fromNow", 2, function() { - equal(_date().add({s:30}).fromNow(), "in seconds", "in seconds"); - equal(_date().add({d:5}).fromNow(), "in 5 days", "in 5 days"); + equal(b.month(), 1, 'add month, jan 31st to feb 28th'); + equal(b.date(), 28, 'add month, jan 31st to feb 28th'); + equal(c.month(), 0, 'subtract month, feb 28th to jan 28th'); + equal(c.date(), 28, 'subtract month, feb 28th to jan 28th'); }); @@ -203,6 +132,7 @@ test("diff", 5, function() { equal(_date(oneHourDate).diff(nowDate), 60 * 60 * 1000, "1 hour from now = 360000"); }); + module("leap year"); @@ -228,7 +158,6 @@ test("getters", 7, function() { equal(a.seconds(), 8, 'second'); }); - test("setters", 7, function() { var a = _date(); a.year(2011); @@ -263,6 +192,7 @@ test("chaining setters", 7, function() { equal(a.seconds(), 8, 'second'); }); + module("underscore mixin"); @@ -275,269 +205,3 @@ test("underscore mixin", 6, function() { deepEqual(_.date(new Date(2010, 1, 14, 15, 25, 50, 125)), _.date([2010, 1, 14, 15, 25, 50, 125]), "constructing with array === constructing with new Date()"); }); - -module("custom"); - -test("format", 9, function() { - var dateTest = _date(new Date(2010, 1, 14, 15, 25, 50, 125)), - _months = _date.months, - _monthsShort = _date.monthsShort, - _weekdays = _date.weekdays, - _weekdaysShort = _date.weekdaysShort, - _ordinal = _date.ordinal, - a = [ - ['dddd, MMMM Do YYYY, h:mm:ss a', 'domingo, febrero 14o 2010, 3:25:50 pm'], - ['ddd, hA', 'dom, 3PM'], - ['M Mo MM MMMM MMM', '2 2o 02 febrero feb'], - ['YYYY YY', '2010 10'], - ['D Do DD', '14 14o 14'], - ['d do dddd ddd', '0 0o domingo dom'], - ['DDD DDDo DDDD', '45 45o 045'], - ['w wo ww', '8 8o 08'], - ['t\\he DDDo \\d\\ay of t\\he ye\\ar', 'the 45o day of the year'] - ], - b = _date(new Date(2010, 1, 14, 15, 25, 50, 125)), - i; - - _date.months = ["enero", "febrero", "marzo", "abril", "mayo", "junio", "julio", "agosto", "septiembre", "octubre", "noviembre", "diciembre"]; - _date.monthsShort = ["ene", "feb", "mar", "abr", "may", "jun", "jul", "ago", "sep", "oct", "nov", "dic"]; - _date.weekdays = ["domingo", "lunes", "martes", "miércoles", "jueves", "viernes", "sábado"]; - _date.weekdaysShort = ["dom", "lun", "mar", "mié", "jue", "vie", "sáb"]; - _date.ordinal = function() { - return 'o'; - }; - - for (i = 0; i < a.length; i++) { - equal(b.format(a[i][0]), a[i][1], a[i][0] + ' ---> ' + a[i][1]); - } - - _date.months = _months; - _date.monthsShort = _monthsShort; - _date.weekdays = _weekdays; - _date.weekdaysShort = _weekdaysShort; - _date.ordinal = _ordinal; -}); - - -test("from date parts", 11, function() { - var backup = _date.relativeTime, - start = _date([2007, 1, 28]); - - _date.relativeTime = { - future: "%s testing a", - past: "%s testing b", - s: "seconds!", - m: "a minute!", - mm: "%d minutes!", - h: "an hour!", - hh: "%d hours!", - d: "a day!", - dd: "%d days!", - M: "a month!", - MM: "%d months!", - y: "a year!", - yy: "%d years!" - }; - - equal(start.from(_date([2007, 1, 28]).add({s:30}), true), "seconds!", "seconds"); - equal(start.from(_date([2007, 1, 28]).add({s:60}), true), "a minute!", "minute"); - equal(start.from(_date([2007, 1, 28]).add({m:5}), true), "5 minutes!", "minutes"); - equal(start.from(_date([2007, 1, 28]).add({h:1}), true), "an hour!", "hour"); - equal(start.from(_date([2007, 1, 28]).add({h:5}), true), "5 hours!", "hours"); - equal(start.from(_date([2007, 1, 28]).add({d:1}), true), "a day!", "day"); - equal(start.from(_date([2007, 1, 28]).add({d:5}), true), "5 days!", "days"); - equal(start.from(_date([2007, 1, 28]).add({M:1}), true), "a month!", "month"); - equal(start.from(_date([2007, 1, 28]).add({M:5}), true), "5 months!", "months"); - equal(start.from(_date([2007, 1, 28]).add({y:1}), true), "a year!", "year"); - equal(start.from(_date([2007, 1, 28]).add({y:5}), true), "5 years!", "years"); - - _date.relativeTime = backup; -}); - -test("from future past", 2, function() { - var backup = _date.relativeTime; - - _date.relativeTime = { - future: "%s testing a", - past: "%s testing b", - s: "seconds", - m: "a minute", - mm: "%d minutes", - h: "an hour", - hh: "%d hours", - d: "a day", - dd: "%d days", - M: "a month", - MM: "%d months", - y: "a year", - yy: "%d years" - }; - - equal(_date(30000).from(0), "seconds testing a", 'future'); - equal(_date(0).from(30000), "seconds testing b", 'past'); - - _date.relativeTime = backup; -}); - - -module('lang'); - -test("format", 48, function() { - var date = _date(new Date(2010, 1, 14, 15, 25, 50, 125)), - en = [ - ['dddd, MMMM Do YYYY, h:mm:ss a', 'Sunday, February 14th 2010, 3:25:50 pm'], - ['ddd, hA', 'Sun, 3PM'], - ['M Mo MM MMMM MMM', '2 2nd 02 February Feb'], - ['D Do DD', '14 14th 14'], - ['d do dddd ddd', '0 0th Sunday Sun'], - ['DDD DDDo DDDD', '45 45th 045'], - ['w wo ww', '8 8th 08'], - ['t\\he DDDo \\d\\ay of t\\he ye\\ar', 'the 45th day of the year'] - ], - es = [ - ['dddd, MMMM Do YYYY, h:mm:ss a', 'domingo, febrero 14o 2010, 3:25:50 pm'], - ['ddd, hA', 'dom, 3PM'], - ['M Mo MM MMMM MMM', '2 2o 02 febrero feb'], - ['D Do DD', '14 14o 14'], - ['d do dddd ddd', '0 0o domingo dom'], - ['DDD DDDo DDDD', '45 45o 045'], - ['w wo ww', '8 8o 08'], - ['t\\he DDDo \\d\\ay of t\\he ye\\ar', 'the 45o day of the year'] - ], - esFrom = [ - [{s:-30}, false, "seconds! testing a", "future"], - [{s:30}, false, "seconds! testing b", "past"], - [{s:30}, true, "seconds!", "seconds"], - [{s:60}, true, "a minute!", "minute"], - [{m:5}, true, "5 minutes!", "minutes"], - [{h:1}, true, "an hour!", "hour"], - [{h:5}, true, "5 hours!", "hours"], - [{d:1}, true, "a day!", "day"], - [{d:5}, true, "5 days!", "days"], - [{M:1}, true, "a month!", "month"], - [{M:5}, true, "5 months!", "months"], - [{y:1}, true, "a year!", "year"], - [{y:5}, true, "5 years!", "years"] - ], - enFrom = [ - [{s:-30}, false, "in seconds", "future"], - [{s:30}, false, "seconds ago", "past"], - [{s:30}, true, "seconds", "seconds"], - [{s:60}, true, "a minute", "minute"], - [{m:5}, true, "5 minutes", "minutes"], - [{h:1}, true, "an hour", "hour"], - [{h:5}, true, "5 hours", "hours"], - [{d:1}, true, "a day", "day"], - [{d:5}, true, "5 days", "days"], - [{M:1}, true, "a month", "month"], - [{M:5}, true, "5 months", "months"], - [{y:1}, true, "a year", "year"], - [{y:5}, true, "5 years", "years"] - ], - i; - - function testLang(formatArray, fromArray) { - var start = [2007, 1, 28]; - for (i = 0; i < formatArray.length; i++) { - equal(date.format(formatArray[i][0]), formatArray[i][1], formatArray[i][0] + ' ---> ' + formatArray[i][1]); - } - - for (i = 0; i < formatArray.length; i++) { - equal(_date(start).from(_date(start).add(fromArray[i][0]), fromArray[i][1]), fromArray[i][2], fromArray[i][3]); - } - } - - // switch to es - _date.lang('es', { - months : ["enero", "febrero", "marzo", "abril", "mayo", "junio", "julio", "agosto", "septiembre", "octubre", "noviembre", "diciembre"], - monthsShort : ["ene", "feb", "mar", "abr", "may", "jun", "jul", "ago", "sep", "oct", "nov", "dic"], - weekdays : ["domingo", "lunes", "martes", "miércoles", "jueves", "viernes", "sábado"], - weekdaysShort : ["dom", "lun", "mar", "mié", "jue", "vie", "sáb"], - ordinal : function() { - return 'o'; - }, - relativeTime : { - future: "%s testing a", - past: "%s testing b", - s: "seconds!", - m: "a minute!", - mm: "%d minutes!", - h: "an hour!", - hh: "%d hours!", - d: "a day!", - dd: "%d days!", - M: "a month!", - MM: "%d months!", - y: "a year!", - yy: "%d years!" - } - }); - - - // test es - testLang(es, esFrom); - - // switch to en - _date.lang('en'); - - // test en - testLang(en, enFrom); - - // switch to es - _date.lang('es'); - - // test es - testLang(es, esFrom); - - // switch back to en to prevent other tests from failing - _date.lang('en'); -}); - -test("loaded module", 16, function() { - var date = _date(new Date(2010, 1, 14, 15, 25, 50, 125)), - test = [ - ['dddd, MMMM Do YYYY, h:mm:ss a', 'a.test, 2.test 14ordinal 2010, 3:25:50 pm'], - ['ddd, hA', 'a.t, 3PM'], - ['M Mo MM MMMM MMM', '2 2ordinal 02 2.test 2.t'], - ['D Do DD', '14 14ordinal 14'], - ['d do dddd ddd', '0 0ordinal a.test a.t'], - ['DDD DDDo DDDD', '45 45ordinal 045'], - ['w wo ww', '8 8ordinal 08'], - ['t\\he DDDo \\d\\ay of t\\he ye\\ar', 'the 45ordinal day of the year'] - ], - testFrom = [ - [{s:-30}, false, "s future", "future"], - [{s:30}, false, "s past", "past"], - [{s:30}, true, "s", "seconds"], - [{s:60}, true, "m", "minute"], - [{m:5}, true, "5 mm", "minutes"], - [{h:1}, true, "h", "hour"], - [{h:5}, true, "5 hh", "hours"], - [{d:1}, true, "d", "day"], - [{d:5}, true, "5 dd", "days"], - [{M:1}, true, "M", "month"], - [{M:5}, true, "5 MM", "months"], - [{y:1}, true, "y", "year"], - [{y:5}, true, "5 yy", "years"] - ], - i; - - function testLang(formatArray, fromArray) { - var start = [2007, 1, 28]; - for (i = 0; i < formatArray.length; i++) { - equal(date.format(formatArray[i][0]), formatArray[i][1], formatArray[i][0] + ' ---> ' + formatArray[i][1]); - } - - for (i = 0; i < formatArray.length; i++) { - equal(_date(start).from(_date(start).add(fromArray[i][0]), fromArray[i][1]), fromArray[i][2], fromArray[i][3]); - } - } - - _date.lang('test'); - - // test 'test' - testLang(test, testFrom); - - // switch back to en to prevent other tests from failing - _date.lang('en'); -}); \ No newline at end of file diff --git a/test/index.html b/test/index.html index 69e725756..f574f2d62 100755 --- a/test/index.html +++ b/test/index.html @@ -6,14 +6,11 @@ - - - + + +
diff --git a/test/lang.js b/test/lang.js new file mode 100644 index 000000000..9389c55bd --- /dev/null +++ b/test/lang.js @@ -0,0 +1,135 @@ +var _date;if (typeof window === 'undefined') {_date = require('../underscore.date.js');module = QUnit.module;} +/************************************************** + English + *************************************************/ + +module("lang:en"); + +test("format", 15, function() { + _date.lang('en'); + var a = [ + ['dddd, MMMM Do YYYY, h:mm:ss a', 'Sunday, February 14th 2010, 3:25:50 pm'], + ['ddd, hA', 'Sun, 3PM'], + ['M Mo MM MMMM MMM', '2 2nd 02 February Feb'], + ['YYYY YY', '2010 10'], + ['D Do DD', '14 14th 14'], + ['d do dddd ddd', '0 0th Sunday Sun'], + ['DDD DDDo DDDD', '45 45th 045'], + ['w wo ww', '8 8th 08'], + ['h hh', '3 03'], + ['H HH', '15 15'], + ['m mm', '25 25'], + ['s ss', '50 50'], + ['a A', 'pm PM'], + ['z zz', 'PST PST'], + ['t\\he DDDo \\d\\ay of t\\he ye\\ar', 'the 45th day of the year'] + ], + b = _date(new Date(2010, 1, 14, 15, 25, 50, 125)), + i; + for (i = 0; i < a.length; i++) { + equal(b.format(a[i][0]), a[i][1], a[i][0] + ' ---> ' + a[i][1]); + } +}); + +test("format ordinal", 31, function() { + _date.lang('en'); + equal(_date([2011, 0, 1]).format('DDDo'), '1st', '1st'); + equal(_date([2011, 0, 2]).format('DDDo'), '2nd', '2nd'); + equal(_date([2011, 0, 3]).format('DDDo'), '3rd', '3rd'); + equal(_date([2011, 0, 4]).format('DDDo'), '4th', '4th'); + equal(_date([2011, 0, 5]).format('DDDo'), '5th', '5th'); + equal(_date([2011, 0, 6]).format('DDDo'), '6th', '6th'); + equal(_date([2011, 0, 7]).format('DDDo'), '7th', '7th'); + equal(_date([2011, 0, 8]).format('DDDo'), '8th', '8th'); + equal(_date([2011, 0, 9]).format('DDDo'), '9th', '9th'); + equal(_date([2011, 0, 10]).format('DDDo'), '10th', '10th'); + + equal(_date([2011, 0, 11]).format('DDDo'), '11th', '11th'); + equal(_date([2011, 0, 12]).format('DDDo'), '12th', '12th'); + equal(_date([2011, 0, 13]).format('DDDo'), '13th', '13th'); + equal(_date([2011, 0, 14]).format('DDDo'), '14th', '14th'); + equal(_date([2011, 0, 15]).format('DDDo'), '15th', '15th'); + equal(_date([2011, 0, 16]).format('DDDo'), '16th', '16th'); + equal(_date([2011, 0, 17]).format('DDDo'), '17th', '17th'); + equal(_date([2011, 0, 18]).format('DDDo'), '18th', '18th'); + equal(_date([2011, 0, 19]).format('DDDo'), '19th', '19th'); + equal(_date([2011, 0, 20]).format('DDDo'), '20th', '20th'); + + equal(_date([2011, 0, 21]).format('DDDo'), '21st', '21st'); + equal(_date([2011, 0, 22]).format('DDDo'), '22nd', '22nd'); + equal(_date([2011, 0, 23]).format('DDDo'), '23rd', '23rd'); + equal(_date([2011, 0, 24]).format('DDDo'), '24th', '24th'); + equal(_date([2011, 0, 25]).format('DDDo'), '25th', '25th'); + equal(_date([2011, 0, 26]).format('DDDo'), '26th', '26th'); + equal(_date([2011, 0, 27]).format('DDDo'), '27th', '27th'); + equal(_date([2011, 0, 28]).format('DDDo'), '28th', '28th'); + equal(_date([2011, 0, 29]).format('DDDo'), '29th', '29th'); + equal(_date([2011, 0, 30]).format('DDDo'), '30th', '30th'); + + equal(_date([2011, 0, 31]).format('DDDo'), '31st', '31st'); +}); + +test("format month", 12, function() { + _date.lang('en'); + var expected = 'January Jan_February Feb_March Mar_April Apr_May May_June Jun_July Jul_August Aug_September Sep_October Oct_November Nov_December Dec'.split("_"); + var i; + for (i = 0; i < expected.length; i++) { + equal(_date([2011, i, 0]).format('MMMM MMM'), expected[i], expected[i]); + } +}); + +test("format week", 7, function() { + _date.lang('en'); + var expected = 'Sunday Sun_Monday Mon_Tuesday Tue_Wednesday Wed_Thursday Thu_Friday Fri_Saturday Sat'.split("_"); + var i; + for (i = 0; i < expected.length; i++) { + equal(_date([2011, 0, 2 + i]).format('dddd ddd'), expected[i], expected[i]); + } +}); + +test("from", 30, function() { + _date.lang('en'); + var start = _date([2007, 1, 28]); + equal(start.from(_date([2007, 1, 28]).add({s:44}), true), "seconds", "44 seconds = seconds"); + equal(start.from(_date([2007, 1, 28]).add({s:45}), true), "a minute", "45 seconds = a minute"); + equal(start.from(_date([2007, 1, 28]).add({s:89}), true), "a minute", "89 seconds = a minute"); + equal(start.from(_date([2007, 1, 28]).add({s:90}), true), "2 minutes", "90 seconds = 2 minutes"); + equal(start.from(_date([2007, 1, 28]).add({m:44}), true), "44 minutes", "44 minutes = 44 minutes"); + equal(start.from(_date([2007, 1, 28]).add({m:45}), true), "an hour", "45 minutes = an hour"); + equal(start.from(_date([2007, 1, 28]).add({m:89}), true), "an hour", "89 minutes = an hour"); + equal(start.from(_date([2007, 1, 28]).add({m:90}), true), "2 hours", "90 minutes = 2 hours"); + equal(start.from(_date([2007, 1, 28]).add({h:5}), true), "5 hours", "5 hours = 5 hours"); + equal(start.from(_date([2007, 1, 28]).add({h:21}), true), "21 hours", "21 hours = 21 hours"); + equal(start.from(_date([2007, 1, 28]).add({h:22}), true), "a day", "22 hours = a day"); + equal(start.from(_date([2007, 1, 28]).add({h:35}), true), "a day", "35 hours = a day"); + equal(start.from(_date([2007, 1, 28]).add({h:36}), true), "2 days", "36 hours = 2 days"); + equal(start.from(_date([2007, 1, 28]).add({d:1}), true), "a day", "1 day = a day"); + equal(start.from(_date([2007, 1, 28]).add({d:5}), true), "5 days", "5 days = 5 days"); + equal(start.from(_date([2007, 1, 28]).add({d:25}), true), "25 days", "25 days = 25 days"); + equal(start.from(_date([2007, 1, 28]).add({d:26}), true), "a month", "26 days = a month"); + equal(start.from(_date([2007, 1, 28]).add({d:30}), true), "a month", "30 days = a month"); + equal(start.from(_date([2007, 1, 28]).add({d:45}), true), "a month", "45 days = a month"); + equal(start.from(_date([2007, 1, 28]).add({d:46}), true), "2 months", "46 days = 2 months"); + equal(start.from(_date([2007, 1, 28]).add({d:75}), true), "2 months", "75 days = 2 months"); + equal(start.from(_date([2007, 1, 28]).add({d:76}), true), "3 months", "76 days = 3 months"); + equal(start.from(_date([2007, 1, 28]).add({M:1}), true), "a month", "1 month = a month"); + equal(start.from(_date([2007, 1, 28]).add({M:5}), true), "5 months", "5 months = 5 months"); + equal(start.from(_date([2007, 1, 28]).add({d:344}), true), "11 months", "344 days = 11 months"); + equal(start.from(_date([2007, 1, 28]).add({d:345}), true), "a year", "345 days = a year"); + equal(start.from(_date([2007, 1, 28]).add({d:547}), true), "a year", "547 days = a year"); + equal(start.from(_date([2007, 1, 28]).add({d:548}), true), "2 years", "548 days = 2 years"); + equal(start.from(_date([2007, 1, 28]).add({y:1}), true), "a year", "1 year = a year"); + equal(start.from(_date([2007, 1, 28]).add({y:5}), true), "5 years", "5 years = 5 years"); +}); + +test("suffix", 2, function() { + _date.lang('en'); + equal(_date(30000).from(0), "in seconds", "prefix"); + equal(_date(0).from(30000), "seconds ago", "suffix"); +}); + +test("fromNow", 2, function() { + _date.lang('en'); + equal(_date().add({s:30}).fromNow(), "in seconds", "in seconds"); + equal(_date().add({d:5}).fromNow(), "in 5 days", "in 5 days"); +}); diff --git a/test/lang/en.js b/test/lang/en.js new file mode 100644 index 000000000..4889cc85e --- /dev/null +++ b/test/lang/en.js @@ -0,0 +1,135 @@ + +/************************************************** + English + *************************************************/ + +module("lang:en"); + +test("format", 15, function() { + _date.lang('en'); + var a = [ + ['dddd, MMMM Do YYYY, h:mm:ss a', 'Sunday, February 14th 2010, 3:25:50 pm'], + ['ddd, hA', 'Sun, 3PM'], + ['M Mo MM MMMM MMM', '2 2nd 02 February Feb'], + ['YYYY YY', '2010 10'], + ['D Do DD', '14 14th 14'], + ['d do dddd ddd', '0 0th Sunday Sun'], + ['DDD DDDo DDDD', '45 45th 045'], + ['w wo ww', '8 8th 08'], + ['h hh', '3 03'], + ['H HH', '15 15'], + ['m mm', '25 25'], + ['s ss', '50 50'], + ['a A', 'pm PM'], + ['z zz', 'PST PST'], + ['t\\he DDDo \\d\\ay of t\\he ye\\ar', 'the 45th day of the year'] + ], + b = _date(new Date(2010, 1, 14, 15, 25, 50, 125)), + i; + for (i = 0; i < a.length; i++) { + equal(b.format(a[i][0]), a[i][1], a[i][0] + ' ---> ' + a[i][1]); + } +}); + +test("format ordinal", 31, function() { + _date.lang('en'); + equal(_date([2011, 0, 1]).format('DDDo'), '1st', '1st'); + equal(_date([2011, 0, 2]).format('DDDo'), '2nd', '2nd'); + equal(_date([2011, 0, 3]).format('DDDo'), '3rd', '3rd'); + equal(_date([2011, 0, 4]).format('DDDo'), '4th', '4th'); + equal(_date([2011, 0, 5]).format('DDDo'), '5th', '5th'); + equal(_date([2011, 0, 6]).format('DDDo'), '6th', '6th'); + equal(_date([2011, 0, 7]).format('DDDo'), '7th', '7th'); + equal(_date([2011, 0, 8]).format('DDDo'), '8th', '8th'); + equal(_date([2011, 0, 9]).format('DDDo'), '9th', '9th'); + equal(_date([2011, 0, 10]).format('DDDo'), '10th', '10th'); + + equal(_date([2011, 0, 11]).format('DDDo'), '11th', '11th'); + equal(_date([2011, 0, 12]).format('DDDo'), '12th', '12th'); + equal(_date([2011, 0, 13]).format('DDDo'), '13th', '13th'); + equal(_date([2011, 0, 14]).format('DDDo'), '14th', '14th'); + equal(_date([2011, 0, 15]).format('DDDo'), '15th', '15th'); + equal(_date([2011, 0, 16]).format('DDDo'), '16th', '16th'); + equal(_date([2011, 0, 17]).format('DDDo'), '17th', '17th'); + equal(_date([2011, 0, 18]).format('DDDo'), '18th', '18th'); + equal(_date([2011, 0, 19]).format('DDDo'), '19th', '19th'); + equal(_date([2011, 0, 20]).format('DDDo'), '20th', '20th'); + + equal(_date([2011, 0, 21]).format('DDDo'), '21st', '21st'); + equal(_date([2011, 0, 22]).format('DDDo'), '22nd', '22nd'); + equal(_date([2011, 0, 23]).format('DDDo'), '23rd', '23rd'); + equal(_date([2011, 0, 24]).format('DDDo'), '24th', '24th'); + equal(_date([2011, 0, 25]).format('DDDo'), '25th', '25th'); + equal(_date([2011, 0, 26]).format('DDDo'), '26th', '26th'); + equal(_date([2011, 0, 27]).format('DDDo'), '27th', '27th'); + equal(_date([2011, 0, 28]).format('DDDo'), '28th', '28th'); + equal(_date([2011, 0, 29]).format('DDDo'), '29th', '29th'); + equal(_date([2011, 0, 30]).format('DDDo'), '30th', '30th'); + + equal(_date([2011, 0, 31]).format('DDDo'), '31st', '31st'); +}); + +test("format month", 12, function() { + _date.lang('en'); + var expected = 'January Jan_February Feb_March Mar_April Apr_May May_June Jun_July Jul_August Aug_September Sep_October Oct_November Nov_December Dec'.split("_"); + var i; + for (i = 0; i < expected.length; i++) { + equal(_date([2011, i, 0]).format('MMMM MMM'), expected[i], expected[i]); + } +}); + +test("format week", 7, function() { + _date.lang('en'); + var expected = 'Sunday Sun_Monday Mon_Tuesday Tue_Wednesday Wed_Thursday Thu_Friday Fri_Saturday Sat'.split("_"); + var i; + for (i = 0; i < expected.length; i++) { + equal(_date([2011, 0, 2 + i]).format('dddd ddd'), expected[i], expected[i]); + } +}); + +test("from", 30, function() { + _date.lang('en'); + var start = _date([2007, 1, 28]); + equal(start.from(_date([2007, 1, 28]).add({s:44}), true), "seconds", "44 seconds = seconds"); + equal(start.from(_date([2007, 1, 28]).add({s:45}), true), "a minute", "45 seconds = a minute"); + equal(start.from(_date([2007, 1, 28]).add({s:89}), true), "a minute", "89 seconds = a minute"); + equal(start.from(_date([2007, 1, 28]).add({s:90}), true), "2 minutes", "90 seconds = 2 minutes"); + equal(start.from(_date([2007, 1, 28]).add({m:44}), true), "44 minutes", "44 minutes = 44 minutes"); + equal(start.from(_date([2007, 1, 28]).add({m:45}), true), "an hour", "45 minutes = an hour"); + equal(start.from(_date([2007, 1, 28]).add({m:89}), true), "an hour", "89 minutes = an hour"); + equal(start.from(_date([2007, 1, 28]).add({m:90}), true), "2 hours", "90 minutes = 2 hours"); + equal(start.from(_date([2007, 1, 28]).add({h:5}), true), "5 hours", "5 hours = 5 hours"); + equal(start.from(_date([2007, 1, 28]).add({h:21}), true), "21 hours", "21 hours = 21 hours"); + equal(start.from(_date([2007, 1, 28]).add({h:22}), true), "a day", "22 hours = a day"); + equal(start.from(_date([2007, 1, 28]).add({h:35}), true), "a day", "35 hours = a day"); + equal(start.from(_date([2007, 1, 28]).add({h:36}), true), "2 days", "36 hours = 2 days"); + equal(start.from(_date([2007, 1, 28]).add({d:1}), true), "a day", "1 day = a day"); + equal(start.from(_date([2007, 1, 28]).add({d:5}), true), "5 days", "5 days = 5 days"); + equal(start.from(_date([2007, 1, 28]).add({d:25}), true), "25 days", "25 days = 25 days"); + equal(start.from(_date([2007, 1, 28]).add({d:26}), true), "a month", "26 days = a month"); + equal(start.from(_date([2007, 1, 28]).add({d:30}), true), "a month", "30 days = a month"); + equal(start.from(_date([2007, 1, 28]).add({d:45}), true), "a month", "45 days = a month"); + equal(start.from(_date([2007, 1, 28]).add({d:46}), true), "2 months", "46 days = 2 months"); + equal(start.from(_date([2007, 1, 28]).add({d:75}), true), "2 months", "75 days = 2 months"); + equal(start.from(_date([2007, 1, 28]).add({d:76}), true), "3 months", "76 days = 3 months"); + equal(start.from(_date([2007, 1, 28]).add({M:1}), true), "a month", "1 month = a month"); + equal(start.from(_date([2007, 1, 28]).add({M:5}), true), "5 months", "5 months = 5 months"); + equal(start.from(_date([2007, 1, 28]).add({d:344}), true), "11 months", "344 days = 11 months"); + equal(start.from(_date([2007, 1, 28]).add({d:345}), true), "a year", "345 days = a year"); + equal(start.from(_date([2007, 1, 28]).add({d:547}), true), "a year", "547 days = a year"); + equal(start.from(_date([2007, 1, 28]).add({d:548}), true), "2 years", "548 days = 2 years"); + equal(start.from(_date([2007, 1, 28]).add({y:1}), true), "a year", "1 year = a year"); + equal(start.from(_date([2007, 1, 28]).add({y:5}), true), "5 years", "5 years = 5 years"); +}); + +test("suffix", 2, function() { + _date.lang('en'); + equal(_date(30000).from(0), "in seconds", "prefix"); + equal(_date(0).from(30000), "seconds ago", "suffix"); +}); + +test("fromNow", 2, function() { + _date.lang('en'); + equal(_date().add({s:30}).fromNow(), "in seconds", "in seconds"); + equal(_date().add({d:5}).fromNow(), "in 5 days", "in 5 days"); +});