-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
186 lines (170 loc) · 3.67 KB
/
test.js
File metadata and controls
186 lines (170 loc) · 3.67 KB
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
177
178
179
180
181
182
183
184
185
186
import {fromString, prettyFromJsonStr} from './mod.js'
import { assertEquals } from "https://deno.land/std@0.165.0/testing/asserts.ts";
Deno.test('readme example', () => {
const ret = fromString(`
This is a comment
title [jevkoconfig1 Example]
owner [
name [tester]
dob \`''2020-08-05T20:30:01+09:00[Asia/Tokyo][u-ca=japanese]''
]
database [
enabled [true]
quoted ['true]
ports [
[8000]
[8001]
[8002]
]
data [ [[delta] [phi]] [3.14] ]
temp targets [ cpu [79.5] case [72.0] ]
]
servers [
alpha [
ip [10.0.0.1]
role [frontend]
]
beta [
ip [10.0.0.2]
role [backend]
]
-discarded key [[with][a][value]]
-[discarded section]
]
embedded documents [
some json \`'json'
{
"id": "b3df0d",
"count": 55,
"props": {
"return code": "59503a7b",
"status": "pending"
},
"associated ids": [
"3adf7c",
"ff0df7",
"3aa670"
],
"parent": null
}
'json'
more json \`'json'55'json'
json string \`'json'"\\n\\tsomething\\u0000"'json'
json array \`'json'[1, 2, 3, 4, null]'json'
]
`)
assertEquals(JSON.stringify(ret, null, 2), String.raw`{
"title": "jevkoconfig1 Example",
"owner": {
"name": "tester",
"dob": "2020-08-05T20:30:01+09:00[Asia/Tokyo][u-ca=japanese]"
},
"database": {
"enabled": true,
"quoted": "true",
"ports": [
8000,
8001,
8002
],
"data": [
[
"delta",
"phi"
],
3.14
],
"temp targets": {
"cpu": 79.5,
"case": 72
}
},
"servers": {
"alpha": {
"ip": "10.0.0.1",
"role": "frontend"
},
"beta": {
"ip": "10.0.0.2",
"role": "backend"
}
},
"embedded documents": {
"some json": {
"id": "b3df0d",
"count": 55,
"props": {
"return code": "59503a7b",
"status": "pending"
},
"associated ids": [
"3adf7c",
"ff0df7",
"3aa670"
],
"parent": null
},
"more json": 55,
"json string": "\n\tsomething\u0000",
"json array": [
1,
2,
3,
4,
null
]
}
}`)
assertEquals(ret.database.test, undefined)
})
Deno.test('array with comments', () => {
const obj = fromString(`array [
Comment for the
frist value of an array
[1]
Comment for the
second value
[2]
]`)
const arr = obj.array
assertEquals(arr.length, 2)
assertEquals(arr[0], 1)
assertEquals(arr[1], 2)
})
Deno.test('quoted keys', () => {
const obj = fromString(`
owner [
name [tester]
dob \`''2020-08-05T20:30:01+09:00[Asia/Tokyo][u-ca=japanese]''
' test ' [999]
'' [value of the empty key]
' key with leading and trailing spaces ' [value1]
'multiline
key' [value2]
' padded
multiline key ' [value3]
]
`)
const owner = obj.owner
assertEquals(owner.name, 'tester')
assertEquals(owner.dob, '2020-08-05T20:30:01+09:00[Asia/Tokyo][u-ca=japanese]')
assertEquals(owner[' test '], 999)
assertEquals(owner[''], 'value of the empty key')
assertEquals(owner[' key with leading and trailing spaces '], 'value1')
assertEquals(owner['multiline\n key'], 'value2')
assertEquals(owner[' padded\n multiline key '], 'value3')
})
Deno.test('empty string', () => {
const obj = fromString(`a [] b [ ] c [''] d [' ']`)
assertEquals(obj, {
a: "",
b: "",
c: "",
d: " ",
})
})
Deno.test('from json str -- "\'", "\'\'"', () => {
const jsonStr = `{"'":"'","''":"''"}`
const pretty = prettyFromJsonStr(jsonStr)
assertEquals(JSON.stringify(fromString(pretty)), jsonStr)
})