forked from mochajs/mocha
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiffs.spec.js
100 lines (91 loc) · 2.69 KB
/
diffs.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
'use strict';
var helpers = require('./helpers');
var run = helpers.runMocha;
var fs = require('fs');
var path = require('path');
/**
* Returns an array of diffs corresponding to exceptions thrown from specs,
* given the plaintext output (-C) of a mocha run.
*
* @param {string} output
* returns {string[]}
*/
function getDiffs(output) {
var diffs, i, inDiff, inStackTrace;
diffs = [];
output.split('\n').forEach(function (line) {
if (line.match(/^\s{2}\d+\)/)) {
// New spec, e.g. "1) spec title"
diffs.push([]);
i = diffs.length - 1;
inStackTrace = false;
inDiff = false;
} else if (!diffs.length || inStackTrace) {
// Haven't encountered a spec yet
// or we're in the middle of a stack trace
} else if (line.indexOf('+ expected - actual') !== -1) {
inDiff = true;
} else if (line.match(/at Context/)) {
// At the start of a stack trace
inStackTrace = true;
inDiff = false;
} else if (inDiff) {
diffs[i].push(line);
}
});
return diffs.map(function (diff) {
return diff
.filter(function (line) {
return line.trim().length;
})
.join('\n');
});
}
/**
* Returns content of test/integration/fixtures/diffs/output,
* post-processed for consumption by tests.
* @returns {string[]} Array of diff lines
*/
function getExpectedOutput() {
var output = fs
.readFileSync(path.join(__dirname, 'fixtures', 'diffs', 'output'), 'UTF8')
.replace(/\r\n/g, '\n');
// Diffs are delimited in file by "// DIFF"
return output
.split(/\s*\/\/ DIFF/)
.slice(1)
.map(function (diff) {
return diff.split('\n').filter(Boolean).join('\n');
});
}
describe('diffs', function () {
var diffs, expected;
before(function (done) {
run('diffs/diffs.fixture.js', [], function (err, res) {
if (err) {
done(err);
return;
}
expected = getExpectedOutput();
diffs = getDiffs(res.output.replace(/\r\n/g, '\n'));
done();
});
});
[
'should display a diff for small strings',
'should display a diff of canonicalized objects',
'should display a diff for medium strings',
'should display a diff for entire object dumps',
'should display a diff for multi-line strings',
'should display a diff for entire object dumps',
'should display a full-comparison with escaped special characters',
'should display a word diff for large strings',
'should work with objects',
'should show value diffs and not be affected by commas',
'should display diff by data and not like an objects'
].forEach(function (title, i) {
it(title, function () {
expect(diffs[i], 'to be', expected[i]);
});
});
});