Skip to content
Myr

split-transform.js

This file

Path

test/transform/split-transform.js

78 lines · 4.1 KiB · JavaScript

Last changed by

Rename files (#977)ehmickybefore adoption4b2e316

History of this file

Take it

Rename files (#977)ehmickybefore adoption4b2e316

lines 1–78why, one step back
1import test from 'ava';
2import {execa, execaSync} from '../../index.js';3import {setFixtureDir} from '../helpers/fixtures-dir.js';4import {getOutputsGenerator, resultGenerator} from '../helpers/generator.js';5import {foobarString, foobarUint8Array, foobarObject, foobarObjectString} from '../helpers/input.js';67setFixtureDir();89const resultUint8ArrayGenerator = function * (lines, chunk) {10 lines.push(chunk);11 yield new TextEncoder().encode(chunk);12};1314// eslint-disable-next-line max-params15const testStringToUint8Array = async (t, expectedOutput, objectMode, preserveNewlines, execaMethod) => {16 const lines = [];17 const {stdout} = await execaMethod('noop-fd.js', ['1', foobarString], {18 stdout: {19 transform: resultUint8ArrayGenerator.bind(undefined, lines),20 objectMode,21 preserveNewlines,22 },23 lines: true,24 });25 t.deepEqual(lines, [foobarString]);26 t.deepEqual(stdout, expectedOutput);27};2829test('Line splitting when converting from string to Uint8Array', testStringToUint8Array, [foobarString], false, true, execa);30test('Line splitting when converting from string to Uint8Array, objectMode', testStringToUint8Array, [foobarUint8Array], true, true, execa);31test('Line splitting when converting from string to Uint8Array, preserveNewlines', testStringToUint8Array, [foobarString], false, false, execa);32test('Line splitting when converting from string to Uint8Array, objectMode, preserveNewlines', testStringToUint8Array, [foobarUint8Array], true, false, execa);33test('Line splitting when converting from string to Uint8Array, sync', testStringToUint8Array, [foobarString], false, true, execaSync);34test('Line splitting when converting from string to Uint8Array, objectMode, sync', testStringToUint8Array, [foobarUint8Array], true, true, execaSync);35test('Line splitting when converting from string to Uint8Array, preserveNewlines, sync', testStringToUint8Array, [foobarString], false, false, execaSync);36test('Line splitting when converting from string to Uint8Array, objectMode, preserveNewlines, sync', testStringToUint8Array, [foobarUint8Array], true, false, execaSync);3738const serializeResultGenerator = function * (lines, chunk) {39 lines.push(chunk);40 yield JSON.stringify(chunk);41};4243const testUnsetObjectMode = async (t, expectedOutput, preserveNewlines, execaMethod) => {44 const lines = [];45 const {stdout} = await execaMethod('noop.js', {46 stdout: [47 getOutputsGenerator([foobarObject])(true),48 {transform: serializeResultGenerator.bind(undefined, lines), preserveNewlines, objectMode: false},49 ],50 stripFinalNewline: false,51 });52 t.deepEqual(lines, [foobarObject]);53 t.is(stdout, expectedOutput);54};5556test('Can switch from objectMode to non-objectMode', testUnsetObjectMode, `${foobarObjectString}\n`, false, execa);57test('Can switch from objectMode to non-objectMode, preserveNewlines', testUnsetObjectMode, foobarObjectString, true, execa);58test('Can switch from objectMode to non-objectMode, sync', testUnsetObjectMode, `${foobarObjectString}\n`, false, execaSync);59test('Can switch from objectMode to non-objectMode, preserveNewlines, sync', testUnsetObjectMode, foobarObjectString, true, execaSync);6061// eslint-disable-next-line max-params62const testYieldArray = async (t, input, expectedLines, expectedOutput, execaMethod) => {63 const lines = [];64 const {stdout} = await execaMethod('noop.js', {65 stdout: [66 getOutputsGenerator(input)(),67 resultGenerator(lines)(),68 ],69 stripFinalNewline: false,70 });71 t.deepEqual(lines, expectedLines);72 t.deepEqual(stdout, expectedOutput);73};7475test('Can use "yield* array" to produce multiple lines', testYieldArray, [foobarString, foobarString], [foobarString, foobarString], `${foobarString}\n${foobarString}\n`, execa);76test('Can use "yield* array" to produce empty lines', testYieldArray, [foobarString, ''], [foobarString, ''], `${foobarString}\n\n`, execa);77test('Can use "yield* array" to produce multiple lines, sync', testYieldArray, [foobarString, foobarString], [foobarString, foobarString], `${foobarString}\n${foobarString}\n`, execaSync);78test('Can use "yield* array" to produce empty lines, sync', testYieldArray, [foobarString, ''], [foobarString, ''], `${foobarString}\n\n`, execaSync);