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