-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser_test.ts
258 lines (209 loc) · 9.1 KB
/
parser_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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import { assertEquals } from "./deps_test.ts";
import { dotEnvParser } from "./mod.ts";
const pairs: string = `
# Numeric keys are skipped
0=ZERO
# Keys starting with a number are skipped
0KEY=ZERO
# Keys can contain a number
KEY0=value0
# Keys can start with an underscore
_KEY=_value
# Keys can contain an underscore
KEY_=value_
# Keys can be in lower case
key=value
# Keys can be in upper case
KEY=VALUE
# Keys can be surrounded by several spaces on both sides
KEY_WITH_SPACE =VALUE_WITH_SPACE
# An empty value gives an empty string
EMPTY=
# An unquoted numeric value gives a string representing the number by default
# An unquoted numeric value gives a number when inference is enabled
# An unquoted numeric value gives a number when inference is disabled
NUMBER=1
# A quoted numeric value surrounded by spaces gives a string representing the number by default
# A quoted numeric value surrounded by spaces gives a number when inference is enabled
# A quoted numeric value surrounded by spaces gives a number when inference is disabled
QUOTED_NUMBER=" 1 "
# An unquoted true boolean value gives a string representing the boolean by default
# An unquoted true boolean value gives a boolean when inference is enabled
# An unquoted true boolean value gives a boolean when inference is disabled
BOOL_TRUE=true
# An unquoted true boolean value in upper case gives a string representing the boolean by default
# An unquoted true boolean value in upper case gives a boolean when inference is enabled
# An unquoted true boolean value in upper case gives a boolean when inference is disabled
BOOL_TRUE_UPPER=TRUE
# An unquoted false boolean value gives a string representing the boolean by default
# An unquoted false boolean value gives a boolean when inference is enabled
# An unquoted false boolean value gives a boolean when inference is disabled
BOOL_FALSE=false
# An unquoted false boolean value in upper case gives a string representing the boolean by default
# An unquoted false boolean value in upper case gives a boolean when inference is enabled
# An unquoted false boolean value in upper case gives a boolean when inference is disabled
BOOL_FALSE_UPPER=FALSE
# A value can contain a hashtag
HASH=#HASH
# A value can contain an equal character
EQUAL=1+1=2
# A string value can be unquoted and surrounding spaces are trimmed
UNQUOTED= unquoted value
# A string value can be single quoted and surrounding spaces are not trimmed
SINGLE_QUOTED= ' single quoted value '
# A string value can be double quoted and surrounding spaces are not trimmed
DOUBLE_QUOTED= " double quoted value "
# A single quoted string can contain escaped single-quotes
ESCAPED_SINGLE_QUOTED='a \\'b\\' c'
# A double quoted string can contain escaped double-quotes
ESCAPED_DOUBLE_QUOTED="a \\"b\\" c"
# Multiline unquoted values are accepted
UNQUOTED_MULTI=unquoted
multi
# Multiline single-quoted values are accepted
SINGLE_QUOTED_MULTI='single
quoted
multi'
# Multiline double-quoted values are accepted
DOUBLE_QUOTED_MULTI="double
quoted
multi"
# Escaped hashtags are not treated as comment in multiline values
HASH_MULTI=hash
\\#multi
# Escaped equal characters without key are not treated as a pair in multiline values
EQUAL_MULTI=equal
\\=multi
# Escaped equal characters with key are not treated as a pair in multiline values
KEY_EQUAL_MULTI=key
equal\\=multi
`;
const response = dotEnvParser(pairs);
const fresponse = dotEnvParser(pairs, false);
const tresponse = dotEnvParser(pairs, true);
Deno.test("Comments and empty lines are skipped", () => {
const res = Object.keys(response).length;
assertEquals(res, 26);
});
Deno.test("Numeric keys are skipped", () => {
const res = response.hasOwnProperty("0");
assertEquals(res, false);
});
Deno.test("Numeric keys are skipped", () => {
const res = response.hasOwnProperty("0KEY");
assertEquals(res, false);
});
Deno.test("Keys can contain a number", () => {
assertEquals(response["KEY0"], "value0");
});
Deno.test("Keys can start with an underscore", () => {
assertEquals(response["_KEY"], "_value");
});
Deno.test("Keys can contain an underscore", () => {
assertEquals(response["KEY_"], "value_");
});
Deno.test("Keys can be in lower case", () => {
assertEquals(response["key"], "value");
});
Deno.test("Keys can be in upper case", () => {
assertEquals(response["KEY"], "VALUE");
});
Deno.test("Keys can be surrounded by several spaces on both sides", () => {
assertEquals(response["KEY_WITH_SPACE"], "VALUE_WITH_SPACE");
});
Deno.test("An empty value gives an empty string", () => {
assertEquals(response["EMPTY"], "");
});
Deno.test("An unquoted numeric value gives a string representing the number by default", () => {
assertEquals(response["NUMBER"], "1");
});
Deno.test("An unquoted numeric value gives a number when inference is enabled", () => {
assertEquals(tresponse["NUMBER"], 1);
});
Deno.test("An unquoted numeric value gives a number when inference is disabled", () => {
assertEquals(fresponse["NUMBER"], "1");
});
Deno.test("A quoted numeric value surrounded by spaces gives a string representing the number by default", () => {
assertEquals(response["QUOTED_NUMBER"], " 1 ");
});
Deno.test("A quoted numeric value surrounded by spaces gives a number when inference is enabled", () => {
assertEquals(tresponse["QUOTED_NUMBER"], 1);
});
Deno.test("A quoted numeric value surrounded by spaces gives a number when inference is disabled", () => {
assertEquals(fresponse["QUOTED_NUMBER"], " 1 ");
});
Deno.test("An unquoted true boolean value gives a string representing the boolean by default", () => {
assertEquals(response["BOOL_TRUE"], "true");
});
Deno.test("An unquoted true boolean value gives a boolean when inference is enabled", () => {
assertEquals(tresponse["BOOL_TRUE"], true);
});
Deno.test("An unquoted true boolean value gives a boolean when inference is disabled", () => {
assertEquals(fresponse["BOOL_TRUE"], "true");
});
Deno.test("An unquoted true boolean value in upper case gives a string representing the boolean by default", () => {
assertEquals(response["BOOL_TRUE_UPPER"], "TRUE");
});
Deno.test("An unquoted true boolean value in upper case gives a boolean when inference is enabled", () => {
assertEquals(tresponse["BOOL_TRUE_UPPER"], true);
});
Deno.test("An unquoted true boolean value in upper case gives a boolean when inference is disabled", () => {
assertEquals(fresponse["BOOL_TRUE_UPPER"], "TRUE");
});
Deno.test("An unquoted false boolean value gives a string representing the boolean by default", () => {
assertEquals(response["BOOL_FALSE"], "false");
});
Deno.test("An unquoted false boolean value gives a boolean when inference is enabled", () => {
assertEquals(tresponse["BOOL_FALSE"], false);
});
Deno.test("An unquoted false boolean value gives a boolean when inference is disabled", () => {
assertEquals(fresponse["BOOL_FALSE"], "false");
});
Deno.test("An unquoted false boolean value in upper case gives a string representing the boolean by default", () => {
assertEquals(response["BOOL_FALSE_UPPER"], "FALSE");
});
Deno.test("An unquoted false boolean value in upper case gives a boolean when inference is enabled", () => {
assertEquals(tresponse["BOOL_FALSE_UPPER"], false);
});
Deno.test("An unquoted false boolean value in upper case gives a boolean when inference is disabled", () => {
assertEquals(fresponse["BOOL_FALSE_UPPER"], "FALSE");
});
Deno.test("A value can contain a hashtag", () => {
assertEquals(response["HASH"], "#HASH");
});
Deno.test("A value can contain an equal character", () => {
assertEquals(response["EQUAL"], "1+1=2");
});
Deno.test("A string value can be unquoted and surrounding spaces are trimmed", () => {
assertEquals(response["UNQUOTED"], "unquoted value");
});
Deno.test("A string value can be single quoted and surrounding spaces are not trimmed", () => {
assertEquals(response["SINGLE_QUOTED"], " single quoted value ");
});
Deno.test("A string value can be double quoted and surrounding spaces are not trimmed", () => {
assertEquals(response["DOUBLE_QUOTED"], " double quoted value ");
});
Deno.test("A single quoted string can contain escaped single-quotes", () => {
assertEquals(response["ESCAPED_SINGLE_QUOTED"], "a 'b' c");
});
Deno.test("A double quoted string can contain escaped double-quotes", () => {
assertEquals(response["ESCAPED_DOUBLE_QUOTED"], 'a "b" c');
});
Deno.test("Multiline unquoted values are accepted", () => {
assertEquals(response["UNQUOTED_MULTI"], "unquoted\nmulti");
});
Deno.test("Multiline single-quoted values are accepted", () => {
assertEquals(response["SINGLE_QUOTED_MULTI"], "single\nquoted\nmulti");
});
Deno.test("Multiline double-quoted values are accepted", () => {
assertEquals(response["DOUBLE_QUOTED_MULTI"], "double\nquoted\nmulti");
});
Deno.test("Escaped hashtags are not treated as comment in multiline values", () => {
assertEquals(response["HASH_MULTI"], "hash\n#multi");
});
Deno.test("Escaped equal characters without key are not treated as a pair in multiline values", () => {
assertEquals(response["EQUAL_MULTI"], "equal\n=multi");
});
Deno.test("Escaped equal characters with key are not treated as a pair in multiline values", () => {
assertEquals(response["KEY_EQUAL_MULTI"], "key\nequal=multi");
});