Skip to content

Commit

Permalink
Refactor tests to use mochas string diffing
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jun 21, 2015
1 parent 042a7fe commit 81a288a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 24 deletions.
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@
},
"devDependencies": {
"browserify": "^10.0.0",
"chalk": "^1.0.0",
"diff": "^1.2.2",
"eslint": "^0.23.0",
"esmangle": "^1.0.0",
"istanbul": "^0.3.0",
Expand Down
45 changes: 23 additions & 22 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ var mdast = require('mdast');
var assert = require('assert');
var fs = require('fs');
var path = require('path');
var diff = require('diff');
var chalk = require('chalk');

/*
* Methods.
Expand All @@ -21,7 +19,6 @@ var chalk = require('chalk');
var read = fs.readFileSync;
var exists = fs.existsSync;
var join = path.join;
var stderr = global.process.stderr;

/*
* Constants.
Expand All @@ -45,6 +42,24 @@ function process(value, config) {
return mdast().use(toc, config).process(value);
}

/**
* Assert two strings.
*
* @param {string} actual
* @param {string} expected
* @throws {Error} - When not equal.
*/
function assertion(actual, expected) {
try {
assert(actual === expected);
} catch (exception) {
exception.expected = expected;
exception.actual = actual;

throw exception;
}
}

/*
* Tests.
*/
Expand Down Expand Up @@ -81,9 +96,9 @@ describe('mdast-toc()', function () {
''
].join('\n');

assert(process(input, {
assertion(process(input, {
'library': require('to-slug-case')
}) === output);
}), output);
});

it('should accept `library` as a file', function () {
Expand All @@ -107,9 +122,9 @@ describe('mdast-toc()', function () {
''
].join('\n');

assert(process(input, {
assertion(process(input, {
'library': 'node_modules/to-slug-case/index'
}) === output);
}), output);
});

it('should add `attributes.id` to headings', function () {
Expand Down Expand Up @@ -169,25 +184,11 @@ function describeFixture(fixture) {
var input = read(join(filepath, 'input.md'), 'utf-8');
var config = join(filepath, 'config.json');
var result;
var difference;

config = exists(config) ? JSON.parse(read(config, 'utf-8')) : {};
result = process(input, config);

try {
assert(result === output);
} catch (exception) {
difference = diff.diffLines(output, result);

difference.forEach(function (change) {
var colour = change.added ?
'green' : change.removed ? 'red' : 'dim';

stderr.write(chalk[colour](change.value));
});

throw exception;
}
assertion(result, output);
});
}

Expand Down

0 comments on commit 81a288a

Please sign in to comment.