Skip to content

Commit 4e3d9ce

Browse files
committed
(test) Prettify tests by abstracting out ast walking
1 parent 62ff368 commit 4e3d9ce

File tree

2 files changed

+21
-14
lines changed

2 files changed

+21
-14
lines changed

test/lib/path.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
'use strict';
2+
3+
4+
module.exports = function walk (node, path) {
5+
return path.length
6+
? walk(node.children[path[0]], path.slice(1))
7+
: node;
8+
};

test/test.js

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
'use strict';
22

33
var select = require('..'),
4-
ast = require('./ast');
4+
ast = require('./ast'),
5+
path = require('./lib/path');
56

67
var test = require('tape');
78

@@ -27,25 +28,23 @@ test('type selector', function (t) {
2728
test('nesting', function (t) {
2829
t.deepEqual(select(ast, 'root heading'), select(ast, 'heading'));
2930
t.deepEqual(select(ast, 'paragraph emphasis'), [
30-
ast.children[2].children[0].children[1],
31-
ast.children[3].children[1],
32-
ast.children[4].children[1].children[1].children[1]
33-
.children[0].children[0].children[1]
31+
path(ast, [2, 0, 1]),
32+
path(ast, [3, 1]),
33+
path(ast, [4, 1, 1, 1, 0, 0, 1])
3434
]);
3535
t.deepEqual(select(ast, 'paragraph > emphasis'), [
36-
ast.children[2].children[0].children[1],
37-
ast.children[3].children[1]
36+
path(ast, [2, 0, 1]),
37+
path(ast, [3, 1])
3838
]);
3939
t.deepEqual(select(ast, 'paragraph emphasis > text'), [
40-
ast.children[2].children[0].children[1].children[0],
41-
ast.children[3].children[1].children[0],
42-
ast.children[4].children[1].children[1].children[1]
43-
.children[0].children[0].children[1].children[0]
40+
path(ast, [2, 0, 1, 0]),
41+
path(ast, [3, 1, 0]),
42+
path(ast, [4, 1, 1, 1, 0, 0, 1, 0])
4443
]);
4544
t.deepEqual(select(ast, 'paragraph > emphasis text'), [
46-
ast.children[2].children[0].children[1].children[0],
47-
ast.children[3].children[1].children[0],
48-
ast.children[3].children[1].children[1].children[0]
45+
path(ast, [2, 0, 1, 0]),
46+
path(ast, [3, 1, 0]),
47+
path(ast, [3, 1, 1, 0])
4948
]);
5049
t.end();
5150
});

0 commit comments

Comments
 (0)