From: Tim Wood Date: Fri, 4 May 2012 01:12:39 +0000 (+0545) Subject: Switching to node js timezone tests for cleaner output X-Git-Tag: 1.7.0~43^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=02cfb833ae41e8db07ea819dde8e713bf4c56b33;p=thirdparty%2Fmoment.git Switching to node js timezone tests for cleaner output --- diff --git a/Makefile b/Makefile index 5327ef4a2..480ff1bec 100644 --- 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 diff --git a/moment.js b/moment.js index e7c818494..d58226d73 100644 --- a/moment.js +++ b/moment.js @@ -864,7 +864,7 @@ }, daysInMonth : function () { - return this.clone().month(this.month() + 1).date(0).date(); + return moment.utc([this.year(), this.month() + 1, 0]).date(); } }; diff --git a/test/moment/days_in_month.js b/test/moment/days_in_month.js index e591b1cb9..52be9b401 100644 --- a/test/moment/days_in_month.js +++ b/test/moment/days_in_month.js @@ -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 index 000000000..5813fbc7e --- /dev/null +++ b/test/zone.js @@ -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 index 0c69ec073..000000000 --- a/test/zone.sh +++ /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