@@ -61,84 +61,56 @@ const unsupportedEncoding = test.macro({
61
61
} ,
62
62
) ;
63
63
} ,
64
- title ( option ) {
65
- return `throws on unsupported ${ option } encoding` ;
64
+ title ( providedTitle ) {
65
+ return `throws on unsupported ${ providedTitle } encoding` ;
66
66
} ,
67
67
} ) ;
68
68
69
69
for ( let option of [ 'from' , 'to' ] ) {
70
70
test ( option , unsupportedEncoding , option , 'ahfkjdsahfk' ) ;
71
71
}
72
72
73
- test ( 'ignores null files' , async ( t ) => {
74
- const stream = convertEncoding ( { to : LATIN1 } ) ;
75
- const promise = pEvent ( stream , 'data' ) ;
76
-
77
- stream . end ( createFile ( { contents : null } ) ) ;
78
-
79
- const file = await promise ;
80
- const actual = file . contents ;
81
- const expected = null ;
82
-
83
- t . is ( actual , expected ) ;
84
- } ) ;
85
-
86
- test ( `converts a buffer from ${ UTF8 } to ${ LATIN1 } ` , async ( t ) => {
87
- const { to, iconvLiteTo, vinylFile, expected } = setUpFromUTF8Buffer ( ) ;
88
-
89
- const stream = convertEncoding ( { to : iconvLiteTo } ) ;
90
- const promise = pEvent ( stream , 'data' ) ;
91
-
92
- stream . end ( vinylFile ) ;
93
-
94
- const file = await promise ;
95
- const contentsBuffer = Buffer . from ( file . contents , to ) ;
96
- const actual = contentsBuffer . toString ( to ) ;
97
-
98
- t . is ( actual , expected ) ;
99
- } ) ;
73
+ const convert = test . macro ( {
74
+ async exec ( t , input , expected ) {
75
+ const { isStream = false , pluginOptions, to, vinylFile } = input ;
100
76
101
- test ( `converts a buffer from ${ LATIN1 } to ${ UTF8 } ` , async ( t ) => {
102
- const { to , iconvLiteFrom , vinylFile , expected } = setUpFromLatin1Buffer ( ) ;
77
+ const stream = convertEncoding ( pluginOptions ) ;
78
+ const promise = pEvent ( stream , 'data' ) ;
103
79
104
- const stream = convertEncoding ( { from : iconvLiteFrom } ) ;
105
- const promise = pEvent ( stream , 'data' ) ;
106
-
107
- stream . end ( vinylFile ) ;
108
-
109
- const file = await promise ;
110
- const contentsBuffer = Buffer . from ( file . contents , to ) ;
111
- const actual = contentsBuffer . toString ( to ) ;
80
+ stream . end ( vinylFile ) ;
112
81
113
- t . is ( actual , expected ) ;
114
- } ) ;
82
+ const file = await promise ;
83
+ const contentsBuffer = isStream
84
+ ? await buffer ( file . contents )
85
+ : Buffer . from ( file . contents , to ) ;
86
+ const actual = contentsBuffer . toString ( to ) ;
115
87
116
- test ( `converts a stream from ${ UTF8 } to ${ LATIN1 } ` , async ( t ) => {
117
- const { to, iconvLiteTo, vinylFile, expected } = setUpFromUTF8Stream ( ) ;
118
-
119
- const stream = convertEncoding ( { to : iconvLiteTo } ) ;
120
- const promise = pEvent ( stream , 'data' ) ;
121
-
122
- stream . end ( vinylFile ) ;
123
-
124
- const file = await promise ;
125
- const contentsBuffer = await buffer ( file . contents ) ;
126
- const actual = contentsBuffer . toString ( to ) ;
127
-
128
- t . is ( actual , expected ) ;
88
+ t . is ( actual , expected ) ;
89
+ } ,
90
+ title ( _ , { isStream, from, to } ) {
91
+ return `converts a ${ isStream ? 'stream' : 'buffer' } from ${ from } to ${ to } ` ;
92
+ } ,
129
93
} ) ;
130
94
131
- test ( `converts a stream from ${ LATIN1 } to ${ UTF8 } ` , async ( t ) => {
132
- const { to, iconvLiteFrom, vinylFile, expected } = setUpFromLatin1Stream ( ) ;
95
+ const conversionTestCases = [
96
+ { ...setUpFromUTF8Buffer ( ) } ,
97
+ { ...setUpFromLatin1Buffer ( ) } ,
98
+ { ...setUpFromUTF8Stream ( ) } ,
99
+ { ...setUpFromLatin1Stream ( ) } ,
100
+ ] ;
101
+ for ( let testCase of conversionTestCases ) {
102
+ test ( convert , testCase , testCase . expected ) ;
103
+ }
133
104
134
- const stream = convertEncoding ( { from : iconvLiteFrom } ) ;
105
+ test ( 'ignores null files' , async ( t ) => {
106
+ const stream = convertEncoding ( { to : LATIN1 } ) ;
135
107
const promise = pEvent ( stream , 'data' ) ;
136
108
137
- stream . end ( vinylFile ) ;
109
+ stream . end ( createFile ( { contents : null } ) ) ;
138
110
139
111
const file = await promise ;
140
- const contentsBuffer = await buffer ( file . contents ) ;
141
- const actual = contentsBuffer . toString ( to ) ;
112
+ const actual = file . contents ;
113
+ const expected = null ;
142
114
143
115
t . is ( actual , expected ) ;
144
116
} ) ;
0 commit comments