mirror of
https://github.com/unittest-cpp/unittest-cpp
synced 2026-09-26 16:19:30 +03:00
coded feature
This commit is contained in:
parent
0be4fefd74
commit
183209faf8
2 changed files with 143 additions and 7 deletions
|
|
@ -7,23 +7,57 @@ using namespace std;
|
|||
using namespace UnitTest;
|
||||
|
||||
|
||||
ParameterizedTest::ParameterizedTest()
|
||||
ParameterizedTestAbstract::ParameterizedTestAbstract()
|
||||
: _iteration(0),
|
||||
_testAnchor(nullptr),
|
||||
_lastTest(nullptr),
|
||||
_nextTestBackup(nullptr)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
ParameterizedTest::~ParameterizedTest()
|
||||
ParameterizedTestAbstract::~ParameterizedTestAbstract()
|
||||
{
|
||||
if (_testAnchor != nullptr)
|
||||
{
|
||||
delete _testAnchor;
|
||||
_testAnchor = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Test* const ParameterizedTest::retrieveCurrentTest()
|
||||
size_t ParameterizedTestAbstract::getIteration()
|
||||
{
|
||||
ensureInitialized();
|
||||
return _iteration;
|
||||
}
|
||||
|
||||
|
||||
void ParameterizedTestAbstract::ensureInitialized()
|
||||
{
|
||||
Test* currentTest = retrieveCurrentTest();
|
||||
|
||||
if (_testAnchor == nullptr)
|
||||
{
|
||||
_testAnchor = new TestAnchor("ParameterizedTestAnchor", currentTest->m_details.suiteName, *this);
|
||||
}
|
||||
|
||||
if (_lastTest != currentTest)
|
||||
{
|
||||
_lastTest = currentTest;
|
||||
_nextTestBackup = currentTest->m_nextTest;
|
||||
onNewIteration(true);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Test* const ParameterizedTestAbstract::retrieveCurrentTest()
|
||||
{
|
||||
return retrieveTest(CurrentTest::Details());
|
||||
}
|
||||
|
||||
|
||||
Test* const ParameterizedTest::retrieveTest(TestDetails const * const details)
|
||||
Test* const ParameterizedTestAbstract::retrieveTest(TestDetails const * const details)
|
||||
{
|
||||
//TODO This workaround is too far complicated, why not simply add pointer to current test in class CurrentTest ?
|
||||
Details2Test::iterator it = _tests.find(details);
|
||||
|
|
@ -44,3 +78,48 @@ Test* const ParameterizedTest::retrieveTest(TestDetails const * const details)
|
|||
|
||||
throw runtime_error(string("Impossible to retrieve test ") + details->testName);
|
||||
}
|
||||
|
||||
|
||||
bool ParameterizedTestAbstract::hasMoreValues(int advance) const
|
||||
{
|
||||
return (_iteration + advance < (int)valuesSize());
|
||||
}
|
||||
|
||||
|
||||
void ParameterizedTestAbstract::onNewIteration(bool first)
|
||||
{
|
||||
if (first)
|
||||
{
|
||||
_iteration = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
_iteration++;
|
||||
}
|
||||
|
||||
if (hasMoreValues(1))
|
||||
{
|
||||
_lastTest->m_nextTest = _testAnchor;
|
||||
_testAnchor->m_nextTest = _lastTest;
|
||||
}
|
||||
else
|
||||
{
|
||||
_lastTest->m_nextTest = _nextTestBackup;
|
||||
_testAnchor->m_nextTest = nullptr;
|
||||
}
|
||||
|
||||
peekCurrentValue(_iteration);
|
||||
}
|
||||
|
||||
|
||||
ParameterizedTestAbstract::TestAnchor::TestAnchor(char const* testName, char const* suiteName, ParameterizedTestAbstract & pt)
|
||||
: Test(testName, suiteName),
|
||||
_pt(pt)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
void ParameterizedTestAbstract::TestAnchor::RunImpl() const
|
||||
{
|
||||
_pt.onNewIteration(false);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,8 @@
|
|||
|
||||
#include <string>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
|
||||
#include "Test.h"
|
||||
|
||||
namespace UnitTest
|
||||
|
|
@ -11,17 +13,72 @@ namespace UnitTest
|
|||
|
||||
typedef map<TestDetails const * const, Test*> Details2Test;
|
||||
|
||||
class ParameterizedTest
|
||||
class ParameterizedTestAbstract
|
||||
{
|
||||
public:
|
||||
ParameterizedTest();
|
||||
virtual ~ParameterizedTest();
|
||||
ParameterizedTestAbstract();
|
||||
virtual ~ParameterizedTestAbstract();
|
||||
size_t getIteration();
|
||||
|
||||
protected:
|
||||
virtual void peekCurrentValue(size_t iteration) = 0;
|
||||
virtual size_t valuesSize() const = 0;
|
||||
void ensureInitialized();
|
||||
|
||||
private:
|
||||
class TestAnchor : public Test
|
||||
{
|
||||
public:
|
||||
TestAnchor(char const* testName, char const* suiteName, ParameterizedTestAbstract & pt);
|
||||
virtual void RunImpl() const override;
|
||||
private:
|
||||
ParameterizedTestAbstract & _pt;
|
||||
};
|
||||
|
||||
Test* const retrieveCurrentTest();
|
||||
Test* const retrieveTest(TestDetails const * const details);
|
||||
|
||||
bool hasMoreValues(int advance = 0) const;
|
||||
void onNewIteration(bool first);
|
||||
|
||||
size_t _iteration;
|
||||
Test* _lastTest;
|
||||
Test* _nextTestBackup;
|
||||
TestAnchor* _testAnchor;
|
||||
Details2Test _tests;
|
||||
};
|
||||
|
||||
|
||||
template<class T_Value>
|
||||
class ParameterizedTest : public ParameterizedTestAbstract
|
||||
{
|
||||
public:
|
||||
ParameterizedTest(vector<T_Value> values)
|
||||
: _values(values)
|
||||
{
|
||||
}
|
||||
|
||||
T_Value operator()()
|
||||
{
|
||||
ensureInitialized();
|
||||
return _currentValue;
|
||||
}
|
||||
|
||||
protected:
|
||||
virtual void peekCurrentValue(size_t iteration) override
|
||||
{
|
||||
_currentValue = _values[iteration];
|
||||
}
|
||||
|
||||
virtual size_t valuesSize() const override
|
||||
{
|
||||
return _values.size();
|
||||
}
|
||||
|
||||
private:
|
||||
vector<T_Value> _values;
|
||||
T_Value _currentValue;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
Loading…
Add table
Add a link
Reference in a new issue