You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
1.9 KiB

import test from "node:test"
import assert from "node:assert"
import { gronReadStream } from "./ngronstream.mjs";
const asString = async (stream) => {
const chunks = [];
for await (const chunk of stream) {
chunks.push(Buffer.from(chunk))
}
return Buffer.concat(chunks).toString('utf8');
}
test("number", async () => {
const rs = gronReadStream(3);
const result = await asString(rs);
assert.strictEqual(result, `json=3;\n`)
});
test("string", async () => {
const rs = gronReadStream("hello")
const result = await asString(rs);
assert.strictEqual(result, `json="hello";\n`)
});
test("null", async () => {
const rs = gronReadStream(null)
const result = await asString(rs);
assert.strictEqual(result, `json=null;\n`)
});
test("empty obj", async () => {
const rs = gronReadStream({})
const result = await asString(rs);
assert.strictEqual(result, `json={};\n`)
});
test("empty array", async () => {
const rs = gronReadStream([])
const result = await asString(rs);
assert.strictEqual(result, `json=[];\n`)
});
test("nested obj", async () => {
const rs = gronReadStream({ one: 1 })
const result = await asString(rs);
assert.strictEqual(result, `
json={};
json.one=1;
`.trimStart())
});
test("nested array", async () => {
const rs = gronReadStream(["hello", "world"])
const result = await asString(rs);
assert.strictEqual(result, `
json=[];
json[0]="hello";
json[1]="world";
`.trimStart())
});
test("complex", async () => {
const rs = gronReadStream([
{ name: "Alice", startYear: 2001 },
{ name: "Ralph", startYear: 2010 },
{ name: "Jesse", startYear: 2007, tired: true },
]);
const result = await asString(rs);
assert.strictEqual(result, `
json=[];
json[0]={};
json[0].name="Alice";
json[0].startYear=2001;
json[1]={};
json[1].name="Ralph";
json[1].startYear=2010;
json[2]={};
json[2].name="Jesse";
json[2].startYear=2007;
json[2].tired=true;
`.trimStart());
});