Skip to content

Commit b73d2bf

Browse files
author
Michael Hayes
committed
improve yaml formatting of diagnostic information
1 parent d26e522 commit b73d2bf

6 files changed

+191
-44
lines changed

lib/results.js

+12-7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ var through = require('through');
44
var resumer = require('resumer');
55
var inspect = require('object-inspect');
66
var hasOwn = Object.prototype.hasOwnProperty;
7+
var regexpTest = RegExp.prototype.test;
8+
var yamlIndicators = /\:|\-|\?/;
79
var nextTick = typeof setImmediate !== 'undefined'
810
? setImmediate
911
: process.nextTick
@@ -146,9 +148,9 @@ function encodeResult (res, count) {
146148
var ex = inspect(res.expected);
147149
var ac = inspect(res.actual);
148150

149-
if (Math.max(ex.length, ac.length) > 65) {
150-
output += inner + 'expected:\n' + inner + ' ' + ex + '\n';
151-
output += inner + 'actual:\n' + inner + ' ' + ac + '\n';
151+
if (Math.max(ex.length, ac.length) > 65 || invalidYaml(ex) || invalidYaml(ac)) {
152+
output += inner + 'expected: |-\n' + inner + ' ' + ex + '\n';
153+
output += inner + 'actual: |-\n' + inner + ' ' + ac + '\n';
152154
}
153155
else {
154156
output += inner + 'expected: ' + ex + '\n';
@@ -160,10 +162,9 @@ function encodeResult (res, count) {
160162
}
161163
if (res.operator === 'error' && res.actual && res.actual.stack) {
162164
var lines = String(res.actual.stack).split('\n');
163-
output += inner + 'stack:\n';
164-
output += inner + ' ' + lines[0] + '\n';
165-
for (var i = 1; i < lines.length; i++) {
166-
output += inner + lines[i] + '\n';
165+
output += inner + 'stack: |-\n';
166+
for (var i = 0; i < lines.length; i++) {
167+
output += inner + ' ' + lines[i] + '\n';
167168
}
168169
}
169170

@@ -188,3 +189,7 @@ function getNextTest (results) {
188189
function has (obj, prop) {
189190
return hasOwn.call(obj, prop);
190191
}
192+
193+
function invalidYaml (str) {
194+
return regexpTest.call(yamlIndicators, str);
195+
}

package.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@
1818
"through": "~2.3.4"
1919
},
2020
"devDependencies": {
21-
"tap": "~0.7.1",
21+
"concat-stream": "~1.4.1",
2222
"falafel": "~1.0.1",
23-
"concat-stream": "~1.4.1"
23+
"js-yaml": "^3.3.1",
24+
"tap": "~0.7.1",
25+
"tap-parser": "^1.1.6"
2426
},
2527
"scripts": {
2628
"test": "tap test/*.js"

test/circular-things.js

+26-33
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,36 @@
11
var tape = require('../');
22
var tap = require('tap');
3+
var concat = require('concat-stream');
34

45
tap.test('circular test', function (assert) {
56
var test = tape.createHarness({ exit : false });
6-
var tc = tap.createConsumer();
7+
assert.plan(1);
78

8-
var rows = [];
9-
tc.on('data', function (r) { rows.push(r) });
10-
tc.on('end', function () {
11-
// console.log("rs", rows)
12-
13-
// console.log("deepEqual?")
14-
15-
assert.same(rows, [
16-
"TAP version 13"
17-
, "circular"
18-
, { id: 1
19-
, ok: false
20-
, name: " should be equal"
21-
, operator: "equal"
22-
, expected: "{}"
23-
, actual: '{ circular: [Circular] }'
24-
}
25-
, "tests 1"
26-
, "pass 0"
27-
, "fail 1"
28-
])
29-
assert.end()
30-
})
31-
32-
// tt.equal(10, 10)
33-
// tt.end()
34-
35-
test.createStream().pipe(tc);
9+
test.createStream().pipe(concat(function (body) {
10+
assert.equal(
11+
body.toString('utf8'),
12+
'TAP version 13\n'
13+
+ '# circular\n'
14+
+ 'not ok 1 should be equal\n'
15+
+ ' ---\n'
16+
+ ' operator: equal\n'
17+
+ ' expected: |-\n'
18+
+ ' {}\n'
19+
+ ' actual: |-\n'
20+
+ ' { circular: [Circular] }\n'
21+
+ ' ...\n'
22+
+ '\n'
23+
+ '1..1\n'
24+
+ '# tests 1\n'
25+
+ '# pass 0\n'
26+
+ '# fail 1\n'
27+
);
28+
}));
3629

3730
test("circular", function (t) {
38-
t.plan(1)
39-
var circular = {}
40-
circular.circular = circular
41-
t.equal(circular, {})
31+
t.plan(1);
32+
var circular = {};
33+
circular.circular = circular;
34+
t.equal(circular, {});
4235
})
4336
})

test/deep-equal-failure.js

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
var tape = require('../');
2+
var tap = require('tap');
3+
var concat = require('concat-stream');
4+
var tapParser = require('tap-parser');
5+
var yaml = require('js-yaml');
6+
7+
tap.test('deep equal failure', function (assert) {
8+
var test = tape.createHarness({ exit : false });
9+
var stream = test.createStream();
10+
var parser = tapParser();
11+
assert.plan(3);
12+
13+
stream.pipe(parser);
14+
stream.pipe(concat(function (body) {
15+
assert.equal(
16+
body.toString('utf8'),
17+
'TAP version 13\n'
18+
+ '# deep equal\n'
19+
+ 'not ok 1 should be equal\n'
20+
+ ' ---\n'
21+
+ ' operator: equal\n'
22+
+ ' expected: |-\n'
23+
+ ' { b: 2 }\n'
24+
+ ' actual: |-\n'
25+
+ ' { a: 1 }\n'
26+
+ ' ...\n'
27+
+ '\n'
28+
+ '1..1\n'
29+
+ '# tests 1\n'
30+
+ '# pass 0\n'
31+
+ '# fail 1\n'
32+
);
33+
34+
assert.deepEqual(getDiag(body), {
35+
operator: 'equal',
36+
expected: '{ b: 2 }',
37+
actual: '{ a: 1 }'
38+
});
39+
}));
40+
41+
parser.once('assert', function (data) {
42+
assert.deepEqual(data, {
43+
ok: false,
44+
id: 1,
45+
name: 'should be equal',
46+
diag: {
47+
operator: 'equal',
48+
expected: '{ b: 2 }',
49+
actual: '{ a: 1 }'
50+
}
51+
});
52+
});
53+
54+
test("deep equal", function (t) {
55+
t.plan(1);
56+
t.equal({a: 1}, {b: 2});
57+
});
58+
})
59+
60+
function getDiag (body) {
61+
var yamlStart = body.indexOf(' ---');
62+
var yamlEnd = body.indexOf(' ...\n');
63+
var diag = body.slice(yamlStart, yamlEnd).split('\n').map(function (line) {
64+
return line.slice(2);
65+
}).join('\n');
66+
67+
return yaml.safeLoad(diag);
68+
}

test/stackTrace.js

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
var tape = require('../');
2+
var tap = require('tap');
3+
var concat = require('concat-stream');
4+
var tapParser = require('tap-parser');
5+
var yaml = require('js-yaml');
6+
7+
tap.test('preserves stack trace with newlines', function (tt) {
8+
tt.plan(3);
9+
10+
var test = tape.createHarness();
11+
var stream = test.createStream();
12+
var parser = stream.pipe(tapParser());
13+
var stackTrace = 'foo\n bar';
14+
15+
parser.once('assert', function (data) {
16+
tt.deepEqual(data, {
17+
ok: false,
18+
id: 1,
19+
name: "Error: Preserve stack",
20+
diag: {
21+
stack: stackTrace,
22+
operator: 'error',
23+
expected: 'undefined',
24+
actual: '[Error: Preserve stack]'
25+
}
26+
});
27+
});
28+
29+
stream.pipe(concat(function (body) {
30+
var body = body.toString('utf8')
31+
tt.equal(
32+
body,
33+
'TAP version 13\n'
34+
+ '# multiline stack trace\n'
35+
+ 'not ok 1 Error: Preserve stack\n'
36+
+ ' ---\n'
37+
+ ' operator: error\n'
38+
+ ' expected: |-\n'
39+
+ ' undefined\n'
40+
+ ' actual: |-\n'
41+
+ ' [Error: Preserve stack]\n'
42+
+ ' stack: |-\n'
43+
+ ' foo\n'
44+
+ ' bar\n'
45+
+ ' ...\n'
46+
+ '\n'
47+
+ '1..1\n'
48+
+ '# tests 1\n'
49+
+ '# pass 0\n'
50+
+ '# fail 1\n'
51+
);
52+
53+
tt.deepEqual(getDiag(body), {
54+
stack: stackTrace,
55+
operator: 'error',
56+
expected: 'undefined',
57+
actual: '[Error: Preserve stack]'
58+
});
59+
}));
60+
61+
test('multiline stack trace', function (t) {
62+
t.plan(1);
63+
var err = new Error('Preserve stack');
64+
err.stack = stackTrace;
65+
t.error(err);
66+
});
67+
});
68+
69+
function getDiag (body) {
70+
var yamlStart = body.indexOf(' ---');
71+
var yamlEnd = body.indexOf(' ...\n');
72+
var diag = body.slice(yamlStart, yamlEnd).split('\n').map(function (line) {
73+
return line.slice(2);
74+
}).join('\n');
75+
76+
return yaml.safeLoad(diag);
77+
}

test/undef.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ tap.test('array test', function (tt) {
1414
+ 'not ok 1 should be equivalent\n'
1515
+ ' ---\n'
1616
+ ' operator: deepEqual\n'
17-
+ ' expected: { beep: undefined }\n'
18-
+ ' actual: {}\n'
17+
+ ' expected: |-\n'
18+
+ ' { beep: undefined }\n'
19+
+ ' actual: |-\n'
20+
+ ' {}\n'
1921
+ ' ...\n'
2022
+ '\n'
2123
+ '1..1\n'

0 commit comments

Comments
 (0)