tests: use nlohmann's json lib to generate conds list
authorSimon Marchi <simon.marchi@efficios.com>
Tue, 23 May 2023 19:47:30 +0000 (15:47 -0400)
committerSimon Marchi <simon.marchi@efficios.com>
Wed, 31 May 2023 19:47:48 +0000 (15:47 -0400)
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 <simon.marchi@efficios.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/10059
Tested-by: jenkins <jenkins@lttng.org>
Reviewed-by: Philippe Proulx <eeppeliteloop@gmail.com>
tests/lib/conds/utils.cpp

index 3ea99597325b4007bc0de0f0dddcc14fc4f22318..2837baaff9445cdacb5a1498d6f993ab9b5866c0 100644 (file)
 #include <assert.h>
 #include <babeltrace2/babeltrace.h>
 #include <glib.h>
+#include <iostream>
 
 #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[],
This page took 0.02542 seconds and 4 git commands to generate.