Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9fd6111

Browse files
Add basic tests for decodeArrayStream (#44)
* Add basic tests for decodeArrayStream * Fix test
1 parent 580e798 commit 9fd6111

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

‎test/decodeArrayStream.test.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import assert from "assert";
2+
import { encode, decodeAsync, decodeArrayStream } from "../src";
3+
4+
describe("decodeArrayStream", () => {
5+
const generateSampleObject = () => {
6+
return {
7+
id: Math.random(),
8+
name: "test",
9+
};
10+
};
11+
12+
const createStream = async function*(object: any) {
13+
for (const byte of encode(object)) {
14+
yield [byte];
15+
}
16+
};
17+
18+
it("decodes numbers array", async () => {
19+
const object = [1, 2, 3, 4, 5];
20+
21+
const result = [];
22+
23+
for await (let item of decodeArrayStream(createStream(object))) {
24+
result.push(item);
25+
}
26+
27+
assert.deepStrictEqual(object, result);
28+
});
29+
30+
it("decodes objects array", async () => {
31+
const objectsArrays: Array<any> = [];
32+
33+
for (let i = 0; i < 10; i++) {
34+
objectsArrays.push(generateSampleObject());
35+
}
36+
37+
const result = [];
38+
39+
for await (let item of decodeArrayStream(createStream(objectsArrays))) {
40+
result.push(item);
41+
}
42+
43+
assert.deepStrictEqual(objectsArrays, result);
44+
});
45+
46+
it("fails for non array input", async () => {
47+
const object = "demo";
48+
49+
await assert.rejects(async () => {
50+
const result = [];
51+
52+
for await (let item of decodeArrayStream(createStream(object))) {
53+
result.push(item);
54+
}
55+
}, /.*Unrecognizedarraytypebyte:.*/i);
56+
});
57+
});

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /