752 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); |
752 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); |
753 } |
753 } |
754 cx_testing_allocator_destroy(&talloc); |
754 cx_testing_allocator_destroy(&talloc); |
755 } |
755 } |
756 |
756 |
|
757 |
|
758 CX_TEST_SUBROUTINE(test_json_write_pretty_default_sub, |
|
759 const CxAllocator *allocator, |
|
760 cxstring expected, |
|
761 bool use_spaces |
|
762 ) { |
|
763 // create the value |
|
764 CxJsonValue *obj = cxJsonCreateObj(allocator); |
|
765 cxJsonObjPutLiteral(obj, CX_STR("bool"), CX_JSON_FALSE); |
|
766 cxJsonObjPutNumber(obj, CX_STR("int"), 47); // purposely use PutNumber to put an int |
|
767 CxJsonValue *strings = cxJsonObjPutArr(obj, CX_STR("strings")); |
|
768 cxJsonArrAddCxStrings(strings, (cxstring[]) {CX_STR("hello"), CX_STR("world")}, 2); |
|
769 CxJsonValue *nested = cxJsonObjPutObj(obj, CX_STR("nested")); |
|
770 CxJsonValue *objects = cxJsonObjPutArr(nested, CX_STR("objects")); |
|
771 CxJsonValue *obj_in_arr[2] = {cxJsonCreateObj(allocator), cxJsonCreateObj(allocator)}; |
|
772 cxJsonObjPutInteger(obj_in_arr[0], CX_STR("name1"), 1); |
|
773 cxJsonObjPutInteger(obj_in_arr[0], CX_STR("name2"), 3); |
|
774 cxJsonObjPutInteger(obj_in_arr[1], CX_STR("name1"), 3); |
|
775 cxJsonObjPutInteger(obj_in_arr[1], CX_STR("name2"), 7); |
|
776 cxJsonArrAddValues(objects, obj_in_arr, 2); |
|
777 cxJsonArrAddNumbers(cxJsonObjPutArr(nested, CX_STR("floats")), |
|
778 (double[]){3.1415, 47.11, 8.15}, 3); |
|
779 cxJsonArrAddLiterals(cxJsonObjPutArr(nested, CX_STR("literals")), |
|
780 (CxJsonLiteral[]){CX_JSON_TRUE, CX_JSON_NULL, CX_JSON_FALSE}, 3); |
|
781 CxJsonValue *ints = cxJsonObjPutArr(nested, CX_STR("ints")); |
|
782 cxJsonArrAddIntegers(ints, (int64_t[]){4, 8, 15}, 3); |
|
783 CxJsonValue *nested_array = cxJsonCreateArr(allocator); |
|
784 cxJsonArrAddValues(ints, &nested_array, 1); |
|
785 cxJsonArrAddIntegers(nested_array, (int64_t[]){16, 23}, 2); |
|
786 cxJsonArrAddIntegers(ints, (int64_t[]){42}, 1); |
|
787 |
|
788 // write it to a buffer |
|
789 CxBuffer buf; |
|
790 cxBufferInit(&buf, NULL, 512, NULL, CX_BUFFER_DEFAULT); |
|
791 CxJsonWriter writer = cxJsonWriterPretty(use_spaces); |
|
792 int result = cxJsonWrite(&buf, obj, (cx_write_func) cxBufferWrite, &writer); |
|
793 cxBufferTerminate(&buf); // makes debugging easier |
|
794 CX_TEST_ASSERT(result == 0); |
|
795 |
|
796 // compare the string |
|
797 CX_TEST_ASSERT(0 == cx_strcmp(cx_strn(buf.space, buf.size), expected)); |
|
798 |
|
799 // destroy everything |
|
800 cxBufferDestroy(&buf); |
|
801 cxJsonValueFree(obj); |
|
802 } |
|
803 |
757 CX_TEST(test_json_write_pretty_default_spaces) { |
804 CX_TEST(test_json_write_pretty_default_spaces) { |
758 CxTestingAllocator talloc; |
805 CxTestingAllocator talloc; |
759 cx_testing_allocator_init(&talloc); |
806 cx_testing_allocator_init(&talloc); |
760 CxAllocator *allocator = &talloc.base; |
807 CxAllocator *allocator = &talloc.base; |
761 CX_TEST_DO { |
808 CX_TEST_DO { |
778 " },\n" |
825 " },\n" |
779 " \"strings\": [\"hello\", \"world\"]\n" |
826 " \"strings\": [\"hello\", \"world\"]\n" |
780 "}" |
827 "}" |
781 ); |
828 ); |
782 |
829 |
783 // create the value |
830 CX_TEST_CALL_SUBROUTINE(test_json_write_pretty_default_sub, allocator, expected, true); |
784 CxJsonValue *obj = cxJsonCreateObj(allocator); |
831 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); |
785 cxJsonObjPutLiteral(obj, CX_STR("bool"), CX_JSON_FALSE); |
832 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); |
786 cxJsonObjPutNumber(obj, CX_STR("int"), 47); // purposely use PutNumber to put an int |
833 } |
787 CxJsonValue *strings = cxJsonObjPutArr(obj, CX_STR("strings")); |
834 cx_testing_allocator_destroy(&talloc); |
788 cxJsonArrAddCxStrings(strings, (cxstring[]) {CX_STR("hello"), CX_STR("world")}, 2); |
835 } |
789 CxJsonValue *nested = cxJsonObjPutObj(obj, CX_STR("nested")); |
836 |
790 CxJsonValue *objects = cxJsonObjPutArr(nested, CX_STR("objects")); |
837 CX_TEST(test_json_write_pretty_default_tabs) { |
791 CxJsonValue *obj_in_arr[2] = {cxJsonCreateObj(allocator), cxJsonCreateObj(allocator)}; |
838 CxTestingAllocator talloc; |
792 cxJsonObjPutInteger(obj_in_arr[0], CX_STR("name1"), 1); |
839 cx_testing_allocator_init(&talloc); |
793 cxJsonObjPutInteger(obj_in_arr[0], CX_STR("name2"), 3); |
840 CxAllocator *allocator = &talloc.base; |
794 cxJsonObjPutInteger(obj_in_arr[1], CX_STR("name1"), 3); |
841 CX_TEST_DO { |
795 cxJsonObjPutInteger(obj_in_arr[1], CX_STR("name2"), 7); |
842 // expected value |
796 cxJsonArrAddValues(objects, obj_in_arr, 2); |
843 cxstring expected = CX_STR( |
797 cxJsonArrAddNumbers(cxJsonObjPutArr(nested, CX_STR("floats")), |
844 "{\n" |
798 (double[]){3.1415, 47.11, 8.15}, 3); |
845 "\t\"bool\": false,\n" |
799 cxJsonArrAddLiterals(cxJsonObjPutArr(nested, CX_STR("literals")), |
846 "\t\"int\": 47,\n" |
800 (CxJsonLiteral[]){CX_JSON_TRUE, CX_JSON_NULL, CX_JSON_FALSE}, 3); |
847 "\t\"nested\": {\n" |
801 CxJsonValue *ints = cxJsonObjPutArr(nested, CX_STR("ints")); |
848 "\t\t\"floats\": [3.1415, 47.11, 8.15],\n" |
802 cxJsonArrAddIntegers(ints, (int64_t[]){4, 8, 15}, 3); |
849 "\t\t\"ints\": [4, 8, 15, [16, 23], 42],\n" |
803 CxJsonValue *nested_array = cxJsonCreateArr(allocator); |
850 "\t\t\"literals\": [true, null, false],\n" |
804 cxJsonArrAddValues(ints, &nested_array, 1); |
851 "\t\t\"objects\": [{\n" |
805 cxJsonArrAddIntegers(nested_array, (int64_t[]){16, 23}, 2); |
852 "\t\t\t\"name1\": 1,\n" |
806 cxJsonArrAddIntegers(ints, (int64_t[]){42}, 1); |
853 "\t\t\t\"name2\": 3\n" |
807 |
854 "\t\t}, {\n" |
808 // write it to a buffer |
855 "\t\t\t\"name1\": 3,\n" |
809 CxBuffer buf; |
856 "\t\t\t\"name2\": 7\n" |
810 cxBufferInit(&buf, NULL, 512, NULL, CX_BUFFER_DEFAULT); |
857 "\t\t}]\n" |
811 CxJsonWriter writer = cxJsonWriterPretty(true); |
858 "\t},\n" |
812 int result = cxJsonWrite(&buf, obj, (cx_write_func) cxBufferWrite, &writer); |
859 "\t\"strings\": [\"hello\", \"world\"]\n" |
813 cxBufferTerminate(&buf); // makes debugging easier |
860 "}" |
814 CX_TEST_ASSERT(result == 0); |
861 ); |
815 |
862 CX_TEST_CALL_SUBROUTINE(test_json_write_pretty_default_sub, allocator, expected, false); |
816 // compare the string |
|
817 CX_TEST_ASSERT(0 == cx_strcmp(cx_strn(buf.space, buf.size), expected)); |
|
818 |
|
819 // destroy everything |
|
820 cxBufferDestroy(&buf); |
|
821 cxJsonValueFree(obj); |
|
822 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); |
863 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); |
823 } |
864 } |
824 cx_testing_allocator_destroy(&talloc); |
865 cx_testing_allocator_destroy(&talloc); |
825 } |
866 } |
826 |
867 |