Change naming convention for enum class enumerators
[babeltrace.git] / tests / lib / conds / utils.cpp
CommitLineData
5d7e8359
PP
1/*
2 * SPDX-License-Identifier: GPL-2.0-only
3 *
4 * Copyright (C) 2020 Philippe Proulx <pproulx@efficios.com>
5 */
6
ba33b664 7#include <glib.h>
c802cacb 8#include <stdlib.h>
5d7e8359 9#include <string.h>
c802cacb 10
5d7e8359 11#include <babeltrace2/babeltrace.h>
5d7e8359
PP
12
13#include "common/assert.h"
6e3150f4 14#include "cpp-common/vendor/fmt/core.h"
78c888d1 15#include "cpp-common/vendor/nlohmann/json.hpp"
c802cacb 16
c802cacb 17#include "utils.hpp"
5d7e8359 18
5d7e57e8 19CondTrigger::CondTrigger(const Type type, const std::string& condId,
4ad43657 20 const bt2c::CStringView nameSuffix) noexcept :
5d7e57e8 21 _mType {type},
1c5ea5eb 22 _mCondId {fmt::format("{}:{}", type == Type::Pre ? "pre" : "post", condId)},
4ad43657 23 _mName {fmt::format("{}{}{}", condId, nameSuffix ? "-" : "", nameSuffix ? nameSuffix : "")}
5d7e8359 24{
5d7e8359
PP
25}
26
5d7e57e8
SM
27SimpleCondTrigger::SimpleCondTrigger(std::function<void()> func, const Type type,
28 const std::string& condId,
4ad43657 29 const bt2c::CStringView nameSuffix) :
5d7e57e8
SM
30 CondTrigger {type, condId, nameSuffix},
31 _mFunc {std::move(func)}
5d7e8359 32{
5d7e57e8 33}
5d7e8359 34
5d7e57e8 35namespace {
5d7e8359 36
25fe6ea5 37void listCondTriggers(const CondTriggers& condTriggers) noexcept
5d7e57e8
SM
38{
39 auto condTriggerArray = nlohmann::json::array();
5d7e8359 40
25fe6ea5 41 for (const auto& condTrigger : condTriggers) {
5d7e57e8
SM
42 condTriggerArray.push_back(nlohmann::json {
43 {"cond-id", condTrigger->condId()},
44 {"name", condTrigger->name()},
45 });
486428e8 46 }
5d7e8359 47
5d7e57e8 48 fmt::println("{}", condTriggerArray.dump());
5d7e8359
PP
49}
50
6e3150f4
SM
51} /* namespace */
52
fb472529 53void condMain(const bt2s::span<const char * const> argv, const CondTriggers& condTriggers) noexcept
5d7e8359 54{
fb472529 55 BT_ASSERT(argv.size() >= 2);
486428e8
SM
56
57 if (strcmp(argv[1], "list") == 0) {
5d7e57e8 58 listCondTriggers(condTriggers);
486428e8 59 } else if (strcmp(argv[1], "run") == 0) {
ba33b664
PP
60 /*
61 * It's expected that calling `*condTriggers[index]` below
62 * aborts (calls bt_common_abort()). In this testing context, we
63 * don't want any custom abortion command to run.
64 */
65 g_unsetenv("BABELTRACE_EXEC_ON_ABORT");
66
67 /* Call the trigger */
fb472529 68 BT_ASSERT(argv.size() >= 3);
5d7e57e8
SM
69
70 const auto index = atoi(argv[2]);
71
72 BT_ASSERT(index >= 0 && index < condTriggers.size());
73 (*condTriggers[index])();
486428e8 74 }
5d7e8359 75}
This page took 0.04415 seconds and 4 git commands to generate.