Skip to content

Commit d87527c

Browse files
committed
TestFixture: added SettingsBuilder to allow inline initialization of objects
1 parent eff646d commit d87527c

2 files changed

Lines changed: 34 additions & 1 deletion

File tree

test/fixture.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,3 +365,8 @@ void TestFixture::reportErr(const ErrorMessage &msg)
365365
if (errout.str().find(errormessage) == std::string::npos)
366366
errout << errormessage << std::endl;
367367
}
368+
369+
TestFixture::SettingsBuilder& TestFixture::SettingsBuilder::library(const char lib[]) {
370+
LOAD_LIB_2_EXE(settings.library, lib, fixture.exename.c_str());
371+
return *this;
372+
}

test/fixture.h

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "color.h"
2525
#include "config.h"
2626
#include "errorlogger.h"
27+
#include "settings.h"
2728

2829
#include <cstddef>
2930
#include <set>
@@ -45,6 +46,7 @@ class TestFixture : public ErrorLogger {
4546
std::string mTestname;
4647

4748
protected:
49+
friend class SettingsBuilder;
4850
std::string exename;
4951
std::string testToRun;
5052
bool quiet_tests;
@@ -120,6 +122,31 @@ class TestFixture : public ErrorLogger {
120122
T& check = getCheck<T>();
121123
check.runChecks(tokenizer, settings, errorLogger);
122124
}
125+
126+
class SettingsBuilder
127+
{
128+
public:
129+
SettingsBuilder(const TestFixture &fixture) : fixture(fixture) {}
130+
131+
SettingsBuilder& severity(Severity::SeverityType sev) {
132+
settings.severity.enable(sev);
133+
return *this;
134+
}
135+
136+
SettingsBuilder& library(const char lib[]);
137+
138+
Settings build() {
139+
return std::move(settings);
140+
}
141+
private:
142+
Settings settings;
143+
const TestFixture &fixture;
144+
};
145+
146+
SettingsBuilder settingsBuilder() const {
147+
return SettingsBuilder(*this);
148+
}
149+
123150
public:
124151
void reportOut(const std::string &outmsg, Color c = Color::Reset) override;
125152
void reportErr(const ErrorMessage &msg) override;
@@ -157,7 +184,8 @@ extern std::ostringstream output;
157184
#define EXPECT_EQ( EXPECTED, ACTUAL ) assertEquals(__FILE__, __LINE__, EXPECTED, ACTUAL)
158185
#define REGISTER_TEST( CLASSNAME ) namespace { CLASSNAME instance_ ## CLASSNAME; }
159186

160-
#define LOAD_LIB_2( LIB, NAME ) do { if (((LIB).load(exename.c_str(), NAME).errorcode != Library::ErrorCode::OK)) throw std::runtime_error("library '" + std::string(NAME) + "' not found"); } while (false)
187+
#define LOAD_LIB_2_EXE( LIB, NAME, EXE ) do { if (((LIB).load((EXE), NAME).errorcode != Library::ErrorCode::OK)) throw std::runtime_error("library '" + std::string(NAME) + "' not found"); } while (false)
188+
#define LOAD_LIB_2( LIB, NAME ) LOAD_LIB_2_EXE(LIB, NAME, exename.c_str())
161189

162190
#define PLATFORM( S, P ) do { std::string errstr; assertEquals(__FILE__, __LINE__, true, S.platform(cppcheck::Platform::platformString(P), errstr, {exename}), errstr); } while (false)
163191

0 commit comments

Comments
 (0)