X-Git-Url: https://git.efficios.com/?a=blobdiff_plain;f=tests%2Flib%2Fconds%2Futils.cpp;h=9bf5450fe5bf56ab461c54e80ccab1439edc5b47;hb=ba33b664622ee13ddf7ce3682986fda048868277;hp=7a70e72f4a01a372d7367a6608e700f2cf077416;hpb=78c888d139a40b58b10a2c92829cbb2c814ad766;p=babeltrace.git diff --git a/tests/lib/conds/utils.cpp b/tests/lib/conds/utils.cpp index 7a70e72f..9bf5450f 100644 --- a/tests/lib/conds/utils.cpp +++ b/tests/lib/conds/utils.cpp @@ -4,76 +4,73 @@ * Copyright (C) 2020 Philippe Proulx */ -#include - #include -#include #include #include #include #include "common/assert.h" +#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 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(); - } } -static void list_triggers(const struct cond_trigger triggers[], size_t trigger_count) +SimpleCondTrigger::SimpleCondTrigger(std::function func, const Type type, + const std::string& condId, + const bt2s::optional& 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])(); } }