|
4 | 4 | var fs = require('fs');
|
5 | 5 | var path = require('path');
|
6 | 6 | var test = require('tape');
|
| 7 | +var not = require('not'); |
| 8 | +var hidden = require('is-hidden'); |
7 | 9 | var vfile = require('vfile');
|
8 | 10 | var parse5 = require('parse5');
|
| 11 | +var visit = require('unist-util-visit'); |
9 | 12 | var fromParse5 = require('..');
|
10 | 13 |
|
11 | 14 | /* Methods. */
|
@@ -298,28 +301,92 @@ test('hast-util-from-parse5', function (t) {
|
298 | 301 | /* Fixtures. */
|
299 | 302 | test('fixtures', function (t) {
|
300 | 303 | var base = join(__dirname, 'fixtures');
|
| 304 | + var entries = dir(base); |
301 | 305 |
|
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); |
312 | 308 |
|
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); |
320 | 317 |
|
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); |
322 | 322 | });
|
| 323 | + } |
323 | 324 |
|
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 | + } |
325 | 379 | });
|
| 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