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