File indexing completed on 2024-04-28 05:50:05

0001 /*
0002  * SPDX-License-Identifier: GPL-3.0-or-later
0003  * SPDX-FileCopyrightText: 2019 Johan Ouwerkerk <jm.ouwerkerk@gmail.com>
0004  */
0005 
0006 #ifndef VALIDATOR_TEST_UTIL_H
0007 #define VALIDATOR_TEST_UTIL_H
0008 
0009 #include <QLocale>
0010 #include <QObject>
0011 #include <QTest>
0012 #include <QValidator>
0013 #include <QtDebug>
0014 
0015 Q_DECLARE_METATYPE(QValidator::State);
0016 Q_DECLARE_METATYPE(QLocale);
0017 
0018 namespace validators
0019 {
0020     namespace test
0021     {
0022         void define_test_data_columns(void)
0023         {
0024             QTest::addColumn<QString>("input");
0025             QTest::addColumn<QString>("fixed");
0026             QTest::addColumn<QLocale>("locale");
0027             QTest::addColumn<QValidator::State>("result");
0028         };
0029 
0030         void define_test_case(
0031             const QString &input,
0032             const QString &fixed,
0033             QValidator::State result=QValidator::Intermediate,
0034             const QLocale &locale=QLocale::c())
0035         {
0036             QString name = QStringLiteral("locale=%1, input=%2").arg(locale.name()).arg(input);
0037             QTest::newRow(qPrintable(name)) << input << fixed << locale << result;
0038         };
0039 
0040         template<class T, auto f_data>
0041         class ValidatorTestBase : public QObject
0042         {
0043         public:
0044             virtual ~ValidatorTestBase()
0045             {
0046             };
0047 
0048             void testValidate(T &uut)
0049             {
0050                 QFETCH(QString, input);
0051                 QFETCH(QLocale, locale);
0052 
0053                 uut.setLocale(locale);
0054                 int position = input.size();
0055                 int copy = position;
0056 
0057                 QTEST(uut.validate(input, position), "result");
0058                 QCOMPARE(position, copy);
0059             };
0060 
0061             void testFixup(T &uut)
0062             {
0063                 QFETCH(QString, input);
0064                 QFETCH(QLocale, locale);
0065 
0066                 uut.setLocale(locale);
0067 
0068                 uut.fixup(input);
0069                 QTEST(input, "fixed");
0070 
0071                 // test if output is stable
0072                 // ie.: fixup(fixup()) == fixup()
0073                 uut.fixup(input);
0074                 QTEST(input, "fixed");
0075             };
0076 
0077             void testValidate_data(void)
0078             {
0079                 define_test_data_columns();
0080                 f_data();
0081             };
0082 
0083             void testFixup_data(void)
0084             {
0085                 define_test_data_columns();
0086                 f_data();
0087             };
0088         };
0089     }
0090 }
0091 
0092 #define DEFINE_VALIDATOR_TEST(name, type, data_tables, ...) \
0093 class name : public validators::test::ValidatorTestBase<type, data_tables> \
0094 { \
0095     Q_OBJECT \
0096 private Q_SLOTS: \
0097     void testFixup(void) \
0098     { \
0099         type uut{__VA_ARGS__}; \
0100         ValidatorTestBase::testFixup(uut); \
0101     }; \
0102     \
0103     void testValidate(void) \
0104     { \
0105         type uut{__VA_ARGS__}; \
0106         ValidatorTestBase::testValidate(uut); \
0107     }; \
0108     \
0109     void testFixup_data(void) \
0110     { \
0111         ValidatorTestBase::testFixup_data(); \
0112     }; \
0113     \
0114     void testValidate_data(void) \
0115     { \
0116         ValidatorTestBase::testValidate_data(); \
0117     }; \
0118 }
0119 
0120 #endif