// This file is under GNU General Public License 3.0 // see LICENSE.txt #ifndef PITYTEST_PITYUNIT_HXX #define PITYTEST_PITYUNIT_HXX #include "../../../src/std_utils.hh" #include "PityUnit.hh" #include #include #include #include #include #include #include #include #include namespace pEp { namespace PityTest11 { template PityUnit::PityUnit( const std::string &name, TestFunction test_func, TestContext *ctx, ExecutionMode exec_mode) : AbstractPityUnit(name, exec_mode), _ctx{ ctx }, _test_func{ test_func } { } template PityUnit::PityUnit( AbstractPityUnit &parent, const std::string &name, TestFunction test_func, TestContext *ctx, ExecutionMode exec_mode) : AbstractPityUnit(parent, name, exec_mode), _ctx{ ctx }, _test_func{ test_func } { } template PityUnit::PityUnit(const PityUnit &rhs) : AbstractPityUnit(rhs, *this) { _copyContext(rhs); _test_func = TestFunction(rhs._test_func); } template PityUnit &PityUnit::operator=(const PityUnit &rhs) { _copyContext(rhs); _test_func = TestFunction(rhs._test_func); return *this; } template PityUnit &PityUnit::getSelf() { return *this; } template PityUnit *PityUnit::clone() { return new PityUnit(*this); } template void PityUnit::_runSelf() { if (_test_func != nullptr) { try { _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())); logH3(_status_string("\033[1m\033[31mFAILED" + Utils::to_termcol(_color()))); } } else { _logRaw("No function to execute"); } } // Inherited (if null see parent recursively) template TestContext *PityUnit::getContext() const { pEpLogClass("called"); TestContext *ret = nullptr; if (_ctx != nullptr) { ret = _ctx; } else { if (!isRoot()) { 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; } } template void PityUnit::setContext(TestContext *ctx) { _ctx = ctx; } template void PityUnit::setContext(TestContext ctx) { _owned_ctx = std::shared_ptr(new TestContext(ctx)); _ctx = _owned_ctx.get(); } } // namespace PityTest11 } // namespace pEp #endif // PITYTEST_PITYUNIT_HXX