Skip to content

Commit aed4632

Browse files
committed
streamline tests
1 parent f4e557f commit aed4632

File tree

2 files changed

+36
-66
lines changed

2 files changed

+36
-66
lines changed

test/_helper.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,13 @@ export function createFile({ contents }) {
1818

1919
const setUp = ({ isFromLatin1 = false, isStream = false } = {}) => {
2020
let from = UTF8;
21-
let iconvLiteFrom = UTF8;
2221
let to = LATIN1;
23-
let iconvLiteTo = LATIN1_ISO;
22+
let pluginOptions = { to: LATIN1_ISO };
2423

2524
if (isFromLatin1) {
2625
from = LATIN1;
27-
iconvLiteFrom = LATIN1_ISO;
2826
to = UTF8;
29-
iconvLiteTo = UTF8;
27+
pluginOptions = { from: LATIN1_ISO };
3028
}
3129

3230
const defaultBuffer = Buffer.from(testString);
@@ -41,10 +39,10 @@ const setUp = ({ isFromLatin1 = false, isStream = false } = {}) => {
4139
return {
4240
from,
4341
to,
44-
iconvLiteFrom,
45-
iconvLiteTo,
42+
pluginOptions,
4643
vinylFile: createFile({ contents: fileContents }),
4744
expected: testString,
45+
isStream,
4846
};
4947
};
5048

test/test.js

Lines changed: 32 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -61,84 +61,56 @@ const unsupportedEncoding = test.macro({
6161
},
6262
);
6363
},
64-
title(option) {
65-
return `throws on unsupported ${option} encoding`;
64+
title(providedTitle) {
65+
return `throws on unsupported ${providedTitle} encoding`;
6666
},
6767
});
6868

6969
for (let option of ['from', 'to']) {
7070
test(option, unsupportedEncoding, option, 'ahfkjdsahfk');
7171
}
7272

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;
10076

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');
10379

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);
11281

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);
11587

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+
},
12993
});
13094

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+
}
133104

134-
const stream = convertEncoding({ from: iconvLiteFrom });
105+
test('ignores null files', async (t) => {
106+
const stream = convertEncoding({ to: LATIN1 });
135107
const promise = pEvent(stream, 'data');
136108

137-
stream.end(vinylFile);
109+
stream.end(createFile({ contents: null }));
138110

139111
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;
142114

143115
t.is(actual, expected);
144116
});

0 commit comments

Comments
 (0)