tests/lib/conds: store triggers in a vector
[babeltrace.git] / tests / lib / conds / utils.cpp
index 53984752a85576deab3cb565ea3b04dccc0f3c30..f1d9969e1ed24f2f8806349aa07b47c826866d94 100644 (file)
@@ -4,77 +4,72 @@
  * Copyright (C) 2020 Philippe Proulx <pproulx@efficios.com>
  */
 
-#include <iostream>
-
-#include <assert.h>
 #include <glib.h>
-#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 
 #include <babeltrace2/babeltrace.h>
 
 #include "common/assert.h"
-#include "cpp-common/nlohmann/json.hpp"
+#include "cpp-common/vendor/fmt/core.h"
+#include "cpp-common/vendor/nlohmann/json.hpp"
 
-#include "../utils/run-in.hpp"
 #include "utils.hpp"
 
-static void run_trigger(const struct cond_trigger *trigger)
+CondTrigger::CondTrigger(const Type type, const std::string& condId,
+                         const bt2c::CStringView nameSuffix) noexcept :
+    _mType {type},
+    _mCondId {fmt::format("{}:{}", type == Type::PRE ? "pre" : "post", condId)},
+    _mName {fmt::format("{}{}{}", condId, nameSuffix ? "-" : "", nameSuffix ? nameSuffix : "")}
 {
-    switch (trigger->func_type) {
-    case COND_TRIGGER_FUNC_TYPE_BASIC:
-        trigger->func.basic();
-        break;
-    case COND_TRIGGER_FUNC_TYPE_RUN_IN_COMP_CLS_INIT:
-        runInCompClsInit(trigger->func.run_in_comp_cls_init);
-        break;
-    default:
-        abort();
-    }
 }
 
-static void list_triggers(const struct cond_trigger triggers[], size_t trigger_count)
+SimpleCondTrigger::SimpleCondTrigger(std::function<void()> func, const Type type,
+                                     const std::string& condId,
+                                     const bt2c::CStringView nameSuffix) :
+    CondTrigger {type, condId, nameSuffix},
+    _mFunc {std::move(func)}
 {
-    nlohmann::json trigger_array = nlohmann::json::array();
-
-    for (size_t i = 0; i < trigger_count; i++) {
-        nlohmann::json trigger_obj = nlohmann::json::object();
-        const cond_trigger& trigger = triggers[i];
-
-        /* Condition ID */
-        trigger_obj["cond-id"] = trigger.cond_id;
+}
 
-        /* Name starts with condition ID */
-        std::string name = trigger.cond_id;
+namespace {
 
-        if (trigger.suffix) {
-            name += '-';
-            name += trigger.suffix;
-        }
+void listCondTriggers(const CondTriggers& condTriggers) noexcept
+{
+    auto condTriggerArray = nlohmann::json::array();
 
-        trigger_obj["name"] = std::move(name);
-        trigger_array.push_back(std::move(trigger_obj));
+    for (const auto& condTrigger : condTriggers) {
+        condTriggerArray.push_back(nlohmann::json {
+            {"cond-id", condTrigger->condId()},
+            {"name", condTrigger->name()},
+        });
     }
 
-    auto str = trigger_array.dump();
-    std::cout << str;
-    std::flush(std::cout);
+    fmt::println("{}", condTriggerArray.dump());
 }
 
-void cond_main(int argc, const char *argv[], const struct cond_trigger triggers[],
-               size_t trigger_count)
+} /* namespace */
+
+void condMain(const int argc, const char ** const argv, const CondTriggers& condTriggers) noexcept
 {
     BT_ASSERT(argc >= 2);
 
     if (strcmp(argv[1], "list") == 0) {
-        list_triggers(triggers, trigger_count);
+        listCondTriggers(condTriggers);
     } else if (strcmp(argv[1], "run") == 0) {
-        int index;
-
+        /*
+         * It's expected that calling `*condTriggers[index]` below
+         * aborts (calls bt_common_abort()). In this testing context, we
+         * don't want any custom abortion command to run.
+         */
+        g_unsetenv("BABELTRACE_EXEC_ON_ABORT");
+
+        /* Call the trigger */
         BT_ASSERT(argc >= 3);
-        index = atoi(argv[2]);
-        BT_ASSERT(index >= 0 && index < trigger_count);
-        run_trigger(&triggers[index]);
+
+        const auto index = atoi(argv[2]);
+
+        BT_ASSERT(index >= 0 && index < condTriggers.size());
+        (*condTriggers[index])();
     }
 }
This page took 0.023577 seconds and 4 git commands to generate.