deno.land / x / msgpack_javascript@v3.0.0-beta2 / test / decodeAsync.test.ts

decodeAsync.test.ts
نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import assert from "assert";import { encode, decodeAsync } from "../src";
describe("decodeAsync", () => { function wrapWithNoisyBuffer(byte: number) { return Uint8Array.from([0x01, byte, 0x02]).subarray(1, 2); }
it("decodes nil", async () => { const createStream = async function* () { yield wrapWithNoisyBuffer(0xc0); // nil };
const object = await decodeAsync(createStream()); assert.deepStrictEqual(object, null); });
it("decodes fixarray [nil]", async () => { const createStream = async function* () { yield wrapWithNoisyBuffer(0x91); // fixarray size=1 yield [0xc0]; // nil };
const object = await decodeAsync(createStream()); assert.deepStrictEqual(object, [null]); });
it("decodes fixmap {'foo': 'bar'}", async () => { const createStream = async function* () { yield [0x81]; // fixmap size=1 yield encode("foo"); yield encode("bar"); };
const object = await decodeAsync(createStream()); assert.deepStrictEqual(object, { "foo": "bar" }); });
it("decodes multi-byte integer byte-by-byte", async () => { const createStream = async function* () { yield [0xcd]; // uint 16 yield [0x12]; yield [0x34]; }; const object = await decodeAsync(createStream()); assert.deepStrictEqual(object, 0x1234); });
it("decodes fixstr byte-by-byte", async () => { const createStream = async function* () { yield [0xa3]; // fixstr size=3 yield [0x66]; // "f" yield [0x6f]; // "o" yield [0x6f]; // "o" }; const object = await decodeAsync(createStream()); assert.deepStrictEqual(object, "foo"); });
it("decodes binary byte-by-byte", async () => { const createStream = async function* () { yield [0xc4]; // bin 8 yield [0x03]; // bin size=3 yield [0x66]; // "f" yield [0x6f]; // "o" yield [0x6f]; // "o" }; const object = await decodeAsync(createStream()); assert.deepStrictEqual(object, Uint8Array.from([0x66, 0x6f, 0x6f])); });
it("decodes binary with noisy buffer", async () => { const createStream = async function* () { yield wrapWithNoisyBuffer(0xc5); // bin 16 yield [0x00]; yield [0x00]; // bin size=0 }; const object = await decodeAsync(createStream()); assert.deepStrictEqual(object, new Uint8Array(0)); });
it("decodes mixed object byte-by-byte", async () => { const object = { nil: null, true: true, false: false, int: -42, uint64: Number.MAX_SAFE_INTEGER, int64: Number.MIN_SAFE_INTEGER, float: Math.PI, string: "Hello, world!", longString: "Hello, world!\n".repeat(100), binary: Uint8Array.from([0xf1, 0xf2, 0xf3]), array: [1000, 2000, 3000], map: { foo: 1, bar: 2, baz: 3 }, timestampExt: new Date(), map0: {}, array0: [], str0: "", bin0: Uint8Array.from([]), };
const createStream = async function* () { for (const byte of encode(object)) { yield [byte]; } }; assert.deepStrictEqual(await decodeAsync(createStream()), object); });
it("decodes BufferSource", async () => { // https://developer.mozilla.org/en-US/docs/Web/API/BufferSource const createStream = async function* () { yield [0x81] as ArrayLike<number>; // fixmap size=1 yield encode("foo") as BufferSource; yield encode("bar") as BufferSource; };
// createStream() returns AsyncGenerator<ArrayLike<number> | BufferSource, ...> const object = await decodeAsync(createStream()); assert.deepStrictEqual(object, { "foo": "bar" }); });});
msgpack_javascript

Version Info

Tagged at
a year ago