Browse Source

Merge pull request #524 from dota17/addTestCase_obj_token

Increase coverage
tags/json-c-0.14-20200419
Eric Haszlakiewicz GitHub 5 years ago
parent
commit
a9114392b4
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 90 additions and 3 deletions
  1. +2
    -1
      tests/CMakeLists.txt
  2. +41
    -0
      tests/test_object_iterator.c
  3. +14
    -0
      tests/test_object_iterator.expected
  4. +1
    -0
      tests/test_object_iterator.test
  5. +30
    -2
      tests/test_util_file.c
  6. +2
    -0
      tests/test_util_file.expected

+ 2
- 1
tests/CMakeLists.txt View File

@@ -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)


+ 41
- 0
tests/test_object_iterator.c View File

@@ -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;
}

+ 14
- 0
tests/test_object_iterator.expected View File

@@ -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

+ 1
- 0
tests/test_object_iterator.test View File

@@ -0,0 +1 @@
test_basic.test

+ 30
- 2
tests/test_util_file.c View File

@@ -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);
}

+ 2
- 0
tests/test_util_file.expected View File

@@ -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 } } } }

Loading…
Cancel
Save