Browse Source

Test: PityTest - PityUnit when copy, make a copy of the inherited model.

LIB-11
heck 4 years ago
parent
commit
5c56fc3a9b
  1. 18
      test/pitytest11/src/PityUnit.hh
  2. 38
      test/pitytest11/src/PityUnit.hxx

18
test/pitytest11/src/PityUnit.hh

@ -34,32 +34,36 @@ namespace pEp {
explicit PityUnit<TestContext>( explicit PityUnit<TestContext>(
const std::string& name, const std::string& name,
TestFunction test_func = nullptr, TestFunction test_func = nullptr,
TestContext* perspective = nullptr, TestContext* ctx = nullptr,
ExecutionMode exec_mode = ExecutionMode::FUNCTION); ExecutionMode exec_mode = ExecutionMode::FUNCTION);
explicit PityUnit<TestContext>( explicit PityUnit<TestContext>(
AbstractPityUnit& parent, AbstractPityUnit& parent,
const std::string& name, const std::string& name,
TestFunction test_func = nullptr, TestFunction test_func = nullptr,
TestContext* perspective = nullptr, TestContext* ctx = nullptr,
ExecutionMode exec_mode = ExecutionMode::FUNCTION); ExecutionMode exec_mode = ExecutionMode::FUNCTION);
PityUnit<TestContext>(const PityUnit<TestContext> &rhs); PityUnit<TestContext>(const PityUnit<TestContext>& rhs);
// copy-assign // copy-assign
PityUnit<TestContext>& operator=(const PityUnit<TestContext>& rhs); PityUnit<TestContext>& operator=(const PityUnit<TestContext>& rhs);
// clone // clone
PityUnit<TestContext> *clone() override; PityUnit<TestContext>* clone() override;
// Read-Only // Read-Only
TestContext* getPerspective() const; TestContext* getContext() const;
private: protected:
void _runSelf() override; void _runSelf() override;
private:
void _copyContext(const PityUnit<TestContext>& rhs);
// Fields // Fields
TestContext* _perspective; //nullptr if inherited TestContext* _ctx; // nullptr if inherited
std::shared_ptr<TestContext> _owned_ctx; // if you copy
TestFunction _test_func; TestFunction _test_func;
}; };
}; // namespace PityTest11 }; // namespace PityTest11

38
test/pitytest11/src/PityUnit.hxx

@ -27,10 +27,10 @@ namespace pEp {
PityUnit<TestContext>::PityUnit( PityUnit<TestContext>::PityUnit(
const std::string &name, const std::string &name,
TestFunction test_func, TestFunction test_func,
TestContext *perspective, TestContext *ctx,
ExecutionMode exec_mode) : ExecutionMode exec_mode) :
AbstractPityUnit(name, exec_mode), AbstractPityUnit(name, exec_mode),
_perspective{ perspective }, _test_func{ test_func } _ctx{ ctx }, _test_func{ test_func }
{ {
} }
@ -39,10 +39,10 @@ namespace pEp {
AbstractPityUnit &parent, AbstractPityUnit &parent,
const std::string &name, const std::string &name,
TestFunction test_func, TestFunction test_func,
TestContext *perspective, TestContext *ctx,
ExecutionMode exec_mode) : ExecutionMode exec_mode) :
AbstractPityUnit(parent, name, exec_mode), AbstractPityUnit(parent, name, exec_mode),
_perspective{ perspective }, _test_func{ test_func } _ctx{ ctx }, _test_func{ test_func }
{ {
} }
@ -50,15 +50,15 @@ namespace pEp {
PityUnit<TestContext>::PityUnit(const PityUnit<TestContext> &rhs) : PityUnit<TestContext>::PityUnit(const PityUnit<TestContext> &rhs) :
AbstractPityUnit(rhs, *this) AbstractPityUnit(rhs, *this)
{ {
_perspective = rhs._perspective; _copyContext(rhs);
_test_func = rhs._test_func; _test_func = TestFunction(rhs._test_func);
} }
template<class TestContext> template<class TestContext>
PityUnit<TestContext> &PityUnit<TestContext>::operator=(const PityUnit<TestContext> &rhs) PityUnit<TestContext> &PityUnit<TestContext>::operator=(const PityUnit<TestContext> &rhs)
{ {
_perspective = rhs._perspective; _copyContext(rhs);
_test_func = rhs._test_func; _test_func = TestFunction(rhs._test_func);
return *this; return *this;
} }
@ -73,7 +73,7 @@ namespace pEp {
{ {
if (_test_func != nullptr) { if (_test_func != nullptr) {
try { try {
_test_func(*this, getPerspective()); _test_func(*this, getContext());
logH3(_status_string("\033[1m\033[32mSUCCESS" + Utils::to_termcol(_color()))); logH3(_status_string("\033[1m\033[32mSUCCESS" + Utils::to_termcol(_color())));
} catch (const std::exception &e) { } catch (const std::exception &e) {
_logRaw("reason: " + std::string(e.what())); _logRaw("reason: " + std::string(e.what()));
@ -86,20 +86,32 @@ namespace pEp {
// Inherited (if null see parent recursively) // Inherited (if null see parent recursively)
template<class TestContext> template<class TestContext>
TestContext *PityUnit<TestContext>::getPerspective() const TestContext *PityUnit<TestContext>::getContext() const
{ {
pEpLogClass("called"); pEpLogClass("called");
TestContext *ret = nullptr; TestContext *ret = nullptr;
if (_perspective != nullptr) { if (_ctx != nullptr) {
ret = _perspective; ret = _ctx;
} else { } else {
if (!isRoot()) { if (!isRoot()) {
ret = (dynamic_cast<PityUnit<TestContext> *>(getParent()))->getPerspective(); ret = (dynamic_cast<PityUnit<TestContext> *>(getParent()))->getContext();
} }
} }
return ret; return ret;
} }
template<class TestContext>
void PityUnit<TestContext>::_copyContext(const PityUnit<TestContext> &rhs)
{
auto *tmp = rhs.getContext();
if (tmp != nullptr) {
_owned_ctx = std::shared_ptr<TestContext>(new TestContext(*tmp));
_ctx = tmp;
} else {
_ctx = nullptr;
}
}
} // namespace PityTest11 } // namespace PityTest11
} // namespace pEp } // namespace pEp

Loading…
Cancel
Save