From: Simon Marchi Date: Tue, 23 May 2023 19:47:30 +0000 (-0400) Subject: tests: use nlohmann's json lib to generate conds list X-Git-Url: http://git.efficios.com/?p=babeltrace.git;a=commitdiff_plain;h=4b4f3ca692cbc2b7ed3632bfd55412351275b013 tests: use nlohmann's json lib to generate conds list Change lib/conds/utils.cpp to use nlohmann to dump the test list in JSON, rather than dumping it by hand. Change-Id: I7641a3fcdf118b3787abd76c395c098e1f991717 Signed-off-by: Simon Marchi Reviewed-on: https://review.lttng.org/c/babeltrace/+/10059 Tested-by: jenkins Reviewed-by: Philippe Proulx --- diff --git a/tests/lib/conds/utils.cpp b/tests/lib/conds/utils.cpp index 3ea99597..2837baaf 100644 --- a/tests/lib/conds/utils.cpp +++ b/tests/lib/conds/utils.cpp @@ -10,8 +10,10 @@ #include #include #include +#include #include "common/assert.h" +#include "cpp-common/nlohmann/json.hpp" #include "utils.hpp" typedef void (*run_in_comp_cls_init_func)(bt_self_component *self_comp, void *user_data); @@ -102,53 +104,32 @@ static void run_trigger(const struct cond_trigger *trigger) } } -static void escape_json_string(const char *str, GString *escaped_str) -{ - g_string_assign(escaped_str, ""); - - for (const char *ch = str; *ch; ch++) { - if (*ch == '\\' || *ch == '"') { - g_string_append_c(escaped_str, '\\'); - } - - g_string_append_c(escaped_str, *ch); - } -} - static void list_triggers(const struct cond_trigger triggers[], size_t trigger_count) { - GString *escaped_str = g_string_new(NULL); - size_t i; - - BT_ASSERT(escaped_str); - printf("["); + nlohmann::json trigger_array = nlohmann::json::array(); - for (i = 0; i < trigger_count; i++) { - const struct cond_trigger *trigger = &triggers[i]; + for (size_t i = 0; i < trigger_count; i++) { + nlohmann::json trigger_obj = nlohmann::json::object(); + const cond_trigger& trigger = triggers[i]; /* Condition ID */ - escape_json_string(trigger->cond_id, escaped_str); - printf("{\"cond-id\":\"%s\",", escaped_str->str); + trigger_obj["cond-id"] = trigger.cond_id; /* Name starts with condition ID */ - printf("\"name\":\"%s", escaped_str->str); + std::string name = trigger.cond_id; - if (trigger->suffix) { - escape_json_string(trigger->suffix, escaped_str); - printf("-%s", escaped_str->str); + if (trigger.suffix) { + name += '-'; + name += trigger.suffix; } - printf("\"}"); - - if (i < trigger_count - 1) { - /* Comma between objects */ - printf(","); - } + trigger_obj["name"] = std::move(name); + trigger_array.push_back(std::move(trigger_obj)); } - printf("]"); - g_string_free(escaped_str, TRUE); - fflush(stdout); + auto str = trigger_array.dump(); + std::cout << str; + std::flush(std::cout); } void cond_main(int argc, const char *argv[], const struct cond_trigger triggers[],