diff --git a/src/PityUnit.hh b/src/PityUnit.hh index 443ff13..90a83e1 100644 --- a/src/PityUnit.hh +++ b/src/PityUnit.hh @@ -34,32 +34,36 @@ namespace pEp { explicit PityUnit( const std::string& name, TestFunction test_func = nullptr, - TestContext* perspective = nullptr, + TestContext* ctx = nullptr, ExecutionMode exec_mode = ExecutionMode::FUNCTION); explicit PityUnit( AbstractPityUnit& parent, const std::string& name, TestFunction test_func = nullptr, - TestContext* perspective = nullptr, + TestContext* ctx = nullptr, ExecutionMode exec_mode = ExecutionMode::FUNCTION); - PityUnit(const PityUnit &rhs); + PityUnit(const PityUnit& rhs); // copy-assign PityUnit& operator=(const PityUnit& rhs); // clone - PityUnit *clone() override; + PityUnit* clone() override; // Read-Only - TestContext* getPerspective() const; + TestContext* getContext() const; - private: + protected: void _runSelf() override; + private: + void _copyContext(const PityUnit& rhs); + // Fields - TestContext* _perspective; //nullptr if inherited + TestContext* _ctx; // nullptr if inherited + std::shared_ptr _owned_ctx; // if you copy TestFunction _test_func; }; }; // namespace PityTest11 diff --git a/src/PityUnit.hxx b/src/PityUnit.hxx index 4439d1e..fda3c1c 100644 --- a/src/PityUnit.hxx +++ b/src/PityUnit.hxx @@ -27,10 +27,10 @@ namespace pEp { PityUnit::PityUnit( const std::string &name, TestFunction test_func, - TestContext *perspective, + TestContext *ctx, ExecutionMode 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, const std::string &name, TestFunction test_func, - TestContext *perspective, + TestContext *ctx, ExecutionMode 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::PityUnit(const PityUnit &rhs) : AbstractPityUnit(rhs, *this) { - _perspective = rhs._perspective; - _test_func = rhs._test_func; + _copyContext(rhs); + _test_func = TestFunction(rhs._test_func); } template PityUnit &PityUnit::operator=(const PityUnit &rhs) { - _perspective = rhs._perspective; - _test_func = rhs._test_func; + _copyContext(rhs); + _test_func = TestFunction(rhs._test_func); return *this; } @@ -73,7 +73,7 @@ namespace pEp { { if (_test_func != nullptr) { try { - _test_func(*this, getPerspective()); + _test_func(*this, getContext()); logH3(_status_string("\033[1m\033[32mSUCCESS" + Utils::to_termcol(_color()))); } catch (const std::exception &e) { _logRaw("reason: " + std::string(e.what())); @@ -86,20 +86,32 @@ namespace pEp { // Inherited (if null see parent recursively) template - TestContext *PityUnit::getPerspective() const + TestContext *PityUnit::getContext() const { pEpLogClass("called"); TestContext *ret = nullptr; - if (_perspective != nullptr) { - ret = _perspective; + if (_ctx != nullptr) { + ret = _ctx; } else { if (!isRoot()) { - ret = (dynamic_cast *>(getParent()))->getPerspective(); + ret = (dynamic_cast *>(getParent()))->getContext(); } } return ret; } + + template + void PityUnit::_copyContext(const PityUnit &rhs) + { + auto *tmp = rhs.getContext(); + if (tmp != nullptr) { + _owned_ctx = std::shared_ptr(new TestContext(*tmp)); + _ctx = tmp; + } else { + _ctx = nullptr; + } + } } // namespace PityTest11 } // namespace pEp