-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeno_test.ts
176 lines (157 loc) · 3.99 KB
/
deno_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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/**
* Tests for CI, run using `deno test`
*/
import { assertEquals } from "https://deno.land/std@0.171.0/testing/asserts.ts";
import { Parser, char, exact, filter, input, manySep0, map, numericChar, oneOf, optional, required, take0, take1, tuple, whitespace } from "./index.ts"
// JSON parser
const nil = map(
exact('null'),
() => null
)
const boolean = map(
oneOf(
exact('true'),
exact('false')
),
s => s === 'true'
)
const number = map(
tuple(
take1(numericChar),
optional(
tuple(
exact("."),
required(take1(numericChar), () => `Expected numbers after decimal point`)
)
),
),
([front, back]) => {
let numberString = front
if (back != null) {
const [_decimal, fractionDigits] = back
numberString += `.${fractionDigits}`
}
return Number(numberString)
}
)
const string = map(
tuple(
exact('"'),
take0(filter(char, ch => ch !== '"')),
required(exact('"'), () => `Expected closing quote '"'`)
),
([_0, contents, _1]) => contents
)
const commaWithWhitespace = tuple(
whitespace,
exact(','),
whitespace
)
const array: Parser<unknown[], string> = input => map(
tuple(
exact('['),
whitespace,
manySep0(
jsonValue,
commaWithWhitespace
),
whitespace,
required(exact(']'), () => `Expected closing bracket ']'`)
),
([_0, _1, elements, _2, _3]) => elements
)(input)
const object: Parser<Record<string, unknown>, string> = input => map(
tuple(
exact('{'),
whitespace,
manySep0(
map(
tuple(
string,
whitespace,
required(exact(':'), () => `Expected ':' after object key`),
whitespace,
required(jsonValue, () => `Expected value after ':'`)
),
([key, _0, _1, _2, value]) => [key, value] as const
),
commaWithWhitespace
),
whitespace,
required(exact('}'), () => `Expected closing curly brace '}'`)
),
([_0, _1, entries, _2, _3]) => Object.fromEntries(entries)
)(input)
const jsonValue: Parser<unknown, string> = input => oneOf(
nil,
boolean,
number,
string,
array,
object
)(input)
const jsonDocument = map(
tuple(
whitespace,
jsonValue,
whitespace
),
([_0, value, _1]) => value
)
Deno.test({
name: 'JSON parser success',
fn() {
const json = {
"foo": "bar",
"blah": 123.4,
"stuff": true,
"other": [
1,
2,
null,
false
]
}
const jsonString = JSON.stringify(json)
const parserInput = input(jsonString)
assertEquals(
jsonDocument(parserInput),
{
kind: 'success',
input: { code: jsonString, index: jsonString.length },
src: { code: jsonString, start: 0, end: jsonString.length },
parsed: json
}
)
}
})
Deno.test({
name: 'JSON parser error 1',
fn() {
const jsonString = `"foo`
const parserInput = input(jsonString)
assertEquals(
jsonDocument(parserInput),
{
kind: 'error',
input: { code: jsonString, index: 4 },
error: `Expected closing quote '"'`
}
)
}
})
Deno.test({
name: 'JSON parser error 2',
fn() {
const jsonString = `{ "foo": 12`
const parserInput = input(jsonString)
assertEquals(
jsonDocument(parserInput),
{
kind: 'error',
input: { code: jsonString, index: 11 },
error: `Expected closing curly brace '}'`
}
)
}
})