@@ -11,8 +11,8 @@ var STOP = 5
11
11
var SKIP = 7
12
12
var SKIP_REVERSE = 6
13
13
14
- var textNodes = 6
15
- var codeNodes = 1
14
+ var texts = 6
15
+ var codes = 1
16
16
17
17
var types = [
18
18
'root' ,
@@ -63,92 +63,77 @@ test('unist-util-visit', function(t) {
63
63
t . test ( 'should iterate over all nodes' , function ( st ) {
64
64
var n = 0
65
65
66
- st . doesNotThrow ( function ( ) {
67
- visit ( tree , visitor )
68
- } , 'should visit all nodes (#1)' )
66
+ visit ( tree , visitor )
69
67
70
- st . equal ( n , types . length , 'should visit all nodes (#2) ' )
68
+ st . equal ( n , types . length , 'should visit all nodes' )
71
69
72
70
st . end ( )
73
71
74
72
function visitor ( node ) {
75
- assert . equal ( node . type , types [ n ++ ] , 'should be the expected type' )
73
+ assert . equal ( node . type , types [ n ] , 'should be the expected type' )
74
+ n ++
76
75
}
77
76
} )
78
77
79
78
t . test ( 'should iterate over all nodes, backwards' , function ( st ) {
80
79
var n = 0
81
80
82
- st . doesNotThrow ( function ( ) {
83
- visit ( tree , visitor , true )
84
- } , 'should visit all nodes in reverse (#1)' )
81
+ visit ( tree , visitor , true )
85
82
86
- st . equal ( n , reverseTypes . length , 'should visit all nodes in reverse (#2) ' )
83
+ st . equal ( n , reverseTypes . length , 'should visit all nodes in reverse' )
87
84
88
85
st . end ( )
89
86
90
87
function visitor ( node ) {
91
- assert . equal ( node . type , reverseTypes [ n ++ ] , 'should be the expected type' )
88
+ assert . equal ( node . type , reverseTypes [ n ] , 'should be the expected type' )
89
+ n ++
92
90
}
93
91
} )
94
92
95
93
t . test ( 'should only visit a given `type`' , function ( st ) {
96
94
var n = 0
97
95
98
- st . doesNotThrow ( function ( ) {
99
- visit ( tree , 'text' , visitor )
100
- } , 'should visit all matching nodes (#1)' )
96
+ visit ( tree , 'text' , visitor )
101
97
102
- st . equal ( n , textNodes , 'should visit all matching nodes (#2) ' )
98
+ st . equal ( n , texts , 'should visit all matching nodes' )
103
99
104
100
st . end ( )
105
101
106
102
function visitor ( node ) {
107
- n ++
108
103
assert . equal ( node . type , 'text' , 'should be the expected type' )
104
+ n ++
109
105
}
110
106
} )
111
107
112
108
t . test ( 'should only visit given `type`s' , function ( st ) {
113
- var n = 0
114
109
var types = [ 'text' , 'inlineCode' ]
110
+ var n = 0
115
111
116
- st . doesNotThrow ( function ( ) {
117
- visit ( tree , types , visitor )
118
- } , 'should visit all matching nodes (#1)' )
112
+ visit ( tree , types , visitor )
119
113
120
- st . equal ( n , textNodes + codeNodes , 'should visit all matching nodes (#2) ' )
114
+ st . equal ( n , texts + codes , 'should visit all matching nodes' )
121
115
122
116
st . end ( )
123
117
124
118
function visitor ( node ) {
125
119
n ++
126
- assert . notEqual (
127
- types . indexOf ( node . type ) ,
128
- - 1 ,
129
- 'should be a requested type: ' + node . type
130
- )
120
+ assert . notEqual ( types . indexOf ( node . type ) , - 1 , 'should match' )
131
121
}
132
122
} )
133
123
134
124
t . test ( 'should accept any `is`-compatible test function' , function ( st ) {
135
125
var n = 0
136
126
137
- st . doesNotThrow ( function ( ) {
138
- visit ( tree , test , visitor )
139
- } , 'should visit all passing nodes (#1)' )
127
+ visit ( tree , test , visitor )
140
128
141
- st . equal ( n , 3 , 'should visit all passing nodes (#2) ' )
129
+ st . equal ( n , 3 , 'should visit all passing nodes' )
142
130
143
131
st . end ( )
144
132
145
133
function visitor ( node , index , parent ) {
146
- var parentType = parent && parent . type
134
+ var info = '(' + ( parent && parent . type ) + ':' + index + ')'
135
+ assert . ok ( test ( node , index ) , 'should be a requested node ' + info )
147
136
n ++
148
- assert . ok (
149
- index > 3 ,
150
- 'should be a requested node (' + parentType + ':' + index + ')'
151
- )
152
137
}
153
138
154
139
function test ( node , index ) {
@@ -157,28 +142,20 @@ test('unist-util-visit', function(t) {
157
142
} )
158
143
159
144
t . test ( 'should accept an array of `is`-compatible tests' , function ( st ) {
160
- var n = 0
161
145
var expected = [ 'root' , 'paragraph' , 'emphasis' , 'strong' ]
146
+ var tests = [ test , 'paragraph' , { value : '.' } , [ 'emphasis' , 'strong' ] ]
147
+ var n = 0
162
148
163
- st . doesNotThrow ( function ( ) {
164
- visit (
165
- tree ,
166
- [ test , 'paragraph' , { value : '.' } , [ 'emphasis' , 'strong' ] ] ,
167
- visitor
168
- )
169
- } , 'should visit all passing nodes (#1)' )
149
+ visit ( tree , tests , visitor )
170
150
171
- st . equal ( n , 5 , 'should visit all passing nodes (#2) ' )
151
+ st . equal ( n , 5 , 'should visit all passing nodes' )
172
152
173
153
st . end ( )
174
154
175
155
function visitor ( node ) {
156
+ var ok = expected . indexOf ( node . type ) !== - 1 || node . value === '.'
157
+ assert . ok ( ok , 'should be a requested type: ' + node . type )
176
158
n ++
177
-
178
- assert . ok (
179
- expected . indexOf ( node . type ) !== - 1 || node . value === '.' ,
180
- 'should be a requested type: ' + node . type
181
- )
182
159
}
183
160
184
161
function test ( node ) {
@@ -189,55 +166,43 @@ test('unist-util-visit', function(t) {
189
166
t . test ( 'should stop if `visitor` stops' , function ( st ) {
190
167
var n = 0
191
168
192
- st . doesNotThrow ( function ( ) {
193
- visit ( tree , visitor )
194
- } , 'should visit nodes until `visit.EXIT` is given (#1)' )
169
+ visit ( tree , visitor )
195
170
196
- st . equal ( n , STOP , 'should visit nodes until `visit.EXIT` is given (#2) ' )
171
+ st . equal ( n , STOP , 'should visit nodes until `visit.EXIT` is given' )
197
172
198
173
st . end ( )
199
174
200
175
function visitor ( node ) {
201
176
assert . equal ( node . type , types [ n ++ ] , 'should be the expected type' )
202
-
203
- if ( n === STOP ) {
204
- return visit . EXIT
205
- }
177
+ return n === STOP ? visit . EXIT : visit . CONTINUE
206
178
}
207
179
} )
208
180
209
181
t . test ( 'should stop if `visitor` stops, backwards' , function ( st ) {
210
182
var n = 0
211
183
212
- st . doesNotThrow ( function ( ) {
213
- visit ( tree , visitor , true )
214
- } , 'should visit nodes until `visit.EXIT` is given (#1)' )
184
+ visit ( tree , visitor , true )
215
185
216
- st . equal ( n , STOP , 'should visit nodes until `visit.EXIT` is given (#2) ' )
186
+ st . equal ( n , STOP , 'should visit nodes until `visit.EXIT` is given' )
217
187
218
188
st . end ( )
219
189
220
190
function visitor ( node ) {
221
191
assert . equal ( node . type , reverseTypes [ n ++ ] , 'should be the expected type' )
222
-
223
- if ( n === STOP ) {
224
- return visit . EXIT
225
- }
192
+ return n === STOP ? visit . EXIT : visit . CONTINUE
226
193
}
227
194
} )
228
195
229
196
t . test ( 'should skip if `visitor` skips' , function ( st ) {
230
197
var n = 0
231
198
var count = 0
232
199
233
- st . doesNotThrow ( function ( ) {
234
- visit ( tree , visitor )
235
- } , 'should visit nodes except when `visit.SKIP` is given (#1)' )
200
+ visit ( tree , visitor )
236
201
237
202
st . equal (
238
203
count ,
239
204
types . length - 1 ,
240
- 'should visit nodes except when `visit.SKIP` is given (#2) '
205
+ 'should visit nodes except when `visit.SKIP` is given'
241
206
)
242
207
243
208
st . end ( )
@@ -257,14 +222,12 @@ test('unist-util-visit', function(t) {
257
222
var n = 0
258
223
var count = 0
259
224
260
- st . doesNotThrow ( function ( ) {
261
- visit ( tree , visitor , true )
262
- } , 'should visit nodes except when `visit.SKIP` is given (#1)' )
225
+ visit ( tree , visitor , true )
263
226
264
227
st . equal (
265
228
count ,
266
229
reverseTypes . length - 1 ,
267
- 'should visit nodes except when `visit.SKIP` is given (#2) '
230
+ 'should visit nodes except when `visit.SKIP` is given'
268
231
)
269
232
270
233
st . end ( )
@@ -305,11 +268,9 @@ test('unist-util-visit', function(t) {
305
268
'text'
306
269
]
307
270
308
- st . doesNotThrow ( function ( ) {
309
- visit ( tree , visitor )
310
- } , 'should visit nodes again (#1)' )
271
+ visit ( tree , visitor )
311
272
312
- st . equal ( n , expected . length , 'should visit nodes again (#2) ' )
273
+ st . equal ( n , expected . length , 'should visit nodes again' )
313
274
314
275
st . end ( )
315
276
@@ -340,11 +301,9 @@ test('unist-util-visit', function(t) {
340
301
'text'
341
302
]
342
303
343
- st . doesNotThrow ( function ( ) {
344
- visit ( tree , visitor )
345
- } , 'should skip nodes (#1)' )
304
+ visit ( tree , visitor )
346
305
347
- st . equal ( n , expected . length , 'should skip nodes (#2) ' )
306
+ st . equal ( n , expected . length , 'should skip nodes' )
348
307
349
308
st . end ( )
350
309
@@ -377,11 +336,9 @@ test('unist-util-visit', function(t) {
377
336
'text'
378
337
]
379
338
380
- st . doesNotThrow ( function ( ) {
381
- visit ( tree , visitor )
382
- } , 'should skip nodes (#1)' )
339
+ visit ( tree , visitor )
383
340
384
- st . equal ( n , expected . length , 'should skip nodes (#2) ' )
341
+ st . equal ( n , expected . length , 'should skip nodes' )
385
342
386
343
st . end ( )
387
344
0 commit comments