11chai = require ' chai'
22sinonChai = require ' sinon-chai'
33{match : {any , instanceOf }} = require ' sinon'
4- {File : {isVinyl }} = require ' gulp-util'
4+ {File : {isVinyl }, PluginError } = require ' gulp-util'
55{wait } = require ' event-stream'
66{buffer , string } = require ' ./fixtures/content'
77{buffered , streaming } = require ' ./fixtures/file'
8- {bufferFn , stringFn } = require ' ./fixtures/fn'
8+ {bufferFn , stringFn , asyncFn } = require ' ./fixtures/fn'
99err = require ' ./helpers/err'
1010transform = require ' ../src'
1111
@@ -30,10 +30,14 @@ describe 'plugin: gulp-transform', ->
3030 it ' throws PluginError' , ->
3131 err -> transform 42
3232
33- context ' returns neither a string nor a Buffer ' , ->
33+ context ' returns null or undefined ' , ->
3434
35- it ' throws PluginError' , ->
36- err -> transform ((content ) -> 42 ).write buffered ()
35+ it ' emits PluginError' , (done ) ->
36+ stream = transform ((content ) -> null )
37+ stream .write buffered ()
38+ stream .on ' error' , (err ) ->
39+ err .should .be .instanceOf PluginError
40+ done ()
3741
3842 context ' returns a Buffer or string' , ->
3943 [fn , file ] = [null , null ]
@@ -52,6 +56,23 @@ describe 'plugin: gulp-transform', ->
5256 it ' is called with vinyl File as second argument' , ->
5357 fn .should .have .been .calledWith any, file
5458
59+ context ' returns a Promise that resolves to a string or Buffer' , ->
60+ [fn , file ] = [null , null ]
61+
62+ beforeEach ->
63+ file = buffered ()
64+ fn = asyncFn ()
65+ transform (fn, {encoding : ' utf8' }).write (file)
66+
67+ it ' is called once per file' , ->
68+ fn .should .have .been .calledOnce
69+
70+ it ' is called with contents as first argument' , ->
71+ fn .should .have .been .calledWith string
72+
73+ it ' is called with vinyl File as second argument' , ->
74+ fn .should .have .been .calledWith any, file
75+
5576 describe ' param: options' , ->
5677
5778 context ' not an object' , ->
@@ -80,40 +101,83 @@ describe 'plugin: gulp-transform', ->
80101 fn .should .have .been .calledOn undefined
81102
82103 describe ' mode: buffer' , ->
83- file = null
84104
85- beforeEach (done ) ->
86- transform (bufferFn ()).once (' data' , (_file ) ->
87- file = _file
88- done ()
89- ).write buffered ()
105+ context ' synchronous' , ->
106+ file = null
107+
108+ beforeEach (done ) ->
109+ transform (bufferFn ()).once (' data' , (_file ) ->
110+ file = _file
111+ done ()
112+ ).write buffered ()
113+
114+ it ' returns a stream of vinyl Files' , ->
115+ isVinyl (file).should .be .true
116+
117+ it ' files are in buffer mode' , ->
118+ file .isBuffer ().should .be .true ;
90119
91- it ' returns a stream of vinyl Files ' , ->
92- isVinyl ( file). should .be . true
120+ it ' transforms file contents ' , ->
121+ file . contents . should .deep . equal Buffer . concat ([buffer, buffer])
93122
94- it ' files are in buffer mode ' , ->
95- file . isBuffer (). should . be . true ;
123+ context ' async ' , ->
124+ file = null
96125
97- it ' transforms file contents' , ->
98- file .contents .should .deep .equal Buffer .concat ([buffer, buffer])
126+ beforeEach (done ) ->
127+ transform (asyncFn (), {encoding : ' utf8' }).once (' data' , (_file ) ->
128+ file = _file
129+ done ()
130+ ).write buffered ()
131+
132+ it ' returns a stream of vinyl Files' , ->
133+ isVinyl (file).should .be .true
134+
135+ it ' files are in buffer mode' , ->
136+ file .isBuffer ().should .be .true ;
137+
138+ it ' transforms file contents' , ->
139+ file .contents .should .deep .equal new Buffer (' un deux trois' )
99140
100141 describe ' mode: streaming' , ->
101- file = null
102142
103- beforeEach (done ) ->
104- transform (stringFn (), {encoding : ' utf8' }).once (' data' , (_file ) ->
105- file = _file
106- done ()
107- ).write streaming ()
143+ context ' synchronous' , ->
144+ file = null
108145
109- it ' returns a stream of vinyl Files' , ->
110- isVinyl (file).should .be .true
146+ beforeEach (done ) ->
147+ transform (stringFn (), {encoding : ' utf8' }).once (' data' , (_file ) ->
148+ file = _file
149+ done ()
150+ ).write streaming ()
111151
112- it ' files are in streaming mode ' , ->
113- file . isStream ( ).should .be .true
152+ it ' returns a stream of vinyl Files ' , ->
153+ isVinyl (file ).should .be .true
114154
115- it ' transforms file contents' , (done ) ->
116- file .pipe (wait ((err , data ) ->
117- data .should .deep .equal new Buffer (' un deux trois' )
118- done ()
119- ))
155+ it ' files are in streaming mode' , ->
156+ file .isStream ().should .be .true
157+
158+ it ' transforms file contents' , (done ) ->
159+ file .pipe (wait ((err , data ) ->
160+ data .should .deep .equal new Buffer (' un deux trois' )
161+ done ()
162+ ))
163+
164+ context ' async' , ->
165+ file = null
166+
167+ beforeEach (done ) ->
168+ transform (asyncFn (), {encoding : ' utf8' }).once (' data' , (_file ) ->
169+ file = _file
170+ done ()
171+ ).write streaming ()
172+
173+ it ' returns a stream of vinyl Files' , ->
174+ isVinyl (file).should .be .true
175+
176+ it ' files are in streaming mode' , ->
177+ file .isStream ().should .be .true
178+
179+ it ' transforms file contents' , (done ) ->
180+ file .pipe (wait ((err , data ) ->
181+ data .should .deep .equal new Buffer (' un deux trois' )
182+ done ()
183+ ))
0 commit comments