Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refs #12080 - testrunner: do not create test implementation until it is used #6691

Merged
merged 1 commit into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions test/fixture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@
**/
namespace {
struct CompareFixtures {
bool operator()(const TestFixture* lhs, const TestFixture* rhs) const {
bool operator()(const TestInstance* lhs, const TestInstance* rhs) const {
return lhs->classname < rhs->classname;
}
};
}
using TestSet = std::set<TestFixture*, CompareFixtures>;
using TestSet = std::set<TestInstance*, CompareFixtures>;
namespace {
class TestRegistry {
TestSet _tests;
Expand All @@ -57,7 +57,7 @@ namespace {
return testreg;
}

void addTest(TestFixture *t) {
void addTest(TestInstance *t) {
_tests.insert(t);
}

Expand All @@ -67,7 +67,11 @@ namespace {
};
}


TestInstance::TestInstance(const char * _name)
: classname(_name)
{
TestRegistry::theInstance().addTest(this);
}


/**
Expand All @@ -83,9 +87,7 @@ std::size_t TestFixture::succeeded_todos_counter = 0;

TestFixture::TestFixture(const char * const _name)
: classname(_name)
{
TestRegistry::theInstance().addTest(this);
}
{}


bool TestFixture::prepareTest(const char testname[])
Expand Down Expand Up @@ -389,10 +391,11 @@ std::size_t TestFixture::runTests(const options& args)
classname.erase(classname.find("::"));
}

for (TestFixture * test : TestRegistry::theInstance().tests()) {
for (TestInstance * test : TestRegistry::theInstance().tests()) {
if (classname.empty() || test->classname == classname) {
test->processOptions(args);
test->run(testname);
TestFixture* fixture = test->create();
fixture->processOptions(args);
fixture->run(testname);
}
}
}
Expand Down
14 changes: 13 additions & 1 deletion test/fixture.h
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,18 @@ class TestFixture : public ErrorLogger {
static std::size_t runTests(const options& args);
};

class TestInstance {
public:
explicit TestInstance(const char * _name);
virtual ~TestInstance() = default;

virtual TestFixture* create() = 0;

const std::string classname;
protected:
std::unique_ptr<TestFixture> impl;
};

// TODO: most asserts do not actually assert i.e. do not return
#define TEST_CASE( NAME ) do { if (prepareTest(#NAME)) { setVerbose(false); try { NAME(); teardownTest(); } catch (...) { assertNoThrowFail(__FILE__, __LINE__); } } } while (false)
#define ASSERT( CONDITION ) if (!assert_(__FILE__, __LINE__, (CONDITION))) return
Expand All @@ -306,7 +318,7 @@ class TestFixture : public ErrorLogger {
#define TODO_ASSERT( CONDITION ) do { const bool condition=(CONDITION); todoAssertEquals(__FILE__, __LINE__, true, false, condition); } while (false)
#define TODO_ASSERT_EQUALS( WANTED, CURRENT, ACTUAL ) todoAssertEquals(__FILE__, __LINE__, WANTED, CURRENT, ACTUAL)
#define EXPECT_EQ( EXPECTED, ACTUAL ) assertEquals(__FILE__, __LINE__, EXPECTED, ACTUAL)
#define REGISTER_TEST( CLASSNAME ) namespace { CLASSNAME instance_ ## CLASSNAME; }
#define REGISTER_TEST( CLASSNAME ) namespace { class CLASSNAME ## Instance : public TestInstance { public: CLASSNAME ## Instance() : TestInstance(#CLASSNAME) {} TestFixture* create() override { impl.reset(new CLASSNAME); return impl.get(); } }; CLASSNAME ## Instance instance_ ## CLASSNAME; }

#define PLATFORM( P, T ) do { std::string errstr; assertEquals(__FILE__, __LINE__, true, P.set(Platform::toString(T), errstr, {exename}), errstr); } while (false)

Expand Down
2 changes: 1 addition & 1 deletion test/testmemleak.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#include <cstddef>
#include <list>

class TestMemleak : private TestFixture {
class TestMemleak : public TestFixture {
public:
TestMemleak() : TestFixture("TestMemleak") {}

Expand Down
Loading