Skip to content

Commit

Permalink
Moved old tests to a separate directory + created the first modern test.
Browse files Browse the repository at this point in the history
  • Loading branch information
uhop committed Oct 24, 2024
1 parent b772c31 commit ebbb22d
Show file tree
Hide file tree
Showing 28 changed files with 114 additions and 0 deletions.
50 changes: 50 additions & 0 deletions tests-old/counter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
'use strict';

function Counter() {
this.objects = 0;
this.keys = 0;
this.arrays = 0;
this.nulls = 0;
this.trues = 0;
this.falses = 0;
this.numbers = 0;
this.strings = 0;
}

Counter.walk = function walk(o, counter) {
switch (typeof o) {
case 'string':
++counter.strings;
return;
case 'number':
++counter.numbers;
return;
case 'boolean':
if (o) {
++counter.trues;
} else {
++counter.falses;
}
return;
}
if (o === null) {
++counter.nulls;
return;
}
if (o instanceof Array) {
++counter.arrays;
o.forEach(function(o) {
walk(o, counter);
});
return;
}
++counter.objects;
for (let key in o) {
if (o.hasOwnProperty(key)) {
++counter.keys;
walk(o[key], counter);
}
}
};

module.exports = {Counter};
23 changes: 23 additions & 0 deletions tests-old/read-string.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict';

const {Readable} = require('node:stream');

const readString = (string, quant, options) => new Readable(Object.assign({objectMode: true}, options, {
read() {
if (isNaN(quant)) {
this.push(string, 'utf8');
} else if (string instanceof Buffer) {
for (let i = 0; i < string.length; i += quant) {
this.push(string.subarray(i, i + quant));
}
} else {
for (let i = 0; i < string.length; i += quant) {
this.push(string.substr(i, quant), 'utf8');
}
}
this.push(null);
}
}));


module.exports = readString;
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Binary file added tests/data/sample.json.gz
Binary file not shown.
Binary file added tests/data/sample.jsonl.gz
Binary file not shown.
41 changes: 41 additions & 0 deletions tests/test-parser.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use strict';

import test from 'tape-six';
import chain from 'stream-chain';

import parser from '../src/parser.js';

import readString from './read-string.js';

test.asPromise('smoke test', (t, resolve, reject) => {
const input = '{"a": 1, "b": true, "c": ["d"]}',
pipeline = chain([readString(input), parser({packValues: false})]),
result = [];

pipeline.on('data', chunk => result.push({name: chunk.name, val: chunk.value}));
pipeline.on('error', () => reject());
pipeline.on('end', () => {
t.ok(result.length === 20);
t.ok(result[0].name === 'startObject');
t.ok(result[1].name === 'startKey');
t.ok(result[2].name === 'stringChunk' && result[2].val === 'a');
t.ok(result[3].name === 'endKey');
t.ok(result[4].name === 'startNumber');
t.ok(result[5].name === 'numberChunk' && result[5].val === '1');
t.ok(result[6].name === 'endNumber');
t.ok(result[7].name === 'startKey');
t.ok(result[8].name === 'stringChunk' && result[8].val === 'b');
t.ok(result[9].name === 'endKey');
t.ok(result[10].name === 'trueValue' && result[10].val === true);
t.ok(result[11].name === 'startKey');
t.ok(result[12].name === 'stringChunk' && result[12].val === 'c');
t.ok(result[13].name === 'endKey');
t.ok(result[14].name === 'startArray');
t.ok(result[15].name === 'startString');
t.ok(result[16].name === 'stringChunk' && result[16].val === 'd');
t.ok(result[17].name === 'endString');
t.ok(result[18].name === 'endArray');
t.ok(result[19].name === 'endObject');
resolve();
});
});

0 comments on commit ebbb22d

Please sign in to comment.