tests/test_json.c

changeset 1081
33c9d7e7d830
parent 1080
e16f4f336e3c
child 1082
46cdc8689fc4
equal deleted inserted replaced
1080:e16f4f336e3c 1081:33c9d7e7d830
711 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); 711 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
712 } 712 }
713 cx_testing_allocator_destroy(&talloc); 713 cx_testing_allocator_destroy(&talloc);
714 } 714 }
715 715
716 CX_TEST(test_json_write_default_format) { 716
717 CxTestingAllocator talloc; 717 CX_TEST_SUBROUTINE(test_json_write_sub,
718 cx_testing_allocator_init(&talloc);
719 CxAllocator *allocator = &talloc.base;
720 CX_TEST_DO {
721 // expected value
722 cxstring expected = CX_STR("{\"bool\":false,\"nested\":{\"floats\":[3.1415,47.11,8.15],\"ints\":[4,8,15,16,23,42],\"literals\":[true,null,false],\"string\":\"test\"},\"num\":47.11,\"strings\":[\"hello\",\"world\"]}");
723
724 // create the value
725 CxJsonValue *obj = cxJsonCreateObj(allocator);
726 cxJsonObjPutLiteral(obj, CX_STR("bool"), CX_JSON_FALSE);
727 cxJsonObjPutNumber(obj, CX_STR("num"), 47.11);
728 CxJsonValue *strings = cxJsonObjPutArr(obj, CX_STR("strings"));
729 cxJsonArrAddCxStrings(strings, (cxstring[]) {CX_STR("hello"), CX_STR("world")}, 2);
730 CxJsonValue *nested = cxJsonObjPutObj(obj, CX_STR("nested"));
731 cxJsonObjPutString(nested, CX_STR("string"), "test");
732 cxJsonArrAddNumbers(cxJsonObjPutArr(nested, CX_STR("floats")),
733 (double[]){3.1415, 47.11, 8.15}, 3);
734 cxJsonArrAddLiterals(cxJsonObjPutArr(nested, CX_STR("literals")),
735 (CxJsonLiteral[]){CX_JSON_TRUE, CX_JSON_NULL, CX_JSON_FALSE}, 3);
736 cxJsonArrAddIntegers(cxJsonObjPutArr(nested, CX_STR("ints")),
737 (int64_t[]){4, 8, 15, 16, 23, 42}, 6);
738
739 // write it to a buffer
740 CxBuffer buf;
741 cxBufferInit(&buf, NULL, 256, NULL, CX_BUFFER_DEFAULT);
742 int result = cxJsonWrite(&buf, obj, (cx_write_func) cxBufferWrite, NULL);
743 cxBufferTerminate(&buf); // makes debugging easier
744 CX_TEST_ASSERT(result == 0);
745
746 // compare the string
747 CX_TEST_ASSERT(0 == cx_strcmp(cx_strn(buf.space, buf.size), expected));
748
749 // destroy everything
750 cxBufferDestroy(&buf);
751 cxJsonValueFree(obj);
752 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
753 }
754 cx_testing_allocator_destroy(&talloc);
755 }
756
757
758 CX_TEST_SUBROUTINE(test_json_write_pretty_default_sub,
759 const CxAllocator *allocator, 718 const CxAllocator *allocator,
760 cxstring expected, 719 cxstring expected,
761 bool use_spaces 720 const CxJsonWriter *writer
762 ) { 721 ) {
763 // create the value 722 // create the value
764 CxJsonValue *obj = cxJsonCreateObj(allocator); 723 CxJsonValue *obj = cxJsonCreateObj(allocator);
765 cxJsonObjPutLiteral(obj, CX_STR("bool"), CX_JSON_FALSE); 724 cxJsonObjPutLiteral(obj, CX_STR("bool"), CX_JSON_FALSE);
766 cxJsonObjPutNumber(obj, CX_STR("int"), 47); // purposely use PutNumber to put an int 725 cxJsonObjPutNumber(obj, CX_STR("int"), 47); // purposely use PutNumber to put an int
786 cxJsonArrAddIntegers(ints, (int64_t[]){42}, 1); 745 cxJsonArrAddIntegers(ints, (int64_t[]){42}, 1);
787 746
788 // write it to a buffer 747 // write it to a buffer
789 CxBuffer buf; 748 CxBuffer buf;
790 cxBufferInit(&buf, NULL, 512, NULL, CX_BUFFER_DEFAULT); 749 cxBufferInit(&buf, NULL, 512, NULL, CX_BUFFER_DEFAULT);
791 CxJsonWriter writer = cxJsonWriterPretty(use_spaces); 750 int result = cxJsonWrite(&buf, obj, (cx_write_func) cxBufferWrite, writer);
792 int result = cxJsonWrite(&buf, obj, (cx_write_func) cxBufferWrite, &writer);
793 cxBufferTerminate(&buf); // makes debugging easier 751 cxBufferTerminate(&buf); // makes debugging easier
794 CX_TEST_ASSERT(result == 0); 752 CX_TEST_ASSERT(result == 0);
795 753
796 // compare the string 754 // compare the string
797 CX_TEST_ASSERT(0 == cx_strcmp(cx_strn(buf.space, buf.size), expected)); 755 CX_TEST_ASSERT(0 == cx_strcmp(cx_strn(buf.space, buf.size), expected));
798 756
799 // destroy everything 757 // destroy everything
800 cxBufferDestroy(&buf); 758 cxBufferDestroy(&buf);
801 cxJsonValueFree(obj); 759 cxJsonValueFree(obj);
760 }
761
762 CX_TEST(test_json_write_default_format) {
763 CxTestingAllocator talloc;
764 cx_testing_allocator_init(&talloc);
765 CxAllocator *allocator = &talloc.base;
766 CX_TEST_DO {
767 // expected value
768 cxstring expected = CX_STR(
769 "{\"bool\":false,"
770 "\"int\":47,"
771 "\"nested\":{"
772 "\"floats\":[3.1415,47.11,8.15],"
773 "\"ints\":[4,8,15,[16,23],42],"
774 "\"literals\":[true,null,false],"
775 "\"objects\":[{"
776 "\"name1\":1,"
777 "\"name2\":3"
778 "},{"
779 "\"name1\":3,"
780 "\"name2\":7"
781 "}]"
782 "},"
783 "\"strings\":[\"hello\",\"world\"]"
784 "}"
785 );
786
787 CxJsonWriter writer = cxJsonWriterCompact();
788 CX_TEST_CALL_SUBROUTINE(test_json_write_sub, allocator, expected, &writer);
789 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
790 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
791 }
792 cx_testing_allocator_destroy(&talloc);
802 } 793 }
803 794
804 CX_TEST(test_json_write_pretty_default_spaces) { 795 CX_TEST(test_json_write_pretty_default_spaces) {
805 CxTestingAllocator talloc; 796 CxTestingAllocator talloc;
806 cx_testing_allocator_init(&talloc); 797 cx_testing_allocator_init(&talloc);
807 CxAllocator *allocator = &talloc.base; 798 CxAllocator *allocator = &talloc.base;
808 CX_TEST_DO { 799 CX_TEST_DO {
809 // expected value
810 cxstring expected = CX_STR( 800 cxstring expected = CX_STR(
811 "{\n" 801 "{\n"
812 " \"bool\": false,\n" 802 " \"bool\": false,\n"
813 " \"int\": 47,\n" 803 " \"int\": 47,\n"
814 " \"nested\": {\n" 804 " \"nested\": {\n"
825 " },\n" 815 " },\n"
826 " \"strings\": [\"hello\", \"world\"]\n" 816 " \"strings\": [\"hello\", \"world\"]\n"
827 "}" 817 "}"
828 ); 818 );
829 819
830 CX_TEST_CALL_SUBROUTINE(test_json_write_pretty_default_sub, allocator, expected, true); 820 CxJsonWriter writer = cxJsonWriterPretty(true);
821 CX_TEST_CALL_SUBROUTINE(test_json_write_sub, allocator, expected, &writer);
831 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); 822 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
832 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); 823 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
833 } 824 }
834 cx_testing_allocator_destroy(&talloc); 825 cx_testing_allocator_destroy(&talloc);
835 } 826 }
837 CX_TEST(test_json_write_pretty_default_tabs) { 828 CX_TEST(test_json_write_pretty_default_tabs) {
838 CxTestingAllocator talloc; 829 CxTestingAllocator talloc;
839 cx_testing_allocator_init(&talloc); 830 cx_testing_allocator_init(&talloc);
840 CxAllocator *allocator = &talloc.base; 831 CxAllocator *allocator = &talloc.base;
841 CX_TEST_DO { 832 CX_TEST_DO {
842 // expected value
843 cxstring expected = CX_STR( 833 cxstring expected = CX_STR(
844 "{\n" 834 "{\n"
845 "\t\"bool\": false,\n" 835 "\t\"bool\": false,\n"
846 "\t\"int\": 47,\n" 836 "\t\"int\": 47,\n"
847 "\t\"nested\": {\n" 837 "\t\"nested\": {\n"
857 "\t\t}]\n" 847 "\t\t}]\n"
858 "\t},\n" 848 "\t},\n"
859 "\t\"strings\": [\"hello\", \"world\"]\n" 849 "\t\"strings\": [\"hello\", \"world\"]\n"
860 "}" 850 "}"
861 ); 851 );
862 CX_TEST_CALL_SUBROUTINE(test_json_write_pretty_default_sub, allocator, expected, false); 852 CxJsonWriter writer = cxJsonWriterPretty(false);
853 CX_TEST_CALL_SUBROUTINE(test_json_write_sub, allocator, expected, &writer);
863 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); 854 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
864 } 855 }
865 cx_testing_allocator_destroy(&talloc); 856 cx_testing_allocator_destroy(&talloc);
866 } 857 }
867 858

mercurial