lots of unit tests

This commit is contained in:
Dave Eddy 2015-09-02 15:38:27 -04:00
parent c028501d66
commit c3a91411f9
2 changed files with 151 additions and 1 deletions

View File

@ -77,6 +77,7 @@ function boolFromString(value, default_, errName) {
*/ */
function jsonStream(arr, stream) { function jsonStream(arr, stream) {
stream = stream || process.stdout; stream = stream || process.stdout;
arr.forEach(function (elem) { arr.forEach(function (elem) {
stream.write(JSON.stringify(elem) + '\n'); stream.write(JSON.stringify(elem) + '\n');
}); });
@ -89,6 +90,9 @@ function jsonStream(arr, stream) {
* @param {Array} valid (optional) - an array to validate pairs * @param {Array} valid (optional) - an array to validate pairs
*/ */
function kvToObj(kvs, valid) { function kvToObj(kvs, valid) {
assert.arrayOfString(kvs);
assert.optionalArrayOfString(valid);
var o = {}; var o = {};
for (var i = 0; i < kvs.length; i++) { for (var i = 0; i < kvs.length; i++) {
var kv = kvs[i]; var kv = kvs[i];
@ -99,7 +103,7 @@ function kvToObj(kvs, valid) {
kv)); kv));
var k = kv.slice(0, idx); var k = kv.slice(0, idx);
var v = kv.slice(idx + 1); var v = kv.slice(idx + 1);
if (valid.indexOf(k) === -1) if (valid && valid.indexOf(k) === -1)
throw new errors.UsageError(format( throw new errors.UsageError(format(
'invalid filter name: "%s" (must be one of "%s")', 'invalid filter name: "%s" (must be one of "%s")',
k, valid.join('", "'))); k, valid.join('", "')));

View File

@ -23,6 +23,152 @@ var log = require('../lib/log');
// ---- tests // ---- tests
test('objCopy+deepObjCopy', function (t) {
var o1 = {foo: 'bar'};
var o2 = {arr: [1,2,3]};
// shallow copy
var c1 = common.objCopy(o1);
var c2 = common.objCopy(o2);
t.notEqual(c1, o1);
t.deepEqual(c1, o1);
t.notEqual(c2, o2);
t.deepEqual(c2, o2);
t.equal(c2.arr, o2.arr);
var foo = {};
common.objCopy(o1, foo);
t.notEqual(foo, o1);
t.deepEqual(foo, o1);
// deep copy
var d1 = common.deepObjCopy(o1);
var d2 = common.deepObjCopy(o2);
t.notEqual(d1, o1);
t.deepEqual(d1, o1);
t.notEqual(d2, o2);
t.deepEqual(d2, o2);
t.notEqual(d2.arr, o2.arr);
t.deepEqual(d2.arr, o2.arr);
t.end();
});
test('zeroPad', function (t) {
var zp = common.zeroPad;
t.equal(zp(5, 3), '005');
t.equal(zp(50, 3), '050');
t.equal(zp(500, 3), '500');
t.equal(zp('5', 3), '005');
t.equal(zp('50', 3), '050');
t.equal(zp('500', 3), '500');
t.end();
});
test('boolFromString', function (t) {
var bfs = common.boolFromString;
t.equal(bfs(true), true);
t.equal(bfs('true'), true);
t.equal(bfs('1'), true);
t.equal(bfs(false), false);
t.equal(bfs('false'), false);
t.equal(bfs('0'), false);
t.equal(bfs(undefined, false), false);
t.equal(bfs(undefined, true), true);
t.equal(bfs(undefined, 'foo'), 'foo');
t.throws(bfs.bind(null, '2'));
t.throws(bfs.bind(null, 500));
t.throws(bfs.bind(null, Infinity));
t.throws(bfs.bind(null, NaN));
t.end();
});
test('jsonStream', function (t) {
// TODO this is a lame excuse for a stream
var s = '';
var stream = {
write: function (o) { s += o; }
};
var obj = [
'foo',
'bar',
'baz'
];
common.jsonStream(obj, stream);
t.equal(s, '"foo"\n"bar"\n"baz"\n');
t.end();
});
test('kvToObj', function (t) {
var arr = ['foo=1', 'bar=2', 'baz=3'];
var o = {
foo: '1',
bar: '2',
baz: '3'
};
var kv;
// no valid parameter
kv = common.kvToObj(arr);
t.deepEqual(kv, o);
// valid parameters
kv = common.kvToObj(arr, ['foo', 'bar', 'baz']);
t.deepEqual(kv, o);
// invalid parameters
t.throws(function () {
common.kvToObj(arr, ['uh-oh']);
});
t.end();
});
test('longAgo', function (t) {
var la = common.longAgo;
var now = new Date();
var then;
t.equal(la(now, now), '0s');
then = now - 1000;
t.equal(la(then, now), '1s');
then = now - 60 * 1000;
t.equal(la(then, now), '1m');
then = now - 60 * 60 * 1000;
t.equal(la(then, now), '1h');
then = now - 24 * 60 * 60 * 1000;
t.equal(la(then, now), '1d');
then = now - 7 * 24 * 60 * 60 * 1000;
t.equal(la(then, now), '1w');
then = now - 365 * 24 * 60 * 60 * 1000;
t.equal(la(then, now), '1y');
t.end();
});
test('humanDurationFromMs', function (t) { test('humanDurationFromMs', function (t) {
var humanDurationFromMs = common.humanDurationFromMs; var humanDurationFromMs = common.humanDurationFromMs;
var ms = 1000; var ms = 1000;