diff options
Diffstat (limited to 'tests/utests/basic/test_json.c')
-rw-r--r-- | tests/utests/basic/test_json.c | 443 |
1 files changed, 211 insertions, 232 deletions
diff --git a/tests/utests/basic/test_json.c b/tests/utests/basic/test_json.c index 1896b8a..08b7719 100644 --- a/tests/utests/basic/test_json.c +++ b/tests/utests/basic/test_json.c @@ -1,4 +1,4 @@ -/* +/** * @file test_json.c * @author: Radek Krejci <rkrejci@cesnet.cz> * @brief unit tests for a generic JSON parser @@ -27,39 +27,40 @@ test_general(void **state) /* empty */ str = ""; assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in)); - assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx)); - assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx, 0)); - lyjson_ctx_free(jsonctx); + assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); + CHECK_LOG_CTX("Empty JSON file.", "Line number 1."); str = " \n\t \n"; assert_non_null(ly_in_memory(in, str)); - assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx)); - assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx, 0)); - lyjson_ctx_free(jsonctx); + assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); + CHECK_LOG_CTX("Empty JSON file.", "Line number 3."); /* constant values */ str = "true"; assert_non_null(ly_in_memory(in, str)); - assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx)); - assert_int_equal(LYJSON_TRUE, lyjson_ctx_status(jsonctx, 0)); + assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); + assert_int_equal(LYJSON_TRUE, lyjson_ctx_status(jsonctx)); + assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); - assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx, 0)); + assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx)); lyjson_ctx_free(jsonctx); str = "false"; assert_non_null(ly_in_memory(in, str)); - assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx)); - assert_int_equal(LYJSON_FALSE, lyjson_ctx_status(jsonctx, 0)); + assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); + assert_int_equal(LYJSON_FALSE, lyjson_ctx_status(jsonctx)); + assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); - assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx, 0)); + assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx)); lyjson_ctx_free(jsonctx); str = "null"; assert_non_null(ly_in_memory(in, str)); - assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx)); - assert_int_equal(LYJSON_NULL, lyjson_ctx_status(jsonctx, 0)); + assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); + assert_int_equal(LYJSON_NULL, lyjson_ctx_status(jsonctx)); + assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); - assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx, 0)); + assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx)); lyjson_ctx_free(jsonctx); ly_in_free(in, 0); @@ -75,8 +76,8 @@ test_number(void **state) /* simple value */ str = "11"; assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in)); - assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx)); - assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0)); + assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); + assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx)); assert_string_equal("11", jsonctx->value); assert_int_equal(2, jsonctx->value_len); assert_int_equal(0, jsonctx->dynamic); @@ -85,8 +86,8 @@ test_number(void **state) /* fraction number */ str = "37.7668"; assert_non_null(ly_in_memory(in, str)); - assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx)); - assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0)); + assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); + assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx)); assert_string_equal("37.7668", jsonctx->value); assert_int_equal(7, jsonctx->value_len); assert_int_equal(0, jsonctx->dynamic); @@ -95,8 +96,8 @@ test_number(void **state) /* negative number */ str = "-122.3959"; assert_non_null(ly_in_memory(in, str)); - assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx)); - assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0)); + assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); + assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx)); assert_string_equal("-122.3959", jsonctx->value); assert_int_equal(9, jsonctx->value_len); assert_int_equal(0, jsonctx->dynamic); @@ -105,8 +106,8 @@ test_number(void **state) /* integer, positive exponent */ str = "550E3"; assert_non_null(ly_in_memory(in, str)); - assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx)); - assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0)); + assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); + assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx)); assert_string_equal("550000", jsonctx->value); assert_int_equal(6, jsonctx->value_len); assert_int_equal(1, jsonctx->dynamic); @@ -114,8 +115,8 @@ test_number(void **state) str = "-550E3"; assert_non_null(ly_in_memory(in, str)); - assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx)); - assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0)); + assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); + assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx)); assert_string_equal("-550000", jsonctx->value); assert_int_equal(7, jsonctx->value_len); assert_int_equal(1, jsonctx->dynamic); @@ -124,8 +125,8 @@ test_number(void **state) /* integer, negative exponent */ str = "1E-1"; assert_non_null(ly_in_memory(in, str)); - assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx)); - assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0)); + assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); + assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx)); assert_string_equal("0.1", jsonctx->value); assert_int_equal(3, jsonctx->value_len); assert_int_equal(1, jsonctx->dynamic); @@ -133,8 +134,8 @@ test_number(void **state) str = "15E-1"; assert_non_null(ly_in_memory(in, str)); - assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx)); - assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0)); + assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); + assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx)); assert_string_equal("1.5", jsonctx->value); assert_int_equal(3, jsonctx->value_len); assert_int_equal(1, jsonctx->dynamic); @@ -142,8 +143,8 @@ test_number(void **state) str = "-15E-1"; assert_non_null(ly_in_memory(in, str)); - assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx)); - assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0)); + assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); + assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx)); assert_string_equal("-1.5", jsonctx->value); assert_int_equal(4, jsonctx->value_len); assert_int_equal(1, jsonctx->dynamic); @@ -151,8 +152,8 @@ test_number(void **state) str = "16E-2"; assert_non_null(ly_in_memory(in, str)); - assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx)); - assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0)); + assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); + assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx)); assert_string_equal("0.16", jsonctx->value); assert_int_equal(4, jsonctx->value_len); assert_int_equal(1, jsonctx->dynamic); @@ -160,8 +161,8 @@ test_number(void **state) str = "-16E-2"; assert_non_null(ly_in_memory(in, str)); - assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx)); - assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0)); + assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); + assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx)); assert_string_equal("-0.16", jsonctx->value); assert_int_equal(5, jsonctx->value_len); assert_int_equal(1, jsonctx->dynamic); @@ -169,8 +170,8 @@ test_number(void **state) str = "17E-3"; assert_non_null(ly_in_memory(in, str)); - assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx)); - assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0)); + assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); + assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx)); assert_string_equal("0.017", jsonctx->value); assert_int_equal(5, jsonctx->value_len); assert_int_equal(1, jsonctx->dynamic); @@ -178,8 +179,8 @@ test_number(void **state) str = "-17E-3"; assert_non_null(ly_in_memory(in, str)); - assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx)); - assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0)); + assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); + assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx)); assert_string_equal("-0.017", jsonctx->value); assert_int_equal(6, jsonctx->value_len); assert_int_equal(1, jsonctx->dynamic); @@ -187,8 +188,8 @@ test_number(void **state) str = "21000E-2"; assert_non_null(ly_in_memory(in, str)); - assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx)); - assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0)); + assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); + assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx)); assert_string_equal("210", jsonctx->value); assert_int_equal(3, jsonctx->value_len); assert_int_equal(1, jsonctx->dynamic); @@ -196,8 +197,8 @@ test_number(void **state) str = "21000E-4"; assert_non_null(ly_in_memory(in, str)); - assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx)); - assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0)); + assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); + assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx)); assert_string_equal("2.1", jsonctx->value); assert_int_equal(3, jsonctx->value_len); assert_int_equal(1, jsonctx->dynamic); @@ -205,8 +206,8 @@ test_number(void **state) str = "21000E-7"; assert_non_null(ly_in_memory(in, str)); - assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx)); - assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0)); + assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); + assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx)); assert_string_equal("0.0021", jsonctx->value); assert_int_equal(6, jsonctx->value_len); assert_int_equal(1, jsonctx->dynamic); @@ -215,8 +216,8 @@ test_number(void **state) /* decimal number, positive exponent */ str = "5.087E1"; assert_non_null(ly_in_memory(in, str)); - assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx)); - assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0)); + assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); + assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx)); assert_string_equal("50.87", jsonctx->value); assert_int_equal(5, jsonctx->value_len); assert_int_equal(1, jsonctx->dynamic); @@ -224,8 +225,8 @@ test_number(void **state) str = "-5.087E1"; assert_non_null(ly_in_memory(in, str)); - assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx)); - assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0)); + assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); + assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx)); assert_string_equal("-50.87", jsonctx->value); assert_int_equal(6, jsonctx->value_len); assert_int_equal(1, jsonctx->dynamic); @@ -233,8 +234,8 @@ test_number(void **state) str = "5.087E5"; assert_non_null(ly_in_memory(in, str)); - assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx)); - assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0)); + assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); + assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx)); assert_string_equal("508700", jsonctx->value); assert_int_equal(6, jsonctx->value_len); assert_int_equal(1, jsonctx->dynamic); @@ -242,8 +243,8 @@ test_number(void **state) str = "59.1e+1"; assert_non_null(ly_in_memory(in, str)); - assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx)); - assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0)); + assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); + assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx)); assert_string_equal("591", jsonctx->value); assert_int_equal(3, jsonctx->value_len); assert_int_equal(1, jsonctx->dynamic); @@ -251,8 +252,8 @@ test_number(void **state) str = "0.005087E1"; assert_non_null(ly_in_memory(in, str)); - assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx)); - assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0)); + assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); + assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx)); assert_string_equal("0.05087", jsonctx->value); assert_int_equal(7, jsonctx->value_len); assert_int_equal(1, jsonctx->dynamic); @@ -260,8 +261,8 @@ test_number(void **state) str = "0.005087E2"; assert_non_null(ly_in_memory(in, str)); - assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx)); - assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0)); + assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); + assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx)); assert_string_equal("0.5087", jsonctx->value); assert_int_equal(6, jsonctx->value_len); assert_int_equal(1, jsonctx->dynamic); @@ -269,8 +270,8 @@ test_number(void **state) str = "0.005087E6"; assert_non_null(ly_in_memory(in, str)); - assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx)); - assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0)); + assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); + assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx)); assert_string_equal("5087", jsonctx->value); assert_int_equal(4, jsonctx->value_len); assert_int_equal(1, jsonctx->dynamic); @@ -278,8 +279,8 @@ test_number(void **state) str = "0.05087E6"; assert_non_null(ly_in_memory(in, str)); - assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx)); - assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0)); + assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); + assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx)); assert_string_equal("50870", jsonctx->value); assert_int_equal(5, jsonctx->value_len); assert_int_equal(1, jsonctx->dynamic); @@ -287,8 +288,8 @@ test_number(void **state) str = "0.005087E8"; assert_non_null(ly_in_memory(in, str)); - assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx)); - assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0)); + assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); + assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx)); assert_string_equal("508700", jsonctx->value); assert_int_equal(6, jsonctx->value_len); assert_int_equal(1, jsonctx->dynamic); @@ -297,8 +298,8 @@ test_number(void **state) /* decimal number, negative exponent */ str = "35.94e-1"; assert_non_null(ly_in_memory(in, str)); - assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx)); - assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0)); + assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); + assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx)); assert_string_equal("3.594", jsonctx->value); assert_int_equal(5, jsonctx->value_len); assert_int_equal(1, jsonctx->dynamic); @@ -306,8 +307,8 @@ test_number(void **state) str = "-35.94e-1"; assert_non_null(ly_in_memory(in, str)); - assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx)); - assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0)); + assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); + assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx)); assert_string_equal("-3.594", jsonctx->value); assert_int_equal(6, jsonctx->value_len); assert_int_equal(1, jsonctx->dynamic); @@ -315,8 +316,8 @@ test_number(void **state) str = "35.94e-2"; assert_non_null(ly_in_memory(in, str)); - assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx)); - assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0)); + assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); + assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx)); assert_string_equal("0.3594", jsonctx->value); assert_int_equal(6, jsonctx->value_len); assert_int_equal(1, jsonctx->dynamic); @@ -324,8 +325,8 @@ test_number(void **state) str = "35.94e-3"; assert_non_null(ly_in_memory(in, str)); - assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx)); - assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0)); + assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); + assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx)); assert_string_equal("0.03594", jsonctx->value); assert_int_equal(7, jsonctx->value_len); assert_int_equal(1, jsonctx->dynamic); @@ -333,8 +334,8 @@ test_number(void **state) str = "0.3594e-1"; assert_non_null(ly_in_memory(in, str)); - assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx)); - assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0)); + assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); + assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx)); assert_string_equal("0.03594", jsonctx->value); assert_int_equal(7, jsonctx->value_len); assert_int_equal(1, jsonctx->dynamic); @@ -342,8 +343,8 @@ test_number(void **state) str = "0.03594e-1"; assert_non_null(ly_in_memory(in, str)); - assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx)); - assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0)); + assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); + assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx)); assert_string_equal("0.003594", jsonctx->value); assert_int_equal(8, jsonctx->value_len); assert_int_equal(1, jsonctx->dynamic); @@ -351,8 +352,8 @@ test_number(void **state) str = "0.003594e-1"; assert_non_null(ly_in_memory(in, str)); - assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx)); - assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0)); + assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); + assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx)); assert_string_equal("0.0003594", jsonctx->value); assert_int_equal(9, jsonctx->value_len); assert_int_equal(1, jsonctx->dynamic); @@ -360,8 +361,8 @@ test_number(void **state) str = "0.3594e-2"; assert_non_null(ly_in_memory(in, str)); - assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx)); - assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0)); + assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); + assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx)); assert_string_equal("0.003594", jsonctx->value); assert_int_equal(8, jsonctx->value_len); assert_int_equal(1, jsonctx->dynamic); @@ -369,8 +370,8 @@ test_number(void **state) str = "0.03594e-2"; assert_non_null(ly_in_memory(in, str)); - assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx)); - assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0)); + assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); + assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx)); assert_string_equal("0.0003594", jsonctx->value); assert_int_equal(9, jsonctx->value_len); assert_int_equal(1, jsonctx->dynamic); @@ -378,8 +379,8 @@ test_number(void **state) str = "0.003594e-2"; assert_non_null(ly_in_memory(in, str)); - assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx)); - assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0)); + assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); + assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx)); assert_string_equal("0.00003594", jsonctx->value); assert_int_equal(10, jsonctx->value_len); assert_int_equal(1, jsonctx->dynamic); @@ -388,8 +389,8 @@ test_number(void **state) /* zero */ str = "0"; assert_non_null(ly_in_memory(in, str)); - assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx)); - assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0)); + assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); + assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx)); assert_true(jsonctx->value[0] == '0'); assert_int_equal(1, jsonctx->value_len); assert_int_equal(0, jsonctx->dynamic); @@ -397,8 +398,8 @@ test_number(void **state) str = "-0"; assert_non_null(ly_in_memory(in, str)); - assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx)); - assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0)); + assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); + assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx)); assert_true(jsonctx->value[0] == '-'); assert_true(jsonctx->value[1] == '0'); assert_int_equal(2, jsonctx->value_len); @@ -407,8 +408,8 @@ test_number(void **state) str = "94E0"; assert_non_null(ly_in_memory(in, str)); - assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx)); - assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0)); + assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); + assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx)); assert_true(jsonctx->value[0] == '9'); assert_true(jsonctx->value[1] == '4'); assert_int_equal(2, jsonctx->value_len); @@ -417,8 +418,8 @@ test_number(void **state) str = "0E2"; assert_non_null(ly_in_memory(in, str)); - assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx)); - assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0)); + assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); + assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx)); assert_true(jsonctx->value[0] == '0'); assert_int_equal(1, jsonctx->value_len); assert_int_equal(0, jsonctx->dynamic); @@ -426,8 +427,8 @@ test_number(void **state) str = "-0E2"; assert_non_null(ly_in_memory(in, str)); - assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx)); - assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0)); + assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); + assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx)); assert_true(jsonctx->value[0] == '-'); assert_true(jsonctx->value[1] == '0'); assert_int_equal(2, jsonctx->value_len); @@ -436,8 +437,8 @@ test_number(void **state) str = "5.320e+2"; assert_non_null(ly_in_memory(in, str)); - assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx)); - assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0)); + assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); + assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx)); assert_string_equal("532", jsonctx->value); assert_int_equal(3, jsonctx->value_len); assert_int_equal(1, jsonctx->dynamic); @@ -445,8 +446,8 @@ test_number(void **state) str = "5.320e-1"; assert_non_null(ly_in_memory(in, str)); - assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx)); - assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0)); + assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); + assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx)); assert_string_equal("0.532", jsonctx->value); assert_int_equal(5, jsonctx->value_len); assert_int_equal(1, jsonctx->dynamic); @@ -455,69 +456,64 @@ test_number(void **state) /* various invalid inputs */ str = "-x"; assert_non_null(ly_in_memory(in, str)); - assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx)); + assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); CHECK_LOG_CTX("Invalid character in JSON Number value (\"x\").", "Line number 1."); str = " -"; assert_non_null(ly_in_memory(in, str)); - assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx)); + assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); CHECK_LOG_CTX("Unexpected end-of-input.", "Line number 1."); str = "--1"; assert_non_null(ly_in_memory(in, str)); - assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx)); + assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); CHECK_LOG_CTX("Invalid character in JSON Number value (\"-\").", "Line number 1."); str = "+1"; assert_non_null(ly_in_memory(in, str)); - assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx)); + assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); CHECK_LOG_CTX("Invalid character sequence \"+1\", expected a JSON value.", "Line number 1."); str = " 1.x "; assert_non_null(ly_in_memory(in, str)); - assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx)); + assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); CHECK_LOG_CTX("Invalid character in JSON Number value (\"x\").", "Line number 1."); str = "1."; assert_non_null(ly_in_memory(in, str)); - assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx)); + assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); CHECK_LOG_CTX("Unexpected end-of-input.", "Line number 1."); str = " 1eo "; assert_non_null(ly_in_memory(in, str)); - assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx)); + assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); CHECK_LOG_CTX("Invalid character in JSON Number value (\"o\").", "Line number 1."); str = "1e"; assert_non_null(ly_in_memory(in, str)); - assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx)); + assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); CHECK_LOG_CTX("Unexpected end-of-input.", "Line number 1."); str = "1E1000"; assert_non_null(ly_in_memory(in, str)); - assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx)); + assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); CHECK_LOG_CTX("Number encoded as a string exceeded the LY_NUMBER_MAXLEN limit.", "Line number 1."); str = "1e9999999999999999999"; assert_non_null(ly_in_memory(in, str)); - assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx)); + assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); CHECK_LOG_CTX("Exponent out-of-bounds in a JSON Number value (1e9999999999999999999).", "Line number 1."); str = "1.1e66000"; assert_non_null(ly_in_memory(in, str)); - assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx)); + assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); CHECK_LOG_CTX("Exponent out-of-bounds in a JSON Number value (1.1e66000).", "Line number 1."); str = "1.1e-66000"; assert_non_null(ly_in_memory(in, str)); - assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx)); + assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); CHECK_LOG_CTX("Exponent out-of-bounds in a JSON Number value (1.1e-66000).", "Line number 1."); - str = "-2.1e0."; - assert_non_null(ly_in_memory(in, str)); - assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx)); - CHECK_LOG_CTX("Unexpected character \".\" after JSON number.", "Line number 1."); - ly_in_free(in, 0); } @@ -532,68 +528,12 @@ test_string(void **state) str = ""; assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in)); -#if 0 - /* simple string */ - str = "\"hello\""; - assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx)); - assert_int_equal(LYJSON_STRING, lyjson_ctx_status(jsonctx, 0)); - assert_ptr_equal(&str[1], jsonctx->value); - assert_int_equal(5, jsonctx->value_len); - assert_int_equal(0, jsonctx->dynamic); - lyjson_ctx_free(jsonctx); - - /* 4-byte utf8 character */ - str = "\"\\t𠜎\""; - assert_non_null(ly_in_memory(in, str)); - assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx)); - assert_int_equal(LYJSON_STRING, lyjson_ctx_status(jsonctx, 0)); - assert_string_equal("\t𠜎", jsonctx->value); - assert_int_equal(5, jsonctx->value_len); - assert_int_equal(1, jsonctx->dynamic); - lyjson_ctx_free(jsonctx); - - /* valid escape sequences - note that here it mixes valid JSON string characters (RFC 7159, sec. 7) and - * valid characters in YANG string type (RFC 7950, sec. 9.4). Since the latter is a subset of JSON string, - * the YANG string type's restrictions apply to the JSON escape sequences */ - str = "\"\\\" \\\\ \\r \\/ \\n \\t \\u20ac\""; - assert_non_null(ly_in_memory(in, str)); - assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx)); - assert_int_equal(LYJSON_STRING, lyjson_ctx_status(jsonctx, 0)); - assert_string_equal("\" \\ \r / \n \t €", jsonctx->value); - assert_int_equal(15, jsonctx->value_len); - assert_int_equal(1, jsonctx->dynamic); - lyjson_ctx_free(jsonctx); - - /* backspace and form feed are valid JSON escape sequences, but the control characters they represents are not allowed values for YANG string type */ - str = "\"\\b\""; - assert_non_null(ly_in_memory(in, str)); - assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx)); - CHECK_LOG_CTX("Invalid character reference \"\\b\" (0x00000008).", "Line number 1."); - - str = "\"\\f\""; - assert_non_null(ly_in_memory(in, str)); - assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx)); - CHECK_LOG_CTX("Invalid character reference \"\\f\" (0x0000000c).", "Line number 1."); -#endif - /* unterminated string */ str = "\"unterminated string"; assert_non_null(ly_in_memory(in, str)); - assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx)); + assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); CHECK_LOG_CTX("Missing quotation-mark at the end of a JSON string.", "Line number 1."); -#if 0 - /* invalid escape sequence */ - str = "\"char \\x \""; - assert_non_null(ly_in_memory(in, str)); - assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx)); - CHECK_LOG_CTX("Invalid character escape sequence \\x.", "Line number 1."); - - /* new line is allowed only as escaped character in JSON */ - str = "\"\n\""; - assert_non_null(ly_in_memory(in, str)); - assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx)); - CHECK_LOG_CTX("Invalid character in JSON string \"\n\" (0x0000000a).", "Line number 1."); -#endif + CHECK_LOG_CTX("Unexpected end-of-input.", "Line number 1."); ly_in_free(in, 0); } @@ -608,95 +548,122 @@ test_object(void **state) /* empty */ str = " { } "; assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in)); - assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx)); - assert_int_equal(LYJSON_OBJECT_EMPTY, lyjson_ctx_status(jsonctx, 0)); + assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); + assert_int_equal(LYJSON_OBJECT, lyjson_ctx_status(jsonctx)); + + assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); + assert_int_equal(LYJSON_OBJECT_CLOSED, lyjson_ctx_status(jsonctx)); + assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); - assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx, 0)); + assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx)); lyjson_ctx_free(jsonctx); /* simple value */ str = "{\"name\" : \"Radek\"}"; assert_non_null(ly_in_memory(in, str)); - assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx)); - assert_int_equal(LYJSON_OBJECT, lyjson_ctx_status(jsonctx, 0)); + + assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); + assert_int_equal(LYJSON_OBJECT, lyjson_ctx_status(jsonctx)); + + assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); + assert_int_equal(LYJSON_OBJECT_NAME, lyjson_ctx_status(jsonctx)); assert_ptr_equal(&str[2], jsonctx->value); assert_int_equal(4, jsonctx->value_len); assert_int_equal(0, jsonctx->dynamic); - assert_string_equal("\"Radek\"}", jsonctx->in->current); + assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); - assert_int_equal(LYJSON_STRING, lyjson_ctx_status(jsonctx, 0)); + assert_int_equal(LYJSON_STRING, lyjson_ctx_status(jsonctx)); assert_string_equal("Radek\"}", jsonctx->value); assert_int_equal(5, jsonctx->value_len); assert_int_equal(0, jsonctx->dynamic); - assert_string_equal("}", jsonctx->in->current); + assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); - assert_int_equal(LYJSON_OBJECT_CLOSED, lyjson_ctx_status(jsonctx, 0)); + assert_int_equal(LYJSON_OBJECT_CLOSED, lyjson_ctx_status(jsonctx)); + assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); - assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx, 0)); + assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx)); lyjson_ctx_free(jsonctx); /* two values */ str = "{\"smart\" : true,\"handsom\":false}"; assert_non_null(ly_in_memory(in, str)); - assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx)); - assert_int_equal(LYJSON_OBJECT, lyjson_ctx_status(jsonctx, 0)); + assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); + assert_int_equal(LYJSON_OBJECT, lyjson_ctx_status(jsonctx)); + + assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); + assert_int_equal(LYJSON_OBJECT_NAME, lyjson_ctx_status(jsonctx)); assert_string_equal("smart\" : true,\"handsom\":false}", jsonctx->value); assert_int_equal(5, jsonctx->value_len); assert_int_equal(0, jsonctx->dynamic); - assert_string_equal("true,\"handsom\":false}", jsonctx->in->current); + + assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); + assert_int_equal(LYJSON_TRUE, lyjson_ctx_status(jsonctx)); + assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); - assert_int_equal(LYJSON_TRUE, lyjson_ctx_status(jsonctx, 0)); - assert_string_equal(",\"handsom\":false}", jsonctx->in->current); + assert_int_equal(LYJSON_OBJECT_NEXT, lyjson_ctx_status(jsonctx)); + assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); - assert_int_equal(LYJSON_OBJECT, lyjson_ctx_status(jsonctx, 0)); + assert_int_equal(LYJSON_OBJECT_NAME, lyjson_ctx_status(jsonctx)); assert_string_equal("handsom\":false}", jsonctx->value); assert_int_equal(7, jsonctx->value_len); assert_int_equal(0, jsonctx->dynamic); - assert_string_equal("false}", jsonctx->in->current); + assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); - assert_int_equal(LYJSON_FALSE, lyjson_ctx_status(jsonctx, 0)); - assert_string_equal("}", jsonctx->in->current); + assert_int_equal(LYJSON_FALSE, lyjson_ctx_status(jsonctx)); + assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); - assert_int_equal(LYJSON_OBJECT_CLOSED, lyjson_ctx_status(jsonctx, 0)); + assert_int_equal(LYJSON_OBJECT_CLOSED, lyjson_ctx_status(jsonctx)); + assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); - assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx, 0)); + assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx)); lyjson_ctx_free(jsonctx); /* inherited objects */ str = "{\"person\" : {\"name\":\"Radek\"}}"; assert_non_null(ly_in_memory(in, str)); - assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx)); - assert_int_equal(LYJSON_OBJECT, lyjson_ctx_status(jsonctx, 0)); + assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); + assert_int_equal(LYJSON_OBJECT, lyjson_ctx_status(jsonctx)); + + assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); + assert_int_equal(LYJSON_OBJECT_NAME, lyjson_ctx_status(jsonctx)); assert_string_equal("person\" : {\"name\":\"Radek\"}}", jsonctx->value); assert_int_equal(6, jsonctx->value_len); assert_int_equal(0, jsonctx->dynamic); - assert_string_equal("{\"name\":\"Radek\"}}", jsonctx->in->current); + assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); - assert_int_equal(LYJSON_OBJECT, lyjson_ctx_status(jsonctx, 0)); + assert_int_equal(LYJSON_OBJECT, lyjson_ctx_status(jsonctx)); + + assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); + assert_int_equal(LYJSON_OBJECT_NAME, lyjson_ctx_status(jsonctx)); assert_string_equal("name\":\"Radek\"}}", jsonctx->value); assert_int_equal(4, jsonctx->value_len); assert_int_equal(0, jsonctx->dynamic); - assert_string_equal("\"Radek\"}}", jsonctx->in->current); + assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); - assert_int_equal(LYJSON_STRING, lyjson_ctx_status(jsonctx, 0)); + assert_int_equal(LYJSON_STRING, lyjson_ctx_status(jsonctx)); assert_string_equal("Radek\"}}", jsonctx->value); assert_int_equal(5, jsonctx->value_len); assert_int_equal(0, jsonctx->dynamic); - assert_string_equal("}}", jsonctx->in->current); + assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); - assert_int_equal(LYJSON_OBJECT_CLOSED, lyjson_ctx_status(jsonctx, 0)); - assert_string_equal("}", jsonctx->in->current); + assert_int_equal(LYJSON_OBJECT_CLOSED, lyjson_ctx_status(jsonctx)); + assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); - assert_int_equal(LYJSON_OBJECT_CLOSED, lyjson_ctx_status(jsonctx, 0)); + assert_int_equal(LYJSON_OBJECT_CLOSED, lyjson_ctx_status(jsonctx)); + assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); - assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx, 0)); + assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx)); lyjson_ctx_free(jsonctx); - /* new line is allowed only as escaped character in JSON */ + /* unquoted string */ str = "{ unquoted : \"data\"}"; assert_non_null(ly_in_memory(in, str)); - assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx)); - CHECK_LOG_CTX("Invalid character sequence \"unquoted : \"data\"}\", expected a JSON object's member.", "Line number 1."); + assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); + assert_int_equal(LYJSON_OBJECT, lyjson_ctx_status(jsonctx)); + + assert_int_equal(LY_EVALID, lyjson_ctx_next(jsonctx, NULL)); + CHECK_LOG_CTX("Invalid character sequence \"unquoted : \"data\"}\", expected a JSON object name.", "Line number 1."); + lyjson_ctx_free(jsonctx); ly_in_free(in, 0); } @@ -711,67 +678,79 @@ test_array(void **state) /* empty */ str = " [ ] "; assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in)); - assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx)); - assert_int_equal(LYJSON_ARRAY_EMPTY, lyjson_ctx_status(jsonctx, 0)); + assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); + assert_int_equal(LYJSON_ARRAY, lyjson_ctx_status(jsonctx)); + + assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); + assert_int_equal(LYJSON_ARRAY_CLOSED, lyjson_ctx_status(jsonctx)); + assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); - assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx, 0)); + assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx)); lyjson_ctx_free(jsonctx); /* simple value */ str = "[ null]"; assert_non_null(ly_in_memory(in, str)); - assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx)); - assert_int_equal(LYJSON_ARRAY, lyjson_ctx_status(jsonctx, 0)); + assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); + assert_int_equal(LYJSON_ARRAY, lyjson_ctx_status(jsonctx)); assert_null(jsonctx->value); assert_int_equal(0, jsonctx->value_len); assert_int_equal(0, jsonctx->dynamic); - assert_string_equal("null]", jsonctx->in->current); + assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); - assert_int_equal(LYJSON_NULL, lyjson_ctx_status(jsonctx, 0)); - assert_string_equal("]", jsonctx->in->current); + assert_int_equal(LYJSON_NULL, lyjson_ctx_status(jsonctx)); + assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); - assert_int_equal(LYJSON_ARRAY_CLOSED, lyjson_ctx_status(jsonctx, 0)); + assert_int_equal(LYJSON_ARRAY_CLOSED, lyjson_ctx_status(jsonctx)); + assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); - assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx, 0)); + assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx)); lyjson_ctx_free(jsonctx); /* two values */ str = "[{\"a\":null},\"x\"]"; assert_non_null(ly_in_memory(in, str)); - assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx)); - assert_int_equal(LYJSON_ARRAY, lyjson_ctx_status(jsonctx, 0)); + assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); + assert_int_equal(LYJSON_ARRAY, lyjson_ctx_status(jsonctx)); assert_null(jsonctx->value); assert_int_equal(0, jsonctx->value_len); assert_int_equal(0, jsonctx->dynamic); - assert_string_equal("{\"a\":null},\"x\"]", jsonctx->in->current); + assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); - assert_int_equal(LYJSON_OBJECT, lyjson_ctx_status(jsonctx, 0)); + assert_int_equal(LYJSON_OBJECT, lyjson_ctx_status(jsonctx)); + + assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); + assert_int_equal(LYJSON_OBJECT_NAME, lyjson_ctx_status(jsonctx)); assert_string_equal("a\":null},\"x\"]", jsonctx->value); assert_int_equal(1, jsonctx->value_len); assert_int_equal(0, jsonctx->dynamic); - assert_string_equal("null},\"x\"]", jsonctx->in->current); + assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); - assert_int_equal(LYJSON_NULL, lyjson_ctx_status(jsonctx, 0)); - assert_string_equal("},\"x\"]", jsonctx->in->current); + assert_int_equal(LYJSON_NULL, lyjson_ctx_status(jsonctx)); + assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); - assert_int_equal(LYJSON_OBJECT_CLOSED, lyjson_ctx_status(jsonctx, 0)); - assert_string_equal(",\"x\"]", jsonctx->in->current); + assert_int_equal(LYJSON_OBJECT_CLOSED, lyjson_ctx_status(jsonctx)); + assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); - assert_int_equal(LYJSON_STRING, lyjson_ctx_status(jsonctx, 0)); + assert_int_equal(LYJSON_ARRAY_NEXT, lyjson_ctx_status(jsonctx)); + + assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); + assert_int_equal(LYJSON_STRING, lyjson_ctx_status(jsonctx)); assert_string_equal("x\"]", jsonctx->value); assert_int_equal(1, jsonctx->value_len); assert_int_equal(0, jsonctx->dynamic); - assert_string_equal("]", jsonctx->in->current); + assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); - assert_int_equal(LYJSON_ARRAY_CLOSED, lyjson_ctx_status(jsonctx, 0)); + assert_int_equal(LYJSON_ARRAY_CLOSED, lyjson_ctx_status(jsonctx)); + assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL)); - assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx, 0)); + assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx)); lyjson_ctx_free(jsonctx); /* new line is allowed only as escaped character in JSON */ str = "[ , null]"; assert_non_null(ly_in_memory(in, str)); - assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx)); + assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); assert_int_equal(LY_EVALID, lyjson_ctx_next(jsonctx, NULL)); CHECK_LOG_CTX("Invalid character sequence \", null]\", expected a JSON value.", "Line number 1."); lyjson_ctx_free(jsonctx); |