File: test-util.h

package info (click to toggle)
keysmith 25.08.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,276 kB
  • sloc: cpp: 11,649; xml: 352; sh: 121; makefile: 3
file content (114 lines) | stat: -rw-r--r-- 6,089 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
 * SPDX-License-Identifier: GPL-3.0-or-later
 * SPDX-FileCopyrightText: 2019 Johan Ouwerkerk <jm.ouwerkerk@gmail.com>
 */

#ifndef VALIDATOR_TEST_UTIL_H
#define VALIDATOR_TEST_UTIL_H

#include <QLocale>
#include <QObject>
#include <QTest>
#include <QValidator>
#include <QtDebug>

Q_DECLARE_METATYPE(QValidator::State);
Q_DECLARE_METATYPE(QLocale);

namespace validators
{
namespace test
{
void define_test_data_columns(void)
{
    QTest::addColumn<QString>("input");
    QTest::addColumn<QString>("fixed");
    QTest::addColumn<QLocale>("locale");
    QTest::addColumn<QValidator::State>("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 T, auto f_data>
class ValidatorTestBase : public QObject
{
public:
    virtual ~ValidatorTestBase() {};

    void testValidate(T &uut)
    {
        QFETCH(QString, input);
        QFETCH(QLocale, locale);

        uut.setLocale(locale);
        int position = input.size();
        int copy = position;

        QTEST(uut.validate(input, position), "result");
        QCOMPARE(position, copy);
    };

    void testFixup(T &uut)
    {
        QFETCH(QString, input);
        QFETCH(QLocale, locale);

        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<type, data_tables>                                                                                 \
    {                                                                                                                                                          \
        Q_OBJECT                                                                                                                                               \
    private Q_SLOTS:                                                                                                                                           \
        void testFixup(void)                                                                                                                                   \
        {                                                                                                                                                      \
            type uut{__VA_ARGS__};                                                                                                                             \
            ValidatorTestBase::testFixup(uut);                                                                                                                 \
        };                                                                                                                                                     \
                                                                                                                                                               \
        void testValidate(void)                                                                                                                                \
        {                                                                                                                                                      \
            type uut{__VA_ARGS__};                                                                                                                             \
            ValidatorTestBase::testValidate(uut);                                                                                                              \
        };                                                                                                                                                     \
                                                                                                                                                               \
        void testFixup_data(void)                                                                                                                              \
        {                                                                                                                                                      \
            ValidatorTestBase::testFixup_data();                                                                                                               \
        };                                                                                                                                                     \
                                                                                                                                                               \
        void testValidate_data(void)                                                                                                                           \
        {                                                                                                                                                      \
            ValidatorTestBase::testValidate_data();                                                                                                            \
        };                                                                                                                                                     \
    }

#endif