Explorar o código

Added tests for custom separators.

Eugene Lazutkin %!s(int64=3) %!d(string=hai) anos
pai
achega
75b5b4d002
Modificáronse 2 ficheiros con 65 adicións e 1 borrados
  1. 11 0
      tests/helpers.mjs
  2. 54 1
      tests/test-jsonl-stringerStream.mjs

+ 11 - 0
tests/helpers.mjs

@@ -28,6 +28,17 @@ export const readString = (string, quant) => new Readable({
   }
 });
 
+export const writeToArray = array => new Writable({
+  write(chunk, _, callback) {
+    if (typeof chunk == 'string') {
+      array.push(chunk);
+    } else {
+      array.push(chunk.toString('utf8'));
+    }
+    callback(null);
+  }
+});
+
 export const delay = (fn, ms = 20) => async (...args) =>
   new Promise((resolve, reject) => {
     setTimeout(() => {

+ 54 - 1
tests/test-jsonl-stringerStream.mjs

@@ -4,7 +4,7 @@ import test from 'tape-six';
 
 import {Writable, Transform} from 'stream';
 
-import {readString} from './helpers.mjs';
+import {readString, writeToArray} from './helpers.mjs';
 
 import parserStream from '../src/jsonl/parserStream.js';
 import stringerStream from '../src/jsonl/stringerStream.js';
@@ -100,3 +100,56 @@ test.asPromise('jsonl stringerStream: multiple', (t, resolve) => {
       })
     );
 });
+
+test.asPromise('jsonl stringerStream: custom separators - one value', (t, resolve) => {
+  const output = [],
+    stringer = stringerStream({emptyValue: '{}', prefix: '[', suffix: ']', separator: ','}),
+    pipeline = stringer.pipe(writeToArray(output));
+
+  pipeline.on('finish', () => {
+    t.equal(output.join(''), '[1]');
+    resolve();
+  });
+
+  stringer.end(1);
+});
+
+test.asPromise('jsonl stringerStream: custom separators - two value', (t, resolve) => {
+  const output = [],
+    stringer = stringerStream({emptyValue: '{}', prefix: '[', suffix: ']', separator: ','}),
+    pipeline = stringer.pipe(writeToArray(output));
+
+  pipeline.on('finish', () => {
+    t.equal(output.join(''), '[2,1]');
+    resolve();
+  });
+
+  stringer.write(2);
+  stringer.end(1);
+});
+
+test.asPromise('jsonl stringerStream: custom separators - no value', (t, resolve) => {
+  const output = [],
+    stringer = stringerStream({emptyValue: '{}', prefix: '[', suffix: ']', separator: ','}),
+    pipeline = stringer.pipe(writeToArray(output));
+
+  pipeline.on('finish', () => {
+    t.equal(output.join(''), '{}');
+    resolve();
+  });
+
+  stringer.end();
+});
+
+test.asPromise('jsonl stringerStream: custom separators - no value (default)', (t, resolve) => {
+  const output = [],
+    stringer = stringerStream({prefix: '[', suffix: ']', separator: ','}),
+    pipeline = stringer.pipe(writeToArray(output));
+
+  pipeline.on('finish', () => {
+    t.equal(output.join(''), '[]');
+    resolve();
+  });
+
+  stringer.end();
+});