tests/lib/conds: make `condMain()` take a span for args
[babeltrace.git] / tests / lib / conds / utils.cpp
index 4df4340c3565624552302948ed9f2fd9dd542fbf..b7d2c487e03504eac70ed44f47f8ebb826154b55 100644 (file)
@@ -4,6 +4,7 @@
  * Copyright (C) 2020 Philippe Proulx <pproulx@efficios.com>
  */
 
+#include <glib.h>
 #include <stdlib.h>
 #include <string.h>
 
 #include "cpp-common/vendor/fmt/core.h"
 #include "cpp-common/vendor/nlohmann/json.hpp"
 
-#include "../utils/run-in.hpp"
 #include "utils.hpp"
 
-namespace {
-
-void runTrigger(const cond_trigger& trigger) noexcept
+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();
-    }
 }
 
-void listTriggers(const bt2s::span<const cond_trigger> triggers) noexcept
+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)}
 {
-    auto triggerArray = nlohmann::json::array();
-
-    for (auto& trigger : triggers) {
-        auto triggerObj = nlohmann::json::object();
-
-        /* Condition ID */
-        triggerObj["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();
 
-        triggerObj["name"] = std::move(name);
-        triggerArray.push_back(std::move(triggerObj));
+    for (const auto& condTrigger : condTriggers) {
+        condTriggerArray.push_back(nlohmann::json {
+            {"cond-id", condTrigger->condId()},
+            {"name", condTrigger->name()},
+        });
     }
 
-    fmt::println("{}", triggerArray.dump());
+    fmt::println("{}", condTriggerArray.dump());
 }
 
 } /* namespace */
 
-void condMain(const int argc, const char ** const argv,
-              const bt2s::span<const cond_trigger> triggers) noexcept
+void condMain(const bt2s::span<const char * const> argv, const CondTriggers& condTriggers) noexcept
 {
-    BT_ASSERT(argc >= 2);
+    BT_ASSERT(argv.size() >= 2);
 
     if (strcmp(argv[1], "list") == 0) {
-        listTriggers(triggers);
+        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(argv.size() >= 3);
+
+        const auto index = atoi(argv[2]);
 
-        BT_ASSERT(argc >= 3);
-        index = atoi(argv[2]);
-        BT_ASSERT(index >= 0 && index < triggers.size());
-        runTrigger(triggers[index]);
+        BT_ASSERT(index >= 0 && index < condTriggers.size());
+        (*condTriggers[index])();
     }
 }
This page took 0.024356 seconds and 4 git commands to generate.