Browse Source

Edits to tests to reflect changes to API

pull/715/head
HexTheDragon 4 years ago
parent
commit
afade9038a
8 changed files with 82 additions and 42 deletions
  1. +3
    -1
      tests/test1.c
  2. +15
    -11
      tests/testReplaceExisting.c
  3. +3
    -3
      tests/test_deep_copy.c
  4. +37
    -6
      tests/test_null_keys_add.c
  5. +2
    -0
      tests/test_null_keys_add.expected
  6. +2
    -1
      tests/test_object_iterator.c
  7. +1
    -1
      tests/test_parse.c
  8. +19
    -19
      tests/test_visit.c

+ 3
- 1
tests/test1.c View File

@@ -306,7 +306,9 @@ int main(int argc, char **argv)
printf("my_object=\n");
json_object_object_foreach(my_object, key, val)
{
printf("\t%s: %s\n", key, json_object_to_json_string(val));
putchar('\t');
lh_string_print(key, stdout);
printf(": %s\n", json_object_to_json_string(val));
}
printf("my_object.to_string()=%s\n", json_object_to_json_string(my_object));



+ 15
- 11
tests/testReplaceExisting.c View File

@@ -24,10 +24,11 @@ int main(int argc, char **argv)
int orig_count = 0;
json_object_object_foreach(my_object, key0, val0)
{
printf("Key at index %d is [%s] %d", orig_count, key0, (val0 == NULL));
if (strcmp(key0, "deleteme") == 0)
printf("Key at index %d is [%s] %d", orig_count, lh_string_data(key0),
(val0 == NULL));
if (strcmp(lh_string_data(key0), "deleteme") == 0)
{
json_object_object_del(my_object, key0);
json_object_object_del(my_object, lh_string_data(key0));
printf(" (deleted)\n");
}
else
@@ -37,17 +38,19 @@ int main(int argc, char **argv)

printf("==== replace-value first loop starting ====\n");

const char *original_key = NULL;
const struct lh_string *original_key = NULL;
orig_count = 0;
json_object_object_foreach(my_object, key, val)
{
printf("Key at index %d is [%s] %d\n", orig_count, key, (val == NULL));
printf("Key at index %d is [%s] %d\n", orig_count, lh_string_data(key),
(val == NULL));
orig_count++;
if (strcmp(key, "foo2") != 0)
if (strcmp(lh_string_data(key), "foo2") != 0)
continue;
printf("replacing value for key [%s]\n", key);
printf("replacing value for key [%s]\n", lh_string_data(key));
original_key = key;
json_object_object_add(my_object, key, json_object_new_string("zzz"));
json_object_object_add(my_object, lh_string_data(key0),
json_object_new_string("zzz"));
}

printf("==== second loop starting ====\n");
@@ -56,11 +59,12 @@ int main(int argc, char **argv)
int retval = 0;
json_object_object_foreach(my_object, key2, val2)
{
printf("Key at index %d is [%s] %d\n", new_count, key2, (val2 == NULL));
printf("Key at index %d is [%s] %d\n", new_count, lh_string_data(key2),
(val2 == NULL));
new_count++;
if (strcmp(key2, "foo2") != 0)
if (strcmp(lh_string_data(key2), "foo2") != 0)
continue;
printf("pointer for key [%s] does %smatch\n", key2,
printf("pointer for key [%s] does %smatch\n", lh_string_data(key2),
(key2 == original_key) ? "" : "NOT ");
if (key2 != original_key)
retval = 1;


+ 3
- 3
tests/test_deep_copy.c View File

@@ -93,14 +93,14 @@ int my_custom_serializer(struct json_object *jso, struct printbuf *pb, int level
}

json_c_shallow_copy_fn my_shallow_copy;
int my_shallow_copy(json_object *src, json_object *parent, const char *key, size_t index,
json_object **dst)
int my_shallow_copy(json_object *src, json_object *parent, const struct lh_string *key,
size_t index, json_object **dst)
{
int rc;
rc = json_c_shallow_copy_default(src, parent, key, index, dst);
if (rc < 0)
return rc;
if (key != NULL && strcmp(key, "with_serializer") == 0)
if (key != NULL && strcmp(lh_string_data(key), "with_serializer") == 0)
{
printf("CALLED: my_shallow_copy on with_serializer object\n");
void *userdata = json_object_get_userdata(src);


+ 37
- 6
tests/test_null_keys_add.c View File

@@ -9,6 +9,7 @@
#include "json_inttypes.h"
#include "json_object.h"
#include "json_tokener.h"
#include "linkhash.h"

int main(void)
{
@@ -65,10 +66,33 @@ int main(void)
return 1;
}

// Check the previous keys are still the same present
struct json_object *foo = json_object_object_get(parsed, foo_key);
if (!json_object_is_type(foo, json_type_double))
{
printf("Key \"%s\" should be `json_type_double` (%d) but was %d (error!)\n",
foo_key, (int)json_type_double, json_object_get_type(foo));
return 1;
}
else
{
printf("Key \"%s\" parsed as right type\n", foo_key);
}
struct json_object *bar = json_object_object_get(parsed, bar_key);
if (!json_object_is_type(bar, json_type_array))
{
printf("Key \"%s\" should be `json_type_array` (%d) but was %d (error!)\n", bar_key,
(int)json_type_array, json_object_get_type(bar));
return 1;
}
else
{
printf("Key \"%s\" parsed as right type\n", bar_key);
}

// Add the new key
struct json_object *new_str = json_object_new_string_len(toadd_value, toadd_value_len);
if (json_object_object_add_ex(parsed, toadd_key, new_str,
JSON_C_OBJECT_ADD_KEY_IS_NEW |
if (json_object_object_add_ex_len(parsed, toadd_key, toadd_key_len, new_str,
JSON_C_OBJECT_KEY_IS_CONSTANT) != 0)
{
printf("An error occured adding the key \"%s\" (error!)\n", toadd_key_printable);
@@ -86,6 +110,13 @@ int main(void)
{
printf("Have three keys, but don't have the right value in \"%s\" (error!)\n",
toadd_key_printable);
printf("Keys :\n");
json_object_object_foreach(parsed, key, val)
{
putchar('\"');
fwrite(lh_string_data(key), lh_string_size(key), 1, stdout);
printf("\" (%zd)\n", lh_string_size(key));
}
return 1;
}
else
@@ -94,7 +125,7 @@ int main(void)
}

// Check the previous keys are still the same present
struct json_object *foo = json_object_object_get(parsed, foo_key);
foo = json_object_object_get(parsed, foo_key);
if (!json_object_is_type(foo, json_type_double))
{
printf("Key \"%s\" should be `json_type_double` (%d) but was %d (error!)\n",
@@ -105,11 +136,11 @@ int main(void)
{
printf("Key \"%s\" is still the same\n", foo_key);
}
struct json_object *bar = json_object_object_get(parsed, bar_key);
if (!json_object_is_type(foo, json_type_array))
bar = json_object_object_get(parsed, bar_key);
if (!json_object_is_type(bar, json_type_array))
{
printf("Key \"%s\" should be `json_type_array` (%d) but was %d (error!)\n", bar_key,
(int)json_type_array, json_object_get_type(foo));
(int)json_type_array, json_object_get_type(bar));
return 1;
}
else


+ 2
- 0
tests/test_null_keys_add.expected View File

@@ -1,5 +1,7 @@
Parsed input: { "foo": 14.5, "bar": [] }
Result is `json_type_object`
Key "foo" parsed as right type
Key "bar" parsed as right type
Added the key "foo\0bar" successfully
Key "foo" is still the same
Key "bar" is still the same


+ 2
- 1
tests/test_object_iterator.c View File

@@ -6,6 +6,7 @@
#include "json_object.h"
#include "json_object_iterator.h"
#include "json_tokener.h"
#include "linkhash.h"

int main(int atgc, char **argv)
{
@@ -30,7 +31,7 @@ int main(int atgc, char **argv)

while (!json_object_iter_equal(&it, &itEnd))
{
printf("%s\n", json_object_iter_peek_name(&it));
printf("%s\n", lh_string_data(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);
}


+ 1
- 1
tests/test_parse.c View File

@@ -215,7 +215,7 @@ static void do_clear_serializer(json_object *jso)
}

static int clear_serializer(json_object *jso, int flags, json_object *parent_jso,
const char *jso_key, size_t *jso_index, void *userarg)
const struct lh_string *jso_key, size_t *jso_index, void *userarg)
{
if (jso)
json_object_set_serializer(jso, NULL, NULL, NULL);


+ 19
- 19
tests/test_visit.c View File

@@ -68,17 +68,17 @@ int main(void)
return 0;
}

static int emit_object(json_object *jso, int flags, json_object *parent_jso, const char *jso_key,
size_t *jso_index, void *userarg)
static int emit_object(json_object *jso, int flags, json_object *parent_jso,
const struct lh_string *jso_key, size_t *jso_index, void *userarg)
{
printf("flags: 0x%x, key: %s, index: %ld, value: %s\n", flags,
(jso_key ? jso_key : "(null)"), (jso_index ? (long)*jso_index : -1L),
(jso_key ? lh_string_data(jso_key) : "(null)"), (jso_index ? (long)*jso_index : -1L),
json_object_to_json_string(jso));
return JSON_C_VISIT_RETURN_CONTINUE;
}

static int skip_arrays(json_object *jso, int flags, json_object *parent_jso, const char *jso_key,
size_t *jso_index, void *userarg)
static int skip_arrays(json_object *jso, int flags, json_object *parent_jso,
const struct lh_string *jso_key, size_t *jso_index, void *userarg)
{
(void)emit_object(jso, flags, parent_jso, jso_key, jso_index, userarg);
if (json_object_get_type(jso) == json_type_array)
@@ -86,16 +86,16 @@ static int skip_arrays(json_object *jso, int flags, json_object *parent_jso, con
return JSON_C_VISIT_RETURN_CONTINUE;
}

static int pop_and_stop(json_object *jso, int flags, json_object *parent_jso, const char *jso_key,
size_t *jso_index, void *userarg)
static int pop_and_stop(json_object *jso, int flags, json_object *parent_jso,
const struct lh_string *jso_key, size_t *jso_index, void *userarg)
{
(void)emit_object(jso, flags, parent_jso, jso_key, jso_index, userarg);
if (jso_key != NULL && strcmp(jso_key, "subobj1") == 0)
if (jso_key != NULL && strcmp(lh_string_data(jso_key), "subobj1") == 0)
{
printf("POP after handling subobj1\n");
return JSON_C_VISIT_RETURN_POP;
}
if (jso_key != NULL && strcmp(jso_key, "obj3") == 0)
if (jso_key != NULL && strcmp(lh_string_data(jso_key), "obj3") == 0)
{
printf("STOP after handling obj3\n");
return JSON_C_VISIT_RETURN_STOP;
@@ -103,11 +103,11 @@ static int pop_and_stop(json_object *jso, int flags, json_object *parent_jso, co
return JSON_C_VISIT_RETURN_CONTINUE;
}

static int err_on_subobj2(json_object *jso, int flags, json_object *parent_jso, const char *jso_key,
size_t *jso_index, void *userarg)
static int err_on_subobj2(json_object *jso, int flags, json_object *parent_jso,
const struct lh_string *jso_key, size_t *jso_index, void *userarg)
{
(void)emit_object(jso, flags, parent_jso, jso_key, jso_index, userarg);
if (jso_key != NULL && strcmp(jso_key, "subobj2") == 0)
if (jso_key != NULL && strcmp(lh_string_data(jso_key), "subobj2") == 0)
{
printf("ERROR after handling subobj1\n");
return JSON_C_VISIT_RETURN_ERROR;
@@ -115,8 +115,8 @@ static int err_on_subobj2(json_object *jso, int flags, json_object *parent_jso,
return JSON_C_VISIT_RETURN_CONTINUE;
}

static int pop_array(json_object *jso, int flags, json_object *parent_jso, const char *jso_key,
size_t *jso_index, void *userarg)
static int pop_array(json_object *jso, int flags, json_object *parent_jso,
const struct lh_string *jso_key, size_t *jso_index, void *userarg)
{
(void)emit_object(jso, flags, parent_jso, jso_key, jso_index, userarg);
if (jso_index != NULL && (*jso_index == 0))
@@ -127,8 +127,8 @@ static int pop_array(json_object *jso, int flags, json_object *parent_jso, const
return JSON_C_VISIT_RETURN_CONTINUE;
}

static int stop_array(json_object *jso, int flags, json_object *parent_jso, const char *jso_key,
size_t *jso_index, void *userarg)
static int stop_array(json_object *jso, int flags, json_object *parent_jso,
const struct lh_string *jso_key, size_t *jso_index, void *userarg)
{
(void)emit_object(jso, flags, parent_jso, jso_key, jso_index, userarg);
if (jso_index != NULL && (*jso_index == 0))
@@ -139,11 +139,11 @@ static int stop_array(json_object *jso, int flags, json_object *parent_jso, cons
return JSON_C_VISIT_RETURN_CONTINUE;
}

static int err_return(json_object *jso, int flags, json_object *parent_jso, const char *jso_key,
size_t *jso_index, void *userarg)
static int err_return(json_object *jso, int flags, json_object *parent_jso,
const struct lh_string *jso_key, size_t *jso_index, void *userarg)
{
printf("flags: 0x%x, key: %s, index: %ld, value: %s\n", flags,
(jso_key ? jso_key : "(null)"), (jso_index ? (long)*jso_index : -1L),
(jso_key ? lh_string_data(jso_key) : "(null)"), (jso_index ? (long)*jso_index : -1L),
json_object_to_json_string(jso));
return 100;
}

Loading…
Cancel
Save