1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2012 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> |
4 | ** Copyright (C) 2016 The Qt Company Ltd. |
5 | ** Contact: https://www.qt.io/licensing/ |
6 | ** |
7 | ** This file is part of the test suite of the Qt Toolkit. |
8 | ** |
9 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
10 | ** Commercial License Usage |
11 | ** Licensees holding valid commercial Qt licenses may use this file in |
12 | ** accordance with the commercial license agreement provided with the |
13 | ** Software or, alternatively, in accordance with the terms contained in |
14 | ** a written agreement between you and The Qt Company. For licensing terms |
15 | ** and conditions see https://www.qt.io/terms-conditions. For further |
16 | ** information use the contact form at https://www.qt.io/contact-us. |
17 | ** |
18 | ** GNU General Public License Usage |
19 | ** Alternatively, this file may be used under the terms of the GNU |
20 | ** General Public License version 3 as published by the Free Software |
21 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
22 | ** included in the packaging of this file. Please review the following |
23 | ** information to ensure the GNU General Public License requirements will |
24 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
25 | ** |
26 | ** $QT_END_LICENSE$ |
27 | ** |
28 | ****************************************************************************/ |
29 | |
30 | #include <QtGui/QRegularExpressionValidator> |
31 | #include <QtTest/QtTest> |
32 | |
33 | class tst_QRegularExpressionValidator : public QObject |
34 | { |
35 | Q_OBJECT |
36 | |
37 | private slots: |
38 | void validate_data(); |
39 | void validate(); |
40 | }; |
41 | |
42 | Q_DECLARE_METATYPE(QValidator::State) |
43 | |
44 | void tst_QRegularExpressionValidator::validate_data() |
45 | { |
46 | QTest::addColumn<QRegularExpression>(name: "re" ); |
47 | QTest::addColumn<QString>(name: "value" ); |
48 | QTest::addColumn<QValidator::State>(name: "state" ); |
49 | |
50 | QTest::newRow(dataTag: "data0" ) << QRegularExpression("[1-9]\\d{0,3}" ) << QString("0" ) << QValidator::Invalid; |
51 | QTest::newRow(dataTag: "data1" ) << QRegularExpression("[1-9]\\d{0,3}" ) << QString("12345" ) << QValidator::Invalid; |
52 | QTest::newRow(dataTag: "data2" ) << QRegularExpression("[1-9]\\d{0,3}" ) << QString("1" ) << QValidator::Acceptable; |
53 | |
54 | QTest::newRow(dataTag: "data3" ) << QRegularExpression("\\S+" ) << QString("myfile.txt" ) << QValidator::Acceptable; |
55 | QTest::newRow(dataTag: "data4" ) << QRegularExpression("\\S+" ) << QString("my file.txt" ) << QValidator::Invalid; |
56 | |
57 | QTest::newRow(dataTag: "data5" ) << QRegularExpression("[A-C]\\d{5}[W-Z]" ) << QString("a12345Z" ) << QValidator::Invalid; |
58 | QTest::newRow(dataTag: "data6" ) << QRegularExpression("[A-C]\\d{5}[W-Z]" ) << QString("A12345Z" ) << QValidator::Acceptable; |
59 | QTest::newRow(dataTag: "data7" ) << QRegularExpression("[A-C]\\d{5}[W-Z]" ) << QString("B12" ) << QValidator::Intermediate; |
60 | |
61 | QTest::newRow(dataTag: "data8" ) << QRegularExpression("read\\S?me(\\.(txt|asc|1st))?" ) << QString("readme" ) << QValidator::Acceptable; |
62 | QTest::newRow(dataTag: "data9" ) << QRegularExpression("read\\S?me(\\.(txt|asc|1st))?" ) << QString("read me.txt" ) << QValidator::Invalid; |
63 | QTest::newRow(dataTag: "data10" ) << QRegularExpression("read\\S?me(\\.(txt|asc|1st))?" ) << QString("readm" ) << QValidator::Intermediate; |
64 | |
65 | QTest::newRow(dataTag: "data11" ) << QRegularExpression("read\\S?me(\\.(txt|asc|1st))?" ) << QString("read me.txt" ) << QValidator::Invalid; |
66 | QTest::newRow(dataTag: "data12" ) << QRegularExpression("read\\S?me(\\.(txt|asc|1st))?" ) << QString("readm" ) << QValidator::Intermediate; |
67 | |
68 | QTest::newRow(dataTag: "data13" ) << QRegularExpression("\\w\\d\\d" ) << QString("A57" ) << QValidator::Acceptable; |
69 | QTest::newRow(dataTag: "data14" ) << QRegularExpression("\\w\\d\\d" ) << QString("E5" ) << QValidator::Intermediate; |
70 | QTest::newRow(dataTag: "data15" ) << QRegularExpression("\\w\\d\\d" ) << QString("+9" ) << QValidator::Invalid; |
71 | |
72 | QTest::newRow(dataTag: "emptystr1" ) << QRegularExpression("[T][e][s][t]" ) << QString("" ) << QValidator::Intermediate; |
73 | QTest::newRow(dataTag: "emptystr2" ) << QRegularExpression("[T][e][s][t]" ) << QString() << QValidator::Intermediate; |
74 | |
75 | QTest::newRow(dataTag: "empty01" ) << QRegularExpression() << QString() << QValidator::Acceptable; |
76 | QTest::newRow(dataTag: "empty02" ) << QRegularExpression() << QString("test" ) << QValidator::Acceptable; |
77 | } |
78 | |
79 | void tst_QRegularExpressionValidator::validate() |
80 | { |
81 | QFETCH(QRegularExpression, re); |
82 | QFETCH(QString, value); |
83 | |
84 | QRegularExpressionValidator rv; |
85 | |
86 | // setting the same regexp won't emit signals |
87 | const int signalCount = (rv.regularExpression() == re) ? 0 : 1; |
88 | |
89 | QSignalSpy spy(&rv, SIGNAL(regularExpressionChanged(QRegularExpression))); |
90 | QSignalSpy changedSpy(&rv, SIGNAL(changed())); |
91 | |
92 | rv.setRegularExpression(re); |
93 | QCOMPARE(rv.regularExpression(), re); |
94 | |
95 | int pos = -1; |
96 | QValidator::State result = rv.validate(input&: value, pos); |
97 | |
98 | QTEST(result, "state" ); |
99 | if (result == QValidator::Invalid) |
100 | QCOMPARE(pos, value.length()); |
101 | else |
102 | QCOMPARE(pos, -1); // ensure pos is not modified if validate returned Acceptable or Intermediate |
103 | |
104 | QCOMPARE(spy.count(), signalCount); |
105 | QCOMPARE(changedSpy.count(), signalCount); |
106 | } |
107 | |
108 | QTEST_GUILESS_MAIN(tst_QRegularExpressionValidator) |
109 | |
110 | #include "tst_qregularexpressionvalidator.moc" |
111 | |