diff options
author | Lennart Poettering <lennart@poettering.net> | 2021-09-03 11:11:18 +0200 |
---|---|---|
committer | Luca Boccassi <luca.boccassi@gmail.com> | 2021-09-03 14:37:26 +0100 |
commit | b03803f0dc46613edaa34355786d6d3c1dec228c (patch) | |
tree | a850e9b94f01bc30bd33d5a95395ebde1548d7d5 /src/test | |
parent | 0d5765f7af96702031a6ce12d2f65238337a2e40 (diff) | |
download | systemd-b03803f0dc46613edaa34355786d6d3c1dec228c.tar.gz |
format-table: allow to explicitly override JSON field names
In some cases it's useful to explicitly generate the JSON field names to
generate for table columns, instead of auto-mangling them from table
header names that are intended for human consumption.
This adds the infra and a test for it.
It's intended to be used by #20544, for the first column, which in text
mode should have an empty header field, but have an explicit name in
json output mode.
Diffstat (limited to 'src/test')
-rw-r--r-- | src/test/test-format-table.c | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/src/test/test-format-table.c b/src/test/test-format-table.c index 44d92a719d..ea96e22391 100644 --- a/src/test/test-format-table.c +++ b/src/test/test-format-table.c @@ -366,6 +366,41 @@ static void test_strv_wrapped(void) { formatted = mfree(formatted); } +static void test_json(void) { + _cleanup_(json_variant_unrefp) JsonVariant *v = NULL, *w = NULL; + _cleanup_(table_unrefp) Table *t = NULL; + + log_info("/* %s */", __func__); + + assert_se(t = table_new("foo bar", "quux", "piep miau")); + assert_se(table_set_json_field_name(t, 2, "zzz") >= 0); + + assert_se(table_add_many(t, + TABLE_STRING, "v1", + TABLE_UINT64, UINT64_C(4711), + TABLE_BOOLEAN, true) >= 0); + + assert_se(table_add_many(t, + TABLE_STRV, STRV_MAKE("a", "b", "c"), + TABLE_EMPTY, + TABLE_MODE, 0755) >= 0); + + assert_se(table_to_json(t, &v) >= 0); + + assert_se(json_build(&w, + JSON_BUILD_ARRAY( + JSON_BUILD_OBJECT( + JSON_BUILD_PAIR("foo_bar", JSON_BUILD_STRING("v1")), + JSON_BUILD_PAIR("quux", JSON_BUILD_UNSIGNED(4711)), + JSON_BUILD_PAIR("zzz", JSON_BUILD_BOOLEAN(true))), + JSON_BUILD_OBJECT( + JSON_BUILD_PAIR("foo_bar", JSON_BUILD_STRV(STRV_MAKE("a", "b", "c"))), + JSON_BUILD_PAIR("quux", JSON_BUILD_NULL), + JSON_BUILD_PAIR("zzz", JSON_BUILD_UNSIGNED(0755))))) >= 0); + + assert_se(json_variant_equal(v, w)); +} + int main(int argc, char *argv[]) { _cleanup_(table_unrefp) Table *t = NULL; _cleanup_free_ char *formatted = NULL; @@ -509,6 +544,7 @@ int main(int argc, char *argv[]) { test_multiline(); test_strv(); test_strv_wrapped(); + test_json(); return 0; } |