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