|
1 | 1 | 'use strict';
|
2 | 2 |
|
3 |
| -/* eslint-env mocha */ |
4 |
| - |
5 | 3 | var assert = require('assert');
|
| 4 | +var test = require('tape'); |
6 | 5 | var mdast = require('mdast');
|
7 | 6 | var findAllBefore = require('./');
|
8 | 7 |
|
9 | 8 | var tree = mdast.parse('Some *emphasis*, **strongness**, and `code`.');
|
10 | 9 | var paragraph = tree.children[0];
|
11 | 10 | var children = paragraph.children;
|
12 | 11 |
|
13 |
| -describe('unist-util-find-all-before', function () { |
14 |
| - it('should fail without parent', function () { |
15 |
| - assert.throws( |
16 |
| - function () { |
17 |
| - findAllBefore(); |
18 |
| - }, |
19 |
| - /Expected parent node/ |
20 |
| - ); |
21 |
| - }); |
22 |
| - |
23 |
| - it('should fail without parent node', function () { |
24 |
| - assert.throws( |
25 |
| - function () { |
26 |
| - findAllBefore({type: 'foo'}); |
27 |
| - }, |
28 |
| - /Expected parent node/ |
29 |
| - ); |
30 |
| - }); |
31 |
| - |
32 |
| - it('should fail without index', function () { |
33 |
| - assert.throws( |
34 |
| - function () { |
35 |
| - findAllBefore({type: 'foo', children: []}); |
36 |
| - }, |
37 |
| - /Expected positive finite index or child node/ |
38 |
| - ); |
39 |
| - |
40 |
| - assert.throws( |
41 |
| - function () { |
42 |
| - findAllBefore({type: 'foo', children: []}, -1); |
43 |
| - }, |
44 |
| - /Expected positive finite index or child node/ |
45 |
| - ); |
46 |
| - |
47 |
| - assert.throws( |
48 |
| - function () { |
49 |
| - findAllBefore({type: 'foo', children: []}, {type: 'bar'}); |
50 |
| - }, |
51 |
| - /Expected positive finite index or child node/ |
52 |
| - ); |
53 |
| - }); |
54 |
| - |
55 |
| - it('should fail for invalid `test`', function () { |
56 |
| - assert.throws( |
57 |
| - function () { |
58 |
| - findAllBefore({type: 'foo', children: [{type: 'bar'}]}, 1, false); |
59 |
| - }, |
60 |
| - /Expected function, string, or node as test/ |
61 |
| - ); |
62 |
| - |
63 |
| - assert.throws( |
64 |
| - function () { |
65 |
| - findAllBefore({ |
66 |
| - type: 'foo', |
67 |
| - children: [{type: 'bar'}] |
68 |
| - }, 1, true); |
69 |
| - }, |
70 |
| - /Expected function, string, or node as test/ |
71 |
| - ); |
72 |
| - }); |
73 |
| - |
74 |
| - it('should return the preceding nodes when without `test`', function () { |
75 |
| - var res = [children[0]]; |
76 |
| - |
77 |
| - assert.deepEqual(findAllBefore(paragraph, children[1]), res); |
78 |
| - assert.deepEqual(findAllBefore(paragraph, 1), res); |
79 |
| - assert.deepEqual(findAllBefore(paragraph, 0), []); |
80 |
| - }); |
81 |
| - |
82 |
| - it('should return `[node]` when given a `node` and existing', function () { |
83 |
| - var res = [children[0]]; |
84 |
| - |
85 |
| - assert.deepEqual(findAllBefore(paragraph, 100, children[0]), res); |
86 |
| - assert.deepEqual(findAllBefore(paragraph, children[1], children[0]), res); |
87 |
| - assert.deepEqual(findAllBefore(paragraph, 1, children[0]), res); |
88 |
| - assert.deepEqual(findAllBefore(paragraph, children[0], children[0]), []); |
89 |
| - assert.deepEqual(findAllBefore(paragraph, 0, children[0]), []); |
90 |
| - assert.deepEqual(findAllBefore(paragraph, 1, children[1]), []); |
91 |
| - }); |
92 |
| - |
93 |
| - it('should return children when given a `type` and existing', function () { |
94 |
| - var result = [children[3]]; |
95 |
| - |
96 |
| - assert.deepEqual(findAllBefore(paragraph, 100, 'strong'), result); |
97 |
| - assert.deepEqual(findAllBefore(paragraph, 3, 'strong'), []); |
98 |
| - assert.deepEqual(findAllBefore(paragraph, children[4], 'strong'), result); |
99 |
| - assert.deepEqual(findAllBefore(paragraph, children[3], 'strong'), []); |
100 |
| - }); |
101 |
| - |
102 |
| - it('should return children when given a `test` and existing', function () { |
103 |
| - var res = children.slice(4).reverse(); |
104 |
| - |
105 |
| - assert.deepEqual(findAllBefore(paragraph, 100, test), res); |
106 |
| - assert.deepEqual(findAllBefore(paragraph, 3, test), []); |
107 |
| - assert.deepEqual(findAllBefore(paragraph, children[4], test), []); |
108 |
| - assert.deepEqual(findAllBefore(paragraph, children[3], test), []); |
109 |
| - |
110 |
| - function test(node, n) { |
111 |
| - return n > 3; |
112 |
| - } |
113 |
| - }); |
| 12 | +test('unist-util-find-all-before', function (t) { |
| 13 | + t.throws( |
| 14 | + function () { |
| 15 | + findAllBefore(); |
| 16 | + }, |
| 17 | + /Expected parent node/, |
| 18 | + 'should fail without parent' |
| 19 | + ); |
| 20 | + |
| 21 | + t.throws( |
| 22 | + function () { |
| 23 | + findAllBefore({type: 'foo'}); |
| 24 | + }, |
| 25 | + /Expected parent node/, |
| 26 | + 'should fail without parent node' |
| 27 | + ); |
| 28 | + |
| 29 | + t.doesNotThrow( |
| 30 | + function () { |
| 31 | + assert.throws( |
| 32 | + function () { |
| 33 | + findAllBefore({type: 'foo', children: []}); |
| 34 | + }, |
| 35 | + /Expected positive finite index or child node/ |
| 36 | + ); |
| 37 | + |
| 38 | + assert.throws( |
| 39 | + function () { |
| 40 | + findAllBefore({type: 'foo', children: []}, -1); |
| 41 | + }, |
| 42 | + /Expected positive finite index or child node/ |
| 43 | + ); |
| 44 | + |
| 45 | + assert.throws( |
| 46 | + function () { |
| 47 | + findAllBefore({type: 'foo', children: []}, {type: 'bar'}); |
| 48 | + }, |
| 49 | + /Expected positive finite index or child node/ |
| 50 | + ); |
| 51 | + }, |
| 52 | + 'should fail without index' |
| 53 | + ); |
| 54 | + |
| 55 | + t.doesNotThrow( |
| 56 | + function () { |
| 57 | + assert.throws( |
| 58 | + function () { |
| 59 | + findAllBefore({type: 'foo', children: [{type: 'bar'}]}, 1, false); |
| 60 | + }, |
| 61 | + /Expected function, string, or node as test/ |
| 62 | + ); |
| 63 | + |
| 64 | + assert.throws( |
| 65 | + function () { |
| 66 | + findAllBefore({ |
| 67 | + type: 'foo', |
| 68 | + children: [{type: 'bar'}] |
| 69 | + }, 1, true); |
| 70 | + }, |
| 71 | + /Expected function, string, or node as test/ |
| 72 | + ); |
| 73 | + }, |
| 74 | + 'should fail for invalid `test`' |
| 75 | + ); |
| 76 | + |
| 77 | + t.doesNotThrow( |
| 78 | + function () { |
| 79 | + var res = [children[0]]; |
| 80 | + |
| 81 | + assert.deepEqual(findAllBefore(paragraph, children[1]), res); |
| 82 | + assert.deepEqual(findAllBefore(paragraph, 1), res); |
| 83 | + assert.deepEqual(findAllBefore(paragraph, 0), []); |
| 84 | + }, |
| 85 | + 'should return the preceding nodes when without `test`' |
| 86 | + ); |
| 87 | + |
| 88 | + t.doesNotThrow( |
| 89 | + function () { |
| 90 | + var res = [children[0]]; |
| 91 | + |
| 92 | + assert.deepEqual(findAllBefore(paragraph, 100, children[0]), res); |
| 93 | + assert.deepEqual(findAllBefore(paragraph, children[1], children[0]), res); |
| 94 | + assert.deepEqual(findAllBefore(paragraph, 1, children[0]), res); |
| 95 | + assert.deepEqual(findAllBefore(paragraph, children[0], children[0]), []); |
| 96 | + assert.deepEqual(findAllBefore(paragraph, 0, children[0]), []); |
| 97 | + assert.deepEqual(findAllBefore(paragraph, 1, children[1]), []); |
| 98 | + }, |
| 99 | + 'should return `[node]` when given a `node` and existing' |
| 100 | + ); |
| 101 | + |
| 102 | + t.doesNotThrow( |
| 103 | + function () { |
| 104 | + var result = [children[3]]; |
| 105 | + |
| 106 | + assert.deepEqual(findAllBefore(paragraph, 100, 'strong'), result); |
| 107 | + assert.deepEqual(findAllBefore(paragraph, 3, 'strong'), []); |
| 108 | + assert.deepEqual(findAllBefore(paragraph, children[4], 'strong'), result); |
| 109 | + assert.deepEqual(findAllBefore(paragraph, children[3], 'strong'), []); |
| 110 | + }, |
| 111 | + 'should return children when given a `type` and existing' |
| 112 | + ); |
| 113 | + |
| 114 | + t.doesNotThrow( |
| 115 | + function () { |
| 116 | + var res = children.slice(4).reverse(); |
| 117 | + |
| 118 | + assert.deepEqual(findAllBefore(paragraph, 100, test), res); |
| 119 | + assert.deepEqual(findAllBefore(paragraph, 3, test), []); |
| 120 | + assert.deepEqual(findAllBefore(paragraph, children[4], test), []); |
| 121 | + assert.deepEqual(findAllBefore(paragraph, children[3], test), []); |
| 122 | + |
| 123 | + function test(node, n) { |
| 124 | + return n > 3; |
| 125 | + } |
| 126 | + }, |
| 127 | + 'should return children when given a `test` and existing' |
| 128 | + ); |
| 129 | + |
| 130 | + t.end(); |
114 | 131 | });
|
0 commit comments