diff options
author | Eric Haszlakiewicz <erh+git@nimenees.com> | 2020-04-15 23:35:21 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-15 23:35:21 -0400 |
commit | a9114392b4d8a3f3a9923625842376a11988245c (patch) | |
tree | 473783bb1c7ddca2600126b19b2828694043482d | |
parent | 04bb0fca739a34f3c8608bfc7d1184b049124a80 (diff) | |
parent | b14363ae323b189ed2d764672c6607912ff35398 (diff) | |
download | json-c-a9114392b4d8a3f3a9923625842376a11988245c.tar.gz |
Merge pull request #524 from dota17/addTestCase_obj_token
Increase coverage
-rw-r--r-- | tests/CMakeLists.txt | 3 | ||||
-rw-r--r-- | tests/test_object_iterator.c | 41 | ||||
-rw-r--r-- | tests/test_object_iterator.expected | 14 | ||||
l--------- | tests/test_object_iterator.test | 1 | ||||
-rw-r--r-- | tests/test_util_file.c | 32 | ||||
-rw-r--r-- | tests/test_util_file.expected | 2 |
6 files changed, 90 insertions, 3 deletions
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index f18d9f3..a871573 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -33,7 +33,8 @@ foreach(TESTNAME test_set_serializer test_set_value test_util_file - test_visit) + test_visit + test_object_iterator) add_executable(${TESTNAME} ${TESTNAME}.c) add_test(NAME ${TESTNAME} COMMAND ${PROJECT_SOURCE_DIR}/tests/${TESTNAME}.test) diff --git a/tests/test_object_iterator.c b/tests/test_object_iterator.c new file mode 100644 index 0000000..da5192a --- /dev/null +++ b/tests/test_object_iterator.c @@ -0,0 +1,41 @@ +#include "config.h" +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "json_object.h" +#include "json_object_iterator.h" +#include "json_tokener.h" + +int main(int atgc, char **argv) +{ + const char *input = "{\n\ + \"string_of_digits\": \"123\",\n\ + \"regular_number\": 222,\n\ + \"decimal_number\": 99.55,\n\ + \"boolean_true\": true,\n\ + \"boolean_false\": false,\n\ + \"big_number\": 2147483649,\n\ + \"a_null\": null,\n\ + }"; + + struct json_object *new_obj; + struct json_object_iterator it; + struct json_object_iterator itEnd; + + it = json_object_iter_init_default(); + new_obj = json_tokener_parse(input); + it = json_object_iter_begin(new_obj); + itEnd = json_object_iter_end(new_obj); + + while (!json_object_iter_equal(&it, &itEnd)) + { + printf("%s\n", json_object_iter_peek_name(&it)); + printf("%s\n", json_object_to_json_string(json_object_iter_peek_value(&it))); + json_object_iter_next(&it); + } + + json_object_put(new_obj); + + return 0; +} diff --git a/tests/test_object_iterator.expected b/tests/test_object_iterator.expected new file mode 100644 index 0000000..e56e288 --- /dev/null +++ b/tests/test_object_iterator.expected @@ -0,0 +1,14 @@ +string_of_digits +"123" +regular_number +222 +decimal_number +99.55 +boolean_true +true +boolean_false +false +big_number +2147483649 +a_null +null diff --git a/tests/test_object_iterator.test b/tests/test_object_iterator.test new file mode 120000 index 0000000..58a13f4 --- /dev/null +++ b/tests/test_object_iterator.test @@ -0,0 +1 @@ +test_basic.test
\ No newline at end of file diff --git a/tests/test_util_file.c b/tests/test_util_file.c index ebc5edb..2f8f8b5 100644 --- a/tests/test_util_file.c +++ b/tests/test_util_file.c @@ -14,6 +14,7 @@ #if HAVE_UNISTD_H #include <unistd.h> #endif /* HAVE_UNISTD_H */ +#include <assert.h> #include <sys/stat.h> #include <sys/types.h> @@ -28,6 +29,7 @@ static void test_read_closed(void); static void test_write_to_file(); static void stat_and_cat(const char *file); +static void test_read_fd_equal(const char *testdir); #ifndef PATH_MAX #define PATH_MAX 256 @@ -142,13 +144,15 @@ int main(int argc, char **argv) if (strncmp(json_c_version(), JSON_C_VERSION, sizeof(JSON_C_VERSION))) { printf("FAIL: Output from json_c_version(): %s " - "does not match %s", json_c_version(), JSON_C_VERSION); + "does not match %s", + json_c_version(), JSON_C_VERSION); return EXIT_FAILURE; } if (json_c_version_num() != JSON_C_VERSION_NUM) { printf("FAIL: Output from json_c_version_num(): %d " - "does not match %d", json_c_version_num(), JSON_C_VERSION_NUM); + "does not match %d", + json_c_version_num(), JSON_C_VERSION_NUM); return EXIT_FAILURE; } @@ -157,6 +161,7 @@ int main(int argc, char **argv) test_read_nonexistant(); test_read_closed(); test_write_to_file(); + test_read_fd_equal(testdir); return EXIT_SUCCESS; } @@ -196,6 +201,7 @@ static void test_read_valid_nested_with_fd(const char *testdir) fprintf(stderr, "FAIL: unable to open %s: %s\n", filename, strerror(errno)); exit(EXIT_FAILURE); } + assert(NULL == json_object_from_fd_ex(d, -2)); json_object *jso = json_object_from_fd_ex(d, 20); if (jso != NULL) { @@ -275,3 +281,25 @@ static void test_read_closed() "expecting NULL, EBADF, got:NULL, %s\n", json_util_get_last_err()); } + +static void test_read_fd_equal(const char *testdir) +{ + char filename[PATH_MAX]; + (void)snprintf(filename, sizeof(filename), "%s/valid_nested.json", testdir); + + json_object *jso = json_object_from_file(filename); + + int d = open(filename, O_RDONLY, 0); + if (d < 0) + { + fprintf(stderr, "FAIL: unable to open %s: %s\n", filename, strerror(errno)); + exit(EXIT_FAILURE); + } + json_object *new_jso = json_object_from_fd(d); + close(d); + + printf("OK: json_object_from_file(valid.json)=%s\n", json_object_to_json_string(jso)); + printf("OK: json_object_from_fd(valid.json)=%s\n", json_object_to_json_string(new_jso)); + json_object_put(jso); + json_object_put(new_jso); +} diff --git a/tests/test_util_file.expected b/tests/test_util_file.expected index 8d87535..f149f85 100644 --- a/tests/test_util_file.expected +++ b/tests/test_util_file.expected @@ -36,3 +36,5 @@ file[json3.out], size=703, contents={ "foo8":"abcdefghijklmnopqrstuvwxyz", "foo9":"abcdefghijklmnopqrstuvwxyz" }{"foo":1234,"foo1":"abcdefghijklmnopqrstuvwxyz","foo2":"abcdefghijklmnopqrstuvwxyz","foo3":"abcdefghijklmnopqrstuvwxyz","foo4":"abcdefghijklmnopqrstuvwxyz","foo5":"abcdefghijklmnopqrstuvwxyz","foo6":"abcdefghijklmnopqrstuvwxyz","foo7":"abcdefghijklmnopqrstuvwxyz","foo8":"abcdefghijklmnopqrstuvwxyz","foo9":"abcdefghijklmnopqrstuvwxyz"} +OK: json_object_from_file(valid.json)={ "foo": 123, "obj2": { "obj3": { "obj4": { "foo": 999 } } } } +OK: json_object_from_fd(valid.json)={ "foo": 123, "obj2": { "obj3": { "obj4": { "foo": 999 } } } } |