diff options
Diffstat (limited to 'tests/auto/corelib/io/qurlquery')
-rw-r--r-- | tests/auto/corelib/io/qurlquery/CMakeLists.txt | 12 | ||||
-rw-r--r-- | tests/auto/corelib/io/qurlquery/tst_qurlquery.cpp | 195 |
2 files changed, 132 insertions, 75 deletions
diff --git a/tests/auto/corelib/io/qurlquery/CMakeLists.txt b/tests/auto/corelib/io/qurlquery/CMakeLists.txt index b0484314d9..c9f5491416 100644 --- a/tests/auto/corelib/io/qurlquery/CMakeLists.txt +++ b/tests/auto/corelib/io/qurlquery/CMakeLists.txt @@ -1,12 +1,20 @@ -# Generated from qurlquery.pro. +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause ##################################################################### ## tst_qurlquery Test: ##################################################################### +if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT) + cmake_minimum_required(VERSION 3.16) + project(tst_qurlquery LANGUAGES CXX) + find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST) +endif() + qt_internal_add_test(tst_qurlquery SOURCES tst_qurlquery.cpp - PUBLIC_LIBRARIES + LIBRARIES Qt::CorePrivate + Qt::TestPrivate ) diff --git a/tests/auto/corelib/io/qurlquery/tst_qurlquery.cpp b/tests/auto/corelib/io/qurlquery/tst_qurlquery.cpp index de911b7aa4..8360bdbe28 100644 --- a/tests/auto/corelib/io/qurlquery/tst_qurlquery.cpp +++ b/tests/auto/corelib/io/qurlquery/tst_qurlquery.cpp @@ -1,38 +1,18 @@ -/**************************************************************************** -** -** Copyright (C) 2016 Intel Corporation. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the test suite of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:GPL-EXCEPT$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 as published by the Free Software -** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2022 The Qt Company Ltd. +// Copyright (C) 2012 Intel Corporation. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only -#include <QtCore/QUrlQuery> #include <QTest> +#include <QtTest/private/qcomparisontesthelper_p.h> + +#include <QtCore/QUrlQuery> typedef QList<QPair<QString, QString> > QueryItems; Q_DECLARE_METATYPE(QueryItems) Q_DECLARE_METATYPE(QUrl::ComponentFormattingOptions) +using namespace Qt::StringLiterals; + class tst_QUrlQuery : public QObject { Q_OBJECT @@ -44,6 +24,9 @@ public: } private Q_SLOTS: + void compareCompiles(); + void compareEquality_data(); + void compareEquality(); void constructing(); void addRemove(); void multiAddRemove(); @@ -67,46 +50,35 @@ private Q_SLOTS: void old_hasQueryItem(); }; -static QString prettyElement(const QString &key, const QString &value) +static QString prettyPair(const QPair<QString, QString> &pair) { - QString result; - if (key.isNull()) - result += "null -> "; - else - result += '"' + key + "\" -> "; - if (value.isNull()) - result += "null"; - else - result += '"' + value + '"'; - return result; -} - -static QString prettyPair(QList<QPair<QString, QString> >::const_iterator it) -{ - return prettyElement(it->first, it->second); + const auto represent = [](const QString &s) { + return s.isNull() ? u"null"_s : u'"' + s + u'"'; + }; + return represent(pair.first) + " -> "_L1 + represent(pair.second); } -template <typename T> -static QByteArray prettyList(const T &items) +static QByteArray prettyList(const QueryItems &items) { - QString result = "("; - bool first = true; - typename T::const_iterator it = items.constBegin(); - for ( ; it != items.constEnd(); ++it) { - if (!first) - result += ", "; - first = false; - result += prettyPair(it); - } - result += QLatin1Char(')'); + if (items.isEmpty()) + return "()"; + auto it = items.constBegin(); + QString result = u'(' + prettyPair(*it); + for (++it; it != items.constEnd(); ++it) + result += ", "_L1 + prettyPair(*it); + result += u')'; return result.toLocal8Bit(); } -static bool compare(const QList<QPair<QString, QString> > &actual, const QueryItems &expected, +static bool compare(const QueryItems &actual, const QueryItems &expected, const char *actualStr, const char *expectedStr, const char *file, int line) { + auto formatter = [](const void *val) -> const char * { + const QueryItems items = *static_cast<const QueryItems *>(val); + return qstrdup(prettyList(items).constData()); + }; return QTest::compare_helper(actual == expected, "Compared values are not the same", - qstrdup(prettyList(actual)), qstrdup(prettyList(expected).data()), + &actual, &expected, formatter, formatter, actualStr, expectedStr, file, line); } @@ -155,6 +127,48 @@ static QUrlQuery emptyQuery() return QUrlQuery(); } +void tst_QUrlQuery::compareCompiles() +{ + QTestPrivate::testEqualityOperatorsCompile<QUrlQuery>(); +} + +void tst_QUrlQuery::compareEquality_data() +{ + QTest::addColumn<QUrlQuery>("url1"); + QTest::addColumn<QUrlQuery>("url2"); + QTest::addColumn<bool>("equal"); + + QTest::newRow("empty-empty") << QUrlQuery() << QUrlQuery() << true; + + QUrlQuery notEmpty; + notEmpty.addQueryItem("a", "b"); + QTest::newRow("empty-notEmpty") << QUrlQuery() << notEmpty << false; + + QUrlQuery notEmpty_copy = notEmpty; + QTest::newRow("sameItems") << notEmpty_copy << notEmpty << true; + + QUrlQuery notEmpty_modified = notEmpty; + notEmpty_modified.addQueryItem("c", "d"); + QTest::newRow("addedItems") << notEmpty_copy << notEmpty_modified << false; + + QUrlQuery notEmpty2; + notEmpty2.addQueryItem("c", "d"); + QTest::newRow("differentItems") << notEmpty2 << notEmpty << false; + + QUrlQuery differentPairDelimiters; + differentPairDelimiters.setQueryDelimiters('(', ')'); + QTest::newRow("defaultDelimiters-differentDelimiters") << QUrlQuery() << differentPairDelimiters + << false; +} + +void tst_QUrlQuery::compareEquality() +{ + QFETCH(QUrlQuery, url1); + QFETCH(QUrlQuery, url2); + QFETCH(bool, equal); + QT_TEST_EQUALITY_OPS(url1, url2, equal); +} + void tst_QUrlQuery::constructing() { QUrlQuery empty; @@ -173,7 +187,7 @@ void tst_QUrlQuery::constructing() QVERIFY(!copy.isDetached()); QCOMPARE(copy, empty); QCOMPARE(qHash(copy), qHash(empty)); - QVERIFY(!(copy != empty)); + QT_TEST_EQUALITY_OPS(copy, empty, true); copy = empty; QCOMPARE(copy, empty); @@ -205,20 +219,50 @@ void tst_QUrlQuery::constructing() other.addQueryItem("a", "b"); QVERIFY(!other.isEmpty()); QVERIFY(other.isDetached()); - QVERIFY(other != empty); - QVERIFY(!(other == empty)); + QCOMPARE_NE(other, empty); + QT_TEST_EQUALITY_OPS(other, empty, false); + // copy-construct QUrlQuery copy(other); QCOMPARE(copy, other); copy.clear(); QVERIFY(copy.isEmpty()); - QVERIFY(copy != other); + QCOMPARE_NE(copy, other); + // copy-assign copy = other; QVERIFY(!copy.isEmpty()); QCOMPARE(copy, other); + // move-construct + QUrlQuery moved(std::move(other)); + QCOMPARE(moved, copy); + + // self move-assign + { + auto &self = moved; // prevent -Wself-move + moved = std::move(self); + } + QCOMPARE(moved, copy); + + // self move-assign of moved-from (Hinnant Criterion) + { + auto &self = other; // prevent -Wself-move + other = std::move(self); + } + // shouldn't crash; here, or further down + + // copy-assign to moved-from object + other = copy; + QCOMPARE(other, copy); + QCOMPARE(other, moved); + + // move-assign + moved = std::move(other); + QCOMPARE(moved, copy); + + // (move-)assign default-constructed copy = QUrlQuery(); QVERIFY(copy.isEmpty()); @@ -246,22 +290,25 @@ void tst_QUrlQuery::constructing() void tst_QUrlQuery::addRemove() { QUrlQuery query; + QCOMPARE(query, query); { // one item query.addQueryItem("a", "b"); QVERIFY(!query.isEmpty()); QVERIFY(query.hasQueryItem("a")); + QCOMPARE_NE(query, QUrlQuery()); QCOMPARE(query.queryItemValue("a"), QString("b")); QCOMPARE(query.allQueryItemValues("a"), QStringList() << "b"); QList<QPair<QString, QString> > allItems = query.queryItems(); - QCOMPARE(allItems.count(), 1); + QCOMPARE(allItems.size(), 1); QCOMPARE(allItems.at(0).first, QString("a")); QCOMPARE(allItems.at(0).second, QString("b")); } QUrlQuery original = query; + QCOMPARE(query, original); { // two items @@ -274,12 +321,12 @@ void tst_QUrlQuery::addRemove() QCOMPARE(query.allQueryItemValues("c"), QStringList() << "d"); QList<QPair<QString, QString> > allItems = query.queryItems(); - QCOMPARE(allItems.count(), 2); + QCOMPARE(allItems.size(), 2); QVERIFY(allItems.contains(qItem("a", "b"))); QVERIFY(allItems.contains(qItem("c", "d"))); - QVERIFY(query != original); - QVERIFY(!(query == original)); + QCOMPARE_NE(query, original); + QT_TEST_EQUALITY_OPS(query, original, false); } { @@ -297,12 +344,12 @@ void tst_QUrlQuery::addRemove() QCOMPARE(query.allQueryItemValues("a"), QStringList() << "b"); QList<QPair<QString, QString> > allItems = query.queryItems(); - QCOMPARE(allItems.count(), 1); + QCOMPARE(allItems.size(), 1); QCOMPARE(allItems.at(0).first, QString("a")); QCOMPARE(allItems.at(0).second, QString("b")); QCOMPARE(query, original); - QVERIFY(!(query != original)); + QT_TEST_EQUALITY_OPS(query, original, true); QCOMPARE(qHash(query), qHash(original)); } @@ -321,12 +368,12 @@ void tst_QUrlQuery::addRemove() QCOMPARE(query.allQueryItemValues("e"), QStringList() << emptyButNotNull); QList<QPair<QString, QString> > allItems = query.queryItems(); - QCOMPARE(allItems.count(), 2); + QCOMPARE(allItems.size(), 2); QVERIFY(allItems.contains(qItem("a", "b"))); QVERIFY(allItems.contains(qItem("e", emptyButNotNull))); - QVERIFY(query != original); - QVERIFY(!(query == original)); + QCOMPARE_NE(query, original); + QT_TEST_EQUALITY_OPS(query, original, false); } { @@ -334,6 +381,9 @@ void tst_QUrlQuery::addRemove() query.removeQueryItem("a"); query.removeQueryItem("e"); QVERIFY(query.isEmpty()); + QVERIFY(query.isDetached()); + QCOMPARE_NE(query, original); + QCOMPARE(query, QUrlQuery()); } } @@ -536,12 +586,11 @@ void tst_QUrlQuery::reconstructQuery_data() baselist << qItem("a", "b") << qItem("c", "d"); QTest::newRow("2-ab-cd") << "a=b&c=d" << baselist; - // the same entry multiply defined + // The same entry multiply defined QTest::newRow("2-a-a") << "a&a" << (QueryItems() << qItem("a", QString()) << qItem("a", QString())); QTest::newRow("2-ab-ab") << "a=b&a=b" << (QueryItems() << qItem("a", "b") << qItem("a", "b")); QTest::newRow("2-ab-ac") << "a=b&a=c" << (QueryItems() << qItem("a", "b") << qItem("a", "c")); QTest::newRow("2-ac-ab") << "a=c&a=b" << (QueryItems() << qItem("a", "c") << qItem("a", "b")); - QTest::newRow("2-ab-cd") << "a=b&c=d" << (QueryItems() << qItem("a", "b") << qItem("c", "d")); QTest::newRow("2-cd-ab") << "c=d&a=b" << (QueryItems() << qItem("c", "d") << qItem("a", "b")); QueryItems list2 = baselist + qItem("somekey", QString()); |