From e46eb57543e215d8d3f2f9e9acc9b61bb3d53d6f Mon Sep 17 00:00:00 2001 From: Marco Bubke Date: Wed, 19 Jul 2023 12:07:41 +0200 Subject: [PATCH] UnitTests: Add more matcher The IsEmpty and IsNull matcher have been extended to be more generic. QVaraint matcher have been added. Task-number: QDS-10290 Change-Id: I34fcb0571190dd37e7706587ee617e1301f9f841 Reviewed-by: Qt CI Patch Build Bot Reviewed-by: Tim Jenssen --- tests/unit/tests/matchers/CMakeLists.txt | 1 + tests/unit/tests/matchers/qvariant-matcher.h | 44 +++++++++ tests/unit/tests/matchers/unittest-matchers.h | 92 ++++++++++++++----- .../unittests/sqlite/sqlitestatement-test.cpp | 2 + .../tests/utils/google-using-declarations.h | 1 - 5 files changed, 116 insertions(+), 24 deletions(-) create mode 100644 tests/unit/tests/matchers/qvariant-matcher.h diff --git a/tests/unit/tests/matchers/CMakeLists.txt b/tests/unit/tests/matchers/CMakeLists.txt index f457c6dd0e9..bea752e670c 100644 --- a/tests/unit/tests/matchers/CMakeLists.txt +++ b/tests/unit/tests/matchers/CMakeLists.txt @@ -9,4 +9,5 @@ add_qtc_library(TestMatchers OBJECT import-matcher.h unittest-matchers.h version-matcher.h + qvariant-matcher.h ) diff --git a/tests/unit/tests/matchers/qvariant-matcher.h b/tests/unit/tests/matchers/qvariant-matcher.h new file mode 100644 index 00000000000..52693ff2dca --- /dev/null +++ b/tests/unit/tests/matchers/qvariant-matcher.h @@ -0,0 +1,44 @@ +// Copyright (C) 2023 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 + +#pragma once + +#include +#include + +#include +#include + +template +class IsQVariantTypeMatcher +{ +public: + using is_gtest_matcher = void; + + bool MatchAndExplain(const QVariant &value, std::ostream *) const + { + return QMetaType::fromType() == value.metaType(); + } + + void DescribeTo(std::ostream *os) const { *os << "is qvariant of type"; } + + void DescribeNegationTo(std::ostream *os) const { *os << "is not qvariant of type"; } +}; + +template +testing::Matcher IsQVariantType() +{ + return IsQVariantTypeMatcher(); +} + +template +auto IsQVariant(const Matcher &matcher) +{ + return AllOf(IsQVariantType(), Property(&QVariant::value, matcher)); +} + +template +auto QVariantIsValid(const Matcher &matcher) +{ + return Property(&QVariant::isValid, matcher); +} diff --git a/tests/unit/tests/matchers/unittest-matchers.h b/tests/unit/tests/matchers/unittest-matchers.h index f4ff225c747..315252dd454 100644 --- a/tests/unit/tests/matchers/unittest-matchers.h +++ b/tests/unit/tests/matchers/unittest-matchers.h @@ -8,6 +8,27 @@ #include +#include + +QT_BEGIN_NAMESPACE + +inline bool empty(const QString &string) +{ + return string.isEmpty(); +} + +inline bool empty(const QByteArray &bytetArray) +{ + return bytetArray.isEmpty(); +} + +inline bool empty(const QUrl &url) +{ + return url.isEmpty(); +} + +QT_END_NAMESPACE + namespace Internal { template @@ -40,8 +61,8 @@ class EndsWithMatcher *os << "doesn't end with " << m_suffix; } - EndsWithMatcher(EndsWithMatcher const &) = default; - EndsWithMatcher &operator=(EndsWithMatcher const &) = delete; + EndsWithMatcher(const EndsWithMatcher &) = default; + EndsWithMatcher &operator=(const EndsWithMatcher &) = delete; private: const StringType m_suffix; @@ -74,31 +95,15 @@ private: const QString m_suffix; }; -class IsEmptyMatcher : public testing::internal::IsEmptyMatcher +class IsEmptyMatcher { public: - using Base = testing::internal::IsEmptyMatcher; - - using Base::MatchAndExplain; - - bool MatchAndExplain(const QString &s, testing::MatchResultListener *listener) const + template + bool MatchAndExplain(const Type &value, testing::MatchResultListener *) const { - if (s.isEmpty()) { - return true; - } + using std::empty; - *listener << "whose size is " << s.size(); - return false; - } - - bool MatchAndExplain(const QByteArray &s, testing::MatchResultListener *listener) const - { - if (s.isEmpty()) { - return true; - } - - *listener << "whose size is " << s.size(); - return false; + return empty(value); } void DescribeTo(std::ostream *os) const { *os << "is empty"; } @@ -106,6 +111,37 @@ public: void DescribeNegationTo(std::ostream *os) const { *os << "isn't empty"; } }; +class IsNullMatcher +{ +public: + template>> + bool MatchAndExplain(const Type &value, testing::MatchResultListener *) const + { + if constexpr (std::is_pointer_v) + return value == nullptr; + else + return value.isNull(); + } + + void DescribeTo(std::ostream *os) const { *os << "is null"; } + + void DescribeNegationTo(std::ostream *os) const { *os << "isn't null"; } +}; + +class IsValidMatcher +{ +public: + template>> + bool MatchAndExplain(const Type &value, testing::MatchResultListener *) const + { + return value.isValid(); + } + + void DescribeTo(std::ostream *os) const { *os << "is null"; } + + void DescribeNegationTo(std::ostream *os) const { *os << "isn't null"; } +}; + } // namespace Internal inline auto EndsWith(const Utils::SmallString &suffix) @@ -122,3 +158,13 @@ inline auto IsEmpty() { return ::testing::PolymorphicMatcher(Internal::IsEmptyMatcher()); } + +inline auto IsNull() +{ + return ::testing::PolymorphicMatcher(Internal::IsNullMatcher()); +} + +inline auto IsValid() +{ + return ::testing::PolymorphicMatcher(Internal::IsValidMatcher()); +} diff --git a/tests/unit/tests/unittests/sqlite/sqlitestatement-test.cpp b/tests/unit/tests/unittests/sqlite/sqlitestatement-test.cpp index 64a9cdd2772..f56de53f041 100644 --- a/tests/unit/tests/unittests/sqlite/sqlitestatement-test.cpp +++ b/tests/unit/tests/unittests/sqlite/sqlitestatement-test.cpp @@ -887,6 +887,8 @@ public: Sqlite::Value value; + bool isNull() const { return value.isNull(); } + template friend bool operator==(const FooValue &value, const Type &other) { diff --git a/tests/unit/tests/utils/google-using-declarations.h b/tests/unit/tests/utils/google-using-declarations.h index 19e8df2ef20..0d63479fbe1 100644 --- a/tests/unit/tests/utils/google-using-declarations.h +++ b/tests/unit/tests/utils/google-using-declarations.h @@ -31,7 +31,6 @@ using testing::HasSubstr; using testing::InSequence; using testing::Invoke; using testing::IsFalse; -using testing::IsNull; using testing::IsSubsetOf; using testing::IsSupersetOf; using testing::IsTrue;