2012-02-08 18:05:56 +00:00
|
|
|
/*
|
2012-02-08 18:30:13 +00:00
|
|
|
* Copyright (c) 2012 Trent Mick. All rights reserved.
|
2012-02-08 18:05:56 +00:00
|
|
|
*
|
|
|
|
* Test type checking on creation of the Logger.
|
|
|
|
*/
|
|
|
|
|
2012-04-27 23:20:57 +00:00
|
|
|
var bunyan = require('../lib/bunyan'),
|
2013-03-29 00:42:32 +00:00
|
|
|
Logger = bunyan;
|
2012-04-27 23:20:57 +00:00
|
|
|
|
2013-01-07 19:18:12 +00:00
|
|
|
// node-tap API
|
|
|
|
if (require.cache[__dirname + '/tap4nodeunit.js'])
|
2013-03-29 00:42:32 +00:00
|
|
|
delete require.cache[__dirname + '/tap4nodeunit.js'];
|
2013-01-07 19:18:12 +00:00
|
|
|
var tap4nodeunit = require('./tap4nodeunit.js');
|
|
|
|
var after = tap4nodeunit.after;
|
|
|
|
var before = tap4nodeunit.before;
|
|
|
|
var test = tap4nodeunit.test;
|
|
|
|
|
2012-02-08 18:05:56 +00:00
|
|
|
|
|
|
|
|
2012-02-08 18:30:13 +00:00
|
|
|
test('ensure Logger creation options', function (t) {
|
2013-03-29 00:42:32 +00:00
|
|
|
t.throws(function () { new Logger(); },
|
2014-08-09 01:25:19 +00:00
|
|
|
/options \(object\) is required/,
|
2013-03-29 00:42:32 +00:00
|
|
|
'no options should throw');
|
|
|
|
|
|
|
|
t.throws(function () { new Logger({}); },
|
2014-08-09 01:25:19 +00:00
|
|
|
/options\.name \(string\) is required/,
|
2013-03-29 00:42:32 +00:00
|
|
|
'no options.name should throw');
|
|
|
|
|
|
|
|
t.doesNotThrow(function () { new Logger({name: 'foo'}); },
|
|
|
|
'just options.name should be sufficient');
|
|
|
|
|
|
|
|
var options = {name: 'foo', stream: process.stdout, streams: []};
|
|
|
|
t.throws(function () { new Logger(options); },
|
2014-08-25 07:50:53 +00:00
|
|
|
/* JSSTYLED */
|
2014-08-09 01:25:19 +00:00
|
|
|
/cannot mix "streams" and "stream" options/,
|
2013-03-29 00:42:32 +00:00
|
|
|
'cannot use "stream" and "streams"');
|
|
|
|
|
|
|
|
// https://github.com/trentm/node-bunyan/issues/3
|
|
|
|
options = {name: 'foo', streams: {}};
|
|
|
|
t.throws(function () { new Logger(options); },
|
2014-08-09 01:25:19 +00:00
|
|
|
/invalid options.streams: must be an array/,
|
2013-03-29 00:42:32 +00:00
|
|
|
'"streams" must be an array');
|
|
|
|
|
|
|
|
options = {name: 'foo', serializers: 'a string'};
|
|
|
|
t.throws(function () { new Logger(options); },
|
2014-08-09 01:25:19 +00:00
|
|
|
/invalid options.serializers: must be an object/,
|
2013-03-29 00:42:32 +00:00
|
|
|
'"serializers" cannot be a string');
|
|
|
|
|
|
|
|
options = {name: 'foo', serializers: [1, 2, 3]};
|
|
|
|
t.throws(function () { new Logger(options); },
|
2014-08-09 01:25:19 +00:00
|
|
|
/invalid options.serializers: must be an object/,
|
2013-03-29 00:42:32 +00:00
|
|
|
'"serializers" cannot be an array');
|
|
|
|
|
|
|
|
t.end();
|
2012-02-08 18:30:13 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
2014-08-09 01:15:00 +00:00
|
|
|
test('ensure Logger constructor is safe without new', function (t) {
|
|
|
|
t.doesNotThrow(function () { Logger({name: 'foo'}); },
|
|
|
|
'constructor should call self with new if necessary');
|
|
|
|
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2012-04-27 23:20:57 +00:00
|
|
|
test('ensure Logger creation options (createLogger)', function (t) {
|
2013-03-29 00:42:32 +00:00
|
|
|
t.throws(function () { bunyan.createLogger(); },
|
2014-08-09 01:25:19 +00:00
|
|
|
/options \(object\) is required/,
|
2013-03-29 00:42:32 +00:00
|
|
|
'no options should throw');
|
|
|
|
|
|
|
|
t.throws(function () { bunyan.createLogger({}); },
|
2014-08-09 01:25:19 +00:00
|
|
|
/options\.name \(string\) is required/,
|
2013-03-29 00:42:32 +00:00
|
|
|
'no options.name should throw');
|
|
|
|
|
|
|
|
t.doesNotThrow(function () { bunyan.createLogger({name: 'foo'}); },
|
|
|
|
'just options.name should be sufficient');
|
|
|
|
|
|
|
|
var options = {name: 'foo', stream: process.stdout, streams: []};
|
|
|
|
t.throws(function () { bunyan.createLogger(options); },
|
2014-08-25 07:50:53 +00:00
|
|
|
/* JSSTYLED */
|
2014-08-09 01:25:19 +00:00
|
|
|
/cannot mix "streams" and "stream" options/,
|
2013-03-29 00:42:32 +00:00
|
|
|
'cannot use "stream" and "streams"');
|
|
|
|
|
|
|
|
// https://github.com/trentm/node-bunyan/issues/3
|
|
|
|
options = {name: 'foo', streams: {}};
|
|
|
|
t.throws(function () { bunyan.createLogger(options); },
|
2014-08-09 01:25:19 +00:00
|
|
|
/invalid options.streams: must be an array/,
|
2013-03-29 00:42:32 +00:00
|
|
|
'"streams" must be an array');
|
|
|
|
|
|
|
|
options = {name: 'foo', serializers: 'a string'};
|
|
|
|
t.throws(function () { bunyan.createLogger(options); },
|
2014-08-09 01:25:19 +00:00
|
|
|
/invalid options.serializers: must be an object/,
|
2013-03-29 00:42:32 +00:00
|
|
|
'"serializers" cannot be a string');
|
|
|
|
|
|
|
|
options = {name: 'foo', serializers: [1, 2, 3]};
|
|
|
|
t.throws(function () { bunyan.createLogger(options); },
|
2014-08-09 01:25:19 +00:00
|
|
|
/invalid options.serializers: must be an object/,
|
2013-03-29 00:42:32 +00:00
|
|
|
'"serializers" cannot be an array');
|
|
|
|
|
|
|
|
t.end();
|
2012-04-27 23:20:57 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
2012-02-08 18:30:13 +00:00
|
|
|
test('ensure Logger child() options', function (t) {
|
2013-03-29 00:42:32 +00:00
|
|
|
var log = new Logger({name: 'foo'});
|
2012-02-08 18:30:13 +00:00
|
|
|
|
2013-03-29 00:42:32 +00:00
|
|
|
t.doesNotThrow(function () { log.child(); },
|
|
|
|
'no options should be fine');
|
2012-02-08 18:30:13 +00:00
|
|
|
|
2013-03-29 00:42:32 +00:00
|
|
|
t.doesNotThrow(function () { log.child({}); },
|
|
|
|
'empty options should be fine too');
|
2012-02-08 18:30:13 +00:00
|
|
|
|
2013-03-29 00:42:32 +00:00
|
|
|
t.throws(function () { log.child({name: 'foo'}); },
|
2014-08-09 01:25:19 +00:00
|
|
|
/invalid options.name: child cannot set logger name/,
|
2013-03-29 00:42:32 +00:00
|
|
|
'child cannot change name');
|
2012-02-08 18:30:13 +00:00
|
|
|
|
2013-03-29 00:42:32 +00:00
|
|
|
var options = {stream: process.stdout, streams: []};
|
|
|
|
t.throws(function () { log.child(options); },
|
2014-08-25 07:50:53 +00:00
|
|
|
/* JSSTYLED */
|
2014-08-09 01:25:19 +00:00
|
|
|
/cannot mix "streams" and "stream" options/,
|
2013-03-29 00:42:32 +00:00
|
|
|
'cannot use "stream" and "streams"');
|
2012-02-08 18:30:13 +00:00
|
|
|
|
2013-03-29 00:42:32 +00:00
|
|
|
// https://github.com/trentm/node-bunyan/issues/3
|
|
|
|
options = {streams: {}};
|
|
|
|
t.throws(function () { log.child(options); },
|
2014-08-09 01:25:19 +00:00
|
|
|
/invalid options.streams: must be an array/,
|
2013-03-29 00:42:32 +00:00
|
|
|
'"streams" must be an array');
|
2012-02-08 18:30:13 +00:00
|
|
|
|
2013-03-29 00:42:32 +00:00
|
|
|
options = {serializers: 'a string'};
|
|
|
|
t.throws(function () { log.child(options); },
|
2014-08-09 01:25:19 +00:00
|
|
|
/invalid options.serializers: must be an object/,
|
2013-03-29 00:42:32 +00:00
|
|
|
'"serializers" cannot be a string');
|
2012-02-08 18:30:13 +00:00
|
|
|
|
2013-03-29 00:42:32 +00:00
|
|
|
options = {serializers: [1, 2, 3]};
|
|
|
|
t.throws(function () { log.child(options); },
|
2014-08-09 01:25:19 +00:00
|
|
|
/invalid options.serializers: must be an object/,
|
2013-03-29 00:42:32 +00:00
|
|
|
'"serializers" cannot be an array');
|
2012-02-08 18:30:13 +00:00
|
|
|
|
2013-03-29 00:42:32 +00:00
|
|
|
t.end();
|
2012-02-08 18:05:56 +00:00
|
|
|
});
|
2014-08-09 01:17:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
test('ensure Logger() rejects non-Logger parents', function (t) {
|
|
|
|
var dad = new Logger({name: 'dad', streams: []});
|
|
|
|
|
|
|
|
t.throws(function () { new Logger({}, {}); },
|
|
|
|
/invalid Logger creation: do not pass a second arg/,
|
|
|
|
'Logger arguments must be valid');
|
|
|
|
|
|
|
|
t.doesNotThrow(function () { new Logger(dad, {}); },
|
|
|
|
'Logger allows Logger instance as parent');
|
|
|
|
|
|
|
|
t.end();
|
|
|
|
});
|