Skip to content
Myr

split-transform.js

This file

Path

test/transform/split-transform.js

83 lines · 4.1 KiB · JavaScript

Last changed by

Upgrade xo (#980)ehmickybefore adoptionccfa412

History of this file

Take it

Rename files (#977)ehmickybefore adoption4b2e316

lines 1–2why, one step back
1import test from 'ava';
2import {execa, execaSync} from '../../index.js';

Upgrade xo (#980)ehmickybefore adoptionccfa412

line 3why, one step back
3import {setFixtureDirectory} from '../helpers/fixtures-directory.js';

Rename files (#977)ehmickybefore adoption4b2e316

line 4why, one step back
4import {getOutputsGenerator, resultGenerator} from '../helpers/generator.js';

Upgrade xo (#980)ehmickybefore adoptionccfa412

lines 5–10why, one step back
5import {
6 foobarString,7 foobarUint8Array,8 foobarObject,9 foobarObjectString,10} from '../helpers/input.js';

Rename files (#977)ehmickybefore adoption4b2e316

line 11why, one step back
11

Upgrade xo (#980)ehmickybefore adoptionccfa412

line 12why, one step back
12setFixtureDirectory();

Rename files (#977)ehmickybefore adoption4b2e316

lines 13–83why, one step back
13
14const resultUint8ArrayGenerator = function * (lines, chunk) {15 lines.push(chunk);16 yield new TextEncoder().encode(chunk);17};1819// eslint-disable-next-line max-params20const testStringToUint8Array = async (t, expectedOutput, objectMode, preserveNewlines, execaMethod) => {21 const lines = [];22 const {stdout} = await execaMethod('noop-fd.js', ['1', foobarString], {23 stdout: {24 transform: resultUint8ArrayGenerator.bind(undefined, lines),25 objectMode,26 preserveNewlines,27 },28 lines: true,29 });30 t.deepEqual(lines, [foobarString]);31 t.deepEqual(stdout, expectedOutput);32};3334test('Line splitting when converting from string to Uint8Array', testStringToUint8Array, [foobarString], false, true, execa);35test('Line splitting when converting from string to Uint8Array, objectMode', testStringToUint8Array, [foobarUint8Array], true, true, execa);36test('Line splitting when converting from string to Uint8Array, preserveNewlines', testStringToUint8Array, [foobarString], false, false, execa);37test('Line splitting when converting from string to Uint8Array, objectMode, preserveNewlines', testStringToUint8Array, [foobarUint8Array], true, false, execa);38test('Line splitting when converting from string to Uint8Array, sync', testStringToUint8Array, [foobarString], false, true, execaSync);39test('Line splitting when converting from string to Uint8Array, objectMode, sync', testStringToUint8Array, [foobarUint8Array], true, true, execaSync);40test('Line splitting when converting from string to Uint8Array, preserveNewlines, sync', testStringToUint8Array, [foobarString], false, false, execaSync);41test('Line splitting when converting from string to Uint8Array, objectMode, preserveNewlines, sync', testStringToUint8Array, [foobarUint8Array], true, false, execaSync);4243const serializeResultGenerator = function * (lines, chunk) {44 lines.push(chunk);45 yield JSON.stringify(chunk);46};4748const testUnsetObjectMode = async (t, expectedOutput, preserveNewlines, execaMethod) => {49 const lines = [];50 const {stdout} = await execaMethod('noop.js', {51 stdout: [52 getOutputsGenerator([foobarObject])(true),53 {transform: serializeResultGenerator.bind(undefined, lines), preserveNewlines, objectMode: false},54 ],55 stripFinalNewline: false,56 });57 t.deepEqual(lines, [foobarObject]);58 t.is(stdout, expectedOutput);59};6061test('Can switch from objectMode to non-objectMode', testUnsetObjectMode, `${foobarObjectString}\n`, false, execa);62test('Can switch from objectMode to non-objectMode, preserveNewlines', testUnsetObjectMode, foobarObjectString, true, execa);63test('Can switch from objectMode to non-objectMode, sync', testUnsetObjectMode, `${foobarObjectString}\n`, false, execaSync);64test('Can switch from objectMode to non-objectMode, preserveNewlines, sync', testUnsetObjectMode, foobarObjectString, true, execaSync);6566// eslint-disable-next-line max-params67const testYieldArray = async (t, input, expectedLines, expectedOutput, execaMethod) => {68 const lines = [];69 const {stdout} = await execaMethod('noop.js', {70 stdout: [71 getOutputsGenerator(input)(),72 resultGenerator(lines)(),73 ],74 stripFinalNewline: false,75 });76 t.deepEqual(lines, expectedLines);77 t.deepEqual(stdout, expectedOutput);78};7980test('Can use "yield* array" to produce multiple lines', testYieldArray, [foobarString, foobarString], [foobarString, foobarString], `${foobarString}\n${foobarString}\n`, execa);81test('Can use "yield* array" to produce empty lines', testYieldArray, [foobarString, ''], [foobarString, ''], `${foobarString}\n\n`, execa);82test('Can use "yield* array" to produce multiple lines, sync', testYieldArray, [foobarString, foobarString], [foobarString, foobarString], `${foobarString}\n${foobarString}\n`, execaSync);83test('Can use "yield* array" to produce empty lines, sync', testYieldArray, [foobarString, ''], [foobarString, ''], `${foobarString}\n\n`, execaSync);