Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added t.diag() method to print yaml metadata #99

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
74 changes: 50 additions & 24 deletions lib/results.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var inherits = require('inherits');
var through = require('through');
var resumer = require('resumer');
var inspect = require('object-inspect');
var YAML = require('yamljs');
var nextTick = typeof setImmediate !== 'undefined'
? setImmediate
: process.nextTick
Expand Down Expand Up @@ -100,7 +101,11 @@ Results.prototype._watch = function (t) {
write('# ' + res + '\n');
return;
}
write(encodeResult(res, self.count + 1));
if (typeof res.ok === 'boolean') {
write(encodeResult(res, self.count + 1));
} else {
return write(diagDelimiters(YAML.stringify(res, 4)));
}
self.count ++;

if (res.ok) self.pass ++
Expand All @@ -125,48 +130,69 @@ Results.prototype.close = function () {
self._stream.queue(null);
};

function encodeResult (res, count) {
function indent(str, spaces) {
return str.split('\n').map(function(line) {
return Array(spaces + 1).join(' ') + line;
}).join('\n');
}

function diagDelimiters (yaml) {
var output = '---\n';
output += yaml;
output += '...\n';
output = indent(output, 2);
// remove two extra spaces
output = output.slice(0, output.length - 2);
return output;
}

function errorYAML (res) {
var output = '';
output += (res.ok ? 'ok ' : 'not ok ') + count;
output += res.name ? ' ' + res.name.toString().replace(/\s+/g, ' ') : '';

if (res.skip) output += ' # SKIP';
else if (res.todo) output += ' # TODO';

output += '\n';
if (res.ok) return output;

var outer = ' ';
var inner = outer + ' ';
output += outer + '---\n';
output += inner + 'operator: ' + res.operator + '\n';
output += 'operator: ' + res.operator + '\n';

if (has(res, 'expected') || has(res, 'actual')) {
var ex = inspect(res.expected);
var ac = inspect(res.actual);

if (Math.max(ex.length, ac.length) > 65) {
output += inner + 'expected:\n' + inner + ' ' + ex + '\n';
output += inner + 'actual:\n' + inner + ' ' + ac + '\n';
output += 'expected:\n ' + ex + '\n';
output += 'actual:\n ' + ac + '\n';
}
else {
output += inner + 'expected: ' + ex + '\n';
output += inner + 'actual: ' + ac + '\n';
output += 'expected: ' + ex + '\n';
output += 'actual: ' + ac + '\n';
}
}
if (res.at) {
output += inner + 'at: ' + res.at + '\n';
output += 'at: ' + res.at + '\n';
}
if (res.operator === 'error' && res.actual && res.actual.stack) {
var lines = String(res.actual.stack).split('\n');
output += inner + 'stack:\n';
output += inner + ' ' + lines[0] + '\n';
output += 'stack:\n';
output += ' ' + lines[0] + '\n';
for (var i = 1; i < lines.length; i++) {
output += inner + lines[i] + '\n';
output += lines[i] + '\n';
}
}

return output;
}

function encodeResult (res, count) {
var output = '';
output += (res.ok ? 'ok ' : 'not ok ') + count;
output += res.name ? ' ' + res.name.toString().replace(/\s+/g, ' ') : '';

output += outer + '...\n';
if (res.skip) output += ' # SKIP';
else if (res.todo) output += ' # TODO';

output += '\n';
if (res.ok) return output;

if (!res.ok && res.operator) {
output += diagDelimiters(errorYAML(res));
}

return output;
}

Expand Down
5 changes: 5 additions & 0 deletions lib/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ Test.prototype.comment = function (msg) {
this.emit('result', msg.trim().replace(/^#\s*/, ''));
};


Test.prototype.diag = function (json) {
this.emit('result', json);
}

Test.prototype.plan = function (n) {
this._plan = n;
this.emit('plan', n);
Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
"inherits": "~2.0.1",
"object-inspect": "~0.4.0",
"resumer": "~0.0.0",
"through": "~2.3.4"
"through": "~2.3.4",
"yamljs": "^0.1.5"
},
"devDependencies": {
"tap" : "~0.4.8",
"falafel" : "~0.3.1",
"tap": "~0.4.8",
"falafel": "~0.3.1",
"concat-stream": "~1.4.1"
},
"scripts": {
Expand Down
51 changes: 51 additions & 0 deletions test/diag.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
var tape = require('../');
var fs = require('fs');
var cp = require('child_process');

var expected = [
'TAP version 13',
'# beep',
'ok 1 should be equal',
' ---',
' message: \'Some diagnostic message\'',
' data:',
' perf: 12.345',
' unit: seconds',
' ...',
'ok 2 should be equivalent',
'',
'1..2',
'# tests 2',
'# pass 2',
'',
'# ok',
'',
''
].join('\n');

var code = [
'var test = require(\'../\');',
'',
'test(\'beep\', function(t){',
' t.plan(2);',
' t.ok(true, \'should be equal\');',
' t.diag({',
' message: \'Some diagnostic message\',',
' data: {',
' perf: 12.345,',
' unit: \'seconds\'',
' }',
' });',
' t.ok(true, \'should be equivalent\');',
'});'
].join('\n');

fs.writeFileSync('diag-fixture.js', code, 'utf8');

tape('diag() method', function(t){
t.plan(1);
cp.exec('node ./diag-fixture.js', function(err, stdout, stderr) {
t.equal(stdout, expected, 'Correct TAP output');
fs.unlinkSync('diag-fixture.js');
});
});
2 changes: 1 addition & 1 deletion test/double_end.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ test(function (t) {
'ok 1 should be equal',
'not ok 2 .end() called twice',
' ---',
' operator: fail',
' operator: fail',
' ...',
'',
'1..2',
Expand Down
6 changes: 3 additions & 3 deletions test/undef.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ tap.test('array test', function (tt) {
+ '# undef\n'
+ 'not ok 1 should be equivalent\n'
+ ' ---\n'
+ ' operator: deepEqual\n'
+ ' expected: { beep: undefined }\n'
+ ' actual: {}\n'
+ ' operator: deepEqual\n'
+ ' expected: { beep: undefined }\n'
+ ' actual: {}\n'
+ ' ...\n'
+ '\n'
+ '1..1\n'
Expand Down