| 1 | /**************************************************************************** | 
| 2 | ** | 
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. | 
| 4 | ** Contact: https://www.qt.io/licensing/ | 
| 5 | ** | 
| 6 | ** This file is part of the test suite of the Qt Toolkit. | 
| 7 | ** | 
| 8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ | 
| 9 | ** Commercial License Usage | 
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in | 
| 11 | ** accordance with the commercial license agreement provided with the | 
| 12 | ** Software or, alternatively, in accordance with the terms contained in | 
| 13 | ** a written agreement between you and The Qt Company. For licensing terms | 
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further | 
| 15 | ** information use the contact form at https://www.qt.io/contact-us. | 
| 16 | ** | 
| 17 | ** GNU General Public License Usage | 
| 18 | ** Alternatively, this file may be used under the terms of the GNU | 
| 19 | ** General Public License version 3 as published by the Free Software | 
| 20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT | 
| 21 | ** included in the packaging of this file. Please review the following | 
| 22 | ** information to ensure the GNU General Public License requirements will | 
| 23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. | 
| 24 | ** | 
| 25 | ** $QT_END_LICENSE$ | 
| 26 | ** | 
| 27 | ****************************************************************************/ | 
| 28 |  | 
| 29 |  | 
| 30 | #include <QtTest/QtTest> | 
| 31 | #include <qregexp.h> | 
| 32 |  | 
| 33 |  | 
| 34 | #include <qvalidator.h> | 
| 35 |  | 
| 36 | class tst_QRegExpValidator : public QObject | 
| 37 | { | 
| 38 |     Q_OBJECT | 
| 39 |  | 
| 40 | private slots: | 
| 41 |     void validate_data(); | 
| 42 |     void validate(); | 
| 43 | }; | 
| 44 |  | 
| 45 | void tst_QRegExpValidator::validate_data() | 
| 46 | { | 
| 47 |  | 
| 48 |     QTest::addColumn<QString>(name: "rx" ); | 
| 49 |     QTest::addColumn<QString>(name: "value" ); | 
| 50 |     QTest::addColumn<int>(name: "state" ); | 
| 51 |  | 
| 52 |     QTest::newRow( dataTag: "data0"  ) << QString("[1-9]\\d{0,3}" ) << QString("0" ) << 0; | 
| 53 |     QTest::newRow( dataTag: "data1"  ) << QString("[1-9]\\d{0,3}" ) << QString("12345" ) << 0; | 
| 54 |     QTest::newRow( dataTag: "data2"  ) << QString("[1-9]\\d{0,3}" ) << QString("1" ) << 2; | 
| 55 |  | 
| 56 |     QTest::newRow( dataTag: "data3"  ) << QString("\\S+" ) << QString("myfile.txt" ) << 2; | 
| 57 |     QTest::newRow( dataTag: "data4"  ) << QString("\\S+" ) << QString("my file.txt" ) << 0; | 
| 58 |  | 
| 59 |     QTest::newRow( dataTag: "data5"  ) << QString("[A-C]\\d{5}[W-Z]" ) << QString("a12345Z" ) << 0; | 
| 60 |     QTest::newRow( dataTag: "data6"  ) << QString("[A-C]\\d{5}[W-Z]" ) << QString("A12345Z" ) << 2; | 
| 61 |     QTest::newRow( dataTag: "data7"  ) << QString("[A-C]\\d{5}[W-Z]" ) << QString("B12" ) << 1; | 
| 62 |  | 
| 63 |     QTest::newRow( dataTag: "data8"  ) << QString("read\\S?me(\\.(txt|asc|1st))?" ) << QString("readme" ) << 2; | 
| 64 |     QTest::newRow( dataTag: "data9"  ) << QString("read\\S?me(\\.(txt|asc|1st))?" ) << QString("read me.txt" ) << 0; | 
| 65 |     QTest::newRow( dataTag: "data10"  ) << QString("read\\S?me(\\.(txt|asc|1st))?" ) << QString("readm" ) << 1; | 
| 66 | } | 
| 67 |  | 
| 68 | void tst_QRegExpValidator::validate() | 
| 69 | { | 
| 70 |     QFETCH( QString, rx ); | 
| 71 |     QFETCH( QString, value ); | 
| 72 |     QFETCH( int, state ); | 
| 73 |  | 
| 74 |     QRegExpValidator rv( 0 ); | 
| 75 |     QSignalSpy spy(&rv, SIGNAL(regExpChanged(QRegExp))); | 
| 76 |     QSignalSpy changedSpy(&rv, SIGNAL(changed())); | 
| 77 |  | 
| 78 |     rv.setRegExp( QRegExp( rx ) ); | 
| 79 |     int pos = -1; | 
| 80 |  | 
| 81 |     QCOMPARE( (int)rv.validate( value, pos ), state ); | 
| 82 |  | 
| 83 |     if (state == QValidator::Invalid) | 
| 84 |         QCOMPARE(pos, value.length()); | 
| 85 |     else | 
| 86 |         QCOMPARE(pos, -1); // untouched on Acceptable or Intermediate | 
| 87 |  | 
| 88 |     QCOMPARE(spy.count(), 1); | 
| 89 |     QCOMPARE(changedSpy.count(), 1); | 
| 90 | } | 
| 91 |  | 
| 92 | QTEST_APPLESS_MAIN(tst_QRegExpValidator) | 
| 93 | #include "tst_qregexpvalidator.moc" | 
| 94 |  |