1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2025
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#pragma once
#include "td/utils/common.h"
#include "td/utils/logging.h"
#include "td/utils/PathView.h"
#include "td/utils/port/thread_local.h"
#include "td/utils/Slice.h"
#include "td/utils/Status.h"
#include "td/utils/StringBuilder.h"
#include "td/utils/tests.h"
#include <utility>
namespace td {
namespace simple_test {
class DebugContext {
public:
static DebugContext &instance() {
static TD_THREAD_LOCAL DebugContext *instance{};
init_thread_local<DebugContext>(instance);
return *instance;
}
template <typename T>
void add(Slice name, const T &value, CSlice file = {}, int line = 0) {
StringBuilder sb;
sb << name << "=" << value;
if (file.empty()) {
sb << " (at " << PathView(file).file_name() << ":" << line << ")";
}
values_.emplace_back(name.str(), sb.as_cslice().str());
}
void pop_back() {
if (!values_.empty()) {
values_.pop_back();
}
}
friend StringBuilder &operator<<(StringBuilder &sb, const DebugContext &ctx) {
if (ctx.values_.empty()) {
return sb;
}
sb << "\nDebug context:";
for (const auto &pair : ctx.values_) {
sb << "\n " << pair.second;
}
return sb;
}
private:
vector<std::pair<string, string>> values_;
};
template <typename T>
class ScopedDebugValue {
public:
ScopedDebugValue(Slice name, const T &value, CSlice file = {}, int line = 0) : name_(name.str()) {
DebugContext::instance().add(name_, value, file, line);
}
~ScopedDebugValue() {
DebugContext::instance().pop_back();
}
private:
string name_;
};
inline Status format_error(StringBuilder &sb, const char *file, int line) {
sb << "\n\tat " << PathView(Slice(file)).file_name() << ":" << line << DebugContext::instance();
return Status::Error(sb.as_cslice());
}
} // namespace simple_test
class StatusTest : public Test {
public:
~StatusTest() override = default;
virtual Status run_test() = 0;
virtual string get_test_name() const = 0;
void run() override {
auto status = run_test();
if (status.is_ok()) {
LOG(INFO) << "Test " << get_test_name() << " PASSED";
} else {
// Include debug context in error message if available
LOG(ERROR) << "Test " << get_test_name() << " FAILED: " << status.message()
<< simple_test::DebugContext::instance();
}
}
};
// Macro for defining status-based tests
#define S_TEST(test_case_name, test_name) \
class TD_CONCAT(StatusTest_, TD_CONCAT(test_case_name, TD_CONCAT(_, test_name))) final : public ::td::StatusTest { \
public: \
::td::Status run_test() override; \
::td::string get_test_name() const override { \
return #test_case_name "." #test_name; \
} \
}; \
::td::RegisterTest<TD_CONCAT(StatusTest_, TD_CONCAT(test_case_name, TD_CONCAT(_, test_name)))> TD_CONCAT( \
status_test_instance_, \
TD_CONCAT(TD_CONCAT(test_case_name, TD_CONCAT(_, test_name)), __LINE__))(#test_case_name "." #test_name); \
::td::Status TD_CONCAT(StatusTest_, TD_CONCAT(test_case_name, TD_CONCAT(_, test_name)))::run_test()
// Add a debug value within current scope with file/line info
#define TEST_DEBUG_VALUE(name, value) \
::td::simple_test::ScopedDebugValue<decltype(value)> TD_CONCAT(debug_value_, __LINE__)(#name, value, __FILE__, \
__LINE__)
#define TEST_TRY_RESULT(name, expr) \
auto TD_CONCAT(r_, __LINE__) = (expr); \
if (TD_CONCAT(r_, __LINE__).is_error()) { \
auto error = TD_CONCAT(r_, __LINE__).move_as_error(); \
::td::StringBuilder sb; \
sb << "\nFailed to execute " << #expr << ":\n\t" << error.message(); \
return ::td::simple_test::format_error(sb, __FILE__, __LINE__); \
} \
auto name = TD_CONCAT(r_, __LINE__).move_as_ok(); \
TEST_DEBUG_VALUE(name, name)
#define TEST_TRY_STATUS(expr) \
{ \
auto result = (expr); \
if (result.is_error()) { \
::td::StringBuilder sb; \
sb << "\nFailed to execute " << #expr << ":\n\t" << result.error().message(); \
return ::td::simple_test::format_error(sb, __FILE__, __LINE__); \
} \
}
#define TEST_ASSERT(condition, message) \
if (!(condition)) { \
::td::StringBuilder sb; \
sb << "\nAssertion failed: " << #condition << " -\n\t" << message; \
return ::td::simple_test::format_error(sb, __FILE__, __LINE__); \
}
#define TEST_ASSERT_EQ(expected_, received_, message) \
{ \
const auto &expected = (expected_); \
const auto &received = (received_); \
if (!(expected == received)) { \
::td::StringBuilder sb; \
sb << "\n" \
<< #received_ << " != " << #expected_ << " - " << message << "\n\texpected: " << expected \
<< "\n\treceived:" << received; \
return ::td::simple_test::format_error(sb, __FILE__, __LINE__); \
} \
}
} // namespace td
|