tests: create Test instance only when it is needed
GitOrigin-RevId: 06bd6063018040bfee9e3081d77c24683940eb34
This commit is contained in:
parent
1b628d3ab7
commit
f74d8ba023
@ -138,13 +138,13 @@ TestsRunner &TestsRunner::get_default() {
|
||||
return default_runner;
|
||||
}
|
||||
|
||||
void TestsRunner::add_test(string name, unique_ptr<Test> test) {
|
||||
void TestsRunner::add_test(string name, std::function<unique_ptr<Test>()> test) {
|
||||
for (auto &it : tests_) {
|
||||
if (it.first == name) {
|
||||
LOG(FATAL) << "Test name collision " << name;
|
||||
}
|
||||
}
|
||||
tests_.emplace_back(name, std::move(test));
|
||||
tests_.emplace_back(name, TestInfo{std::move(test), nullptr});
|
||||
}
|
||||
|
||||
void TestsRunner::add_substr_filter(string str) {
|
||||
@ -176,7 +176,7 @@ bool TestsRunner::run_all_step() {
|
||||
|
||||
while (state_.it != state_.end) {
|
||||
auto &name = tests_[state_.it].first;
|
||||
auto test = tests_[state_.it].second.get();
|
||||
auto &test = tests_[state_.it].second.test;
|
||||
if (!state_.is_running) {
|
||||
bool ok = true;
|
||||
for (const auto &filter : substr_filters_) {
|
||||
@ -194,12 +194,17 @@ bool TestsRunner::run_all_step() {
|
||||
state_.start = Time::now();
|
||||
state_.start_unadjusted = Time::now_unadjusted();
|
||||
state_.is_running = true;
|
||||
|
||||
CHECK(!test);
|
||||
test = tests_[state_.it].second.creator();
|
||||
}
|
||||
|
||||
if (test->step()) {
|
||||
break;
|
||||
}
|
||||
|
||||
test = {};
|
||||
|
||||
auto passed = Time::now() - state_.start;
|
||||
auto real_passed = Time::now_unadjusted() - state_.start_unadjusted;
|
||||
if (real_passed + 1e-1 > passed) {
|
||||
|
@ -96,7 +96,7 @@ class TestsRunner : public TestContext {
|
||||
public:
|
||||
static TestsRunner &get_default();
|
||||
|
||||
void add_test(string name, unique_ptr<Test> test);
|
||||
void add_test(string name, std::function<unique_ptr<Test>()> test);
|
||||
void add_substr_filter(string str);
|
||||
void set_stress_flag(bool flag);
|
||||
void run_all();
|
||||
@ -113,7 +113,11 @@ class TestsRunner : public TestContext {
|
||||
};
|
||||
bool stress_flag_{false};
|
||||
vector<string> substr_filters_;
|
||||
vector<std::pair<string, unique_ptr<Test>>> tests_;
|
||||
struct TestInfo {
|
||||
std::function<unique_ptr<Test>()> creator;
|
||||
unique_ptr<Test> test;
|
||||
};
|
||||
vector<std::pair<string, TestInfo>> tests_;
|
||||
State state_;
|
||||
unique_ptr<RegressionTester> regression_tester_;
|
||||
|
||||
@ -125,7 +129,7 @@ template <class T>
|
||||
class RegisterTest {
|
||||
public:
|
||||
explicit RegisterTest(string name, TestsRunner &runner = TestsRunner::get_default()) {
|
||||
runner.add_test(name, make_unique<T>());
|
||||
runner.add_test(name, [] { return make_unique<T>(); });
|
||||
}
|
||||
};
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user