-
Notifications
You must be signed in to change notification settings - Fork 576
/
json_parse_stream_test.ts
127 lines (112 loc) Β· 3.17 KB
/
json_parse_stream_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
125
126
127
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
import { assertEquals } from "../testing/asserts.ts";
import { TextDelimiterStream } from "../streams/text_delimiter_stream.ts";
import { TextLineStream } from "../streams/text_line_stream.ts";
import { JsonParseStream } from "./json_parse_stream.ts";
import { assertInvalidParse, assertValidParse } from "./_test_common.ts";
Deno.test({
name: "[json] JsonParseStream",
async fn() {
await assertValidParse(
JsonParseStream,
['{"foo": "bar"}', '["foo"]', '"foo"', "0", "null", "true", "false"],
[{ foo: "bar" }, ["foo"], "foo", 0, null, true, false],
);
},
});
Deno.test({
name: "[json] JsonParseStream: empty line",
async fn() {
await assertValidParse(
JsonParseStream,
[" \t\r\n", ""],
[],
);
},
});
Deno.test({
name: "[json] JsonParseStream: special character",
async fn() {
await assertValidParse(
JsonParseStream,
['"πͺ"', '"π¦"', '"\u3053\u3093\u306b\u3061\u306f"'],
["πͺ", "π¦", "\u3053\u3093\u306b\u3061\u306f"],
);
},
});
Deno.test({
name: "[json] JsonParseStream: expect error",
async fn() {
await assertInvalidParse(
JsonParseStream,
['{"foo": "bar"}', '{"foo": '],
{},
SyntaxError,
`Unexpected end of JSON input (parsing: '{"foo": ')`,
);
},
});
Deno.test({
name: "[json] parse: testdata(jsonl)",
async fn() {
// Read the test data file
const url = "./testdata/test.jsonl";
const { body } = await fetch(new URL(url, import.meta.url).toString());
const readable = body!
.pipeThrough(new TextDecoderStream())
.pipeThrough(new TextLineStream())
.pipeThrough(new JsonParseStream());
const result = [];
for await (const data of readable) {
result.push(data);
}
assertEquals(result, [
{ "hello": "world" },
["π", "π", "π"],
{ "deno": "π¦" },
]);
},
});
Deno.test({
name: "[json] parse: testdata(ndjson)",
async fn() {
// Read the test data file
const url = "./testdata/test.ndjson";
const { body } = await fetch(new URL(url, import.meta.url).toString());
const readable = body!
.pipeThrough(new TextDecoderStream())
.pipeThrough(new TextLineStream())
.pipeThrough(new JsonParseStream());
const result = [];
for await (const data of readable) {
result.push(data);
}
assertEquals(result, [
{ "hello": "world" },
["π", "π", "π"],
{ "deno": "π¦" },
]);
},
});
Deno.test({
name: "[json] parse: testdata(json-seq)",
async fn() {
// Read the test data file
const recordSeparator = "\x1E";
const url = "./testdata/test.json-seq";
const { body } = await fetch(new URL(url, import.meta.url).toString());
const readable = body!
.pipeThrough(new TextDecoderStream())
.pipeThrough(new TextDelimiterStream(recordSeparator))
.pipeThrough(new JsonParseStream());
const result = [];
for await (const data of readable) {
result.push(data);
}
assertEquals(result, [
{ "hello": "world" },
["π", "π", "π"],
{ "deno": "π¦" },
]);
},
});