1
1
'use strict'
2
2
3
- var assert = require ( 'assert' )
4
3
var test = require ( 'tape' )
5
4
var remark = require ( 'remark' )
6
5
var findAllBefore = require ( '.' )
@@ -26,85 +25,140 @@ test('unist-util-find-all-before', function (t) {
26
25
'should fail without parent node'
27
26
)
28
27
29
- t . doesNotThrow ( function ( ) {
30
- assert . throws ( function ( ) {
28
+ t . throws (
29
+ function ( ) {
31
30
findAllBefore ( { type : 'foo' , children : [ ] } )
32
- } , / E x p e c t e d p o s i t i v e f i n i t e i n d e x o r c h i l d n o d e / )
31
+ } ,
32
+ / E x p e c t e d c h i l d n o d e o r i n d e x / ,
33
+ 'should fail without index (#1)'
34
+ )
33
35
34
- assert . throws ( function ( ) {
36
+ t . throws (
37
+ function ( ) {
35
38
findAllBefore ( { type : 'foo' , children : [ ] } , - 1 )
36
- } , / E x p e c t e d p o s i t i v e f i n i t e i n d e x o r c h i l d n o d e / )
39
+ } ,
40
+ / E x p e c t e d p o s i t i v e f i n i t e n u m b e r a s i n d e x / ,
41
+ 'should fail without index (#2)'
42
+ )
37
43
38
- assert . throws ( function ( ) {
44
+ t . throws (
45
+ function ( ) {
39
46
findAllBefore ( { type : 'foo' , children : [ ] } , { type : 'bar' } )
40
- } , / E x p e c t e d p o s i t i v e f i n i t e i n d e x o r c h i l d n o d e / )
41
- } , 'should fail without index' )
47
+ } ,
48
+ / E x p e c t e d c h i l d n o d e o r i n d e x / ,
49
+ 'should fail without index (#3)'
50
+ )
42
51
43
- t . doesNotThrow ( function ( ) {
44
- assert . throws ( function ( ) {
52
+ t . throws (
53
+ function ( ) {
45
54
findAllBefore ( { type : 'foo' , children : [ { type : 'bar' } ] } , 1 , false )
46
- } , / E x p e c t e d f u n c t i o n , s t r i n g , o r o b j e c t a s t e s t / )
47
-
48
- assert . throws ( function ( ) {
49
- findAllBefore (
50
- {
51
- type : 'foo' ,
52
- children : [ { type : 'bar' } ]
53
- } ,
54
- 1 ,
55
- true
56
- )
57
- } , / E x p e c t e d f u n c t i o n , s t r i n g , o r o b j e c t a s t e s t / )
58
- } , 'should fail for invalid `test`' )
59
-
60
- t . doesNotThrow ( function ( ) {
61
- assert . deepStrictEqual ( findAllBefore ( paragraph , children [ 1 ] ) , [ children [ 0 ] ] )
62
- assert . deepStrictEqual ( findAllBefore ( paragraph , 1 ) , [ children [ 0 ] ] )
63
- assert . deepStrictEqual ( findAllBefore ( paragraph , 0 ) , [ ] )
64
- } , 'should return the preceding nodes when without `test`' )
65
-
66
- t . doesNotThrow ( function ( ) {
67
- assert . deepStrictEqual ( findAllBefore ( paragraph , 100 , children [ 0 ] ) , [
68
- children [ 0 ]
69
- ] )
70
- assert . deepStrictEqual ( findAllBefore ( paragraph , children [ 1 ] , children [ 0 ] ) , [
71
- children [ 0 ]
72
- ] )
73
- assert . deepStrictEqual ( findAllBefore ( paragraph , 1 , children [ 0 ] ) , [
74
- children [ 0 ]
75
- ] )
76
- assert . deepStrictEqual (
77
- findAllBefore ( paragraph , children [ 0 ] , children [ 0 ] ) ,
78
- [ ]
79
- )
80
- assert . deepStrictEqual ( findAllBefore ( paragraph , 0 , children [ 0 ] ) , [ ] )
81
- assert . deepStrictEqual ( findAllBefore ( paragraph , 1 , children [ 1 ] ) , [ ] )
82
- } , 'should return `[node]` when given a `node` and existing' )
83
-
84
- t . doesNotThrow ( function ( ) {
85
- var result = [ children [ 3 ] ]
86
-
87
- assert . deepStrictEqual ( findAllBefore ( paragraph , 100 , 'strong' ) , result )
88
- assert . deepStrictEqual ( findAllBefore ( paragraph , 3 , 'strong' ) , [ ] )
89
- assert . deepStrictEqual (
90
- findAllBefore ( paragraph , children [ 4 ] , 'strong' ) ,
91
- result
92
- )
93
- assert . deepStrictEqual ( findAllBefore ( paragraph , children [ 3 ] , 'strong' ) , [ ] )
94
- } , 'should return children when given a `type` and existing' )
95
-
96
- t . doesNotThrow ( function ( ) {
97
- var result = children . slice ( 4 ) . reverse ( )
98
-
99
- assert . deepStrictEqual ( findAllBefore ( paragraph , 100 , test ) , result )
100
- assert . deepStrictEqual ( findAllBefore ( paragraph , 3 , test ) , [ ] )
101
- assert . deepStrictEqual ( findAllBefore ( paragraph , children [ 4 ] , test ) , [ ] )
102
- assert . deepStrictEqual ( findAllBefore ( paragraph , children [ 3 ] , test ) , [ ] )
103
-
104
- function test ( node , n ) {
105
- return n > 3
106
- }
107
- } , 'should return children when given a `test` and existing' )
55
+ } ,
56
+ / E x p e c t e d f u n c t i o n , s t r i n g , o r o b j e c t a s t e s t / ,
57
+ 'should fail for invalid `test` (#1)'
58
+ )
59
+
60
+ t . throws (
61
+ function ( ) {
62
+ findAllBefore ( { type : 'foo' , children : [ { type : 'bar' } ] } , 1 , true )
63
+ } ,
64
+ / E x p e c t e d f u n c t i o n , s t r i n g , o r o b j e c t a s t e s t / ,
65
+ 'should fail for invalid `test` (#2)'
66
+ )
67
+
68
+ t . deepEqual (
69
+ findAllBefore ( paragraph , children [ 1 ] ) ,
70
+ [ children [ 0 ] ] ,
71
+ 'should return the preceding nodes when without `test` (#1)'
72
+ )
73
+ t . deepEqual (
74
+ findAllBefore ( paragraph , 1 ) ,
75
+ [ children [ 0 ] ] ,
76
+ 'should return the preceding nodes when without `test` (#1)'
77
+ )
78
+ t . deepEqual (
79
+ findAllBefore ( paragraph , 0 ) ,
80
+ [ ] ,
81
+ 'should return the preceding nodes when without `test` (#1)'
82
+ )
83
+
84
+ t . deepEqual (
85
+ findAllBefore ( paragraph , 100 , children [ 0 ] ) ,
86
+ [ children [ 0 ] ] ,
87
+ 'should return `[node]` when given a `node` and existing (#1)'
88
+ )
89
+ t . deepEqual (
90
+ findAllBefore ( paragraph , children [ 1 ] , children [ 0 ] ) ,
91
+ [ children [ 0 ] ] ,
92
+ 'should return `[node]` when given a `node` and existing (#2)'
93
+ )
94
+ t . deepEqual (
95
+ findAllBefore ( paragraph , 1 , children [ 0 ] ) ,
96
+ [ children [ 0 ] ] ,
97
+ 'should return `[node]` when given a `node` and existing (#3)'
98
+ )
99
+ t . deepEqual (
100
+ findAllBefore ( paragraph , children [ 0 ] , children [ 0 ] ) ,
101
+ [ ] ,
102
+ 'should return `[node]` when given a `node` and existing (#4)'
103
+ )
104
+ t . deepEqual (
105
+ findAllBefore ( paragraph , 0 , children [ 0 ] ) ,
106
+ [ ] ,
107
+ 'should return `[node]` when given a `node` and existing (#5)'
108
+ )
109
+ t . deepEqual (
110
+ findAllBefore ( paragraph , 1 , children [ 1 ] ) ,
111
+ [ ] ,
112
+ 'should return `[node]` when given a `node` and existing (#6)'
113
+ )
114
+
115
+ t . deepEqual (
116
+ findAllBefore ( paragraph , 100 , 'strong' ) ,
117
+ [ children [ 3 ] ] ,
118
+ 'should return children when given a `type` and existing (#1)'
119
+ )
120
+ t . deepEqual (
121
+ findAllBefore ( paragraph , 3 , 'strong' ) ,
122
+ [ ] ,
123
+ 'should return children when given a `type` and existing (#2)'
124
+ )
125
+ t . deepEqual (
126
+ findAllBefore ( paragraph , children [ 4 ] , 'strong' ) ,
127
+ [ children [ 3 ] ] ,
128
+ 'should return children when given a `type` and existing (#3)'
129
+ )
130
+ t . deepEqual (
131
+ findAllBefore ( paragraph , children [ 3 ] , 'strong' ) ,
132
+ [ ] ,
133
+ 'should return children when given a `type` and existing (#4)'
134
+ )
135
+
136
+ var result = children . slice ( 4 ) . reverse ( )
137
+
138
+ t . deepEqual (
139
+ findAllBefore ( paragraph , 100 , test ) ,
140
+ result ,
141
+ 'should return children when given a `test` and existing (#1)'
142
+ )
143
+ t . deepEqual (
144
+ findAllBefore ( paragraph , 3 , test ) ,
145
+ [ ] ,
146
+ 'should return children when given a `test` and existing (#2)'
147
+ )
148
+ t . deepEqual (
149
+ findAllBefore ( paragraph , children [ 4 ] , test ) ,
150
+ [ ] ,
151
+ 'should return children when given a `test` and existing (#3)'
152
+ )
153
+ t . deepEqual (
154
+ findAllBefore ( paragraph , children [ 3 ] , test ) ,
155
+ [ ] ,
156
+ 'should return children when given a `test` and existing (#4)'
157
+ )
158
+
159
+ function test ( node , n ) {
160
+ return n > 3
161
+ }
108
162
109
163
t . end ( )
110
164
} )
0 commit comments