| 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 | #include <QtTest/QtTest> |
| 30 | #include <qstringmatcher.h> |
| 31 | |
| 32 | class tst_QStringMatcher : public QObject |
| 33 | { |
| 34 | Q_OBJECT |
| 35 | |
| 36 | private slots: |
| 37 | void qstringmatcher(); |
| 38 | void caseSensitivity(); |
| 39 | void indexIn_data(); |
| 40 | void indexIn(); |
| 41 | void setCaseSensitivity_data(); |
| 42 | void setCaseSensitivity(); |
| 43 | void assignOperator(); |
| 44 | }; |
| 45 | |
| 46 | void tst_QStringMatcher::qstringmatcher() |
| 47 | { |
| 48 | QStringMatcher matcher; |
| 49 | QCOMPARE(matcher.caseSensitivity(), Qt::CaseSensitive); |
| 50 | QCOMPARE(matcher.indexIn("foo" , 1), 1); |
| 51 | QCOMPARE(matcher.pattern(), QString()); |
| 52 | } |
| 53 | |
| 54 | // public Qt::CaseSensitivity caseSensitivity() const |
| 55 | void tst_QStringMatcher::caseSensitivity() |
| 56 | { |
| 57 | const QString haystack = QStringLiteral("foobarFoo" ); |
| 58 | const QStringRef needle = haystack.rightRef(n: 3); // "Foo" |
| 59 | QStringMatcher matcher(needle.data(), needle.size()); |
| 60 | |
| 61 | QCOMPARE(matcher.caseSensitivity(), Qt::CaseSensitive); |
| 62 | QCOMPARE(matcher.indexIn(haystack), 6); |
| 63 | |
| 64 | matcher.setCaseSensitivity(Qt::CaseInsensitive); |
| 65 | |
| 66 | QCOMPARE(matcher.caseSensitivity(), Qt::CaseInsensitive); |
| 67 | QCOMPARE(matcher.indexIn(haystack), 0); |
| 68 | |
| 69 | matcher.setCaseSensitivity(Qt::CaseSensitive); |
| 70 | QCOMPARE(matcher.caseSensitivity(), Qt::CaseSensitive); |
| 71 | QCOMPARE(matcher.indexIn(haystack), 6); |
| 72 | } |
| 73 | |
| 74 | void tst_QStringMatcher::indexIn_data() |
| 75 | { |
| 76 | QTest::addColumn<QString>(name: "needle" ); |
| 77 | QTest::addColumn<QString>(name: "haystack" ); |
| 78 | QTest::addColumn<int>(name: "from" ); |
| 79 | QTest::addColumn<int>(name: "indexIn" ); |
| 80 | QTest::newRow(dataTag: "empty-1" ) << QString() << QString("foo" ) << 0 << 0; |
| 81 | QTest::newRow(dataTag: "empty-2" ) << QString() << QString("foo" ) << 10 << -1; |
| 82 | QTest::newRow(dataTag: "empty-3" ) << QString() << QString("foo" ) << -10 << 0; |
| 83 | |
| 84 | QTest::newRow(dataTag: "simple-1" ) << QString("a" ) << QString("foo" ) << 0 << -1; |
| 85 | QTest::newRow(dataTag: "simple-2" ) << QString("a" ) << QString("bar" ) << 0 << 1; |
| 86 | QTest::newRow(dataTag: "harder-1" ) << QString("foo" ) << QString("slkdf sldkjf slakjf lskd ffools ldjf" ) << 0 << 26; |
| 87 | QTest::newRow(dataTag: "harder-2" ) << QString("foo" ) << QString("slkdf sldkjf slakjf lskd ffools ldjf" ) << 20 << 26; |
| 88 | QTest::newRow(dataTag: "harder-3" ) << QString("foo" ) << QString("slkdf sldkjf slakjf lskd ffools ldjf" ) << 26 << 26; |
| 89 | QTest::newRow(dataTag: "harder-4" ) << QString("foo" ) << QString("slkdf sldkjf slakjf lskd ffools ldjf" ) << 27 << -1; |
| 90 | } |
| 91 | |
| 92 | void tst_QStringMatcher::indexIn() |
| 93 | { |
| 94 | QFETCH(QString, needle); |
| 95 | QFETCH(QString, haystack); |
| 96 | QFETCH(int, from); |
| 97 | QFETCH(int, indexIn); |
| 98 | |
| 99 | QStringMatcher matcher; |
| 100 | matcher.setPattern(needle); |
| 101 | |
| 102 | QCOMPARE(matcher.indexIn(haystack, from), indexIn); |
| 103 | |
| 104 | const auto needleSV = QStringView(needle); |
| 105 | QStringMatcher matcherSV(needleSV); |
| 106 | |
| 107 | QCOMPARE(matcherSV.indexIn(QStringView(haystack), from), indexIn); |
| 108 | } |
| 109 | |
| 110 | void tst_QStringMatcher::setCaseSensitivity_data() |
| 111 | { |
| 112 | QTest::addColumn<QString>(name: "needle" ); |
| 113 | QTest::addColumn<QString>(name: "haystack" ); |
| 114 | QTest::addColumn<int>(name: "from" ); |
| 115 | QTest::addColumn<int>(name: "indexIn" ); |
| 116 | QTest::addColumn<int>(name: "cs" ); |
| 117 | |
| 118 | QTest::newRow(dataTag: "overshot" ) << QString("foo" ) << QString("baFooz foo bar" ) << 14 << -1 << (int) Qt::CaseSensitive; |
| 119 | QTest::newRow(dataTag: "sensitive" ) << QString("foo" ) << QString("baFooz foo bar" ) << 1 << 7 << (int) Qt::CaseSensitive; |
| 120 | QTest::newRow(dataTag: "insensitive" ) << QString("foo" ) << QString("baFooz foo bar" ) << 1 << 2 << (int) Qt::CaseInsensitive; |
| 121 | } |
| 122 | |
| 123 | void tst_QStringMatcher::setCaseSensitivity() |
| 124 | { |
| 125 | QFETCH(QString, needle); |
| 126 | QFETCH(QString, haystack); |
| 127 | QFETCH(int, from); |
| 128 | QFETCH(int, indexIn); |
| 129 | QFETCH(int, cs); |
| 130 | |
| 131 | QStringMatcher matcher; |
| 132 | matcher.setPattern(needle); |
| 133 | matcher.setCaseSensitivity(static_cast<Qt::CaseSensitivity> (cs)); |
| 134 | |
| 135 | QCOMPARE(matcher.indexIn(haystack, from), indexIn); |
| 136 | QCOMPARE(matcher.indexIn(QStringView(haystack), from), indexIn); |
| 137 | } |
| 138 | |
| 139 | void tst_QStringMatcher::assignOperator() |
| 140 | { |
| 141 | QString needle("d" ); |
| 142 | QString hayStack("abcdef" ); |
| 143 | QStringMatcher m1(needle); |
| 144 | QCOMPARE(m1.indexIn(hayStack), 3); |
| 145 | |
| 146 | QStringMatcher m2 = m1; |
| 147 | QCOMPARE(m2.pattern(), needle); |
| 148 | QCOMPARE(m2.indexIn(hayStack), 3); |
| 149 | } |
| 150 | |
| 151 | QTEST_MAIN(tst_QStringMatcher) |
| 152 | #include "tst_qstringmatcher.moc" |
| 153 | |
| 154 | |