moar test coverage

This commit is contained in:
Sami Samhuri 2011-06-08 00:57:02 -07:00
parent 4b9672b953
commit 2349fcd6d3

View file

@ -1,33 +1,107 @@
// Based on CoffeeScript by andrewschaaf on github
// TODO:
// - past and future dates, especially < 1900 and > 2100
// - locales
// - look for edge cases
var assert = require('assert')
, lib = require('./../lib')
// Tue, 07 Jun 2011 18:51:45 GMT
, Time = new Date(1307472705067)
, Tests =
[ { format: '%L', expected: '067' }
, { format: '%Y', expected: '2011' }
, { format: '%m', expected: '06' }
, { format: '%b', expected: 'Jun' }
, { format: '%B', expected: 'June' }
, { format: '%d', expected: null, expectedUTC: '07' }
, { format: '%H', expected: null, expectedUTC: '18' }
, { format: '%M', expected: '51' }
, { format: '%S', expected: '45' }
, { format: '%s', expected: '1307472705' }
, { format: '%l', expected: null, expectedUTC: ' 6' }
]
/// check exports
Tests.forEach(function(t) {
if (t.expected) test('strftime', t.format, t.expected)
test('strftimeUTC', t.format, t.expectedUTC || t.expected)
})
function test(name, format, expected) {
var actual = lib[name](format, Time)
assert.equal(expected, actual, name + '("' + format + '", Time) is ' + JSON.stringify(actual) + ', expected ' + JSON.stringify(expected))
assert.fn = function(value, msg) {
assert.equal('function', typeof value, msg)
}
console.log('OK')
assert.fn(lib.strftime)
assert.fn(lib.strftimeUTC)
assert.fn(lib.getLocalizedStrftime)
/// check formats
assert.format = function(format, expected, expectedUTC) {
function _assertFmt(expected, name) {
name = name || 'strftime'
var actual = lib[name](format, Time)
assert.equal(expected, actual,
name + '("' + format + '", Time) is ' + JSON.stringify(actual)
+ ', expected ' + JSON.stringify(expected))
}
if (expected) _assertFmt(expected)
_assertFmt(expectedUTC || expected, 'strftimeUTC')
}
assert.format('%A', 'Tuesday')
assert.format('%a', 'Tue')
assert.format('%B', 'June')
assert.format('%b', 'Jun')
assert.format('%D', '06/07/11')
assert.format('%d', '07')
assert.format('%e', '7')
assert.format('%F', '2011-06-07')
assert.format('%H', null, '18')
assert.format('%I', null, '06')
assert.format('%k', null, '18')
assert.format('%L', '067')
assert.format('%l', null, ' 6')
assert.format('%M', null, '51')
assert.format('%m', '06')
assert.format('%n', '\n')
assert.format('%p', null, 'PM')
assert.format('%R', null, '18:51')
assert.format('%r', null, '06:51:45 PM')
assert.format('%S', '45')
assert.format('%s', '1307472705')
assert.format('%T', null, '18:51:45')
assert.format('%t', '\t')
assert.format('%u', '2')
assert.format('%v', '7-Jun-2011')
assert.format('%w', '2')
assert.format('%Y', '2011')
assert.format('%y', '11')
assert.format('%Z', null, 'GMT')
assert.format('%z', null, '+0000')
assert.format('%%', '%') // any other char
ok('GMT')
// PST/PDT
testTimezone('P[DS]T')
// test other time zones you care about here
function ok(s) { console.log('[ \033[32mOK\033[0m ] ' + s) }
// Pass a regex or string that matches the timezone abbrev, e.g. %Z above.
// Don't pass GMT! Every date includes it and it will fail.
// Be careful if you pass a regex, it has to quack like the default one.
function testTimezone(regex) {
regex = typeof regex === 'string' ? RegExp('\\((' + regex + ')\\)$') : regex
var match = Time.toString().match(regex)
if (match) {
var hourDiff = Math.floor(Time.getTimezoneOffset() / 60)
, hours = String(18 - hourDiff)
, sign = hourDiff < 0 ? '+' : '-'
, minDiff = Time.getTimezoneOffset() - (hourDiff * 60)
, mins = String(51 - minDiff)
, R = hours + ':' + mins
, tz = match[1]
assert.format('%H', hours, '18')
assert.format('%I', hours, '06')
assert.format('%k', hours, '18')
assert.format('%l', hours, ' 6')
assert.format('%M', mins)
assert.format('%p', 'AM', 'PM')
assert.format('%R', R, '18:51')
assert.format('%r', R + ':45 AM', '06:51:45 PM')
assert.format('%T', R + ':45', '18:51:45')
assert.format('%Z', tz, 'GMT')
assert.format('%z', sign + '0' + hourDiff + '00', '+0000')
ok(tz)
}
}