diff --git a/doctor.cpp b/doctor.cpp index 74db51d..f48bc4f 100644 --- a/doctor.cpp +++ b/doctor.cpp @@ -5,13 +5,75 @@ * of this license document, but changing it is not allowed. */ +#include + #include "doctor.h" namespace QuasarAppUtils { -Doctor::Doctor() -{ +Doctor::Doctor(const QList > &base) { + _pillsData = base; +} +void Doctor::diagnostic(bool fix) const { + + QList > failed; + QList > detected; + QList > fixedSuccessful; + + for (const auto &pill: _pillsData) { + if (pill->diagnostic()) { + if (fix) { + if (!pill->fix()) { + failed.push_back(pill); + } else { + fixedSuccessful.push_back(pill); + } + } else { + detected.push_back(pill); + } + } + } + + if (failed.size()) { + emit sigFixesFailed(failed); + } + + if (detected.count()) { + emit sigTroubleDetected(detected); + } + + if (fixedSuccessful.count()) { + emit sigFixesFinishedSuccessful(fixedSuccessful); + } +} + +void Doctor::fix(const QList > &pills) const { + + QList > failed; + QList > fixedSuccessful; + + for (const auto &pill: pills) { + if (pill->diagnostic()) { + if (!pill->fix()) { + failed.push_back(pill); + } else { + fixedSuccessful.push_back(pill); + } + } + } + + if (failed.size()) { + emit sigFixesFailed(failed); + } + + if (fixedSuccessful.count()) { + emit sigFixesFinishedSuccessful(fixedSuccessful); + } +} + +void Doctor::addPill(const QSharedPointer &pill) { + _pillsData.push_back(pill); } } diff --git a/doctor.h b/doctor.h index 9a805a6..a785e24 100644 --- a/doctor.h +++ b/doctor.h @@ -27,44 +27,65 @@ public: * @brief Doctor This is base contructor of doctor calss. * @param base This is list of known issues. */ - Doctor(const QList>& base); + Doctor(const QList> &base); /** - * @brief diagnostick This method run full diagnostick of this application. + * @brief diagnostic This method run full diagnostick of this application. * If the @a fix is true then doctor try fix the foundet issues. * If the @a fix value if false then the Doctor emit the sigTroubleDetected signal. * @param fix set this argument to tru if you want fix all foundet issues. - */ - void diagnostick(bool fix = false) const; + * @see Doctor::fix + * @see Doctor::addPill + * @see Doctor::sigFixesFailed + * @see Doctor::sigFixesFinishedSuccessful + */ + void diagnostic(bool fix = false) const; /** - * @brief fix - * @param pill + * @brief fix This method try run fixes by input pills. + * @note All fixes will be checked before execute fix. + * @param pills This is list of fixes that will be executed. + * @see Doctor::diagnostick + * @see Doctor::addPill + * @see Doctor::sigFixesFailed + * @see Doctor::sigFixesFinishedSuccessful */ - void fix(const QList>& pill) const; + void fix(const QList>& pills) const; + + /** + * @brief addPill This method add new pill object to doctor library + * @param pill This is pill object. + * @see Doctor::diagnostick + * @see Doctor::fix + * @see Doctor::sigFixesFailed + * @see Doctor::sigFixesFinishedSuccessful + */ + void addPill(const QSharedPointer& pill); signals: /** * @brief sigTroubleDetected This signal will emited when The doctor object found issues in this application. - * @param issues + * @param issues this is list of detected issues. * @see Doctor::diagnostick + * @see Doctor::fix + * @see Doctor::sigFixesFailed + * @see Doctor::sigFixesFinishedSuccessful */ - void sigTroubleDetected(QList> issues); + void sigTroubleDetected(QList> issues) const; /** * @brief sigFixesFailed This signal emited when the doctor can't fix foundet issues. * @param issues This is list of the unfixable issues. */ - void sigFixesFailed(QList> issues); + void sigFixesFailed(QList> issues) const; /** * @brief sigFixesFinishedSuccessful This signal emited when the doctor fix foundet issues successfull. * @param issues This is list of the fixed issues. */ - void sigFixesFinishedSuccessful(QList> issues); - + void sigFixesFinishedSuccessful(QList> issues) const; private: - QHash> _pillsData; + QList> _pillsData; }; diff --git a/doctortest.cpp b/doctortest.cpp new file mode 100644 index 0000000..e368b51 --- /dev/null +++ b/doctortest.cpp @@ -0,0 +1,30 @@ +/* + * Copyright (C) 2018-2022 QuasarApp. + * Distributed under the lgplv3 software license, see the accompanying + * Everyone is permitted to copy and distribute verbatim copies + * of this license document, but changing it is not allowed. +*/ + +#include "doctortest.h" +#include "ipill.h" + +namespace QuasarAppUtils { + +DoctorTest::DoctorTest() { +} + +bool DoctorTest::test(const QSharedPointer &pill, + bool appIsBroken) const { + + if (pill->diagnostic() != appIsBroken) { + return false; + } + + if (appIsBroken && !pill->fix()) { + return false; + } + + return pill->diagnostic(); +} + +} diff --git a/doctortest.h b/doctortest.h new file mode 100644 index 0000000..3e9f97c --- /dev/null +++ b/doctortest.h @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2018-2022 QuasarApp. + * Distributed under the lgplv3 software license, see the accompanying + * Everyone is permitted to copy and distribute verbatim copies + * of this license document, but changing it is not allowed. +*/ + +#ifndef DOCTORTEST_H +#define DOCTORTEST_H + +#include + +namespace QuasarAppUtils { + +class iPill; +class Doctor; + +/** + * @brief The DoctorTest class This class contains auto tests for pills objects. + * @see Doctor class for get more information. + */ +class DoctorTest +{ +public: + DoctorTest(); + + /** + * @brief test This method run simple test for the pill object. + * **Test algorithm** + * + * 1. Doctor run the iPill::diagnostic method. + * 2. If an @a appIsBroken is false then diagnostic method should return true + * 3. If an @a appIsBroken is true then diagnostic method should return true. + * 4. Doctor run the fix and check diagnostic method again. + * 5. The diagnostic method should return false value. + * + * @param pill This is checked pill for solve issue. + * @param appIsBroken bollean variable that should be true if app is broken else false. + * @return true if test passed elase false. + */ + bool test(const QSharedPointer& pill, bool appIsBroken) const; + +}; + +} +#endif // DOCTORTEST_H diff --git a/ipill.h b/ipill.h index 9708fa0..9fa90f0 100644 --- a/ipill.h +++ b/ipill.h @@ -53,11 +53,11 @@ public: protected: /** - * @brief diagnostick This method execute action that should be check if exits issues or not. + * @brief diagnostic This method execute action that should be check if exits issues or not. * @note This method will executed only on the Doctor object. * @return true if the issues is detected. */ - virtual bool diagnostick() const = 0; + virtual bool diagnostic() const = 0; /** * @brief fix This method should be fix detected issue. @@ -67,6 +67,7 @@ protected: friend class Doctor; + friend class DoctorTest; }; }