test-jsonl-stringer.mjs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. 'use strict';
  2. import test from 'tape-six';
  3. import {Writable, Transform} from 'stream';
  4. import {readString} from './helpers.mjs';
  5. import parser from '../src/jsonl/parser.js';
  6. import stringer from '../src/jsonl/stringer.js';
  7. test.asPromise('jsonl stringer: smoke test', (t, resolve) => {
  8. const pattern = {
  9. a: [[[]]],
  10. b: {a: 1},
  11. c: {a: 1, b: 2},
  12. d: [true, 1, "'x\"y'", null, false, true, {}, [], ''],
  13. e: 1,
  14. f: '',
  15. g: true,
  16. h: false,
  17. i: null,
  18. j: [],
  19. k: {}
  20. },
  21. string = JSON.stringify(pattern);
  22. let buffer = '';
  23. readString(string)
  24. .pipe(parser())
  25. .pipe(
  26. new Transform({
  27. writableObjectMode: true,
  28. readableObjectMode: true,
  29. transform(chunk, _, callback) {
  30. this.push(chunk);
  31. callback(null);
  32. }
  33. })
  34. )
  35. .pipe(stringer())
  36. .pipe(
  37. new Writable({
  38. write(chunk, _, callback) {
  39. buffer += chunk;
  40. callback(null);
  41. },
  42. final(callback) {
  43. t.deepEqual(string, buffer);
  44. resolve();
  45. callback(null);
  46. }
  47. })
  48. );
  49. });
  50. test.asPromise('jsonl stringer: multiple', (t, resolve) => {
  51. const pattern = {
  52. a: [[[]]],
  53. b: {a: 1},
  54. c: {a: 1, b: 2},
  55. d: [true, 1, "'x\"y'", null, false, true, {}, [], ''],
  56. e: 1,
  57. f: '',
  58. g: true,
  59. h: false,
  60. i: null,
  61. j: [],
  62. k: {}
  63. };
  64. let string = JSON.stringify(pattern),
  65. buffer = '';
  66. string = string + '\n' + string + '\n' + string;
  67. readString(string + '\n')
  68. .pipe(parser())
  69. .pipe(
  70. new Transform({
  71. writableObjectMode: true,
  72. readableObjectMode: true,
  73. transform(chunk, _, callback) {
  74. this.push(chunk);
  75. callback(null);
  76. }
  77. })
  78. )
  79. .pipe(stringer())
  80. .pipe(
  81. new Writable({
  82. write(chunk, _, callback) {
  83. buffer += chunk;
  84. callback(null);
  85. },
  86. final(callback) {
  87. t.deepEqual(string, buffer);
  88. resolve();
  89. callback(null);
  90. }
  91. })
  92. );
  93. });