forked from qt-creator/qt-creator
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 <ci_patchbuild_bot@qt.io> Reviewed-by: Tim Jenssen <tim.jenssen@qt.io>
This commit is contained in:
@@ -9,4 +9,5 @@ add_qtc_library(TestMatchers OBJECT
|
||||
import-matcher.h
|
||||
unittest-matchers.h
|
||||
version-matcher.h
|
||||
qvariant-matcher.h
|
||||
)
|
||||
|
44
tests/unit/tests/matchers/qvariant-matcher.h
Normal file
44
tests/unit/tests/matchers/qvariant-matcher.h
Normal file
@@ -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 <QMetaType>
|
||||
#include <QVariant>
|
||||
|
||||
#include <gmock/gmock-matchers.h>
|
||||
#include <gmock/gmock-more-matchers.h>
|
||||
|
||||
template<typename Type>
|
||||
class IsQVariantTypeMatcher
|
||||
{
|
||||
public:
|
||||
using is_gtest_matcher = void;
|
||||
|
||||
bool MatchAndExplain(const QVariant &value, std::ostream *) const
|
||||
{
|
||||
return QMetaType::fromType<Type>() == 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<typename Type>
|
||||
testing::Matcher<QVariant> IsQVariantType()
|
||||
{
|
||||
return IsQVariantTypeMatcher<Type>();
|
||||
}
|
||||
|
||||
template<typename Type, typename Matcher>
|
||||
auto IsQVariant(const Matcher &matcher)
|
||||
{
|
||||
return AllOf(IsQVariantType<Type>(), Property(&QVariant::value<Type>, matcher));
|
||||
}
|
||||
|
||||
template<typename Matcher>
|
||||
auto QVariantIsValid(const Matcher &matcher)
|
||||
{
|
||||
return Property(&QVariant::isValid, matcher);
|
||||
}
|
@@ -8,6 +8,27 @@
|
||||
|
||||
#include <utils/smallstringio.h>
|
||||
|
||||
#include <QUrl>
|
||||
|
||||
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 <typename StringType>
|
||||
@@ -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<typename Type>
|
||||
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<typename Type, typename = std::enable_if_t<!std::is_pointer_v<Type>>>
|
||||
bool MatchAndExplain(const Type &value, testing::MatchResultListener *) const
|
||||
{
|
||||
if constexpr (std::is_pointer_v<Type>)
|
||||
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<typename Type, typename = std::enable_if_t<!std::is_pointer_v<Type>>>
|
||||
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());
|
||||
}
|
||||
|
@@ -887,6 +887,8 @@ public:
|
||||
|
||||
Sqlite::Value value;
|
||||
|
||||
bool isNull() const { return value.isNull(); }
|
||||
|
||||
template<typename Type>
|
||||
friend bool operator==(const FooValue &value, const Type &other)
|
||||
{
|
||||
|
@@ -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;
|
||||
|
Reference in New Issue
Block a user