@@ -20,86 +20,78 @@ describe('gulp-replace', function() {
20
20
} ) ;
21
21
22
22
describe ( 'buffered input' , function ( ) {
23
- var file ;
23
+ var file , check ;
24
24
25
25
beforeEach ( function ( ) {
26
26
file = new File ( {
27
27
path : 'test/fixtures/helloworld.txt' ,
28
28
contents : fs . readFileSync ( 'test/fixtures/helloworld.txt' )
29
29
} ) ;
30
+
31
+ check = function ( stream , done , cb ) {
32
+ stream . on ( 'data' , function ( newFile ) {
33
+ cb ( newFile ) ;
34
+ done ( ) ;
35
+ } ) ;
36
+
37
+ stream . write ( file ) ;
38
+ stream . end ( ) ;
39
+ } ;
30
40
} ) ;
31
41
32
42
it ( 'should replace string on a buffer' , function ( done ) {
33
43
var stream = replacePlugin ( 'world' , 'person' ) ;
34
- stream . on ( 'data' , function ( newFile ) {
44
+
45
+ check ( stream , done , function ( newFile ) {
35
46
String ( newFile . contents ) . should . equal ( fs . readFileSync ( 'test/expected/helloworld.txt' , 'utf8' ) ) ;
36
- done ( ) ;
37
47
} ) ;
38
-
39
- stream . write ( file ) ;
40
- stream . end ( ) ;
41
48
} ) ;
42
49
43
50
it ( 'should replace regex on a buffer' , function ( done ) {
44
51
var stream = replacePlugin ( / w o r l d / g, 'person' ) ;
45
- stream . on ( 'data' , function ( newFile ) {
52
+
53
+ check ( stream , done , function ( newFile ) {
46
54
String ( newFile . contents ) . should . equal ( fs . readFileSync ( 'test/expected/helloworld.txt' , 'utf8' ) ) ;
47
- done ( ) ;
48
55
} ) ;
49
-
50
- stream . write ( file ) ;
51
- stream . end ( ) ;
52
56
} ) ;
53
57
54
58
it ( 'should replace regex on a buffer with a function' , function ( done ) {
55
59
var stream = replacePlugin ( / w o r l d / g, function ( ) { return 'person' ; } ) ;
56
- stream . on ( 'data' , function ( newFile ) {
60
+
61
+ check ( stream , done , function ( newFile ) {
57
62
String ( newFile . contents ) . should . equal ( fs . readFileSync ( 'test/expected/helloworld.txt' , 'utf8' ) ) ;
58
- done ( ) ;
59
63
} ) ;
60
-
61
- stream . write ( file ) ;
62
- stream . end ( ) ;
63
64
} ) ;
64
65
65
66
it ( 'should replace string on a buffer with a function' , function ( done ) {
66
67
var stream = replacePlugin ( 'world' , function ( ) { return 'person' ; } ) ;
67
- stream . on ( 'data' , function ( newFile ) {
68
+
69
+ check ( stream , done , function ( newFile ) {
68
70
String ( newFile . contents ) . should . equal ( fs . readFileSync ( 'test/expected/helloworld.txt' , 'utf8' ) ) ;
69
- done ( ) ;
70
71
} ) ;
71
-
72
- stream . write ( file ) ;
73
- stream . end ( ) ;
74
72
} ) ;
75
73
76
74
77
75
it ( 'should call function once for each replacement when replacing a string on a buffer' , function ( done ) {
78
76
var stream = replacePlugin ( 'world' , function ( ) { return replacements . shift ( ) ; } ) ;
79
- stream . on ( 'data' , function ( newFile ) {
77
+ check ( stream , done , function ( newFile ) {
78
+
80
79
String ( newFile . contents ) . should . equal ( fs . readFileSync ( 'test/expected/hellofarm.txt' , 'utf8' ) ) ;
81
- done ( ) ;
82
80
} ) ;
83
-
84
- stream . write ( file ) ;
85
- stream . end ( ) ;
86
81
} ) ;
87
82
88
83
89
84
it ( 'should call function once for each replacement when replacing a regex on a buffer' , function ( done ) {
90
85
var stream = replacePlugin ( / w o r l d / g, function ( ) { return replacements . shift ( ) ; } ) ;
91
- stream . on ( 'data' , function ( newFile ) {
86
+
87
+ check ( stream , done , function ( newFile ) {
92
88
String ( newFile . contents ) . should . equal ( fs . readFileSync ( 'test/expected/hellofarm.txt' , 'utf8' ) ) ;
93
- done ( ) ;
94
89
} ) ;
95
-
96
- stream . write ( file ) ;
97
- stream . end ( ) ;
98
90
} ) ;
99
91
100
92
it ( 'should trigger events on a buffer' , function ( done ) {
101
93
var stream = replacePlugin ( 'world' , 'elephant' )
102
- . on ( 'finish' , function ( ) {
94
+ stream . on ( 'finish' , function ( ) {
103
95
// No assertion required, we should end up here, if we don't the test will time out
104
96
done ( ) ;
105
97
} ) ;
@@ -110,91 +102,67 @@ describe('gulp-replace', function() {
110
102
} ) ;
111
103
112
104
describe ( 'streamed input' , function ( ) {
113
- var file ;
105
+ var file , check ;
114
106
115
107
beforeEach ( function ( ) {
116
108
file = new File ( {
117
- path : 'test/fixtures/helloworld.txt' ,
118
- contents : fs . createReadStream ( 'test/fixtures/helloworld.txt' )
109
+ path : 'test/fixtures/helloworld.txt' ,
110
+ contents : fs . createReadStream ( 'test/fixtures/helloworld.txt' )
111
+ } ) ;
112
+
113
+ check = function ( stream , done , cb ) {
114
+ stream . on ( 'data' , function ( newFile ) {
115
+ newFile . contents . pipe ( concatStream ( { encoding : 'string' } , function ( data ) {
116
+ cb ( data ) ;
117
+ done ( ) ;
118
+ } ) ) ;
119
119
} ) ;
120
+
121
+ stream . write ( file ) ;
122
+ stream . end ( ) ;
123
+ } ;
120
124
} ) ;
121
125
122
126
it ( 'should replace string on a stream' , function ( done ) {
123
127
var stream = replacePlugin ( 'world' , 'person' ) ;
124
- stream . on ( 'data' , function ( newFile ) {
125
- newFile . contents . pipe ( concatStream ( { encoding : 'string' } , function ( data ) {
126
- data . should . equal ( fs . readFileSync ( 'test/expected/helloworld.txt' , 'utf8' ) ) ;
127
- done ( ) ;
128
- } ) ) ;
128
+ check ( stream , done , function ( data ) {
129
+ data . should . equal ( fs . readFileSync ( 'test/expected/helloworld.txt' , 'utf8' ) ) ;
129
130
} ) ;
130
-
131
- stream . write ( file ) ;
132
- stream . end ( ) ;
133
131
} ) ;
134
132
135
133
it ( 'should replace regex on a stream' , function ( done ) {
136
134
var stream = replacePlugin ( / w o r l d / g, 'person' ) ;
137
- stream . on ( 'data' , function ( newFile ) {
138
- newFile . contents . pipe ( concatStream ( { encoding : 'string' } , function ( data ) {
139
- data . should . equal ( fs . readFileSync ( 'test/expected/helloworld.txt' , 'utf8' ) ) ;
140
- done ( ) ;
141
- } ) ) ;
135
+ check ( stream , done , function ( data ) {
136
+ data . should . equal ( fs . readFileSync ( 'test/expected/helloworld.txt' , 'utf8' ) ) ;
142
137
} ) ;
143
-
144
- stream . write ( file ) ;
145
- stream . end ( ) ;
146
138
} ) ;
147
139
148
140
it ( 'should replace regex on a stream with a function' , function ( done ) {
149
141
var stream = replacePlugin ( / w o r l d / g, function ( ) { return 'person' ; } ) ;
150
- stream . on ( 'data' , function ( newFile ) {
151
- newFile . contents . pipe ( concatStream ( { encoding : 'string' } , function ( data ) {
152
- data . should . equal ( fs . readFileSync ( 'test/expected/helloworld.txt' , 'utf8' ) ) ;
153
- done ( ) ;
154
- } ) ) ;
142
+ check ( stream , done , function ( data ) {
143
+ data . should . equal ( fs . readFileSync ( 'test/expected/helloworld.txt' , 'utf8' ) ) ;
155
144
} ) ;
156
-
157
- stream . write ( file ) ;
158
- stream . end ( ) ;
159
145
} ) ;
160
146
161
147
it ( 'should replace string on a stream with a function' , function ( done ) {
162
148
var stream = replacePlugin ( 'world' , function ( ) { return 'person' ; } ) ;
163
- stream . on ( 'data' , function ( newFile ) {
164
- newFile . contents . pipe ( concatStream ( { encoding : 'string' } , function ( data ) {
165
- data . should . equal ( fs . readFileSync ( 'test/expected/helloworld.txt' , 'utf8' ) ) ;
166
- done ( ) ;
167
- } ) ) ;
149
+ check ( stream , done , function ( data ) {
150
+ data . should . equal ( fs . readFileSync ( 'test/expected/helloworld.txt' , 'utf8' ) ) ;
168
151
} ) ;
169
-
170
- stream . write ( file ) ;
171
- stream . end ( ) ;
172
152
} ) ;
173
153
174
154
it ( 'should call function once for each replacement when replacing a string on a stream' , function ( done ) {
175
155
var stream = replacePlugin ( 'world' , function ( ) { return replacements . shift ( ) ; } ) ;
176
- stream . on ( 'data' , function ( newFile ) {
177
- newFile . contents . pipe ( concatStream ( { encoding : 'string' } , function ( data ) {
178
- data . should . equal ( fs . readFileSync ( 'test/expected/hellofarm.txt' , 'utf8' ) ) ;
179
- done ( ) ;
180
- } ) ) ;
156
+ check ( stream , done , function ( data ) {
157
+ data . should . equal ( fs . readFileSync ( 'test/expected/hellofarm.txt' , 'utf8' ) ) ;
181
158
} ) ;
182
-
183
- stream . write ( file ) ;
184
- stream . end ( ) ;
185
159
} ) ;
186
160
187
161
it ( 'should call function once for each replacement when replacing a regex on a stream' , function ( done ) {
188
162
var stream = replacePlugin ( / w o r l d / g, function ( ) { return replacements . shift ( ) ; } ) ;
189
- stream . on ( 'data' , function ( newFile ) {
190
- newFile . contents . pipe ( concatStream ( { encoding : 'string' } , function ( data ) {
191
- data . should . equal ( fs . readFileSync ( 'test/expected/hellofarm.txt' , 'utf8' ) ) ;
192
- done ( ) ;
193
- } ) ) ;
163
+ check ( stream , done , function ( data ) {
164
+ data . should . equal ( fs . readFileSync ( 'test/expected/hellofarm.txt' , 'utf8' ) ) ;
194
165
} ) ;
195
-
196
- stream . write ( file ) ;
197
- stream . end ( ) ;
198
166
} ) ;
199
167
} ) ;
200
168
0 commit comments