@@ -9,6 +9,7 @@ var tree = remark().parse('Some _emphasis_, **importance**, and `code`.');
99var STOP = 5 ;
1010
1111var textNodes = 6 ;
12+ var codeNodes = 1 ;
1213
1314var types = [
1415 'root' ,
@@ -78,15 +79,69 @@ test('unist-util-visit', function (t) {
7879 st . end ( ) ;
7980 } ) ;
8081
81- t . test ( 'should only visit given `types `' , function ( st ) {
82+ t . test ( 'should only visit a given `type `' , function ( st ) {
8283 var n = 0 ;
8384
8485 visit ( tree , 'text' , function ( node ) {
8586 n ++ ;
8687 st . equal ( node . type , 'text' ) ;
8788 } ) ;
8889
89- st . equal ( n , textNodes , 'should visit all nodes' ) ;
90+ st . equal ( n , textNodes , 'should visit all matching nodes' ) ;
91+
92+ st . end ( ) ;
93+ } ) ;
94+
95+ t . test ( 'should only visit given `type`s' , function ( st ) {
96+ var n = 0 ;
97+ var types = [ 'text' , 'inlineCode' ] ;
98+
99+ visit ( tree , types , function ( node ) {
100+ n ++ ;
101+ st . ok ( types . indexOf ( node . type ) !== - 1 , 'should be a requested type: ' + node . type ) ;
102+ } ) ;
103+
104+ st . equal ( n , textNodes + codeNodes , 'should visit all matching nodes' ) ;
105+
106+ st . end ( ) ;
107+ } ) ;
108+
109+ t . test ( 'should accept any `is`-compatible test' , function ( st ) {
110+ var n = 0 ;
111+ var test = function ( node , index ) {
112+ return index > 3 ;
113+ } ;
114+
115+ visit ( tree , test , function ( node , index , parent ) {
116+ n ++ ;
117+ var parentType = parent && parent . type ;
118+ st . ok ( index > 3 , 'should be a requested node: ' + parentType + '/[' + index + ']' ) ;
119+ } ) ;
120+
121+ st . equal ( n , 3 , 'should visit all matching nodes' ) ;
122+
123+ st . end ( ) ;
124+ } ) ;
125+
126+ t . test ( 'should accept an array of `is`-compatible tests' , function ( st ) {
127+ var n = 0 ;
128+ var tests = [
129+ function ( node ) {
130+ return node . type === 'root' ;
131+ } ,
132+ 'paragraph' ,
133+ { value : '.' } ,
134+ [ 'emphasis' , 'strong' ]
135+ ] ;
136+ var expectedTypes = [ 'root' , 'paragraph' , 'emphasis' , 'strong' ] ;
137+
138+ visit ( tree , tests , function ( node ) {
139+ n ++ ;
140+ st . ok ( expectedTypes . indexOf ( node . type ) !== - 1 || node . value === '.' ,
141+ 'should be a requested type: ' + node . type ) ;
142+ } ) ;
143+
144+ st . equal ( n , 5 , 'should visit all matching nodes' ) ;
90145
91146 st . end ( ) ;
92147 } ) ;
0 commit comments