X-Git-Url: http://git.efficios.com/?p=babeltrace.git;a=blobdiff_plain;f=tests%2Flib%2Fconds%2Futils.cpp;fp=tests%2Flib%2Fconds%2Futils.cpp;h=28f0ba40748eecf0000e2811817eea603ac08546;hp=4df4340c3565624552302948ed9f2fd9dd542fbf;hb=5d7e57e846a27172cc3bc5d0fcf5b3e55551e289;hpb=0133a2ba3063b5b8526990bf95f2f53ed212f6a6 diff --git a/tests/lib/conds/utils.cpp b/tests/lib/conds/utils.cpp index 4df4340c..28f0ba40 100644 --- a/tests/lib/conds/utils.cpp +++ b/tests/lib/conds/utils.cpp @@ -13,65 +13,55 @@ #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 bt2s::optional& nameSuffix) noexcept : + _mType {type}, + _mCondId {fmt::format("{}:{}", type == Type::PRE ? "pre" : "post", condId)}, + _mName { + fmt::format("{}{}{}", condId, nameSuffix ? "-" : "", nameSuffix ? nameSuffix->data() : "")} { - 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 triggers) noexcept +SimpleCondTrigger::SimpleCondTrigger(std::function func, const Type type, + const std::string& condId, + const bt2s::optional& 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 triggers) noexcept +void condMain(const int argc, const char ** const argv, const CondTriggers condTriggers) noexcept { BT_ASSERT(argc >= 2); if (strcmp(argv[1], "list") == 0) { - listTriggers(triggers); + listCondTriggers(condTriggers); } else if (strcmp(argv[1], "run") == 0) { - int index; - BT_ASSERT(argc >= 3); - index = atoi(argv[2]); - BT_ASSERT(index >= 0 && index < triggers.size()); - runTrigger(triggers[index]); + + const auto index = atoi(argv[2]); + + BT_ASSERT(index >= 0 && index < condTriggers.size()); + (*condTriggers[index])(); } }