Skip to content

Commit

Permalink
convert test/integration/diffs.spec.js to unexpected
Browse files Browse the repository at this point in the history
this required a bit of massaging, since I had to change the
fixture to use `assert`, which produces slightly different output.

also move `getDiffs()` out of `test/integration/helpers.js`,
since it's only used in `test/integration/diffs.spec.js`.

Signed-off-by: Christopher Hiller <boneskull@boneskull.com>
  • Loading branch information
boneskull committed Apr 21, 2018
1 parent 436e85d commit ec857ef
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 56 deletions.
63 changes: 58 additions & 5 deletions test/integration/diffs.spec.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,60 @@
'use strict';

var assert = require('assert');
var inspect = require('util').inspect;
var helpers = require('./helpers');
var run = helpers.runMocha;
var fs = require('fs');
var getDiffs = helpers.getDiffs;
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('test/integration/fixtures/diffs/output', 'UTF8').replace(/\r\n/g, '\n');
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) {
Expand All @@ -20,9 +67,15 @@ describe('diffs', function () {

before(function (done) {
run('diffs/diffs.fixture.js', ['-C'], function (err, res) {
if (err) {
done(err);
return;
}
expected = getExpectedOutput();
console.log('EXPECTED', inspect(expected));
diffs = getDiffs(res.output.replace(/\r\n/g, '\n'));
done(err);
console.log('DIFFS', inspect(diffs));
done();
});
});

Expand All @@ -40,7 +93,7 @@ describe('diffs', function () {
'should display diff by data and not like an objects'
].forEach(function (title, i) {
it(title, function () {
assert.equal(diffs[i], expected[i]);
expect(diffs[i], 'to be', expected[i]);
});
});
});
24 changes: 13 additions & 11 deletions test/integration/fixtures/diffs/diffs.fixture.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

// assert is used because unexpected doesn't use mocha's diffs.
var assert = require('assert');
var fs = require('fs');
var cssin = fs.readFileSync('test/integration/fixtures/diffs/diffs.css.in', 'ascii');
var cssout = fs.readFileSync('test/integration/fixtures/diffs/diffs.css.out', 'ascii');
Expand All @@ -10,19 +12,19 @@ describe('diffs', function () {
it('should display a diff for small strings', function () {
actual = 'foo rar baz';
expected = 'foo bar baz';
expect(actual).to.eql(expected);
assert.equal(actual, expected);
});

it('should display a diff of canonicalized objects', function () {
actual = { name: 'travis j', age: 23 };
expected = { age: 23, name: 'travis' };
expect(actual).to.eql(expected);
assert.deepEqual(actual, expected);
});

it('should display a diff for medium strings', function () {
actual = 'foo bar baz\nfoo rar baz\nfoo bar raz';
expected = 'foo bar baz\nfoo bar baz\nfoo bar baz';
expect(actual).to.eql(expected);
assert.equal(actual, expected);
});

it('should display a diff for entire object dumps', function () {
Expand All @@ -42,13 +44,13 @@ describe('diffs', function () {
country: 'us'
}
};
expect(actual).to.eql(expected);
assert.deepEqual(actual, expected);
});

it('should display a diff for multi-line strings', function () {
actual = 'one two three\nfour zzzz six\nseven eight nine';
expected = 'one two three\nfour five six\nseven eight nine';
expect(actual).to.eql(expected);
assert.equal(actual, expected);
});

it('should display a diff for entire object dumps', function () {
Expand All @@ -68,17 +70,17 @@ describe('diffs', function () {
country: 'us'
}
};
expect(actual).to.eql(expected);
assert.deepEqual(actual, expected);
});

it('should display a full-comparison with escaped special characters', function () {
actual = 'one\ttab\ntwo\t\t\ttabs';
expected = 'one\ttab\ntwo\t\ttabs';
expect(actual).to.eql(expected);
assert.equal(actual, expected);
});

it('should display a word diff for large strings', function () {
expect(cssin).to.eql(cssout);
assert.equal(cssin, cssout);
});

it('should work with objects', function () {
Expand All @@ -96,18 +98,18 @@ describe('diffs', function () {
age: 2
};

expect(actual).to.eql(expected);
assert.deepEqual(actual, expected);
});

it('should show value diffs and not be affected by commas', function () {
actual = { a: 123 };
expected = { a: 123, b: 456 };
expect(actual).to.eql(expected);
assert.deepEqual(actual, expected);
});

it('should display diff by data and not like an objects', function () {
actual = Buffer.from([0x01]);
expected = Buffer.from([0x02]);
expect(actual).to.eql(expected);
assert.deepEqual(actual, expected);
});
});
1 change: 0 additions & 1 deletion test/integration/fixtures/diffs/output
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@
- color: white;
+ color: #fff;
}

a {
- color: blue
+ color: blue;
Expand Down
39 changes: 0 additions & 39 deletions test/integration/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,45 +68,6 @@ module.exports = {
});
},

/**
* 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[]}
*/
getDiffs: function (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);
}
});

// Ignore empty lines before/after diff
return diffs.map(function (diff) {
return diff.slice(1, -3).join('\n');
});
},

/**
* regular expression used for splitting lines based on new line / dot symbol.
*/
Expand Down

0 comments on commit ec857ef

Please sign in to comment.