Browse Source

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

LIB-11
heck 11 months ago
parent
commit
5c56fc3a9b
2 changed files with 36 additions and 20 deletions
  1. +11
    -7
      test/pitytest11/src/PityUnit.hh
  2. +25
    -13
      test/pitytest11/src/PityUnit.hxx

+ 11
- 7
test/pitytest11/src/PityUnit.hh View File

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


+ 25
- 13
test/pitytest11/src/PityUnit.hxx View File

@ -27,10 +27,10 @@ namespace pEp {
PityUnit<TestContext>::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<TestContext>::PityUnit(const PityUnit<TestContext> &rhs) :
AbstractPityUnit(rhs, *this)
{
_perspective = rhs._perspective;
_test_func = rhs._test_func;
_copyContext(rhs);
_test_func = TestFunction(rhs._test_func);
}
template<class TestContext>
PityUnit<TestContext> &PityUnit<TestContext>::operator=(const PityUnit<TestContext> &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<class TestContext>
TestContext *PityUnit<TestContext>::getPerspective() const
TestContext *PityUnit<TestContext>::getContext() const
{
pEpLogClass("called");
TestContext *ret = nullptr;
if (_perspective != nullptr) {
ret = _perspective;
if (_ctx != nullptr) {
ret = _ctx;
} else {
if (!isRoot()) {
ret = (dynamic_cast<PityUnit<TestContext> *>(getParent()))->getPerspective();
ret = (dynamic_cast<PityUnit<TestContext> *>(getParent()))->getContext();
}
}
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 pEp


Loading…
Cancel
Save