Skip to content

Commit d0927c1

Browse files
committed
Add tests for non-positional outcomes
1 parent 223f2e1 commit d0927c1

File tree

2 files changed

+89
-19
lines changed

2 files changed

+89
-19
lines changed

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,14 @@
2727
"devDependencies": {
2828
"browserify": "^14.0.0",
2929
"esmangle": "^1.0.1",
30+
"is-hidden": "^1.1.0",
31+
"not": "^0.1.0",
3032
"nyc": "^11.0.0",
3133
"parse5": "^3.0.0",
3234
"remark-cli": "^3.0.0",
3335
"remark-preset-wooorm": "^3.0.0",
3436
"tape": "^4.0.0",
37+
"unist-util-visit": "^1.1.3",
3538
"vfile": "^2.0.0",
3639
"xo": "^0.18.0"
3740
},

test/index.js

Lines changed: 86 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44
var fs = require('fs');
55
var path = require('path');
66
var test = require('tape');
7+
var not = require('not');
8+
var hidden = require('is-hidden');
79
var vfile = require('vfile');
810
var parse5 = require('parse5');
11+
var visit = require('unist-util-visit');
912
var fromParse5 = require('..');
1013

1114
/* Methods. */
@@ -298,28 +301,92 @@ test('hast-util-from-parse5', function (t) {
298301
/* Fixtures. */
299302
test('fixtures', function (t) {
300303
var base = join(__dirname, 'fixtures');
304+
var entries = dir(base);
301305

302-
dir(base)
303-
.filter(function (fixture) {
304-
return fixture.charAt(0) !== '.';
305-
})
306-
.forEach(function (fixture) {
307-
var filePath = join(base, fixture, 'index.json');
308-
var input = read(join(base, fixture, 'index.html'), 'utf8');
309-
var ast = parse5.parse(input, {locationInfo: true});
310-
var doc = fromParse5(ast, {file: vfile(input), verbose: true});
311-
var output;
306+
t.plan(entries.length);
307+
entries.filter(not(hidden)).forEach(each);
312308

313-
try {
314-
output = read(filePath, 'utf8');
315-
} catch (err) {
316-
/* New fixture. */
317-
write(filePath, JSON.stringify(doc, 0, 2) + '\n');
318-
return;
319-
}
309+
function each(fixture) {
310+
t.test(fixture, function (st) {
311+
var opts = {
312+
file: vfile(read(join(base, fixture, 'index.html'), 'utf8')),
313+
out: join(base, fixture, 'index.json')
314+
};
315+
316+
st.plan(4);
320317

321-
t.deepEqual(doc, JSON.parse(output), fixture);
318+
checkYesYes(st, fixture, opts);
319+
checkNoYes(st, fixture, opts);
320+
checkYesNo(st, fixture, opts);
321+
checkNoNo(st, fixture, opts);
322322
});
323+
}
323324

324-
t.end();
325+
function checkYesYes(t, fixture, options) {
326+
var input = parse5.parse(String(options.file), {locationInfo: true});
327+
var actual = fromParse5(input, {file: options.file, verbose: true});
328+
var expected;
329+
330+
try {
331+
expected = JSON.parse(read(options.out));
332+
} catch (err) {
333+
/* New fixture. */
334+
write(options.out, JSON.stringify(actual, 0, 2) + '\n');
335+
return;
336+
}
337+
338+
t.deepEqual(actual, expected, 'p5 w/ position, hast w/ intent of position');
339+
}
340+
341+
function checkYesNo(t, fixture, options) {
342+
var input = parse5.parse(String(options.file), {locationInfo: true});
343+
var actual = fromParse5(input);
344+
var expected = JSON.parse(read(options.out));
345+
346+
clean(expected);
347+
348+
t.deepEqual(actual, expected, 'p5 w/ position, hast w/o intent of position');
349+
}
350+
351+
function checkNoYes(t, fixture, options) {
352+
var input = parse5.parse(String(options.file));
353+
var actual = fromParse5(input, {file: options.file, verbose: true});
354+
var expected = JSON.parse(read(options.out));
355+
356+
clean(expected);
357+
358+
t.deepEqual(actual, expected, 'p5 w/o position, hast w/ intent of position');
359+
}
360+
361+
function checkNoNo(t, fixture, options) {
362+
var input = parse5.parse(String(options.file), {locationInfo: true});
363+
var actual = fromParse5(input);
364+
var expected = JSON.parse(read(options.out));
365+
366+
clean(expected);
367+
368+
try {
369+
require('assert').deepEqual(actual, expected, 'w/o position');
370+
} catch (err) {
371+
console.log('actual: ');
372+
console.dir(actual, {depth: null});
373+
console.log('expected: ');
374+
console.dir(expected, {depth: null});
375+
}
376+
377+
t.deepEqual(actual, expected, 'p5 w/o position, hast w/o intent of position');
378+
}
325379
});
380+
381+
function clean(tree) {
382+
visit(tree, cleaner);
383+
}
384+
385+
function cleaner(node) {
386+
delete node.position;
387+
388+
/* Remove verbose data */
389+
if (node.type === 'element') {
390+
delete node.data;
391+
}
392+
}

0 commit comments

Comments
 (0)