Skip to content

Commit 29d44cd

Browse files
committed
Add non-asserting test expectation macros
These fail test instead of killing the process.
1 parent c590566 commit 29d44cd

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

tdutils/td/utils/tests.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,10 @@ bool TestsRunner::run_all_step() {
213213

214214
auto passed = Time::now() - state_.start;
215215
auto real_passed = Time::now_unadjusted() - state_.start_unadjusted;
216-
if (real_passed + 1e-9 > passed) {
216+
if (test_failed_) {
217+
LOG(ERROR) << "FAILED in " << format::as_time(passed);
218+
any_test_failed_ = true;
219+
} else if (real_passed + 1e-9 > passed) {
217220
LOG(ERROR) << format::as_time(passed);
218221
} else {
219222
LOG(ERROR) << format::as_time(passed) << " real[" << format::as_time(real_passed) << "]";
@@ -222,12 +225,14 @@ bool TestsRunner::run_all_step() {
222225
regression_tester_->save_db();
223226
}
224227
state_.is_running = false;
228+
test_failed_ = false;
225229
++state_.it;
226230
}
227231

228232
auto ret = state_.it != state_.end;
229233
if (!ret) {
230234
state_ = State();
235+
test_failed_ = false;
231236
}
232237
return ret || stress_flag_;
233238
}
@@ -246,4 +251,13 @@ Status TestsRunner::verify(Slice data) {
246251
return regression_tester_->verify_test(PSLICE() << name() << "_default", data);
247252
}
248253

254+
void TestsRunner::register_test_failure() {
255+
CHECK(state_.is_running);
256+
test_failed_ = true;
257+
}
258+
259+
bool TestsRunner::any_test_failed() const {
260+
return any_test_failed_;
261+
}
262+
249263
} // namespace td

tdutils/td/utils/tests.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ class TestContext : public Context<TestContext> {
103103
virtual ~TestContext() = default;
104104
virtual Slice name() = 0;
105105
virtual Status verify(Slice data) = 0;
106+
virtual void register_test_failure() = 0;
106107
};
107108

108109
class TestsRunner : public TestContext {
@@ -115,6 +116,7 @@ class TestsRunner : public TestContext {
115116
void run_all();
116117
bool run_all_step();
117118
void set_regression_tester(unique_ptr<RegressionTester> regression_tester);
119+
bool any_test_failed() const;
118120

119121
private:
120122
struct State {
@@ -128,10 +130,13 @@ class TestsRunner : public TestContext {
128130
vector<string> substr_filters_;
129131
vector<std::pair<string, unique_ptr<Test>>> tests_;
130132
State state_;
133+
std::atomic<bool> test_failed_ = false;
131134
unique_ptr<RegressionTester> regression_tester_;
135+
bool any_test_failed_{false};
132136

133137
Slice name() override;
134138
Status verify(Slice data) override;
139+
void register_test_failure() override;
135140
};
136141

137142
template <class T>
@@ -238,6 +243,22 @@ std::optional<std::string> check_eq(auto const &a_value, auto const &b_value, ch
238243
} \
239244
} while (0)
240245

246+
#define EXPECT(cond) \
247+
do { \
248+
if (auto error_message = ::td::detail::check(static_cast<bool>(cond), #cond)) { \
249+
LOG(ERROR) << *error_message; \
250+
::td::TestContext::get()->register_test_failure(); \
251+
} \
252+
} while (0)
253+
254+
#define EXPECT_EQ(a, b) \
255+
do { \
256+
if (auto error_message = ::td::detail::check_eq(a, b, #a, #b)) { \
257+
LOG(ERROR) << *error_message; \
258+
::td::TestContext::get()->register_test_failure(); \
259+
} \
260+
} while (0)
261+
241262
#define REGRESSION_VERIFY(data) ::td::TestContext::get()->verify(data).ensure()
242263

243264
#define TEST_NAME(test_case_name, test_name) \

test/test-td-main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,5 @@ int main(int argc, char **argv) {
5050
}
5151
}
5252
runner.run_all();
53-
return 0;
53+
return runner.any_test_failed() ? 1 : 0;
5454
}

0 commit comments

Comments
 (0)