From c76af16680bec8271ccaf6ce3794881dcef515df Mon Sep 17 00:00:00 2001 From: Tim Wood Date: Tue, 16 Aug 2011 17:00:31 -0700 Subject: [PATCH] Adapting test suite to work in node as well as the browser --- test.js | 13 ++ test/customize.html | 17 -- test/customizeDate.js | 86 -------- test/date.js | 441 +++++++++++++++++++++++++++++------------- test/speed.js | 51 +++-- test/test.html | 38 ++-- 6 files changed, 364 insertions(+), 282 deletions(-) create mode 100755 test.js delete mode 100755 test/customize.html delete mode 100755 test/customizeDate.js diff --git a/test.js b/test.js new file mode 100755 index 000000000..4fc8ed594 --- /dev/null +++ b/test.js @@ -0,0 +1,13 @@ +var testrunner = require('qunit'); + +testrunner.options.coverage = false; + +testrunner.run({ + code: "./underscore.date.js", + tests: "./test/date.js" +}); + +testrunner.run({ + code: "./underscore.date.min.js", + tests: "./test/date.js" +}); \ No newline at end of file diff --git a/test/customize.html b/test/customize.html deleted file mode 100755 index 275dfd62d..000000000 --- a/test/customize.html +++ /dev/null @@ -1,17 +0,0 @@ - - - - Underscore.date customization test suite - - - - - - - -

Underscore.date customization test suite

-

-

-
    - - diff --git a/test/customizeDate.js b/test/customizeDate.js deleted file mode 100755 index 19cfdaf48..000000000 --- a/test/customizeDate.js +++ /dev/null @@ -1,86 +0,0 @@ -$(function() { - - module("Customize"); - - test("_date.format()", 14, function() { - var dateTest = _date(new Date(2010, 1, 14, 15, 25, 50, 125)); - - _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'; - } - - equal(dateTest.format("dddd, MMMM Do YYYY, h:mm:ss a"), "domingo, febrero 14o 2010, 3:25:50 pm"); - equal(dateTest.format("ddd, hA"), "dom, 3PM"); - equal(dateTest.format("M Mo MM MMMM MMM"), "2 2o 02 febrero feb"); - equal(dateTest.format("YYYY YY"), "2010 10"); - equal(dateTest.format("D Do DD"), "14 14o 14"); - equal(dateTest.format("d do dddd ddd"), "0 0o domingo dom"); - equal(dateTest.format("DDD DDDo DDDD"), "45 45o 045"); - equal(dateTest.format("w wo ww"), "8 8o 08"); - equal(dateTest.format("h hh"), "3 03"); - equal(dateTest.format("H HH"), "15 15"); - equal(dateTest.format("m mm"), "25 25"); - equal(dateTest.format("s ss"), "50 50"); - equal(dateTest.format("a A"), "pm PM"); - equal(dateTest.format("t\\he DDDo \\d\\ay of t\\he ye\\ar"), "the 45o day of the year"); - }); - - test("_date().relative()", 11, function() { - var start = _date([2007, 1, 28]); - - _date.relativeTime = { - future: "%s from now", - past: "%s ago", - 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!"); - equal(start.from(_date([2007, 1, 28]).add({s:60}), true), "a minute!"); - equal(start.from(_date([2007, 1, 28]).add({m:5}), true), "5 minutes!"); - equal(start.from(_date([2007, 1, 28]).add({h:1}), true), "an hour!"); - equal(start.from(_date([2007, 1, 28]).add({h:5}), true), "5 hours!"); - equal(start.from(_date([2007, 1, 28]).add({d:1}), true), "a day!"); - equal(start.from(_date([2007, 1, 28]).add({d:5}), true), "5 days!"); - equal(start.from(_date([2007, 1, 28]).add({M:1}), true), "a month!"); - equal(start.from(_date([2007, 1, 28]).add({M:5}), true), "5 months!"); - equal(start.from(_date([2007, 1, 28]).add({y:1}), true), "a year!"); - equal(start.from(_date([2007, 1, 28]).add({y:5}), true), "5 years!"); - }); - - test("_date().fromNow()", 2, function() { - - _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"); - equal(_date(0).from(30000), "seconds testing b"); - }); - -}); diff --git a/test/date.js b/test/date.js index 4c21a6778..cb8f7cddd 100755 --- a/test/date.js +++ b/test/date.js @@ -1,140 +1,319 @@ -$(function() { +/************************************************** + Node + *************************************************/ - module("Date"); +var _date, _; +if (typeof window === 'undefined') { + _date = require('underscore.date'); + module = QUnit.module; + _ = { date : _date }; +} + + +/************************************************** + Tests + *************************************************/ + + +module("create"); + + +test("array", 8, function() { + ok(_date([2010]).date instanceof Date, "[2010]"); + ok(_date([2010, 1]).date instanceof Date, "[2010, 1]"); + ok(_date([2010, 1, 12]).date instanceof Date, "[2010, 1, 12]"); + ok(_date([2010, 1, 12, 1]).date instanceof Date, "[2010, 1, 12, 1]"); + ok(_date([2010, 1, 12, 1, 1]).date instanceof Date, "[2010, 1, 12, 1, 1]"); + ok(_date([2010, 1, 12, 1, 1, 1]).date instanceof Date, "[2010, 1, 12, 1, 1, 1]"); + ok(_date([2010, 1, 12, 1, 1, 1, 1]).date instanceof Date, "[2010, 1, 12, 1, 1, 1, 1]"); + 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()"); +}); + + +test("date", 1, function() { + ok(_date(new Date()).date instanceof Date, "new Date()"); +}); + + +test("_date", 2, function() { + ok(_date(_date()).date instanceof Date, "_date(_date())"); + ok(_date(_date(_date())).date instanceof Date, "_date(_date(_date()))"); +}); + +test("undefined", 1, function() { + ok(_date().date instanceof Date, "undefined"); +}); + + +test("string without format", 2, function() { + ok(_date("Aug 9, 1995").date instanceof Date, "Aug 9, 1995"); + ok(_date("Mon, 25 Dec 1995 13:30:00 GMT").date instanceof Date, "Mon, 25 Dec 1995 13:30:00 GMT"); +}); + + +test("string with format", 11, function() { + var a = [ + ['MM-DD-YYYY', '12-02-1999'], + ['DD-MM-YYYY', '12-02-1999'], + ['DD/MM/YYYY', '12/02/1999'], + ['DD_MM_YYYY', '12_02_1999'], + ['DD:MM:YYYY', '12:02:1999'], + ['D-M-YY', '2-2-99'], + ['YY', '99'], + ['DDD-YYYY', '300-1999'], + ['DD-MM-YYYY h:m:s', '12-02-1999 2:45:10'], + ['DD-MM-YYYY h:m:s a', '12-02-1999 2:45:10 am'], + ['DD-MM-YYYY h:m:s a', '12-02-1999 2:45:10 pm'] + ], + i; + for (i = 0; i < a.length; i++) { + equal(_date(a[i][1], a[i][0]).format(a[i][0]), a[i][1], a[i][0] + ' ---> ' + a[i][1]); + } +}); + +test("string with format - years", 2, function() { + equal(_date('71', 'YY').format('YYYY'), '1971', '71 > 1971'); + equal(_date('69', 'YY').format('YYYY'), '2069', '69 > 2069'); +}); + + +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 Pacific Standard Time'], + ['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("milliseconds", 5, function() { + equal(_date(1000).from(0, true, true), 1000, "1 second - 0 = 1000"); + equal(_date(1000).from(500, false, true), 500, "1 second - .5 second = -500"); + equal(_date(0).from(1000, false, true), -1000, "0 - 1 second = -1000"); + equal(_date(new Date(1000)).from(1000, false, true), 0, "1 second - 1 second = 0"); + var oneHourDate = new Date(), + nowDate = new Date(); + oneHourDate.setHours(oneHourDate.getHours() + 1); + equal(_date(oneHourDate).from(nowDate, false, true), 60 * 60 * 1000, "1 hour from now = 360000"); +}); + + +test("suffix", 2, function() { + equal(_date(30000).from(0), "in seconds", "prefix"); + equal(_date(0).from(30000), "seconds ago", "suffix"); +}); + + +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"); +}); + + +module("leap year"); + + +test("leap year", function() { + expect(4); + equal(_date([2010, 0, 1]).isLeapYear(), false, '2010'); + equal(_date([2100, 0, 1]).isLeapYear(), false, '2100'); + equal(_date([2008, 0, 1]).isLeapYear(), true, '2008'); + equal(_date([2000, 0, 1]).isLeapYear(), true, '2000'); +}); + + +module("underscore mixin"); + + +test("underscore mixin", 6, function() { + ok(_.date([2010, 1, 12]).date instanceof Date, "[2010, 1, 12]"); + ok(_.date([2010, 1, 12, 1]).date instanceof Date, "[2010, 1, 12, 1]"); + ok(_.date().date instanceof Date, "undefined"); + ok(_.date("Aug 9, 1995").date instanceof Date, "Aug 9, 1995"); + ok(_.date("Mon, 25 Dec 1995 13:30:00 GMT").date instanceof Date, "Mon, 25 Dec 1995 13:30:00 GMT"); + 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'; + } - test("_date()", 6, function() { - ok(_date([2010, 1, 12]).date instanceof Date, "[2010, 1, 12]"); - ok(_date([2010, 1, 12, 1]).date instanceof Date, "[2010, 1, 12, 1]"); - ok(_date().date instanceof Date, "undefined"); - ok(_date("Aug 9, 1995").date instanceof Date, "Aug 9, 1995"); - ok(_date("Mon, 25 Dec 1995 13:30:00 GMT").date instanceof Date, "Mon, 25 Dec 1995 13:30:00 GMT"); - 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()"); - }); + for (i = 0; i < a.length; i++) { + equal(b.format(a[i][0]), a[i][1], a[i][0] + ' ---> ' + a[i][1]); + } - var stringTests = [ - ['MM-DD-YYYY', '12-02-1999'], - ['DD-MM-YYYY', '12-02-1999'], - ['DD/MM/YYYY', '12/02/1999'], - ['DD_MM_YYYY', '12_02_1999'], - ['DD:MM:YYYY', '12:02:1999'], - ['D-M-YY', '2-2-99'], - ['YY', '99'], - ['DDD-YYYY', '300-1999'], - ['DD-MM-YYYY h:m:s', '12-02-1999 2:45:10'], - ['DD-MM-YYYY h:m:s a', '12-02-1999 2:45:10 am'], - ['DD-MM-YYYY h:m:s a', '12-02-1999 2:45:10 pm'] - ]; + _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]); - test("_date() with string format", stringTests.length, function() { - for (var i = 0; i < stringTests.length; i++) { - equal(_date(stringTests[i][1], stringTests[i][0]).format(stringTests[i][0]), stringTests[i][1]); - } - }); + _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!" + }; - test("_date() with string format", 2, function() { - equal(_date('71', 'YY').format('YYYY'), '1971'); - equal(_date('69', 'YY').format('YYYY'), '2069'); - }); + 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("_date().format()", 15, function() { - var dateCache = _date(new Date(2010, 1, 14, 15, 25, 50, 125)); - - equal(dateCache.format("dddd, MMMM Do YYYY, h:mm:ss a"), "Sunday, February 14th 2010, 3:25:50 pm"); - equal(dateCache.format("ddd, hA"), "Sun, 3PM"); - equal(dateCache.format("M Mo MM MMMM MMM"), "2 2nd 02 February Feb"); - equal(dateCache.format("YYYY YY"), "2010 10"); - equal(dateCache.format("D Do DD"), "14 14th 14"); - equal(dateCache.format("d do dddd ddd"), "0 0th Sunday Sun"); - equal(dateCache.format("DDD DDDo DDDD"), "45 45th 045"); - equal(dateCache.format("w wo ww"), "8 8th 08"); - equal(dateCache.format("h hh"), "3 03"); - equal(dateCache.format("H HH"), "15 15"); - equal(dateCache.format("m mm"), "25 25"); - equal(dateCache.format("s ss"), "50 50"); - equal(dateCache.format("a A"), "pm PM"); - equal(dateCache.format("z zz"), "PST Pacific Standard Time"); - equal(dateCache.format("t\\he DDDo \\d\\ay of t\\he ye\\ar"), "the 45th day of the year"); - }); - - test("_date().add() + _date().subtract()", 5, 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"); - equal(_date([2010, 0, 31]).format("MMMM Do YYYY"), "January 31st 2010"); - equal(_date([2010, 0, 31]).add({M:1}).format("MMMM Do YYYY"), "February 28th 2010"); - equal(_date([2007, 1, 28]).format("MMMM Do YYYY"), "February 28th 2007"); - equal(_date([2007, 1, 28]).subtract({M:1}).format("MMMM Do YYYY"), "January 28th 2007"); - }); - - - test("_date().from() -- suffixless", 30, function() { - var start = _date([2007, 1, 28]); - equal(start.from(_date([2007, 1, 28]).add({s:44}), true), "seconds"); - equal(start.from(_date([2007, 1, 28]).add({s:45}), true), "a minute"); - equal(start.from(_date([2007, 1, 28]).add({s:89}), true), "a minute"); - equal(start.from(_date([2007, 1, 28]).add({s:90}), true), "2 minutes"); - equal(start.from(_date([2007, 1, 28]).add({m:44}), true), "44 minutes"); - equal(start.from(_date([2007, 1, 28]).add({m:45}), true), "an hour"); - equal(start.from(_date([2007, 1, 28]).add({m:89}), true), "an hour"); - equal(start.from(_date([2007, 1, 28]).add({m:90}), true), "2 hours"); - equal(start.from(_date([2007, 1, 28]).add({h:5}), true), "5 hours"); - equal(start.from(_date([2007, 1, 28]).add({h:21}), true), "21 hours"); - equal(start.from(_date([2007, 1, 28]).add({h:22}), true), "a day"); - equal(start.from(_date([2007, 1, 28]).add({h:35}), true), "a day"); - equal(start.from(_date([2007, 1, 28]).add({h:36}), true), "2 days"); - equal(start.from(_date([2007, 1, 28]).add({d:1}), true), "a day"); - equal(start.from(_date([2007, 1, 28]).add({d:5}), true), "5 days"); - equal(start.from(_date([2007, 1, 28]).add({d:25}), true), "25 days"); - equal(start.from(_date([2007, 1, 28]).add({d:26}), true), "a month"); - equal(start.from(_date([2007, 1, 28]).add({d:30}), true), "a month"); - equal(start.from(_date([2007, 1, 28]).add({d:45}), true), "a month"); - equal(start.from(_date([2007, 1, 28]).add({d:46}), true), "2 months"); - equal(start.from(_date([2007, 1, 28]).add({d:75}), true), "2 months"); - equal(start.from(_date([2007, 1, 28]).add({d:76}), true), "3 months"); - equal(start.from(_date([2007, 1, 28]).add({M:1}), true), "a month"); - equal(start.from(_date([2007, 1, 28]).add({M:5}), true), "5 months"); - equal(start.from(_date([2007, 1, 28]).add({d:344}), true), "11 months"); - equal(start.from(_date([2007, 1, 28]).add({d:345}), true), "a year"); - equal(start.from(_date([2007, 1, 28]).add({d:547}), true), "a year"); - equal(start.from(_date([2007, 1, 28]).add({d:548}), true), "2 years"); - equal(start.from(_date([2007, 1, 28]).add({y:1}), true), "a year"); - equal(start.from(_date([2007, 1, 28]).add({y:5}), true), "5 years"); - }); - - test("_date().from() -- asMilliseconds", 5, function() { - equal(_date(1000).from(0, true, true), 1000, "1 second (ms) - 0 = 1000"); - equal(_date(1000).from(500, false, true), 500, "1 second (ms) - .5 second (ms) = -500"); - equal(_date(0).from(1000, false, true), -1000, "0 - 1 second (ms) = -1000"); - equal(_date(new Date(1000)).from(1000, false, true), 0, "1 second (date) - 1 second (ms) = 0"); - var oneHourDate = new Date(), - nowDate = new Date(); - oneHourDate.setHours(oneHourDate.getHours() + 1); - equal(_date(oneHourDate).from(nowDate, false, true), 60 * 60 * 1000, "1 hour from now = 360000"); - }); - - test("_date().from() -- with suffix", 2, function() { - equal(_date(30000).from(0), "in seconds"); - equal(_date(0).from(30000), "seconds ago"); - }); +test("from future past", 2, function() { + var backup = _date.relativeTime; - test("_date().fromNow()", 2, function() { - equal(_date().add({s:30}).fromNow(), "in seconds"); - equal(_date().add({d:5}).fromNow(), "in 5 days"); - }); - - test("_date().isLeapYear()", function() { - expect(4); - equal(_date([2010, 0, 1]).isLeapYear(), false); - equal(_date([2100, 0, 1]).isLeapYear(), false); - equal(_date([2008, 0, 1]).isLeapYear(), true); - equal(_date([2000, 0, 1]).isLeapYear(), true); - }); + _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'); - test("underscore mixin", 6, function() { - ok(_.date([2010, 1, 12]).date instanceof Date, "[2010, 1, 12]"); - ok(_.date([2010, 1, 12, 1]).date instanceof Date, "[2010, 1, 12, 1]"); - ok(_.date().date instanceof Date, "undefined"); - ok(_.date("Aug 9, 1995").date instanceof Date, "Aug 9, 1995"); - ok(_.date("Mon, 25 Dec 1995 13:30:00 GMT").date instanceof Date, "Mon, 25 Dec 1995 13:30:00 GMT"); - 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()"); - }); -}); \ No newline at end of file + _date.relativeTime = backup; +}); diff --git a/test/speed.js b/test/speed.js index 665f6f726..8055d587f 100755 --- a/test/speed.js +++ b/test/speed.js @@ -1,30 +1,25 @@ -(function() { +var date = new Date(); - var date1 = new Date(2010, 2, 6, 15, 25, 50, 125), - date2 = new Date(1000); +JSLitmus.test('create from date', function() { + return _date(date); +}); - JSLitmus.test('_.date(date1)', function() { - return _.date(date1); - }); - - JSLitmus.test('_.date([2010, 2, 6, 15, 25, 50, 125])', function() { - return _.date([2010, 2, 6, 15, 25, 50, 125]); - }); - - JSLitmus.test('_.date(100000)', function() { - return _.date(100000); - }); - - JSLitmus.test('_.date("Mon, 25 Dec 1995 13:30:00 GMT")', function() { - return _.date("Mon, 25 Dec 1995 13:30:00 GMT"); - }); - - JSLitmus.test('_.date("12-02-1999 2:45:10", "DD-MM-YYYY h:m:s")', function() { - return _.date("12-02-1999 2:45:10", "DD-MM-YYYY h:m:s"); - }); - - JSLitmus.test('_.date()', function() { - return _.date(); - }); - -})(); \ No newline at end of file +JSLitmus.test('create from undefined', function() { + return _date(); +}); + +JSLitmus.test('create from number', function() { + return _date(100000); +}); + +JSLitmus.test('create from string', function() { + return _date("Mon, 25 Dec 1995 13:30:00 GMT"); +}); + +JSLitmus.test('create from array', function() { + return _date([2010, 2, 6, 15, 25, 50, 125]); +}); + +JSLitmus.test('create from string with format', function() { + return _date("12-02-1999 2:45:10", "DD-MM-YYYY h:m:s"); +}); diff --git a/test/test.html b/test/test.html index d002a62d6..e2b8a6ff4 100755 --- a/test/test.html +++ b/test/test.html @@ -1,22 +1,20 @@ - + - - Underscore.date test suite - - - - - - - - - - -

    Underscore.date test suite

    -

    -

    -
      -
      - - + + Uunderscore.date unit test suite + + + + + + + + + + +

      underscore.date unit test suite

      +

      +

      +
        + -- 2.47.3