File indexing completed on 2024-05-12 16:45:28

0001 /***************************************************************************
0002  * SPDX-FileCopyrightText: 2022 S. MANKOWSKI stephane@mankowski.fr
0003  * SPDX-FileCopyrightText: 2022 G. DE BURE support@mankowski.fr
0004  * SPDX-License-Identifier: GPL-3.0-or-later
0005  ***************************************************************************/
0006 #ifndef SKGTESTMACRO_H
0007 #define SKGTESTMACRO_H
0008 /** @file
0009  * This file contains macro for unit tests.
0010  *
0011  * @author Stephane MANKOWSKI / Guillaume DE BURE
0012  */
0013 #include <qcoreapplication.h>
0014 #include <qdir.h>
0015 #include <qfile.h>
0016 
0017 #include <kaboutdata.h>
0018 
0019 #include "skgdocument.h"
0020 #include "skgerror.h"
0021 #include "skgnodeobject.h"
0022 #include "skgpropertyobject.h"
0023 #include "skgservices.h"
0024 #include "skgtraces.h"
0025 #include "skgtransactionmng.h"
0026 
0027 class SKGTest
0028 {
0029 public:
0030     /**
0031     * Return the test directory
0032     * @param iPath can be IN or OUT or REF
0033     * @return the test directory
0034     */
0035     static QString getTestPath(const QString& iPath)
0036     {
0037         QString pPath = SKGServices::getEnvVariable(iPath);
0038         if (pPath.isEmpty()) {
0039             return QStringLiteral("./tests/") % (iPath == QStringLiteral("IN") ? "input" : (iPath == QStringLiteral("OUT") ? "output" : "ref"));
0040         }
0041         return pPath;
0042     }
0043 };
0044 
0045 /**
0046  * @def SKGINITTEST(SHOWERRONLY)
0047  * Initialise the test
0048  */
0049 #define SKGINITTEST(SHOWERRONLY) \
0050     QCoreApplication app(argc, argv); \
0051     app.setApplicationName(QStringLiteral("qttest")); \
0052     KAboutData::setApplicationData(KAboutData(QStringLiteral("qttest"), QStringLiteral("qttest"), QStringLiteral("1.0"))); \
0053     KLocalizedString::setApplicationDomain("skrooge"); \
0054     SKGTRACESEPARATOR; \
0055     SKGTRACE << "STARTING TEST" << SKGENDL << SKGFLUSH; \
0056     SKGTRACESEPARATOR; \
0057     bool showonlyfailures = SHOWERRONLY; \
0058     if (showonlyfailures) {SKGTRACE << "Only failures will be displayed" << SKGENDL;}\
0059     int nberror = 0;  \
0060     int nbcheck = 0;
0061 
0062 /**
0063  * @def SKGENDTEST()
0064  * Exit test
0065  */
0066 #define SKGENDTEST()\
0067     if (nbcheck > 0) \
0068     { \
0069         SKGTRACE << nberror << " errors / " << nbcheck << " checks =" << (100.0*(static_cast<double>(nberror)) / (static_cast<double>(nbcheck))) << "%" << SKGENDL;\
0070     } \
0071     SKGTraces::dumpProfilingStatistics();\
0072     return nberror;
0073 
0074 /**
0075  * @def SKGRETURNTEST()
0076  * Return test
0077  */
0078 #define SKGRETURNTEST()\
0079     if (nbcheck > 0) \
0080     { \
0081         SKGTRACE << nberror << " errors / " << nbcheck << " checks =" << (100.0*(static_cast<double>(nberror)) / (static_cast<double>(nbcheck))) << "%" << SKGENDL;\
0082     } \
0083     return nberror;
0084 
0085 /**
0086  * @def SKGTEST(MESSAGE, RESULT, EXPECTEDRESULT)
0087  * To check the return of a method
0088  * Example of usage:
0089  * @code
0090  * SKGTEST(QStringLiteral("ERR:getHistoricalSize"), err.getHistoricalSize(), 0)
0091  * @endcode
0092  */
0093 #define SKGTEST(MESSAGE, RESULT, EXPECTEDRESULT) \
0094     if ((RESULT) == (EXPECTEDRESULT))\
0095     {\
0096         if (!showonlyfailures) {SKGTRACE << "Test [" << (MESSAGE) << "] : OK" << SKGENDL;}\
0097     }\
0098     else\
0099     {\
0100         SKGTRACE << "!!! Test [" << (MESSAGE) << "] : KO in line " << __LINE__ << SKGENDL;\
0101         SKGTRACE << "     Expected: [" << (EXPECTEDRESULT) << ']' << SKGENDL;\
0102         SKGTRACE << "     Result  : [" << (RESULT) << ']' << SKGENDL;\
0103         ++nberror;\
0104     }\
0105     ++nbcheck;
0106 /**
0107  * @def SKGTESTBOOL(MESSAGE, RESULT, EXPECTEDRESULT)
0108 * To check the return of a method returning a boll
0109 * Example of usage:
0110 * @code
0111 * SKGTESTBOOL("isWarning", err.isWarning(), true)
0112 * @endcode
0113 */
0114 #define SKGTESTBOOL(MESSAGE, RESULT, EXPECTEDRESULT) \
0115     if ((RESULT) == (EXPECTEDRESULT))\
0116     {\
0117         if (!showonlyfailures) {SKGTRACE << "Test [" << (MESSAGE) << "] : OK" << SKGENDL;}\
0118     }\
0119     else\
0120     {\
0121         SKGTRACE << "!!! Test [" << (MESSAGE) << "] : KO in line " << __LINE__ << SKGENDL;\
0122         SKGTRACE << "     Expected: [" << ((EXPECTEDRESULT) ? "TRUE" : "FALSE") << ']' << SKGENDL;\
0123         SKGTRACE << "     Result  : [" << ((RESULT) ? "TRUE" : "FALSE") << ']' << SKGENDL;\
0124         ++nberror;\
0125     }\
0126     ++nbcheck;
0127 
0128 /**
0129  * @def SKGTESTERROR(MESSAGE, RESULT, EXPECTEDRESULT)
0130  * To check the return of a method
0131  * Example of usage:
0132  * @code
0133  * SKGTESTERROR(QStringLiteral("DOC:setParameter"), document1.setParameter(QStringLiteral("ATT1"), QStringLiteral("VAL1")), true)
0134  * @endcode
0135  */
0136 #define SKGTESTERROR(MESSAGE, RESULT, EXPECTEDRESULT) \
0137     { \
0138         SKGError _err_ = RESULT; \
0139         if (!_err_ == (EXPECTEDRESULT)) { \
0140             if (!showonlyfailures) { \
0141                 SKGTRACE << "Test [" << (MESSAGE) << "] : OK" << SKGENDL; \
0142                 if (!(EXPECTEDRESULT)) { \
0143                     SKGTRACE << "     Error Message  :\n" << _err_.getFullMessageWithHistorical()  << SKGENDL; \
0144                 }     \
0145             }\
0146         } else {\
0147             SKGTRACE << "!!! Test [" << (MESSAGE) << "] : KO in line " << __LINE__ << SKGENDL;\
0148             SKGTRACE << "     Expected: [" << ((EXPECTEDRESULT) ? "TRUE" : "FALSE") << ']' << SKGENDL;\
0149             SKGTRACE << "     Result  : [" << (!_err_ ? "TRUE" : "FALSE") << ']' << SKGENDL;\
0150             SKGTRACE << "     Error Message  :\n" << _err_.getFullMessageWithHistorical()  << SKGENDL;\
0151             ++nberror;\
0152         }\
0153         ++nbcheck;\
0154     }
0155 
0156 /**
0157  * @def SKGTESTACCOUNT(DOC, ACCOUNT, AMOUNT)
0158  * To check the account amount
0159  * Example of usage:
0160  * @code
0161  * SKGTESTACCOUNT(document1, QStringLiteral("act1"), 12345);
0162  * @endcode
0163  */
0164 #define SKGTESTACCOUNT(DOC, ACCOUNT, AMOUNT) \
0165     { \
0166         SKGAccountObject account(&(DOC)); \
0167         SKGTESTERROR((ACCOUNT) % ".setName(QStringLiteral(" % (ACCOUNT) % "))", account.setName(ACCOUNT), true) \
0168         SKGTESTERROR((ACCOUNT) % ".load(QStringLiteral(" % (ACCOUNT) % "))", account.load(), true) \
0169         if (qAbs(account.getCurrentAmount()-AMOUNT) > 10e-4) {\
0170             SKGTEST((ACCOUNT) % ".getCurrentAmount(" % (ACCOUNT) % ")", SKGServices::toCurrencyString(account.getCurrentAmount(), QStringLiteral("Euros"), 2), SKGServices::toCurrencyString(AMOUNT, QStringLiteral("Euros"), 2))\
0171         }\
0172     }
0173 #endif
0174 
0175 /**
0176  * @def SKGTESTTRIGGERACTION(PLUGIN)
0177  * To trigger an action
0178  */
0179 #define SKGTESTTRIGGERACTION(NAME) \
0180     { \
0181         QAction* act = plugin.action(NAME); \
0182         QVERIFY(act != nullptr); \
0183         act->trigger(); \
0184     }
0185 
0186 /**
0187  * @def SKGTESTPLUGIN(PLUGIN)
0188  * To do common checks on a plugin
0189  */
0190 #define SKGTESTPLUGIN(PLUGIN, DOC) \
0191     { \
0192         QVERIFY(!PLUGIN.setupActions(nullptr)); \
0193         QVERIFY(PLUGIN.setupActions(&DOC)); \
0194         QVERIFY(!PLUGIN.title().isEmpty()); \
0195         QVERIFY(!PLUGIN.icon().isEmpty()); \
0196         QVERIFY(!PLUGIN.toolTip().isEmpty()); \
0197         QVERIFY(!PLUGIN.statusTip().isEmpty()); \
0198         QVERIFY(PLUGIN.getOrder() > 0); \
0199         SKGTabPage* page = plugin.getWidget(); \
0200         plugin.getPreferenceWidget(); \
0201         plugin.getPreferenceSkeleton(); \
0202         plugin.savePreferences(); \
0203         if (page) page->setState(page->getState()); \
0204         int nb = PLUGIN.getNbDashboardWidgets(); \
0205         for (int i = 0; i < nb; ++i) { \
0206             QVERIFY(!PLUGIN.getDashboardWidgetTitle(i).isEmpty()); \
0207         } \
0208         auto tips = PLUGIN.tips(); \
0209         for (int i = 0; i < tips.count(); ++i) { \
0210             QVERIFY(!tips.at(i).isEmpty()); \
0211         } \
0212         auto subplugins = PLUGIN.subPlugins(); \
0213         for (int i = 0; i < subplugins.count(); ++i) { \
0214             QVERIFY(!subplugins.at(i).isEmpty()); \
0215         } \
0216         QStringList iIgnoredAdvice; \
0217         SKGAdviceList adv = PLUGIN.advice(iIgnoredAdvice); \
0218         for (int i = 0; i < adv.count(); ++i) { \
0219             QVERIFY(!adv.at(i).getUUID().isEmpty()); \
0220         } \
0221     }