Skip to content

Commit 1eebd46

Browse files
authored
Cppunit and data test enhancements (#4616)
* enh(CppUnit): Source code enhancements. * enh(DataTest): Code enhancements (mostly to use override) to prevent wrong test calls when renaming.
1 parent 669be63 commit 1eebd46

File tree

20 files changed

+199
-206
lines changed

20 files changed

+199
-206
lines changed

CppUnit/include/CppUnit/CppUnitException.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ class CppUnit_API CppUnitException: public std::exception
3333
long data2lineNumber,
3434
const std::string& fileName);
3535
CppUnitException(const CppUnitException& other);
36-
virtual ~CppUnitException() noexcept;
36+
~CppUnitException() noexcept override;
3737

3838
CppUnitException& operator = (const CppUnitException& other);
3939

40-
const char* what() const noexcept;
40+
const char* what() const noexcept override;
4141

4242
long lineNumber() const;
4343
long data1LineNumber() const;

CppUnit/include/CppUnit/Orthodox.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class Orthodox: public TestCase
5555

5656
protected:
5757
ClassUnderTest call(ClassUnderTest object);
58-
void runTest ();
58+
void runTest () override;
5959
};
6060

6161

CppUnit/include/CppUnit/RepeatedTest.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class CppUnit_API RepeatedTest: public TestDecorator
3434

3535
int countTestCases();
3636
std::string toString();
37-
void run(TestResult* result, const Test::Callback& callback = nullptr);
37+
void run(TestResult* result, const Test::Callback& callback = nullptr) override;
3838

3939
private:
4040
const int _timesRepeat;

CppUnit/include/CppUnit/TestCase.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#include "CppUnit/TestResult.h"
1414
#include "CppUnit/CppUnitException.h"
1515
#include <string>
16-
#include <utility>
1716
#include <vector>
1817
#include <typeinfo>
1918

CppUnit/src/TestCase.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55

66
#include <stdexcept>
7-
#include <math.h>
7+
#include <cmath>
88
#include "CppUnit/TestCase.h"
99
#include "CppUnit/TestResult.h"
1010
#include "CppUnit/estring.h"
@@ -117,7 +117,7 @@ void TestCase::run(TestResult *result, const Test::Callback& callback)
117117
}
118118
catch (CppUnitException& e)
119119
{
120-
CppUnitException* copy = new CppUnitException(e);
120+
auto* copy = new CppUnitException(e);
121121
result->addFailure(this, copy);
122122
}
123123
catch (std::exception& e)
@@ -128,7 +128,7 @@ void TestCase::run(TestResult *result, const Test::Callback& callback)
128128
}
129129
catch (...)
130130
{
131-
CppUnitException *e = new CppUnitException ("unknown exception");
131+
auto* e = new CppUnitException ("unknown exception");
132132
result->addError (this, e);
133133
}
134134
tearDown ();

CppUnit/src/TestDecorator.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@ TestDecorator::TestDecorator(Test* test)
1515
}
1616

1717

18-
TestDecorator::~TestDecorator()
19-
{
20-
}
18+
TestDecorator::~TestDecorator() = default;
2119

2220

2321
int TestDecorator::countTestCases() const

CppUnit/src/TestRunner.cpp

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#include "CppUnit/TestSuite.h"
99
#include "CppUnit/TextTestResult.h"
1010
#include <iostream>
11-
#include <fstream>
1211

1312

1413
namespace CppUnit {
@@ -28,8 +27,8 @@ TestRunner::TestRunner(std::ostream& ostr):
2827

2928
TestRunner::~TestRunner()
3029
{
31-
for (Mappings::iterator it = _mappings.begin(); it != _mappings.end(); ++it)
32-
delete it->second;
30+
for (auto & _mapping : _mappings)
31+
delete _mapping.second;
3332
}
3433

3534

@@ -80,9 +79,9 @@ bool TestRunner::run(const std::vector<std::string>& args, const Test::Callback&
8079
}
8180
else if (arg == "-print")
8281
{
83-
for (Mappings::iterator it = _mappings.begin(); it != _mappings.end(); ++it)
82+
for (auto& _mapping : _mappings)
8483
{
85-
print(it->first, it->second, 0);
84+
print(_mapping.first, _mapping.second, 0);
8685
}
8786
printed = true;
8887
continue;
@@ -104,8 +103,8 @@ bool TestRunner::run(const std::vector<std::string>& args, const Test::Callback&
104103
return false;
105104
}
106105

107-
Test* testToRun = 0;
108-
for (Mappings::iterator it = _mappings.begin(); !testToRun && it != _mappings.end(); ++it)
106+
Test* testToRun = nullptr;
107+
for (auto it = _mappings.begin(); !testToRun && it != _mappings.end(); ++it)
109108
{
110109
testToRun = find(testCase, it->second, it->first);
111110
}
@@ -124,18 +123,18 @@ bool TestRunner::run(const std::vector<std::string>& args, const Test::Callback&
124123
if (all)
125124
{
126125
tests.clear();
127-
for (Mappings::iterator it = _mappings.begin(); it != _mappings.end(); ++it)
126+
for (auto& _mapping : _mappings)
128127
{
129-
collectAllTestCases(it->second, tests);
128+
collectAllTestCases(_mapping.second, tests);
130129
}
131130
}
132131

133132
TextTestResult result(_ostr, ignore);
134-
for (std::vector<Test*>::const_iterator it = tests.begin(); it != tests.end(); ++it)
133+
for (auto testToRun : tests)
135134
{
136-
Test* testToRun = *it;
137135
if(testToRun->getType() == Test::Long && !longRunning)
138136
continue;
137+
139138
if (setup.size() > 0)
140139
testToRun->addSetup(setup);
141140

@@ -163,7 +162,7 @@ bool TestRunner::run(const std::vector<std::string>& args, const Test::Callback&
163162

164163
void TestRunner::addTest(const std::string& name, Test* test)
165164
{
166-
_mappings.push_back(Mapping(name, test));
165+
_mappings.emplace_back(name, test);
167166
}
168167

169168

@@ -176,9 +175,9 @@ void TestRunner::print(const std::string& name, Test* pTest, int indent)
176175
if (pSuite)
177176
{
178177
const std::vector<Test*>& tests = pSuite->tests();
179-
for (std::vector<Test*>::const_iterator it = tests.begin(); it != tests.end(); ++it)
178+
for (auto* test : tests)
180179
{
181-
print((*it)->toString(), *it, indent + 1);
180+
print(test->toString(), test, indent + 1);
182181
}
183182
}
184183
}
@@ -192,17 +191,17 @@ Test* TestRunner::find(const std::string& name, Test* pTest, const std::string&
192191
}
193192
else
194193
{
195-
TestSuite* pSuite = dynamic_cast<TestSuite*>(pTest);
194+
auto* pSuite = dynamic_cast<TestSuite*>(pTest);
196195
if (pSuite)
197196
{
198197
const std::vector<Test*>& tests = pSuite->tests();
199-
for (std::vector<Test*>::const_iterator it = tests.begin(); it != tests.end(); ++it)
198+
for (auto* test : tests)
200199
{
201-
Test* result = find(name, *it, (*it)->toString());
200+
Test* result = find(name, test, test->toString());
202201
if (result) return result;
203202
}
204203
}
205-
return 0;
204+
return nullptr;
206205
}
207206
}
208207

@@ -212,14 +211,14 @@ int TestRunner::collectAllTestCases(Test* pTest, std::vector<Test*>& testcases)
212211
int added = 0;
213212
if (pTest->getType() == Test::Suite)
214213
{
215-
TestSuite* pSuite = dynamic_cast<TestSuite*>(pTest);
214+
auto* pSuite = dynamic_cast<TestSuite*>(pTest);
216215

217216
if (pSuite)
218217
{
219218
const std::vector<Test*>& tests = pSuite->tests();
220-
for (std::vector<Test*>::const_iterator it = tests.begin(); it != tests.end(); ++it)
219+
for (auto* test : tests)
221220
{
222-
added += collectAllTestCases(*it, testcases);
221+
added += collectAllTestCases(test, testcases);
223222
}
224223
}
225224
}

CppUnit/src/TestSuite.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,22 @@ namespace CppUnit {
1313
// Deletes all tests in the suite.
1414
void TestSuite::deleteContents()
1515
{
16-
for (std::vector<Test*>::iterator it = _tests.begin(); it != _tests.end(); ++it)
17-
delete *it;
16+
for (auto* _test : _tests)
17+
delete _test;
1818
}
1919

2020

2121
// Runs the tests and collects their result in a TestResult.
2222
void TestSuite::run(TestResult *result, const Test::Callback& callback)
2323
{
24-
for (std::vector<Test*>::iterator it = _tests.begin(); it != _tests.end(); ++it)
24+
for (auto* test : _tests)
2525
{
2626
if (result->shouldStop())
2727
break;
2828

29-
Test *test = *it;
3029
if (!setup().empty())
3130
test->addSetup(setup());
31+
3232
test->run(result, callback);
3333
}
3434
}
@@ -39,8 +39,8 @@ int TestSuite::countTestCases() const
3939
{
4040
int count = 0;
4141

42-
for (std::vector<Test*>::const_iterator it = _tests.begin(); it != _tests.end(); ++it)
43-
count += (*it)->countTestCases();
42+
for (auto* _test : _tests)
43+
count += _test->countTestCases();
4444

4545
return count;
4646
}

Data/DataTest/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ file(GLOB SRCS_G ${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp)
33
POCO_SOURCES_AUTO(DATA_TEST_LIB_SRCS ${SRCS_G})
44

55
# Headers
6-
file(GLOB HDRS_G ${CMAKE_CURRENT_SOURCE_DIR}/include/*.h)
6+
file(GLOB HDRS_G ${CMAKE_CURRENT_SOURCE_DIR}/include/Poco/Data/Test/*.h)
77
POCO_HEADERS_AUTO(DATA_TEST_LIB_SRCS ${HDRS_G})
88

99
# Version Resource

Data/DataTest/include/Poco/Data/Test/SQLExecutor.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,13 @@
1414
#define DataTest_SQLExecutor_INCLUDED
1515

1616

17+
#include "CppUnit/TestCase.h"
1718
#include "Poco/Data/Test/DataTest.h"
1819
#include "Poco/Data/Session.h"
1920
#include "Poco/Data/BulkExtraction.h"
2021
#include "Poco/Data/BulkBinding.h"
21-
#include "Poco/NumberFormatter.h"
22-
#include "Poco/String.h"
2322
#include "Poco/Exception.h"
2423
#include <iostream>
25-
#include <string_view>
2624

2725

2826
namespace Poco {
@@ -55,7 +53,7 @@ class DataTest_API SQLExecutor: public CppUnit::TestCase
5553
};
5654

5755
SQLExecutor(const std::string& name, Poco::Data::Session* pSession, Poco::Data::Session* pEncSession = nullptr, bool numberedPlaceHolders = false);
58-
~SQLExecutor();
56+
~SQLExecutor() override;
5957

6058
template <typename C>
6159
void connection(C& c, const std::string& connectString)

0 commit comments

Comments
 (0)