Skip to content

Commit

Permalink
Add more tests related to the encoding option (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky authored Aug 7, 2023
1 parent 7eb6900 commit c960887
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 17 deletions.
1 change: 0 additions & 1 deletion fixture

This file was deleted.

45 changes: 29 additions & 16 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import fs from 'node:fs';
import {readFile} from 'node:fs/promises';
import {Buffer} from 'node:buffer';
import {setTimeout} from 'node:timers/promises';
import {compose} from 'node:stream';
import {text, buffer} from 'node:stream/consumers';
import test from 'ava';
import getStream, {getStreamAsBuffer, MaxBufferError} from './index.js';

const fixtureString = await readFile('fixture', 'utf8');
const fixtureString = 'unicorn\n';
const fixtureBuffer = Buffer.from(fixtureString);
const fixtureHex = fixtureBuffer.toString('hex');
const fixtureBase64Url = fixtureBuffer.toString('base64url');

const shortString = 'abc';
const longString = `${shortString}d`;
Expand All @@ -18,15 +17,34 @@ const maxBuffer = shortString.length;
const setup = (streamDef, options) => getStream(compose(streamDef), options);
const setupBuffer = (streamDef, options) => getStreamAsBuffer(compose(streamDef), options);

test('get stream', async t => {
const result = await getStream(fs.createReadStream('fixture'));
const getStreamToUtf8 = async (t, inputStream) => {
const result = await setup(inputStream);
t.is(result, fixtureString);
});
};

test('get stream as a buffer', async t => {
const result = await getStreamAsBuffer(fs.createReadStream('fixture'));
const getStreamToBuffer = async (t, inputStream) => {
const result = await setupBuffer(inputStream);
t.true(result.equals(fixtureBuffer));
});
};

const getStreamToHex = async (t, inputStream) => {
const result = await setup(inputStream, {encoding: 'hex'});
t.is(result, fixtureHex);
};

const getStreamToBase64Url = async (t, inputStream) => {
const result = await setup(inputStream, {encoding: 'base64Url'});
t.is(result, fixtureBase64Url);
};

test('get stream from buffer to utf8', getStreamToUtf8, fixtureBuffer);
test('get stream from buffer to buffer', getStreamToBuffer, fixtureBuffer);
test('get stream from buffer to hex', getStreamToHex, fixtureBuffer);
test('get stream from buffer to base64url', getStreamToBase64Url, fixtureBuffer);
test('get stream from utf8 to utf8', getStreamToUtf8, fixtureString);
test('get stream from utf8 to buffer', getStreamToBuffer, fixtureString);
test('get stream from utf8 to hex', getStreamToHex, fixtureString);
test('get stream from utf8 to base64url', getStreamToBase64Url, fixtureString);

test('getStream should not affect additional listeners attached to the stream', async t => {
t.plan(3);
Expand Down Expand Up @@ -70,17 +88,12 @@ test('handles infinite stream', async t => {
await t.throwsAsync(setup(infiniteIteration(), {maxBuffer: 1}), {instanceOf: MaxBufferError});
});

test('`encoding` option sets the encoding', async t => {
const result = await getStream(fs.createReadStream('fixture'), {encoding: 'hex'});
t.is(result, fixtureHex);
});

test('native string', async t => {
const result = await text(fs.createReadStream('fixture', {encoding: 'utf8'}));
const result = await text(compose(fixtureString));
t.is(result, fixtureString);
});

test('native buffer', async t => {
const result = await buffer(fs.createReadStream('fixture', {encoding: 'utf8'}));
const result = await buffer(compose(fixtureString));
t.true(result.equals(fixtureBuffer));
});

0 comments on commit c960887

Please sign in to comment.