File indexing completed on 2024-07-21 07:16:58

0001 // SPDX-FileCopyrightText: Lukas Sommer <sommerluk@gmail.com>
0002 // SPDX-License-Identifier: BSD-2-Clause OR MIT
0003 
0004 // First included header is the public header of the class we are testing;
0005 // this forces the header to be self-contained.
0006 #include "extendeddoublevalidator.h"
0007 
0008 #include <qglobal.h>
0009 #include <qobject.h>
0010 #include <qstring.h>
0011 #include <qstringliteral.h>
0012 #include <qtest.h>
0013 #include <qtestcase.h>
0014 #include <qvalidator.h>
0015 
0016 #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
0017 #include <qtmetamacros.h>
0018 #else
0019 #include <qobjectdefs.h>
0020 #endif
0021 
0022 namespace PerceptualColor
0023 {
0024 class TestExtendedDoubleValidator : public QObject
0025 {
0026     Q_OBJECT
0027 
0028 public:
0029     explicit TestExtendedDoubleValidator(QObject *parent = nullptr)
0030         : QObject(parent)
0031     {
0032     }
0033 
0034 private Q_SLOTS:
0035     void initTestCase()
0036     {
0037         // Called before the first test function is executed
0038     }
0039 
0040     void cleanupTestCase()
0041     {
0042         // Called after the last test function was executed
0043     }
0044 
0045     void init()
0046     {
0047         // Called before each test function is executed
0048     }
0049 
0050     void cleanup()
0051     {
0052         // Called after every test function
0053     }
0054 
0055     void testConstructor()
0056     {
0057         PerceptualColor::ExtendedDoubleValidator myValidator;
0058     }
0059 
0060     void testPrefix()
0061     {
0062         PerceptualColor::ExtendedDoubleValidator myValidator;
0063         QCOMPARE(myValidator.prefix(), QString());
0064         const QString myString = QStringLiteral("abc");
0065         myValidator.setPrefix(myString);
0066         QCOMPARE(myValidator.prefix(), myString);
0067         myValidator.setRange(0, 1000);
0068         QString testString;
0069         int testPosition = 0;
0070         testString = QStringLiteral("abc1");
0071         QCOMPARE(myValidator.validate(testString, testPosition), QValidator::State::Acceptable);
0072         testString = QStringLiteral("abc1000");
0073         QCOMPARE(myValidator.validate(testString, testPosition), QValidator::State::Acceptable);
0074         testString = QStringLiteral("ab1");
0075         QCOMPARE(myValidator.validate(testString, testPosition), QValidator::State::Invalid);
0076         testString = QStringLiteral("ab1000");
0077         QCOMPARE(myValidator.validate(testString, testPosition), QValidator::State::Invalid);
0078         testString = QStringLiteral("1");
0079         QCOMPARE(myValidator.validate(testString, testPosition), QValidator::State::Invalid);
0080         testString = QStringLiteral("1000");
0081         QCOMPARE(myValidator.validate(testString, testPosition), QValidator::State::Invalid);
0082         testString = QString();
0083         QCOMPARE(myValidator.validate(testString, testPosition), QValidator::State::Invalid);
0084     }
0085 
0086     void testSuffix()
0087     {
0088         PerceptualColor::ExtendedDoubleValidator myValidator;
0089         QCOMPARE(myValidator.suffix(), QString());
0090         const QString myString = QStringLiteral("abc");
0091         myValidator.setSuffix(myString);
0092         QCOMPARE(myValidator.suffix(), myString);
0093         myValidator.setRange(0, 1000);
0094         QString testString;
0095         int testPosition = 0;
0096         testString = QStringLiteral("1abc");
0097         QCOMPARE(myValidator.validate(testString, testPosition), QValidator::State::Acceptable);
0098         testString = QStringLiteral("1000abc");
0099         QCOMPARE(myValidator.validate(testString, testPosition), QValidator::State::Acceptable);
0100         testString = QStringLiteral("1ab");
0101         QCOMPARE(myValidator.validate(testString, testPosition), QValidator::State::Invalid);
0102         testString = QStringLiteral("1000ab");
0103         QCOMPARE(myValidator.validate(testString, testPosition), QValidator::State::Invalid);
0104         testString = QStringLiteral("1");
0105         QCOMPARE(myValidator.validate(testString, testPosition), QValidator::State::Invalid);
0106         testString = QStringLiteral("1000");
0107         QCOMPARE(myValidator.validate(testString, testPosition), QValidator::State::Invalid);
0108         testString = QString();
0109         QCOMPARE(myValidator.validate(testString, testPosition), QValidator::State::Invalid);
0110     }
0111 
0112     void testValidate()
0113     {
0114         ExtendedDoubleValidator myValidator;
0115         myValidator.setPrefix(QStringLiteral("abc"));
0116         myValidator.setSuffix(QStringLiteral("def"));
0117         myValidator.setRange(0, 1000);
0118         const QString originalInput = QStringLiteral("abc123def");
0119         QString myInput = originalInput;
0120         const int originalPos = 5;
0121         int myPos = originalPos;
0122         QValidator::State result = myValidator.validate(myInput, myPos);
0123         // The input should be considered valid.
0124         QCOMPARE(result, QValidator::State::Acceptable);
0125         // On simple cases of valid input, the string should not change.
0126         QCOMPARE(myInput, originalInput);
0127         // On simple cases of valid input, the position should not change.
0128         QCOMPARE(myPos, originalPos);
0129     }
0130 };
0131 
0132 } // namespace PerceptualColor
0133 
0134 QTEST_MAIN(PerceptualColor::TestExtendedDoubleValidator)
0135 
0136 // The following “include” is necessary because we do not use a header file:
0137 #include "testextendeddoublevalidator.moc"