migrated layout tests to mocha

This commit is contained in:
Gareth Jones
2013-08-26 21:49:59 +10:00
parent 7c0cfbdcfd
commit b6dc0b9557
+226 -192
View File
@@ -1,23 +1,16 @@
"use strict"; "use strict";
var vows = require('vows') var assert = require('assert');
, assert = require('assert');
//used for patternLayout tests. //used for patternLayout tests.
function test(args, pattern, value) { function test(layout, event, tokens, pattern, value) {
var layout = args[0]
, event = args[1]
, tokens = args[2];
assert.equal(layout(pattern, tokens)(event), value); assert.equal(layout(pattern, tokens)(event), value);
} }
vows.describe('log4js layouts').addBatch({ describe('log4js layouts', function() {
'colouredLayout': { describe('colouredLayout', function() {
topic: function() { var layout = require('../lib/layouts').colouredLayout;
return require('../lib/layouts').colouredLayout;
},
'should apply level colour codes to output': function(layout) { it('should apply level colour codes to output', function() {
var output = layout({ var output = layout({
data: ["nonsense"], data: ["nonsense"],
startTime: new Date(2010, 11, 5, 14, 18, 30, 45), startTime: new Date(2010, 11, 5, 14, 18, 30, 45),
@@ -27,8 +20,9 @@ vows.describe('log4js layouts').addBatch({
} }
}); });
assert.equal(output, '\x1B[31m[2010-12-05 14:18:30.045] [ERROR] cheese - \x1B[39mnonsense'); assert.equal(output, '\x1B[31m[2010-12-05 14:18:30.045] [ERROR] cheese - \x1B[39mnonsense');
}, });
'should support the console.log format for the message': function(layout) {
it('should support the console.log format for the message', function() {
var output = layout({ var output = layout({
data: ["thing %d", 2], data: ["thing %d", 2],
startTime: new Date(2010, 11, 5, 14, 18, 30, 45), startTime: new Date(2010, 11, 5, 14, 18, 30, 45),
@@ -38,14 +32,14 @@ vows.describe('log4js layouts').addBatch({
} }
}); });
assert.equal(output, '\x1B[31m[2010-12-05 14:18:30.045] [ERROR] cheese - \x1B[39mthing 2'); assert.equal(output, '\x1B[31m[2010-12-05 14:18:30.045] [ERROR] cheese - \x1B[39mthing 2');
} });
},
});
'messagePassThroughLayout': { describe('messagePassThroughLayout', function() {
topic: function() { var layout = require('../lib/layouts').messagePassThroughLayout;
return require('../lib/layouts').messagePassThroughLayout;
}, it('should take a logevent and output only the message', function() {
'should take a logevent and output only the message' : function(layout) {
assert.equal(layout({ assert.equal(layout({
data: ["nonsense"], data: ["nonsense"],
startTime: new Date(2010, 11, 5, 14, 18, 30, 45), startTime: new Date(2010, 11, 5, 14, 18, 30, 45),
@@ -55,8 +49,9 @@ vows.describe('log4js layouts').addBatch({
toString: function() { return "ERROR"; } toString: function() { return "ERROR"; }
} }
}), "nonsense"); }), "nonsense");
}, });
'should support the console.log format for the message' : function(layout) {
it('should support the console.log format for the message', function() {
assert.equal(layout({ assert.equal(layout({
data: ["thing %d", 1, "cheese"], data: ["thing %d", 1, "cheese"],
startTime: new Date(2010, 11, 5, 14, 18, 30, 45), startTime: new Date(2010, 11, 5, 14, 18, 30, 45),
@@ -66,8 +61,9 @@ vows.describe('log4js layouts').addBatch({
toString: function() { return "ERROR"; } toString: function() { return "ERROR"; }
} }
}), "thing 1 cheese"); }), "thing 1 cheese");
}, });
'should output the first item even if it is not a string': function(layout) {
it('should output the first item even if it is not a string', function() {
assert.equal(layout({ assert.equal(layout({
data: [ { thing: 1} ], data: [ { thing: 1} ],
startTime: new Date(2010, 11, 5, 14, 18, 30, 45), startTime: new Date(2010, 11, 5, 14, 18, 30, 45),
@@ -77,25 +73,36 @@ vows.describe('log4js layouts').addBatch({
toString: function() { return "ERROR"; } toString: function() { return "ERROR"; }
} }
}), "{ thing: 1 }"); }), "{ thing: 1 }");
}, });
'should print the stacks of a passed error objects': function(layout) {
assert.isArray(layout({ it('should print the stacks of a passed error objects', function() {
data: [ new Error() ], assert.ok(
startTime: new Date(2010, 11, 5, 14, 18, 30, 45), Array.isArray(
categoryName: "cheese", layout({
level: { data: [ new Error() ],
colour: "green", startTime: new Date(2010, 11, 5, 14, 18, 30, 45),
toString: function() { return "ERROR"; } categoryName: "cheese",
} level: {
}).match(/Error\s+at Object\..*\s+\((.*)test[\\\/]layouts-test\.js\:\d+\:\d+\)\s+at runTest/) colour: "green",
, 'regexp did not return a match'); toString: function() { return "ERROR"; }
}, }
'with passed augmented errors': { }).match(
topic: function(layout){ /Error\s+at Context\..*\s+\((.*)test[\\\/]layouts-test\.js\:\d+\:\d+\)\s/
)
),
'regexp did not return a match'
);
});
describe('with passed augmented errors', function() {
var layoutOutput;
before(function() {
var e = new Error("My Unique Error Message"); var e = new Error("My Unique Error Message");
e.augmented = "My Unique attribute value"; e.augmented = "My Unique attribute value";
e.augObj = { at1: "at2" }; e.augObj = { at1: "at2" };
return layout({
layoutOutput = layout({
data: [ e ], data: [ e ],
startTime: new Date(2010, 11, 5, 14, 18, 30, 45), startTime: new Date(2010, 11, 5, 14, 18, 30, 45),
categoryName: "cheese", categoryName: "cheese",
@@ -104,45 +111,46 @@ vows.describe('log4js layouts').addBatch({
toString: function() { return "ERROR"; } toString: function() { return "ERROR"; }
} }
}); });
}, });
'should print error the contained error message': function(layoutOutput) {
it('should print error the contained error message', function() {
var m = layoutOutput.match(/\{ \[Error: My Unique Error Message\]/); var m = layoutOutput.match(/\{ \[Error: My Unique Error Message\]/);
assert.isArray(m); assert.ok(Array.isArray(m));
}, });
'should print error augmented string attributes': function(layoutOutput) {
it('should print error augmented string attributes', function() {
var m = layoutOutput.match(/augmented:\s'My Unique attribute value'/); var m = layoutOutput.match(/augmented:\s'My Unique attribute value'/);
assert.isArray(m); assert.ok(Array.isArray(m));
}, });
'should print error augmented object attributes': function(layoutOutput) {
it('should print error augmented object attributes', function() {
var m = layoutOutput.match(/augObj:\s\{ at1: 'at2' \}/); var m = layoutOutput.match(/augObj:\s\{ at1: 'at2' \}/);
assert.isArray(m); assert.ok(Array.isArray(m));
} });
} });
});
},
'basicLayout': { describe('basicLayout', function() {
topic: function() { var layout = require('../lib/layouts').basicLayout
var layout = require('../lib/layouts').basicLayout, , event = {
event = { data: ['this is a test'],
data: ['this is a test'], startTime: new Date(2010, 11, 5, 14, 18, 30, 45),
startTime: new Date(2010, 11, 5, 14, 18, 30, 45), categoryName: "tests",
categoryName: "tests", level: {
level: { toString: function() { return "DEBUG"; }
toString: function() { return "DEBUG"; } }
} };
};
return [layout, event]; it('should take a logevent and output a formatted string', function() {
},
'should take a logevent and output a formatted string': function(args) {
var layout = args[0], event = args[1];
assert.equal(layout(event), "[2010-12-05 14:18:30.045] [DEBUG] tests - this is a test"); assert.equal(layout(event), "[2010-12-05 14:18:30.045] [DEBUG] tests - this is a test");
}, });
'should output a stacktrace, message if the event has an error attached': function(args) {
var layout = args[0], event = args[1], output, lines, it('should output a stacktrace, message if the event has an error attached', function() {
error = new Error("Some made-up error"), var output
stack = error.stack.split(/\n/); , lines
, error = new Error("Some made-up error")
, stack = error.stack.split(/\n/);
event.data = ['this is a test', error]; event.data = ['this is a test', error];
output = layout(event); output = layout(event);
@@ -157,140 +165,166 @@ vows.describe('log4js layouts').addBatch({
for (var i = 1; i < stack.length; i++) { for (var i = 1; i < stack.length; i++) {
assert.equal(lines[i+2], stack[i+1]); assert.equal(lines[i+2], stack[i+1]);
} }
}, });
'should output any extra data in the log event as util.inspect strings': function(args) {
var layout = args[0], event = args[1], output, lines; it('should output any extra data in the log event as util.inspect strings', function() {
var output, lines;
event.data = ['this is a test', { event.data = ['this is a test', {
name: 'Cheese', name: 'Cheese',
message: 'Gorgonzola smells.' message: 'Gorgonzola smells.'
}]; }];
output = layout(event); output = layout(event);
assert.equal( assert.equal(
output, output,
"[2010-12-05 14:18:30.045] [DEBUG] tests - this is a test " + "[2010-12-05 14:18:30.045] [DEBUG] tests - this is a test " +
"{ name: 'Cheese', message: 'Gorgonzola smells.' }" "{ name: 'Cheese', message: 'Gorgonzola smells.' }"
); );
} });
}, });
'patternLayout': { describe('patternLayout', function() {
topic: function() { var event = {
var event = { data: ['this is a test'],
data: ['this is a test'], startTime: new Date(2010, 11, 5, 14, 18, 30, 45),
startTime: new Date(2010, 11, 5, 14, 18, 30, 45), categoryName: "multiple.levels.of.tests",
categoryName: "multiple.levels.of.tests", level: {
level: { toString: function() { return "DEBUG"; }
toString: function() { return "DEBUG"; } }
} }
}, layout = require('../lib/layouts').patternLayout , layout = require('../lib/layouts').patternLayout
, tokens = { , tokens = {
testString: 'testStringToken', testString: 'testStringToken',
testFunction: function() { return 'testFunctionToken'; }, testFunction: function() { return 'testFunctionToken'; },
fnThatUsesLogEvent: function(logEvent) { return logEvent.level.toString(); } fnThatUsesLogEvent: function(logEvent) { return logEvent.level.toString(); }
}; };
return [layout, event, tokens];
}, event.startTime.getTimezoneOffset = function() { return 0; };
'should default to "time logLevel loggerName - message"': function(args) { it('should default to "time logLevel loggerName - message"', function() {
test(args, null, "14:18:30 DEBUG multiple.levels.of.tests - this is a test\n"); test(layout, event, tokens, null, "14:18:30 DEBUG multiple.levels.of.tests - this is a test\n");
}, });
'%r should output time only': function(args) {
test(args, '%r', '14:18:30'); it('%r should output time only', function() {
}, test(layout, event, tokens, '%r', '14:18:30');
'%p should output the log level': function(args) { });
test(args, '%p', 'DEBUG');
}, it('%p should output the log level', function() {
'%c should output the log category': function(args) { test(layout, event, tokens, '%p', 'DEBUG');
test(args, '%c', 'multiple.levels.of.tests'); });
},
'%m should output the log data': function(args) { it('%c should output the log category', function() {
test(args, '%m', 'this is a test'); test(layout, event, tokens, '%c', 'multiple.levels.of.tests');
}, });
'%n should output a new line': function(args) {
test(args, '%n', '\n'); it('%m should output the log data', function() {
}, test(layout, event, tokens, '%m', 'this is a test');
'%h should output hostname' : function(args) { });
test(args, '%h', require('os').hostname().toString());
}, it('%n should output a new line', function() {
'%c should handle category names like java-style package names': function(args) { test(layout, event, tokens, '%n', '\n');
test(args, '%c{1}', 'tests'); });
test(args, '%c{2}', 'of.tests');
test(args, '%c{3}', 'levels.of.tests'); it('%h should output hostname', function() {
test(args, '%c{4}', 'multiple.levels.of.tests'); test(layout, event, tokens, '%h', require('os').hostname().toString());
test(args, '%c{5}', 'multiple.levels.of.tests'); });
test(args, '%c{99}', 'multiple.levels.of.tests');
}, it('%c should handle category names like java-style package names', function() {
'%d should output the date in ISO8601 format': function(args) { test(layout, event, tokens, '%c{1}', 'tests');
test(args, '%d', '2010-12-05 14:18:30.045'); test(layout, event, tokens, '%c{2}', 'of.tests');
}, test(layout, event, tokens, '%c{3}', 'levels.of.tests');
'%d should allow for format specification': function(args) { test(layout, event, tokens, '%c{4}', 'multiple.levels.of.tests');
test(args, '%d{ISO8601_WITH_TZ_OFFSET}', '2010-12-05T14:18:30-0000'); test(layout, event, tokens, '%c{5}', 'multiple.levels.of.tests');
test(args, '%d{ISO8601}', '2010-12-05 14:18:30.045'); test(layout, event, tokens, '%c{99}', 'multiple.levels.of.tests');
test(args, '%d{ABSOLUTE}', '14:18:30.045'); });
test(args, '%d{DATE}', '05 12 2010 14:18:30.045');
test(args, '%d{yy MM dd hh mm ss}', '10 12 05 14 18 30'); it('%d should output the date in ISO8601 format', function() {
test(args, '%d{yyyy MM dd}', '2010 12 05'); test(layout, event, tokens, '%d', '2010-12-05 14:18:30.045');
test(args, '%d{yyyy MM dd hh mm ss SSS}', '2010 12 05 14 18 30 045'); });
},
'%% should output %': function(args) { it('%d should allow for format specification', function() {
test(args, '%%', '%'); test(layout, event, tokens, '%d{ISO8601_WITH_TZ_OFFSET}', '2010-12-05T14:18:30-0000');
}, test(layout, event, tokens, '%d{ISO8601}', '2010-12-05 14:18:30.045');
'should output anything not preceded by % as literal': function(args) { test(layout, event, tokens, '%d{ABSOLUTE}', '14:18:30.045');
test(args, 'blah blah blah', 'blah blah blah'); test(layout, event, tokens, '%d{DATE}', '05 12 2010 14:18:30.045');
}, test(layout, event, tokens, '%d{yy MM dd hh mm ss}', '10 12 05 14 18 30');
'should output the original string if no replacer matches the token': function(args) { test(layout, event, tokens, '%d{yyyy MM dd}', '2010 12 05');
test(args, '%a{3}', 'a{3}'); test(layout, event, tokens, '%d{yyyy MM dd hh mm ss SSS}', '2010 12 05 14 18 30 045');
}, });
'should handle complicated patterns': function(args) {
test(args, it('%% should output %', function() {
test(layout, event, tokens, '%%', '%');
});
it('should output anything not preceded by % as literal', function() {
test(layout, event, tokens, 'blah blah blah', 'blah blah blah');
});
it('should output the original string if no replacer matches the token', function() {
test(layout, event, tokens, '%a{3}', 'a{3}');
});
it('should handle complicated patterns', function() {
test(layout, event, tokens,
'%m%n %c{2} at %d{ABSOLUTE} cheese %p%n', '%m%n %c{2} at %d{ABSOLUTE} cheese %p%n',
'this is a test\n of.tests at 14:18:30.045 cheese DEBUG\n' 'this is a test\n of.tests at 14:18:30.045 cheese DEBUG\n'
); );
}, });
'should truncate fields if specified': function(args) {
test(args, '%.4m', 'this'); it('should truncate fields if specified', function() {
test(args, '%.7m', 'this is'); test(layout, event, tokens, '%.4m', 'this');
test(args, '%.9m', 'this is a'); test(layout, event, tokens, '%.7m', 'this is');
test(args, '%.14m', 'this is a test'); test(layout, event, tokens, '%.9m', 'this is a');
test(args, '%.2919102m', 'this is a test'); test(layout, event, tokens, '%.14m', 'this is a test');
}, test(layout, event, tokens, '%.2919102m', 'this is a test');
'should pad fields if specified': function(args) { });
test(args, '%10p', ' DEBUG');
test(args, '%8p', ' DEBUG'); it('should pad fields if specified', function() {
test(args, '%6p', ' DEBUG'); test(layout, event, tokens, '%10p', ' DEBUG');
test(args, '%4p', 'DEBUG'); test(layout, event, tokens, '%8p', ' DEBUG');
test(args, '%-4p', 'DEBUG'); test(layout, event, tokens, '%6p', ' DEBUG');
test(args, '%-6p', 'DEBUG '); test(layout, event, tokens, '%4p', 'DEBUG');
test(args, '%-8p', 'DEBUG '); test(layout, event, tokens, '%-4p', 'DEBUG');
test(args, '%-10p', 'DEBUG '); test(layout, event, tokens, '%-6p', 'DEBUG ');
}, test(layout, event, tokens, '%-8p', 'DEBUG ');
'%[%r%] should output colored time': function(args) { test(layout, event, tokens, '%-10p', 'DEBUG ');
test(args, '%[%r%]', '\x1B[36m14:18:30\x1B[39m'); });
},
'%x{testString} should output the string stored in tokens': function(args) { it('%[%r%] should output colored time', function() {
test(args, '%x{testString}', 'testStringToken'); test(layout, event, tokens, '%[%r%]', '\x1B[36m14:18:30\x1B[39m');
}, });
'%x{testFunction} should output the result of the function stored in tokens': function(args) {
test(args, '%x{testFunction}', 'testFunctionToken'); it('%x{testString} should output the string stored in tokens', function() {
}, test(layout, event, tokens, '%x{testString}', 'testStringToken');
'%x{doesNotExist} should output the string stored in tokens': function(args) { });
test(args, '%x{doesNotExist}', '%x{doesNotExist}');
}, it('%x{testFunction} should output the result of the function stored in tokens', function() {
'%x{fnThatUsesLogEvent} should be able to use the logEvent': function(args) { test(layout, event, tokens, '%x{testFunction}', 'testFunctionToken');
test(args, '%x{fnThatUsesLogEvent}', 'DEBUG'); });
},
'%x should output the string stored in tokens': function(args) { it('%x{doesNotExist} should output the string stored in tokens', function() {
test(args, '%x', '%x'); test(layout, event, tokens, '%x{doesNotExist}', '%x{doesNotExist}');
}, });
},
'layout makers': { it('%x{fnThatUsesLogEvent} should be able to use the logEvent', function() {
topic: require('../lib/layouts'), test(layout, event, tokens, '%x{fnThatUsesLogEvent}', 'DEBUG');
'should have a maker for each layout': function(layouts) { });
it('%x should output the string stored in tokens', function() {
test(layout, event, tokens, '%x', '%x');
});
});
describe('layout makers', function() {
var layouts = require('../lib/layouts');
it('should have a maker for each layout', function() {
assert.ok(layouts.layout("messagePassThrough")); assert.ok(layouts.layout("messagePassThrough"));
assert.ok(layouts.layout("basic")); assert.ok(layouts.layout("basic"));
assert.ok(layouts.layout("colored")); assert.ok(layouts.layout("colored"));
assert.ok(layouts.layout("coloured")); assert.ok(layouts.layout("coloured"));
assert.ok(layouts.layout("pattern")); assert.ok(layouts.layout("pattern"));
} });
} });
}).export(module); });