-
Notifications
You must be signed in to change notification settings - Fork 8
/
json_test.sp
1362 lines (1090 loc) · 45.4 KB
/
json_test.sp
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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* vim: set ts=4 :
* =============================================================================
* sm-json
* A pure SourcePawn JSON encoder/decoder.
* https://github.com/clugg/sm-json
*
* sm-json (C)2022 James Dickens. (clug)
* SourceMod (C)2004-2008 AlliedModders LLC. All rights reserved.
* =============================================================================
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, version 3.0, as published by the
* Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*
* As a special exception, AlliedModders LLC gives you permission to link the
* code of this program (as well as its derivative works) to "Half-Life 2," the
* "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
* by the Valve Corporation. You must obey the GNU General Public License in
* all respects for all other code used. Additionally, AlliedModders LLC grants
* this exception to all derivative works. AlliedModders LLC defines further
* exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
* or <http://www.sourcemod.net/license.php>.
*/
#pragma semicolon 1
#pragma newdecls required
#include <sourcemod>
#include <testsuite>
#include <json>
public Plugin myinfo = {
name = "JSON Tester",
author = "clug",
description = "Tests dumping and loading JSON objects.",
version = "5.0.0",
url = "https://github.com/clugg/sm-json"
};
/**
* @section Globals
*/
char json_encode_output[1024];
/**
* @section Helpers
*/
/**
* Encodes a JSON_Object to a hardcoded output.
*/
void _json_encode(JSON_Object obj, int options = JSON_NONE)
{
obj.Encode(json_encode_output, sizeof(json_encode_output), options);
Test_Assert(
"calculated buffer size fits output",
json_encode_size(obj, options) > strlen(json_encode_output)
);
}
/**
* Encodes a JSON_Object and prints it to the test output.
*/
void print_json(JSON_Object obj, int options = JSON_NONE)
{
_json_encode(obj, options);
Test_Output("%s", json_encode_output);
}
/**
* Removes the specified index from the array and confirms
* that the removed value no longer exists.
*/
bool check_array_remove(JSON_Array arr, int index)
{
Test_Output("Removing element at index %d", index);
// get current value at index
JSONCellType type = arr.GetType(index);
int str_size = 0;
if (type == JSON_Type_String) {
str_size = arr.GetSize(index);
}
any value;
char[] str = new char[str_size];
if (type == JSON_Type_String) {
arr.GetString(index, str, str_size);
} else {
arr.GetValue(index, value);
}
// remove the index from the array
arr.Remove(index);
print_json(arr);
// confirm that it is gone
int found = -1;
if (type == JSON_Type_String) {
found = arr.IndexOfString(str);
} else {
found = arr.IndexOf(value);
}
if (found != -1) {
LogError(
"json_test: found removed value in array at position %d",
found
);
return false;
}
return true;
}
void check_key_at_index(JSON_Object obj, const char[] key, int index)
{
Test_AssertEqual("key -> index", index, obj.GetIndex(key));
int max_size = JSON_INT_BUFFER_SIZE + 8;
char[] index_size_key = new char[max_size];
FormatEx(index_size_key, max_size, "%d:len", index);
int key_size = obj.Meta.GetInt(index_size_key);
char[] real_key = new char[key_size];
char[] index_key = new char[JSON_INT_BUFFER_SIZE];
IntToString(index, index_key, JSON_INT_BUFFER_SIZE);
obj.Meta.GetString(index_key, real_key, key_size);
Test_AssertStringsEqual("keys match", key, real_key);
}
void check_no_key_at_index(JSON_Object obj, int index)
{
int max_size = JSON_INT_BUFFER_SIZE + 8;
char[] index_size_key = new char[max_size];
FormatEx(index_size_key, max_size, "%d:len", index);
Test_AssertFalse("no key length tracked for index", obj.Meta.HasKey(index_size_key));
char[] index_key = new char[JSON_INT_BUFFER_SIZE];
IntToString(index, index_key, JSON_INT_BUFFER_SIZE);
Test_AssertFalse("no key tracked for index", obj.Meta.HasKey(index_key));
}
/**
* @section Methodmaps
*/
methodmap Weapon < JSON_Object
{
public Weapon()
{
return view_as<Weapon>(new JSON_Object());
}
property int id
{
public get()
{
return this.GetInt("id");
}
public set(int value)
{
this.SetInt("id", value);
}
}
public bool GetName(char[] buffer, int max_size)
{
return this.GetString("name", buffer, max_size);
}
public void SetName(const char[] value)
{
this.SetString("name", value);
}
}
methodmap Player < JSON_Object
{
public Player()
{
return view_as<Player>(new JSON_Object());
}
property int id
{
public get()
{
return this.GetInt("id");
}
public set(int value)
{
this.SetInt("id", value);
}
}
property Weapon weapon
{
public get()
{
return view_as<Weapon>(this.GetObject("weapon"));
}
public set(Weapon value)
{
this.SetObject("weapon", view_as<JSON_Object>(value));
}
}
}
/**
* @section Tests
**/
void it_should_decode(char[] data)
{
JSON_Object obj = json_decode(data);
if (Test_AssertNotNull("obj", obj)) {
json_cleanup_and_delete(obj);
}
}
void it_should_not_decode(char[] data)
{
JSON_Object obj = json_decode(data);
if (! Test_AssertNull("obj", obj)) {
_json_encode(obj);
LogError(
"json_test: malformed JSON was parsed as valid: %s",
json_encode_output
);
json_cleanup_and_delete(obj);
}
char error[1024];
Test_Assert(
"last error is not empty",
json_get_last_error(error, sizeof(error))
);
Test_Output("%s", error);
}
void it_should_encode_empty_objects()
{
JSON_Object obj = new JSON_Object();
_json_encode(obj);
json_cleanup_and_delete(obj);
Test_AssertStringsEqual("output", json_encode_output, "{}");
}
void it_should_encode_empty_arrays()
{
JSON_Array arr = new JSON_Array();
_json_encode(arr);
json_cleanup_and_delete(arr);
Test_AssertStringsEqual("output", json_encode_output, "[]");
}
void it_should_support_objects()
{
JSON_Object obj = new JSON_Object();
Test_Assert("set string", obj.SetString("str", "leet"));
Test_Assert("set escaped string", obj.SetString("escaped_str", "\"leet\""));
Test_Assert("set int", obj.SetInt("int", 9001));
Test_Assert("set negative int", obj.SetInt("negative_int", -9001));
Test_Assert("set int zero", obj.SetInt("int_zero", 0));
Test_Assert("set float", obj.SetFloat("float", 13.37));
Test_Assert("set negative float", obj.SetFloat("negative_float", -13.37));
Test_Assert("set float zero", obj.SetFloat("float_zero", 0.0));
Test_Assert("set true", obj.SetBool("true", true));
Test_Assert("set false", obj.SetBool("false", false));
Test_Assert("set handle", obj.SetObject("handle", null));
print_json(obj);
json_cleanup_and_delete(obj);
JSON_Object decoded = json_decode(json_encode_output);
if (! Test_AssertNotNull("decoded", decoded)) {
// if this assertion fails, testing cannot continue
return;
}
char string[32];
Test_Assert("get decoded string", decoded.GetString("str", string, sizeof(string)));
Test_AssertStringsEqual("decoded string", string, "leet");
Test_Assert("get decoded escaped string", decoded.GetString("escaped_str", string, sizeof(string)));
Test_AssertStringsEqual("decoded escaped string", string, "\"leet\"");
Test_AssertEqual("decoded int", decoded.GetInt("int"), 9001);
Test_AssertEqual("decoded negative int", decoded.GetInt("negative_int"), -9001);
Test_AssertEqual("decoded int zero", decoded.GetInt("int_zero"), 0);
Test_AssertFloatsEqual("decoded float", decoded.GetFloat("float"), 13.37);
Test_AssertFloatsEqual("decoded negative float", decoded.GetFloat("negative_float"), -13.37);
Test_AssertFloatsEqual("decoded float zero", decoded.GetFloat("float_zero"), 0.0);
Test_AssertTrue("decoded true", decoded.GetBool("true"));
Test_AssertFalse("decoded false", decoded.GetBool("false"));
Test_AssertNull("decoded handle", decoded.GetObject("handle"));
json_cleanup_and_delete(decoded);
}
void it_should_support_objects_with_ordered_keys()
{
JSON_Object obj = new JSON_Object();
obj.SetInt("first", 1);
check_key_at_index(obj, "first", 0);
obj.SetInt("second", 2);
check_key_at_index(obj, "second", 1);
obj.SetInt("third", 3);
check_key_at_index(obj, "third", 2);
check_no_key_at_index(obj, 3);
_json_encode(obj);
Test_AssertStringsEqual("output upon insert", json_encode_output, "{\"first\":1,\"second\":2,\"third\":3}");
obj.Remove("second");
check_key_at_index(obj, "first", 0);
check_key_at_index(obj, "third", 1);
check_no_key_at_index(obj, 2);
obj.SetBool("last", true);
check_key_at_index(obj, "last", 2);
check_no_key_at_index(obj, 3);
_json_encode(obj);
Test_AssertStringsEqual("output after removing second and adding", json_encode_output, "{\"first\":1,\"third\":3,\"last\":true}");
obj.Remove("first");
check_key_at_index(obj, "third", 0);
check_key_at_index(obj, "last", 1);
check_no_key_at_index(obj, 2);
_json_encode(obj);
Test_AssertStringsEqual("output after removing first", json_encode_output, "{\"third\":3,\"last\":true}");
obj.Remove("last");
check_key_at_index(obj, "third", 0);
check_no_key_at_index(obj, 1);
_json_encode(obj);
Test_AssertStringsEqual("output after removing last", json_encode_output, "{\"third\":3}");
obj.Remove("third");
check_no_key_at_index(obj, 0);
_json_encode(obj);
Test_AssertStringsEqual("output after removing third", json_encode_output, "{}");
json_cleanup_and_delete(obj);
}
void it_should_support_decoding_objects_with_ordered_keys()
{
JSON_Object obj = json_decode("{\"first\":1,\"second\":2,\"third\":3}");
_json_encode(obj);
Test_AssertStringsEqual("decode -> encode output", json_encode_output, "{\"first\":1,\"second\":2,\"third\":3}");
json_cleanup_and_delete(obj);
}
void it_should_support_decoding_objects_with_duplicate_keys()
{
JSON_Object obj = json_decode("{\"third\":1,\"second\":2,\"third\":\"hello\"}");
Test_AssertEqual("key type", obj.GetType("third"), JSON_Type_String);
_json_encode(obj);
Test_AssertStringsEqual("decode -> encode output", json_encode_output, "{\"second\":2,\"third\":\"hello\"}");
json_cleanup_and_delete(obj);
}
void it_should_support_arrays()
{
JSON_Array arr = new JSON_Array();
Test_AssertNotEqual("push string", arr.PushString("leet"), -1);
Test_AssertNotEqual("push escaped string", arr.PushString("\"leet\""), -1);
Test_AssertNotEqual("push int", arr.PushInt(9001), -1);
Test_AssertNotEqual("push negative int", arr.PushInt(-9001), -1);
Test_AssertNotEqual("push int zero", arr.PushInt(0), -1);
Test_AssertNotEqual("push float", arr.PushFloat(13.37), -1);
Test_AssertNotEqual("push negative float", arr.PushFloat(-13.37), -1);
Test_AssertNotEqual("push float zero", arr.PushFloat(0.0), -1);
Test_AssertNotEqual("push true", arr.PushBool(true), -1);
Test_AssertNotEqual("push false", arr.PushBool(false), -1);
Test_AssertNotEqual("push handle", arr.PushObject(null), -1);
print_json(arr);
json_cleanup_and_delete(arr);
JSON_Array decoded = view_as<JSON_Array>(
json_decode(json_encode_output)
);
if (! Test_AssertNotNull("decoded", decoded)) {
// if this assertion fails, testing cannot continue
return;
}
int index = 0;
char string[32];
Test_Assert("get decoded string", decoded.GetString(index++, string, sizeof(string)));
Test_AssertStringsEqual("decoded string", string, "leet");
Test_Assert("get decoded escaped string",decoded.GetString(index++, string, sizeof(string)));
Test_AssertStringsEqual("decoded escaped string", string, "\"leet\"");
Test_AssertEqual("decoded int", decoded.GetInt(index++), 9001);
Test_AssertEqual("decoded negative int", decoded.GetInt(index++), -9001);
Test_AssertEqual("decoded int zero", decoded.GetInt(index++), 0);
Test_AssertFloatsEqual("decoded float", decoded.GetFloat(index++), 13.37);
Test_AssertFloatsEqual("decoded negative float", decoded.GetFloat(index++), -13.37);
Test_AssertFloatsEqual("decoded float zero", decoded.GetFloat(index++), 0.0);
Test_AssertTrue("decoded true", decoded.GetBool(index++));
Test_AssertFalse("decoded false", decoded.GetBool(index++));
Test_AssertNull("decoded handle", decoded.GetObject(index++));
json_cleanup_and_delete(decoded);
}
void it_should_support_objects_nested_in_objects()
{
JSON_Object nested_obj = new JSON_Object();
nested_obj.SetBool("nested", true);
JSON_Object obj = new JSON_Object();
obj.SetBool("nested", false);
obj.SetObject("object", nested_obj);
print_json(obj);
Test_AssertTrue("object.nested", obj.GetObject("object").GetBool("nested"));
json_cleanup_and_delete(obj);
}
void it_should_support_objects_nested_in_arrays()
{
JSON_Object nested_obj = new JSON_Object();
nested_obj.SetBool("nested", true);
JSON_Array arr = new JSON_Array();
arr.PushObject(nested_obj);
print_json(arr);
Test_AssertTrue("0.nested", arr.GetObject(0).GetBool("nested"));
json_cleanup_and_delete(arr);
}
void it_should_support_arrays_nested_in_objects()
{
JSON_Array nested_arr = new JSON_Array();
nested_arr.PushBool(true);
JSON_Object obj = new JSON_Object();
obj.SetObject("array", nested_arr);
print_json(obj);
JSON_Array obj_array = view_as<JSON_Array>(obj.GetObject("array"));
Test_AssertTrue("array.0", obj_array.GetBool(0));
json_cleanup_and_delete(obj);
}
void it_should_support_arrays_nested_in_arrays()
{
JSON_Array nested_arr = new JSON_Array();
nested_arr.PushBool(true);
JSON_Array arr = new JSON_Array();
arr.PushObject(nested_arr);
print_json(arr);
JSON_Array arr_array = view_as<JSON_Array>(arr.GetObject(0));
Test_AssertTrue("0.0", arr_array.GetBool(0));
json_cleanup_and_delete(arr);
}
void it_should_support_basic_methodmaps()
{
Player player = new Player();
player.id = 1;
print_json(player);
json_cleanup_and_delete(player);
Player decoded = view_as<Player>(json_decode(json_encode_output));
Test_AssertEqual("decoded.id", decoded.id, 1);
json_cleanup_and_delete(decoded);
}
void it_should_support_nested_methodmaps()
{
Weapon weapon = new Weapon();
weapon.id = 1;
weapon.SetName("ak47");
Player player = new Player();
player.id = 1;
player.weapon = weapon;
print_json(player);
Test_AssertEqual("weapon.id", weapon.id, 1);
Test_Output("changing weapon.id via player");
player.weapon.id = 2;
Test_AssertEqual("weapon.id", weapon.id, 2);
json_cleanup_and_delete(player);
}
void it_should_pretty_print()
{
JSON_Array empty_arr = new JSON_Array();
_json_encode(empty_arr, JSON_ENCODE_PRETTY);
json_cleanup_and_delete(empty_arr);
Test_AssertStringsEqual("output", json_encode_output, "[]");
JSON_Object empty_obj = new JSON_Object();
_json_encode(empty_obj, JSON_ENCODE_PRETTY);
json_cleanup_and_delete(empty_obj);
Test_AssertStringsEqual("output", json_encode_output, "{}");
JSON_Array child_arr = new JSON_Array();
child_arr.PushInt(1);
child_arr.PushObject(new JSON_Array());
JSON_Object child_obj = new JSON_Object();
child_obj.SetObject("im_indented", null);
child_obj.SetObject("second_depth", child_arr);
JSON_Object parent_obj = new JSON_Object();
parent_obj.SetBool("pretty_printing", true);
parent_obj.SetObject("first_depth", child_obj);
_json_encode(parent_obj, JSON_ENCODE_PRETTY);
Test_AssertStringsEqual("output", json_encode_output, "{\n \"pretty_printing\": true,\n \"first_depth\": {\n \"im_indented\": null,\n \"second_depth\": [\n 1,\n []\n ]\n }\n}");
strcopy(JSON_PP_AFTER_COLON, sizeof(JSON_PP_AFTER_COLON), " ");
strcopy(JSON_PP_INDENT, sizeof(JSON_PP_INDENT), "");
strcopy(JSON_PP_NEWLINE, sizeof(JSON_PP_NEWLINE), " ");
_json_encode(parent_obj, JSON_ENCODE_PRETTY);
json_cleanup_and_delete(parent_obj);
Test_AssertStringsEqual("output", json_encode_output, "{ \"pretty_printing\": true, \"first_depth\": { \"im_indented\": null, \"second_depth\": [ 1, [] ] } }");
}
void it_should_trim_floats()
{
JSON_Array arr = new JSON_Array();
arr.PushFloat(0.0);
arr.PushFloat(1.0);
arr.PushFloat(10.01);
arr.PushFloat(-0.0);
arr.PushFloat(-1.0);
arr.PushFloat(-10.01);
_json_encode(arr);
json_cleanup_and_delete(arr);
Test_AssertStringsEqual("output", json_encode_output, "[0.0,1.0,10.01,-0.0,-1.0,-10.01]");
}
#if SM_INT64_SUPPORTED
void it_should_support_int64()
{
char[] input = "[-9999999999,-2147483649,-2147483648,-1,0,1,2147483647,4294967295,4294967296,9999999999]";
JSON_Array arr = view_as<JSON_Array>(json_decode(input));
Test_AssertEqual("0 type", arr.GetType(0), JSON_Type_Int64);
Test_AssertEqual("1 type", arr.GetType(1), JSON_Type_Int64);
Test_AssertEqual("2 type", arr.GetType(2), JSON_Type_Int);
Test_AssertEqual("3 type", arr.GetType(3), JSON_Type_Int);
Test_AssertEqual("4 type", arr.GetType(4), JSON_Type_Int);
Test_AssertEqual("5 type", arr.GetType(5), JSON_Type_Int);
Test_AssertEqual("6 type", arr.GetType(6), JSON_Type_Int);
Test_AssertEqual("7 type", arr.GetType(7), JSON_Type_Int64);
Test_AssertEqual("8 type", arr.GetType(8), JSON_Type_Int64);
Test_AssertEqual("9 type", arr.GetType(9), JSON_Type_Int64);
_json_encode(arr);
json_cleanup_and_delete(arr);
Test_AssertStringsEqual("output", json_encode_output, input);
}
#endif
void it_should_remove_meta_keys_from_arrays()
{
JSON_Array arr = new JSON_Array();
arr.PushString("hello");
arr.PushInt(0);
Test_AssertEqual("0 type", arr.GetType(0), JSON_Type_String);
Test_AssertEqual("0 length", arr.GetSize(0), 6);
Test_AssertEqual("1 type", arr.GetType(1), JSON_Type_Int);
arr.Remove(0);
Test_AssertEqual("0 type", arr.GetType(0), JSON_Type_Int);
Test_AssertEqual("0 length", arr.GetSize(0), -1);
Test_AssertEqual("1 type", arr.GetType(1), JSON_Type_Invalid);
json_cleanup_and_delete(arr);
}
void it_should_remove_meta_keys_from_objects()
{
JSON_Object obj = new JSON_Object();
obj.SetString("hello", "world");
obj.SetInt("zero", 0);
Test_AssertEqual("hello type", obj.GetType("hello"), JSON_Type_String);
Test_AssertEqual("hello length", obj.GetSize("hello"), 6);
Test_AssertEqual("zero type", obj.GetType("zero"), JSON_Type_Int);
obj.Remove("hello");
Test_AssertEqual("hello type", obj.GetType("hello"), JSON_Type_Invalid);
Test_AssertEqual("hello length", obj.GetSize("hello"), -1);
Test_AssertEqual("zero type", obj.GetType("zero"), JSON_Type_Int);
json_cleanup_and_delete(obj);
}
void it_should_shift_array_down_after_removed_index()
{
JSON_Array arr = new JSON_Array();
arr.PushString("leet");
arr.PushString("\"leet\"");
arr.PushInt(9001);
arr.PushFloat(-13.37);
arr.PushBool(true);
arr.PushBool(false);
print_json(arr);
Test_Assert("remove first element", check_array_remove(arr, 0));
Test_Assert("remove last element", check_array_remove(arr, arr.Length - 1));
int max = arr.Length - 1;
Test_Assert("remove random element", check_array_remove(arr, GetRandomInt(0, max)));
Test_AssertEqual("array length", arr.Length, max);
json_cleanup_and_delete(arr);
}
void it_should_not_merge_using_array()
{
JSON_Object obj = new JSON_Object();
JSON_Array arr = new JSON_Array();
JSON_Array arr2 = new JSON_Array();
Test_Assert("merge failed: obj <- arr", obj.Merge(arr) == false);
Test_Assert("merge failed: arr <- arr", arr.Merge(arr2) == false);
json_cleanup_and_delete(obj);
json_cleanup_and_delete(arr);
json_cleanup_and_delete(arr2);
}
void it_should_merge_objects_with_replacement()
{
JSON_Object obj1 = new JSON_Object();
obj1.SetInt("key1", 1);
obj1.SetBool("replaced", false);
obj1.SetHidden("replaced", false);
JSON_Object obj2 = new JSON_Object();
obj2.SetInt("key2", 2);
obj2.SetBool("replaced", true);
obj2.SetHidden("replaced", true);
if (! Test_Assert("merged successfully", obj1.Merge(obj2))) {
// if this assertion fails, testing cannot continue
return;
}
print_json(obj1);
Test_Assert("merged has key2", obj1.HasKey("key2"));
Test_AssertEqual("merged key2 type", obj1.GetType("key2"), JSON_Type_Int);
Test_AssertEqual("merged key2", obj1.GetInt("key2"), 2);
Test_AssertEqual("merged replaced type", obj1.GetType("replaced"), JSON_Type_Bool);
Test_AssertTrue("merged replaced", obj1.GetBool("replaced"));
Test_AssertTrue("merged replaced hidden", obj1.GetHidden("replaced"));
json_cleanup_and_delete(obj1);
json_cleanup_and_delete(obj2);
}
void it_should_merge_objects_without_replacement()
{
JSON_Object obj1 = new JSON_Object();
obj1.SetInt("key1", 1);
obj1.SetBool("replaced", false);
obj1.SetHidden("replaced", false);
JSON_Object obj2 = new JSON_Object();
obj2.SetInt("key2", 2);
obj2.SetBool("replaced", true);
obj2.SetHidden("replaced", true);
if (! Test_Assert("merged successfully", obj1.Merge(obj2, JSON_NONE))) {
// if this assertion fails, testing cannot continue
return;
}
print_json(obj1);
Test_Assert("merged has key2", obj1.HasKey("key2"));
Test_AssertEqual("merged key2 type", obj1.GetType("key2"), JSON_Type_Int);
Test_AssertEqual("merged key2", obj1.GetInt("key2"), 2);
Test_AssertEqual("merged replaced type", obj1.GetType("replaced"), JSON_Type_Bool);
Test_AssertFalse("merged replaced", obj1.GetBool("replaced"));
Test_AssertFalse("merged replaced hidden", obj1.GetHidden("replaced"));
json_cleanup_and_delete(obj1);
json_cleanup_and_delete(obj2);
}
void it_should_not_concat_using_object()
{
JSON_Array arr = new JSON_Array();
JSON_Array obj = view_as<JSON_Array>(new JSON_Object());
Test_Assert("concat failed: arr <- obj", arr.Concat(obj) == false);
json_cleanup_and_delete(arr);
json_cleanup_and_delete(obj);
}
void it_should_concat_arrays()
{
JSON_Array arr1 = new JSON_Array();
arr1.PushInt(1);
arr1.SetHidden(0, true);
JSON_Array arr2 = new JSON_Array();
arr2.PushInt(2);
arr2.PushInt(3);
arr2.PushInt(4);
arr2.SetHidden(2, true);
if (! Test_Assert("merged successfully", arr1.Concat(arr2))) {
// if this assertion fails, testing cannot continue
return;
}
print_json(arr1);
Test_AssertEqual("merged length", arr1.Length, 4);
Test_AssertStringsEqual("output", json_encode_output, "[2,3]");
json_cleanup_and_delete(arr1);
json_cleanup_and_delete(arr2);
}
void it_should_copy_flat_arrays()
{
JSON_Array arr = new JSON_Array();
arr.PushInt(1);
arr.PushInt(2);
arr.PushInt(3);
JSON_Array copy = arr.DeepCopy();
Test_AssertEqual("copy length", copy.Length, arr.Length);
Test_AssertEqual("copy 0", copy.GetInt(0), 1);
Test_AssertEqual("copy 1", copy.GetInt(1), 2);
Test_AssertEqual("copy 2", copy.GetInt(2), 3);
arr.PushInt(4);
Test_AssertNotEqual("copy length", copy.Length, arr.Length);
json_cleanup_and_delete(arr);
json_cleanup_and_delete(copy);
}
void it_should_copy_flat_objects()
{
JSON_Object obj = new JSON_Object();
obj.SetInt("key1", 1);
obj.SetInt("key2", 2);
obj.SetInt("key3", 3);
JSON_Object copy = obj.DeepCopy();
Test_AssertEqual("copy length", copy.Length, obj.Length);
Test_AssertEqual("copy key1", copy.GetInt("key1"), 1);
Test_AssertEqual("copy key2", copy.GetInt("key2"), 2);
Test_AssertEqual("copy key3", copy.GetInt("key3"), 3);
obj.SetInt("key4", 4);
Test_AssertNotEqual("copy length", copy.Length, obj.Length);
json_cleanup_and_delete(obj);
json_cleanup_and_delete(copy);
}
void it_should_shallow_copy_arrays()
{
JSON_Array arr = new JSON_Array();
arr.PushObject(new JSON_Array());
JSON_Array copy = arr.ShallowCopy();
Test_AssertEqual("copy length", copy.Length, arr.Length);
Test_Assert("copy 0 == arr 0", copy.GetObject(0) == arr.GetObject(0));
json_cleanup_and_delete(arr);
copy.Remove(0);
json_cleanup_and_delete(copy);
}
void it_should_shallow_copy_objects()
{
JSON_Object obj = new JSON_Object();
obj.SetObject("nested", new JSON_Object());
JSON_Object copy = obj.ShallowCopy();
Test_AssertEqual("copy length", copy.Length, obj.Length);
Test_Assert("copy nested == obj nested", copy.GetObject("nested") == obj.GetObject("nested"));
json_cleanup_and_delete(obj);
copy.Remove("nested");
json_cleanup_and_delete(copy);
}
void it_should_preserve_ordered_keys_on_shallow_copy()
{
JSON_Object obj = new JSON_Object();
obj.SetInt("keyA", 1);
obj.SetInt("key2", 2);
obj.SetInt("keyC", 3);
obj.SetInt("key4", 4);
JSON_Object copy = obj.ShallowCopy();
int length = obj.Length;
int original_key_length = 0;
int copy_key_length = 0;
char key_assert_desc[16];
for (int i = 0; i < length; i += 1) {
FormatEx(key_assert_desc, sizeof(key_assert_desc), "copy key %d", i);
original_key_length = obj.GetKeySize(i);
char[] original_key = new char[original_key_length];
obj.GetKey(i, original_key, original_key_length);
copy_key_length = copy.GetKeySize(i);
char[] copy_key = new char[copy_key_length];
copy.GetKey(i, copy_key, copy_key_length);
Test_AssertStringsEqual(key_assert_desc, original_key, copy_key);
}
json_cleanup_and_delete(obj);
json_cleanup_and_delete(copy);
}
void it_should_deep_copy_arrays()
{
JSON_Array arr = new JSON_Array();
arr.PushObject(new JSON_Array());
JSON_Array copy = arr.DeepCopy();
Test_AssertEqual("copy length", copy.Length, arr.Length);
Test_Assert("copy 0 != arr 0", copy.GetObject(0) != arr.GetObject(0));
json_cleanup_and_delete(arr);
json_cleanup_and_delete(copy);
}
void it_should_deep_copy_objects()
{
JSON_Object obj = new JSON_Object();
obj.SetObject("nested", new JSON_Object());
obj.SetObject("null", null);
JSON_Object copy = obj.DeepCopy();
Test_AssertEqual("copy length", copy.Length, obj.Length);
Test_Assert("copy nested != obj nested", copy.GetObject("nested") != obj.GetObject("nested"));
Test_Assert("null is still null", copy.GetObject("null") == obj.GetObject("null"));
json_cleanup_and_delete(obj);
json_cleanup_and_delete(copy);
}
void it_should_allow_single_quotes()
{
// array
JSON_Array arr = view_as<JSON_Array>(
json_decode(
"['single quotes', \"double quotes\", 'single \\'single\\' quotes', 'single \"double\" quotes', \"double 'single' quotes\", \"double \\\"double\\\" quotes\"]",
JSON_DECODE_SINGLE_QUOTES
)
);
if (Test_AssertNotNull("array", arr)) {
print_json(arr);
Test_AssertEqual("array length", arr.Length, 6);
json_cleanup_and_delete(arr);
}
// object
JSON_Object obj = json_decode(
"{'key': \"value\"}",
JSON_DECODE_SINGLE_QUOTES
);
if (Test_AssertNotNull("object", obj)) {
print_json(obj);
Test_Assert("object has key", obj.HasKey("key"));
json_cleanup_and_delete(obj);
}
}
void it_should_return_default_values_for_missing_elements()
{
// array
JSON_Array arr = new JSON_Array();
Test_AssertEqual("array default int value", arr.GetInt(0, 1), 1);
Test_AssertFloatsEqual("array default float value", arr.GetFloat(0, 1.0), 1.0);
Test_AssertTrue("array default bool value", arr.GetBool(0, true));
Test_AssertNull("array default null value", arr.GetObject(0, null));
Test_AssertEqual("array default arr value", arr.GetObject(0, arr), arr);
json_cleanup_and_delete(arr);
// object
JSON_Object obj = new JSON_Object();
Test_AssertEqual("object default int value", obj.GetInt("_", 1), 1);
Test_AssertFloatsEqual("object default float value", obj.GetFloat("_", 1.0), 1.0);
Test_AssertTrue("object default bool value", obj.GetBool("_", true));
Test_AssertNull("object default null value", obj.GetObject("_", null));
Test_AssertEqual("object default obj value", obj.GetObject("_", obj), obj);
json_cleanup_and_delete(obj);
}
void it_should_autocleanup_merged_objects()
{
JSON_Object obj1 = new JSON_Object();
JSON_Object obj2 = new JSON_Object();
JSON_Object nested1 = new JSON_Object();
JSON_Object nested2 = new JSON_Object();
Test_Output("merging without replacement");
obj1.SetObject("nested", nested1);
obj2.SetObject("nested", nested2);
obj1.Merge(obj2, JSON_MERGE_CLEANUP);
Test_Assert("nested1 is valid", IsValidHandle(nested1));
Test_Assert("nested2 is valid", IsValidHandle(nested2));
Test_Output("merging with replacement and without cleanup");
obj1.SetObject("nested", nested1);
obj2.SetObject("nested", nested2);
obj1.Merge(obj2, JSON_MERGE_REPLACE);
Test_Assert("nested1 is valid", IsValidHandle(nested1));
Test_Assert("nested2 is valid", IsValidHandle(nested2));