]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
Switching to node js timezone tests for cleaner output
authorTim Wood <washwithcare@gmail.com>
Fri, 4 May 2012 01:12:39 +0000 (06:57 +0545)
committerTim Wood <washwithcare@gmail.com>
Fri, 4 May 2012 01:12:39 +0000 (06:57 +0545)
Makefile
moment.js
test/moment/days_in_month.js
test/zone.js [new file with mode: 0644]
test/zone.sh [deleted file]

index 5327ef4a2e38e9f9e85bc08ae1cbfd550f1f6f61..480ff1bec5851edbc8df3c9baa601d6f90be2deb 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -52,7 +52,7 @@ size-history: moment
 .PHONY: test hint test-moment test-lang
 test: hint test-moment test-lang
 test-zone:
-       sh ./test/zone.sh
+       node test/zone
 
 hint:
        node_modules/.bin/jshint moment.js
index e7c818494dd53de9415ec67c3f3237ba45f286a2..d58226d730ce0c93f709ed718401ac4a000789f1 100644 (file)
--- a/moment.js
+++ b/moment.js
         },
 
         daysInMonth : function () {
-            return this.clone().month(this.month() + 1).date(0).date();
+            return moment.utc([this.year(), this.month() + 1, 0]).date();
         }
     };
 
index e591b1cb90e58595728a0750ddc26c0f34572f4f..52be9b401a433fc30b21ac3830551d83bb9f7914 100644 (file)
@@ -2,12 +2,17 @@ var moment = require("../../moment");
 
 exports.days_in_month = {
     "days in month" : function(test) {
-        test.expect(12);
-        var months = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
+        test.expect(24);
+        var months = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
         for (var i = 0; i < 12; i++) {
-            test.equal(moment([2011, i]).daysInMonth(),
+            test.equal(moment([2012, i]).daysInMonth(),
                        months[i],
-                       moment([2011, i]).format('L') + "should have " + months[i] + " days.")
+                       moment([2012, i]).format('L') + " should have " + months[i] + " days. (beginning of month " + i + ')')
+        }
+        for (var i = 0; i < 12; i++) {
+            test.equal(moment([2012, i, months[i]]).daysInMonth(),
+                       months[i],
+                       moment([2012, i, months[i]]).format('L') + " should have " + months[i] + " days. (end of month " + i + ')')
         }
         test.done();
     },
diff --git a/test/zone.js b/test/zone.js
new file mode 100644 (file)
index 0000000..5813fbc
--- /dev/null
@@ -0,0 +1,181 @@
+var terminal = require('child_process').spawn('bash'),
+       path = require('path'),
+    util = require('util'),
+       nodeunit = require('nodeunit');
+
+var ZONES;
+
+
+/******************************
+       Get Timezone
+******************************/
+
+var currentTimezone = '';
+
+function getTimezone() {
+       var term = require('child_process').spawn('bash');
+
+       term.stdout.on('data', function (data) {
+               currentTimezone = data.toString().replace('Time Zone: ', '');
+               getTimezoneList();
+       });
+
+       term.stdin.write('systemsetup gettimezone');
+       term.stdin.end();
+}
+
+function getTimezoneList() {
+       var term = require('child_process').spawn('bash'),
+               data = '';
+
+       term.stdout.on('data', function (d) {
+               data += d;
+       });
+
+       term.stdout.on('end', function () {
+               data = data.replace('Time Zones:', '');
+               ZONES = data.match(/\S+/g);
+               // console.log(ZONES);
+               startTests();
+       });
+
+       term.stdin.write('systemsetup listtimezones');
+       term.stdin.end();
+
+}
+
+
+
+/******************************
+       Set Timezone
+******************************/
+
+var currentTimezone = '';
+
+function setTimezone() {
+       terminal.stdin.write('systemsetup settimezone ' + currentTimezone);
+       terminal.stdin.end();
+}
+
+
+/******************************
+       Tests
+******************************/
+
+
+var files = ['test/moment'],
+       paths = files.map(function (p) {
+               return path.join(process.cwd(), p);
+       });
+
+var globalfailures = '';
+
+var f = 0;
+var p = 0;
+
+function errorLog(assertion) {
+       if (!assertion.error) {
+               return assertion;
+       }
+
+       var e = assertion.error;
+       if (e.actual && e.expected) {
+               var actual = util.inspect(e.actual, false, 10).replace(/\n$/, ''),
+                       expected = util.inspect(e.expected, false, 10).replace(/\n$/, ''),
+                       multiline = (actual.indexOf('\n') !== -1 || expected.indexOf('\n') !== -1),
+                       spacing = (multiline ? '\n' : ' ');
+               e._message = e.message;
+               e.stack = (
+                       e.name + ':' + spacing +
+                       actual + spacing + e.operator + spacing +
+                       expected + '\n' +
+                       e.stack.split('\n').slice(1).join('\n')
+               );
+       }
+       return assertion;
+}
+
+var zone = '',
+       nextZone = '';
+
+function runTestIfNeeded() {
+       if (zone === nextZone) {
+               return;
+       }
+       nextZone = zone;
+
+       console.log("-----------------------");
+       console.log(zone + " testing started...");
+       nodeunit.runFiles(paths, {
+               testDone: function (name, assertions) {
+                       if (assertions.failures()) {
+                               console.log('\nFAILURE: ' + name);
+                               assertions.forEach(function (a) {
+                                       if (a.failed()) {
+                                               a = errorLog(a);
+                                               if (a.error && a.message) {
+                                                       console.log('Assertion Message: ' + a.message);
+                                               }
+                                               console.log(a.error.stack);
+                                       }
+                               });
+                       }
+               },
+               done: function (assertions) {
+                       if (assertions.failures()) {
+                               console.log('\n' + zone + ' had ' + assertions.failures() + ' failures');
+                               globalfailures += zone + ' had ' + assertions.failures() + ' failures\n';
+                               f++;
+                       } else {
+                               console.log(zone + ' passed all tests!');
+                               p++;
+                       }
+                       console.log("-----------------------");
+                       nextTest();
+               }
+       });
+}
+
+function runTest(zoned) {
+       zone = zoned;
+       terminal.stdin.write('systemsetup settimezone ' + zone + '\n');
+}
+
+terminal.stdout.on('data', function(d) {
+       setTimeout(runTestIfNeeded, 100);
+});
+
+
+var i = 0;
+
+function startTests() {
+       nextTest();
+       //setTimezone();
+}
+
+function nextTest() {
+       if (i < ZONES.length) {
+               runTest(ZONES[i]);
+       } else {
+               console.log("----------------------");
+               console.log("----------------------");
+               console.log(globalfailures);
+               console.log("----------------------");
+               console.log("----------------------");
+               console.log("--- Total Failing " + f);
+               console.log("----------------------");
+               console.log("--- Total Passing " + p);
+               console.log("----------------------");
+               console.log("----------------------");
+               setTimezone();
+       }
+       i++;
+}
+
+
+/******************************
+       Start it off
+******************************/
+
+
+getTimezone();
\ No newline at end of file
diff --git a/test/zone.sh b/test/zone.sh
deleted file mode 100644 (file)
index 0c69ec0..0000000
+++ /dev/null
@@ -1,14 +0,0 @@
-ZONE=$(systemsetup -gettimezone | sed "s/Time Zone: //g")
-
-names=(Pacific/Pago_Pago Pacific/Honolulu America/Adak Pacific/Apia Pacific/Marquesas Pacific/Gambier America/Anchorage Pacific/Pitcairn America/Los_Angeles America/Santa_Isabel America/Phoenix America/Denver America/Mazatlan America/Guatemala America/Chicago America/Mexico_City America/Bogota Pacific/Easter America/Havana America/New_York America/Caracas America/Santo_Domingo America/Goose_Bay America/Halifax America/St_Johns America/Argentina/Buenos_Aires America/Campo_Grande America/Santiago America/Miquelon America/Godthab America/Asuncion Atlantic/Stanley America/Noronha America/Sao_Paulo America/Montevideo Atlantic/Cape_Verde Atlantic/Azores Africa/Casablanca Europe/London Africa/Lagos Europe/Berlin Asia/Gaza Asia/Beirut Europe/Minsk Europe/Istanbul Asia/Damascus Asia/Jerusalem Africa/Windhoek Africa/Cairo Africa/Johannesburg Asia/Baghdad Europe/Moscow Asia/Tehran Asia/Dubai Asia/Yerevan Asia/Baku Asia/Kabul Asia/Karachi Asia/Yekaterinburg Asia/Kolkata Asia/Kathmandu Asia/Dhaka Asia/Omsk Asia/Rangoon Asia/Jakarta Asia/Krasnoyarsk Asia/Shanghai Asia/Irkutsk Australia/Eucla Asia/Tokyo Asia/Yakutsk Australia/Darwin Australia/Brisbane Asia/Vladivostok Australia/Adelaide Pacific/Noumea Asia/Kamchatka Australia/Lord_Howe Australia/Sydney Pacific/Norfolk Pacific/Tarawa Pacific/Tongatapu Pacific/Fiji Pacific/Auckland Pacific/Chatham Pacific/Kiritimati)
-
-for name in ${names[@]}
-do
-       echo "\n\n\n\n\n\n--- Start tests for $name ---"
-       systemsetup settimezone $name
-       node_modules/.bin/nodeunit ./test/moment ./test/lang --reporter minimal
-       echo "--- End tests ---"
-done
-
-echo "\n\n--- All tests done! Resetting timezone to $ZONE ---"
-systemsetup settimezone $ZONE
\ No newline at end of file