/* * SPDX-License-Identifier: GPL-3.0-or-later * SPDX-FileCopyrightText: 2019 Johan Ouwerkerk */ #ifndef VALIDATOR_TEST_UTIL_H #define VALIDATOR_TEST_UTIL_H #include #include #include #include #include Q_DECLARE_METATYPE(QValidator::State); Q_DECLARE_METATYPE(QLocale); namespace validators { namespace test { void define_test_data_columns(void) { QTest::addColumn("input"); QTest::addColumn("fixed"); QTest::addColumn("locale"); QTest::addColumn("result"); }; void define_test_case( const QString &input, const QString &fixed, QValidator::State result=QValidator::Intermediate, const QLocale &locale=QLocale::c()) { QString name = QStringLiteral("locale=%1, input=%2").arg(locale.name()).arg(input); QTest::newRow(qPrintable(name)) << input << fixed << locale << result; }; template class ValidatorTestBase : public QObject { public: virtual ~ValidatorTestBase() { }; void testValidate(void) { QFETCH(QString, input); QFETCH(QLocale, locale); T uut; uut.setLocale(locale); int position = input.size(); int copy = position; QTEST(uut.validate(input, position), "result"); QCOMPARE(position, copy); }; void testFixup(void) { QFETCH(QString, input); QFETCH(QLocale, locale); T uut; uut.setLocale(locale); uut.fixup(input); QTEST(input, "fixed"); // test if output is stable // ie.: fixup(fixup()) == fixup() uut.fixup(input); QTEST(input, "fixed"); }; void testValidate_data(void) { define_test_data_columns(); f_data(); }; void testFixup_data(void) { define_test_data_columns(); f_data(); }; }; } } #define DEFINE_VALIDATOR_TEST(name, type, data_tables) \ class name : public validators::test::ValidatorTestBase \ { \ Q_OBJECT \ private Q_SLOTS: \ void testFixup(void) \ { \ ValidatorTestBase::testFixup(); \ }; \ \ void testValidate(void) \ { \ ValidatorTestBase::testValidate(); \ }; \ \ void testFixup_data(void) \ { \ ValidatorTestBase::testFixup_data(); \ }; \ \ void testValidate_data(void) \ { \ ValidatorTestBase::testValidate_data(); \ }; \ } #endif