summaryrefslogtreecommitdiffstats
path: root/src/test/test-json.c
diff options
context:
space:
mode:
authorDaan De Meyer <daan.j.demeyer@gmail.com>2024-08-09 13:40:43 +0200
committerDaan De Meyer <daan.j.demeyer@gmail.com>2024-09-03 12:08:41 +0200
commit9ba489c7f6d6da48772ed3f7862c781340aecda7 (patch)
tree684cfe789a1e4c283367a00c7400f8c6ab51b928 /src/test/test-json.c
parentjson-util: Add JSON_BUILD_PAIR_DUAL_TIMESTAMP_NON_NULL() (diff)
downloadsystemd-9ba489c7f6d6da48772ed3f7862c781340aecda7.tar.xz
systemd-9ba489c7f6d6da48772ed3f7862c781340aecda7.zip
json-util: Add JSON_BUILD_PAIR_CALLBACK_NON_NULL()
Like JSON_BUILD_PAIR_CALLBACK(), but doesn't add anything to the variant if the callback doesn't put anything in the return argument.
Diffstat (limited to '')
-rw-r--r--src/test/test-json.c12
1 files changed, 12 insertions, 0 deletions
diff --git a/src/test/test-json.c b/src/test/test-json.c
index f9dc9e74c7..378a3d0d8d 100644
--- a/src/test/test-json.c
+++ b/src/test/test-json.c
@@ -281,6 +281,8 @@ static int test_callback(sd_json_variant **ret, const char *name, void *userdata
assert_se(PTR_TO_INT(userdata) == 4711);
else if (streq_ptr(name, "mypid2"))
assert_se(PTR_TO_INT(userdata) == 4712);
+ else if (streq_ptr(name, "mypid3"))
+ return 0;
else
assert_not_reached();
@@ -402,6 +404,16 @@ TEST(build) {
assert_se(sd_json_variant_format(y, /* flags= */ 0, &f2));
ASSERT_STREQ(f1, f2);
+ _cleanup_(sd_json_variant_unrefp) sd_json_variant *z = NULL;
+ ASSERT_OK(sd_json_build(&z, SD_JSON_BUILD_OBJECT(JSON_BUILD_PAIR_CALLBACK_NON_NULL("mypid3", test_callback, INT_TO_PTR(4713)))));
+ ASSERT_TRUE(sd_json_variant_is_blank_object(z));
+ z = sd_json_variant_unref(z);
+ f2 = mfree(f2);
+ ASSERT_OK(sd_json_build(&z, SD_JSON_BUILD_OBJECT(JSON_BUILD_PAIR_CALLBACK_NON_NULL("mypid1", test_callback, INT_TO_PTR(4711)),
+ JSON_BUILD_PAIR_CALLBACK_NON_NULL("mypid2", test_callback, INT_TO_PTR(4712)))));
+ ASSERT_OK(sd_json_variant_format(z, /* flags= */ 0, &f2));
+ ASSERT_STREQ(f1, f2);
+
_cleanup_set_free_ Set *ss = NULL;
assert_se(set_ensure_put(&ss, &string_hash_ops_free, ASSERT_PTR(strdup("pief"))) >= 0);
assert_se(set_ensure_put(&ss, &string_hash_ops_free, ASSERT_PTR(strdup("xxxx"))) >= 0);