X-Git-Url: https://git.efficios.com/?a=blobdiff_plain;f=tests%2Flib%2Fconds%2Futils.cpp;h=f1d9969e1ed24f2f8806349aa07b47c826866d94;hb=25fe6ea5b2626380612aeb0db1a9fa71d3c53952;hp=53984752a85576deab3cb565ea3b04dccc0f3c30;hpb=c802cacb9f0879a42e01575595a75bbefe7d3db9;p=babeltrace.git diff --git a/tests/lib/conds/utils.cpp b/tests/lib/conds/utils.cpp index 53984752..f1d9969e 100644 --- a/tests/lib/conds/utils.cpp +++ b/tests/lib/conds/utils.cpp @@ -4,77 +4,72 @@ * Copyright (C) 2020 Philippe Proulx */ -#include - -#include #include -#include #include #include #include #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 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])(); } }