-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtest.js
494 lines (450 loc) · 13.6 KB
/
test.js
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
const VDF = require('./main');
let anyTestFailed = false;
function perform_test(source_string, test_expect, options, test_id, expect_failure = false) {
try {
let test_result = VDF.parse(source_string, options);
if (JSON.stringify(test_result) === JSON.stringify(test_expect)) {
//console.log("Test passed");
if (expect_failure) {
console.log(test_result);
console.error("==========================================\nTest failed (expected an error): "+test_id+"\n==========================================");
}
} else {
if (expect_failure) return;
anyTestFailed = true;
console.log(test_result);
//console.log('expected:', JSON.stringify(test_expect, null, ' '));
//console.log('actual:', JSON.stringify(test_result, null, ' '));
console.error("==========================================\nTest failed (mismatch): "+test_id+"\n==========================================");
}
try {
// test if we can restringify without an error at all
VDF.stringify(test_result);
}
catch (e) {
if (expect_failure) return;
anyTestFailed = true;
console.log(e);
console.error("==========================================\nTest failed (restringify): "+test_id+"\n==========================================");
}
return test_result;
} catch (e) {
if (expect_failure) return;
anyTestFailed = true;
console.log(e);
console.error("==========================================\nTest failed (exception): "+test_id+"\n==========================================");
}
}
function perform_stringify_test(source_object, test_expect, options, test_id) {
try {
let test_result = VDF.stringify(source_object, options);
if (test_result && test_result.trim() === test_expect.trim()) {
//console.log("Test passed");
} else {
anyTestFailed = true;
console.log(test_result);
console.error("==========================================\nTest failed (mismatch): "+test_id+"\n==========================================");
}
} catch (e) {
anyTestFailed = true;
console.log(e);
console.error("==========================================\nTest failed (exception): "+test_id+"\n==========================================");
}
}
let test, test_expect;
// basic test
test = `"basic"
{
"key" "value"
}`;
test_expect = { basic: { key: 'value' }};
perform_test(test, test_expect, {}, "basic");
// main test
test = `
"Inline objects and arrayifying"//{}
{//{}
"0" { "label" "#SFUI_CashColon" }
"1" { "label" "#SFUI_WinMatchColon" "value" "#SFUI_Rounds" }
"2" { "label" "#SFUI_TimePerRoundColon" "value" "2 #SFUI_Minutes" }
"2" { "label" "#SFUI_TimePerRoundColon" "value" "2 #SFUI_Minutes" }
"3" "value before object"
"3" { "label" "#SFUI_BuyTimeColon" "value" "45 #SFUI_Seconds" }
"4" { "label" "#SFUI_SpectateColon" "value" "#SFUI_SpectateTeamOnly" }
"4" { "label" "#SFUI_SpectateColon" "value" "#SFUI_SpectateTeamOnly" }
"4" "value after object"
"5" { "label" "#SFUI_BotsColon_1" "value" "#SFUI_BotDifficulty3_1" // random comment, don't mind me
"label" "#SFUI_BotsColon_2" "value" "#SFUI_BotDifficulty3_2" }
"6" { "label" "#SFUI_WinMatchColon" }// ignore: "value" "#SFUI_Rounds" }
"7" { "label" "#SFUI_WinMatchColon"//}
"more" "here"}
// Syntax error: "8" { "label" "#SFUI_WinMatchColon" "value" } // ignore: "#SFUI_Rounds" }
} empty{}empty2{empty{}} empty3{} empty3{}
no_quotes_tests { // dat no newline + a comment!
0 "-12.34"
0 -1234
0 12.34
0 -12.34
test1 1
test2 val2 test3 true test3 false
test4 ""
test5{}
//test6 a{} // illegal ({} cannot be used inside of unquoted values), would cause an uncaught error
"no""space"
//no_value // this would fail, but is probably malformed anyways, not handling this case
}
arrayifying {
string_then_obj "hi"
string_then_obj { "obj_key" "obj_val" }
obj_then_string { "obj_key" "obj_val" }
obj_then_string "hi"
}
whitespacing_test
// a comment in the middle, don't mind me
{
}
misc//{}
{//{}
"EscapedQuotes" "aaa/////\\\\\\nooo\\"{{uuu\\"\\"{{\\"hhh"
// with conditional to ignore
"Menu_Dlg_Leaderboards_Lost_Connection" "You must be connected to Xbox LIVE to view Leaderboards. Please check your connection and try again." [$X360]
"SFUI_GameModeProgressDisclaimer" "No stats tracking." [$WIN32||$X360]
"SFUI_SinglePlayer_Invite_On" "" [!$PS3]
"SFUI_Upsell_Nav" "\${west} Unlock Full Game \${confirm} Quit \${cancel} Back to Game" [!$WIN32&&!$OSX]
"Cstrike_TitlesTXT_TRAINING6" "Defuse the bomb by aiming at bomb and holding \${use}."
"bool_test_ok1" True
"bool_test_ok2" "true"
"bool_test_not_ok1" "AA_TRuE_BB"
"bool_test_not_ok2" notTRuE
"not_ps3" "not_ps3" [!$PS3] "ps3" "ps3" [$PS3]
"not_ps3_""not_ps3"[!$PS3]"ps3_""ps3"[$PS3][$PS3] // the orphan one will be ignored, but the original parser probably did not do that
"not_ps3__""not_ps3"[!$PS3][!$PS3]"ps3__""ps3"[$PS3][$PS3] // the orphan ones will be ignored, but the original parser probably did not do that
}// some empty lines:
//
/
multiline {
z z z{a a}
"a" "line1
line2
line3"
"b" "
line1
line2
"
c c d"
ddd" e e f "reee
eee
"
g "g
g" f{a a}
h "hf{a a}
" f{a a b{b b} c{a"fd
f"}} // multiline keys are not supported however
}
`;
test_expect = {
'Inline objects and arrayifying': {
'0': { label: '#SFUI_CashColon' },
'1': { label: '#SFUI_WinMatchColon', value: '#SFUI_Rounds' },
'2': [
{ label: '#SFUI_TimePerRoundColon', value: '2 #SFUI_Minutes' },
{ label: '#SFUI_TimePerRoundColon', value: '2 #SFUI_Minutes' }
],
'3': [
'value before object',
{ label: '#SFUI_BuyTimeColon', value: '45 #SFUI_Seconds' }
],
'4': [
{ label: '#SFUI_SpectateColon', value: '#SFUI_SpectateTeamOnly' },
{ label: '#SFUI_SpectateColon', value: '#SFUI_SpectateTeamOnly' },
'value after object'
],
'5': {
label: [ '#SFUI_BotsColon_1', '#SFUI_BotsColon_2' ],
value: [ '#SFUI_BotDifficulty3_1', '#SFUI_BotDifficulty3_2' ]
},
'6': { label: '#SFUI_WinMatchColon' },
'7': { label: '#SFUI_WinMatchColon', more: 'here' }
},
empty: {},
empty2: {empty: {}},
empty3: [{}, {}],
no_quotes_tests: {
'0': [ -12.34, -1234, 12.34, -12.34 ],
test1: 1,
test2: 'val2',
test3: [ true, false ],
test4: '',
test5: {},
no: 'space'
},
arrayifying: {
string_then_obj: ['hi', { 'obj_key': 'obj_val' }],
obj_then_string: [{ 'obj_key': 'obj_val' }, 'hi']
},
whitespacing_test: {},
misc: {
EscapedQuotes: 'aaa/////\\\\\\nooo\\"{{uuu\\"\\"{{\\"hhh',
Menu_Dlg_Leaderboards_Lost_Connection: 'You must be connected to Xbox LIVE to view Leaderboards. Please check your connection and try again.',
SFUI_GameModeProgressDisclaimer: 'No stats tracking.',
SFUI_SinglePlayer_Invite_On: '',
SFUI_Upsell_Nav: '${west} Unlock Full Game ${confirm} Quit ${cancel} Back to Game',
Cstrike_TitlesTXT_TRAINING6: 'Defuse the bomb by aiming at bomb and holding ${use}.',
bool_test_ok1: true,
bool_test_ok2: true,
bool_test_not_ok1: 'AA_TRuE_BB',
bool_test_not_ok2: 'notTRuE',
not_ps3: 'not_ps3', ps3: 'ps3',
not_ps3_: 'not_ps3', ps3_: 'ps3',
not_ps3__: 'not_ps3', ps3__: 'ps3'
},
multiline: {
z: [ 'z', {a: 'a'} ],
a: "line1\nline2\nline3",
b: "\nline1\nline2\n",
c: 'c',
d: "\nddd",
e: 'e',
f: [
"reee\neee \n \n",
{a:'a'},
{a:'a', b:{b:'b'}, c:{ a:"fd \n \n\nf" }}
],
g: "g\ng",
h: "hf{a a}\n"
}
};
perform_test(test, test_expect, {}, "main");
// arrayify tests
test = `
regular_values {
key val1 key val2
}
array_values {
key { test1 aaa }
key { test1 bbb } // without "arrayify" this will patch the previous value of "test1" under "key"
key { test2 bbb }
}
`;
test_expect = {
regular_values: { key: ['val1', 'val2'] },
array_values: { key: [ // array of three objects
{ test1: 'aaa' },
{ test1: 'bbb' },
{ test2: 'bbb' },
] }
};
perform_test(test, test_expect, { arrayify: true }, "arrayify:true");
perform_test(test, test_expect, undefined, "arrayify:true (default)");
test_expect = {
regular_values: { key: 'val2' },
array_values: { key:
{ test1: 'bbb', test2: 'bbb' } // notice it's one object now, not an array of objects
}
};
perform_test(test, test_expect, { arrayify: false }, "arrayify:false");
// types tests
test = `
0 "-12.34"
0 -1234
0 12.34
0 -12.34
test1 1
test2 val2 test3 true test3 false
test4 True test5 TRue test6 atrue test7 trueb
`;
test_expect = {
'0': [ -12.34, -1234, 12.34, -12.34 ],
test1: 1,
test2: 'val2',
test3: [ true, false ],
test4: true,
test5: true,
test6: 'atrue',
test7: 'trueb'
};
perform_test(test, test_expect, { types: true }, "types:true");
perform_test(test, test_expect, true, "types:true (legacy)");
perform_test(test, test_expect, undefined, "types:true (default)");
test_expect = {
'0': [ '-12.34', '-1234', '12.34', '-12.34' ],
test1: '1',
test2: 'val2',
test3: [ 'true', 'false' ],
test4: 'True',
test5: 'TRue',
test6: 'atrue',
test7: 'trueb'
};
perform_test(test, test_expect, { types: false }, "types:false");
perform_test(test, test_expect, false, "types:false (legacy)");
// conditional tests
test = `
"ps3" "ps3" [$PS3]
"not_ps3" "not_ps3" [!$PS3]
"ps3_or_win32" "ps3_or_win32" [$PS3||$WIN32]
"not_win32_and_not_osx" "not_win32_and_not_osx" [!$WIN32&&!$OSX]
"not_xbox_and_not_osx" "not_xbox_and_not_osx" [!$XBOX&&!$OSX]
"not_win32_or_not_osx" "not_win32_or_not_osx" [!$WIN32||!$OSX]
"not_win32_or_xbox" "not_win32_or_xbox" [!$WIN32||$XBOX]
"not_win32_or_not_xbox" "not_win32_or_not_xbox" [!$WIN32||!$XBOX]
"not_win32_and_not_xbox" "not_win32_and_not_xbox" [!$WIN32&&!$XBOX]
"not_win32_and_xbox" "not_win32_and_xbox" [!$WIN32&&$XBOX]
"duped" "duped1" [$PS3]
"duped" "duped2" [!$PS3]
`;
test_expect = {
ps3: 'ps3',
not_ps3: 'not_ps3',
ps3_or_win32: 'ps3_or_win32',
not_win32_and_not_osx: 'not_win32_and_not_osx',
not_xbox_and_not_osx: 'not_xbox_and_not_osx',
not_win32_or_not_osx: 'not_win32_or_not_osx',
not_win32_or_xbox: 'not_win32_or_xbox',
not_win32_or_not_xbox: 'not_win32_or_not_xbox',
not_win32_and_not_xbox: 'not_win32_and_not_xbox',
not_win32_and_xbox: 'not_win32_and_xbox',
duped: ['duped1', 'duped2']
};
perform_test(test, test_expect, undefined, "conditionals:undefined (default)");
test_expect = {
not_ps3: 'not_ps3',
ps3_or_win32: 'ps3_or_win32',
not_xbox_and_not_osx: 'not_xbox_and_not_osx',
not_win32_or_not_osx: 'not_win32_or_not_osx',
not_win32_or_not_xbox: 'not_win32_or_not_xbox',
duped: 'duped2'
};
perform_test(test, test_expect, { conditionals: ['WIN32'] }, "conditionals:WIN32");
test_expect = {
not_ps3: 'not_ps3',
not_win32_and_not_osx: 'not_win32_and_not_osx',
not_xbox_and_not_osx: 'not_xbox_and_not_osx',
not_win32_or_not_osx: 'not_win32_or_not_osx',
not_win32_or_xbox: 'not_win32_or_xbox',
not_win32_or_not_xbox: 'not_win32_or_not_xbox',
not_win32_and_not_xbox: 'not_win32_and_not_xbox',
duped: 'duped2'
};
perform_test(test, test_expect, { conditionals: [] }, "conditionals:[]");
test = `
"invalid" "invalid" [$XBOX || $PS3|| $WIN32]
"invalid" "invalid" [$Xbox||$PS3||WIN32]
"invalid" "invalid" [$Xbox || $PS3 || WIN32]
`;
perform_test(test, {}, { conditionals: [] }, "conditionals:[] (invalid)", true);
//
// stringify de-arrayify
//
test = {
regular_values: { key: ['val1', 'val2', -12.34, false, true] },
array_values: { key: [ // array of three objects
{ test1: 'aaa' },
{ test1: 'bbb' },
{ test2: 'bbb' },
] },
b: [{}],
};
test_expect = `
"regular_values"
{
"key" "val1"
"key" "val2"
"key" "-12.34"
"key" "false"
"key" "true"
}
"array_values"
{
"key"
{
"test1" "aaa"
}
"key"
{
"test1" "bbb"
}
"key"
{
"test2" "bbb"
}
}
"b"
{
}
`;
perform_stringify_test(test, test_expect, {}, "stringify => de-arrayify");
// stringify pretty and indent tests
test = {
a: 'b',
b: {
a: {},
b: [{}],
b2: [{}, {}],
c: 'a',
d: {
a: 'a',
b: 'b'
},
e: ['a', 'b'] // we test whether we can dearrayify
}
};
test_expect = `
"a" "b"
"b"
{
"a"
{
}
"b"
{
}
"b2"
{
}
"b2"
{
}
"c" "a"
"d"
{
"a" "a"
"b" "b"
}
"e" "a"
"e" "b"
}
`;
perform_stringify_test(test, test_expect, { pretty: false }, "stringify => pretty:false");
perform_stringify_test(test, test_expect, false, "stringify => pretty:false (legacy)");
perform_stringify_test(test, test_expect, undefined, "stringify => pretty:false (default)");
test_expect = `
"a" "b"
"b"
{
"a"
{
}
"b"
{
}
"b2"
{
}
"b2"
{
}
"c" "a"
"d"
{
"a" "a"
"b" "b"
}
"e" "a"
"e" "b"
}
`;
perform_stringify_test(test, test_expect, { pretty: true, indent: " " }, "stringify => pretty:true, indent:<4 spaces>");
perform_stringify_test(test, test_expect.replace(/ /g, "\t"), true, "stringify => pretty:true (legacy)");
// END
if (!anyTestFailed)
console.log("All tests passed.");