2017-06-14 18:41:50 +02:00
|
|
|
//===- llvm/Testing/Support/Error.h ---------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_TESTING_SUPPORT_ERROR_H
|
|
|
|
#define LLVM_TESTING_SUPPORT_ERROR_H
|
|
|
|
|
|
|
|
#include "llvm/ADT/Optional.h"
|
|
|
|
#include "llvm/Support/Error.h"
|
|
|
|
#include "llvm/Testing/Support/SupportHelpers.h"
|
|
|
|
|
|
|
|
#include "gmock/gmock.h"
|
|
|
|
#include <ostream>
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
namespace detail {
|
|
|
|
ErrorHolder TakeError(Error Err);
|
|
|
|
|
|
|
|
template <typename T> ExpectedHolder<T> TakeExpected(Expected<T> &Exp) {
|
2017-12-07 11:54:23 +01:00
|
|
|
return {TakeError(Exp.takeError()), Exp};
|
2017-06-14 18:41:50 +02:00
|
|
|
}
|
|
|
|
|
2017-06-22 15:11:50 +02:00
|
|
|
template <typename T> ExpectedHolder<T> TakeExpected(Expected<T> &&Exp) {
|
|
|
|
return TakeExpected(Exp);
|
2017-06-14 18:41:50 +02:00
|
|
|
}
|
2017-12-13 11:00:38 +01:00
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
class ValueMatchesMono
|
|
|
|
: public testing::MatcherInterface<const ExpectedHolder<T> &> {
|
|
|
|
public:
|
|
|
|
explicit ValueMatchesMono(const testing::Matcher<T> &Matcher)
|
|
|
|
: Matcher(Matcher) {}
|
|
|
|
|
|
|
|
bool MatchAndExplain(const ExpectedHolder<T> &Holder,
|
|
|
|
testing::MatchResultListener *listener) const override {
|
[Testing/Support]: Better matching of Error failure states
Summary:
The existing Failed() matcher only allowed asserting that the operation
failed, but it was not possible to verify any details of the returned
error.
This patch adds two new matchers, which make this possible:
- Failed<InfoT>() verifies that the operation failed with a single error
of a given type.
- Failed<InfoT>(M) additionally check that the contained error info
object is matched by the nested matcher M.
To make these work, I've changed the implementation of the ErrorHolder
class. Now, instead of just storing the string representation of the
Error, it fetches the ErrorInfo objects and stores then as a list of
shared pointers. This way, ErrorHolder remains copyable, while still
retaining the full information contained in the Error object.
In case the Error object contains two or more errors, the new matchers
will fail to match, instead of trying to match all (or any) of the
individual ErrorInfo objects. This seemed to be the most sensible
behavior for when one wants to match exact error details, but I could be
convinced otherwise...
Reviewers: zturner, lhames
Subscribers: llvm-commits
Differential Revision: https://reviews.llvm.org/D44925
llvm-svn: 329288
2018-04-05 16:32:10 +02:00
|
|
|
if (!Holder.Success())
|
2017-12-13 11:00:38 +01:00
|
|
|
return false;
|
|
|
|
|
|
|
|
bool result = Matcher.MatchAndExplain(*Holder.Exp, listener);
|
|
|
|
|
|
|
|
if (result)
|
|
|
|
return result;
|
|
|
|
*listener << "(";
|
|
|
|
Matcher.DescribeNegationTo(listener->stream());
|
|
|
|
*listener << ")";
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DescribeTo(std::ostream *OS) const override {
|
|
|
|
*OS << "succeeded with value (";
|
|
|
|
Matcher.DescribeTo(OS);
|
|
|
|
*OS << ")";
|
|
|
|
}
|
|
|
|
|
|
|
|
void DescribeNegationTo(std::ostream *OS) const override {
|
|
|
|
*OS << "did not succeed or value (";
|
|
|
|
Matcher.DescribeNegationTo(OS);
|
|
|
|
*OS << ")";
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
testing::Matcher<T> Matcher;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename M>
|
|
|
|
class ValueMatchesPoly {
|
|
|
|
public:
|
|
|
|
explicit ValueMatchesPoly(const M &Matcher) : Matcher(Matcher) {}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
operator testing::Matcher<const ExpectedHolder<T> &>() const {
|
|
|
|
return MakeMatcher(
|
|
|
|
new ValueMatchesMono<T>(testing::SafeMatcherCast<T>(Matcher)));
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
M Matcher;
|
|
|
|
};
|
|
|
|
|
[Testing/Support]: Better matching of Error failure states
Summary:
The existing Failed() matcher only allowed asserting that the operation
failed, but it was not possible to verify any details of the returned
error.
This patch adds two new matchers, which make this possible:
- Failed<InfoT>() verifies that the operation failed with a single error
of a given type.
- Failed<InfoT>(M) additionally check that the contained error info
object is matched by the nested matcher M.
To make these work, I've changed the implementation of the ErrorHolder
class. Now, instead of just storing the string representation of the
Error, it fetches the ErrorInfo objects and stores then as a list of
shared pointers. This way, ErrorHolder remains copyable, while still
retaining the full information contained in the Error object.
In case the Error object contains two or more errors, the new matchers
will fail to match, instead of trying to match all (or any) of the
individual ErrorInfo objects. This seemed to be the most sensible
behavior for when one wants to match exact error details, but I could be
convinced otherwise...
Reviewers: zturner, lhames
Subscribers: llvm-commits
Differential Revision: https://reviews.llvm.org/D44925
llvm-svn: 329288
2018-04-05 16:32:10 +02:00
|
|
|
template <typename InfoT>
|
|
|
|
class ErrorMatchesMono : public testing::MatcherInterface<const ErrorHolder &> {
|
|
|
|
public:
|
2018-04-10 16:11:53 +02:00
|
|
|
explicit ErrorMatchesMono(Optional<testing::Matcher<InfoT &>> Matcher)
|
[Testing/Support]: Better matching of Error failure states
Summary:
The existing Failed() matcher only allowed asserting that the operation
failed, but it was not possible to verify any details of the returned
error.
This patch adds two new matchers, which make this possible:
- Failed<InfoT>() verifies that the operation failed with a single error
of a given type.
- Failed<InfoT>(M) additionally check that the contained error info
object is matched by the nested matcher M.
To make these work, I've changed the implementation of the ErrorHolder
class. Now, instead of just storing the string representation of the
Error, it fetches the ErrorInfo objects and stores then as a list of
shared pointers. This way, ErrorHolder remains copyable, while still
retaining the full information contained in the Error object.
In case the Error object contains two or more errors, the new matchers
will fail to match, instead of trying to match all (or any) of the
individual ErrorInfo objects. This seemed to be the most sensible
behavior for when one wants to match exact error details, but I could be
convinced otherwise...
Reviewers: zturner, lhames
Subscribers: llvm-commits
Differential Revision: https://reviews.llvm.org/D44925
llvm-svn: 329288
2018-04-05 16:32:10 +02:00
|
|
|
: Matcher(std::move(Matcher)) {}
|
|
|
|
|
|
|
|
bool MatchAndExplain(const ErrorHolder &Holder,
|
|
|
|
testing::MatchResultListener *listener) const override {
|
|
|
|
if (Holder.Success())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (Holder.Infos.size() > 1) {
|
|
|
|
*listener << "multiple errors";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto &Info = *Holder.Infos[0];
|
|
|
|
if (!Info.isA<InfoT>()) {
|
|
|
|
*listener << "Error was not of given type";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Matcher)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return Matcher->MatchAndExplain(static_cast<InfoT &>(Info), listener);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DescribeTo(std::ostream *OS) const override {
|
|
|
|
*OS << "failed with Error of given type";
|
|
|
|
if (Matcher) {
|
|
|
|
*OS << " and the error ";
|
|
|
|
Matcher->DescribeTo(OS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DescribeNegationTo(std::ostream *OS) const override {
|
|
|
|
*OS << "succeeded or did not fail with the error of given type";
|
|
|
|
if (Matcher) {
|
|
|
|
*OS << " or the error ";
|
|
|
|
Matcher->DescribeNegationTo(OS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2018-04-10 16:11:53 +02:00
|
|
|
Optional<testing::Matcher<InfoT &>> Matcher;
|
[Testing/Support]: Better matching of Error failure states
Summary:
The existing Failed() matcher only allowed asserting that the operation
failed, but it was not possible to verify any details of the returned
error.
This patch adds two new matchers, which make this possible:
- Failed<InfoT>() verifies that the operation failed with a single error
of a given type.
- Failed<InfoT>(M) additionally check that the contained error info
object is matched by the nested matcher M.
To make these work, I've changed the implementation of the ErrorHolder
class. Now, instead of just storing the string representation of the
Error, it fetches the ErrorInfo objects and stores then as a list of
shared pointers. This way, ErrorHolder remains copyable, while still
retaining the full information contained in the Error object.
In case the Error object contains two or more errors, the new matchers
will fail to match, instead of trying to match all (or any) of the
individual ErrorInfo objects. This seemed to be the most sensible
behavior for when one wants to match exact error details, but I could be
convinced otherwise...
Reviewers: zturner, lhames
Subscribers: llvm-commits
Differential Revision: https://reviews.llvm.org/D44925
llvm-svn: 329288
2018-04-05 16:32:10 +02:00
|
|
|
};
|
2017-06-14 18:41:50 +02:00
|
|
|
} // namespace detail
|
|
|
|
|
|
|
|
#define EXPECT_THAT_ERROR(Err, Matcher) \
|
|
|
|
EXPECT_THAT(llvm::detail::TakeError(Err), Matcher)
|
|
|
|
#define ASSERT_THAT_ERROR(Err, Matcher) \
|
|
|
|
ASSERT_THAT(llvm::detail::TakeError(Err), Matcher)
|
|
|
|
|
|
|
|
#define EXPECT_THAT_EXPECTED(Err, Matcher) \
|
|
|
|
EXPECT_THAT(llvm::detail::TakeExpected(Err), Matcher)
|
|
|
|
#define ASSERT_THAT_EXPECTED(Err, Matcher) \
|
|
|
|
ASSERT_THAT(llvm::detail::TakeExpected(Err), Matcher)
|
|
|
|
|
[Testing/Support]: Better matching of Error failure states
Summary:
The existing Failed() matcher only allowed asserting that the operation
failed, but it was not possible to verify any details of the returned
error.
This patch adds two new matchers, which make this possible:
- Failed<InfoT>() verifies that the operation failed with a single error
of a given type.
- Failed<InfoT>(M) additionally check that the contained error info
object is matched by the nested matcher M.
To make these work, I've changed the implementation of the ErrorHolder
class. Now, instead of just storing the string representation of the
Error, it fetches the ErrorInfo objects and stores then as a list of
shared pointers. This way, ErrorHolder remains copyable, while still
retaining the full information contained in the Error object.
In case the Error object contains two or more errors, the new matchers
will fail to match, instead of trying to match all (or any) of the
individual ErrorInfo objects. This seemed to be the most sensible
behavior for when one wants to match exact error details, but I could be
convinced otherwise...
Reviewers: zturner, lhames
Subscribers: llvm-commits
Differential Revision: https://reviews.llvm.org/D44925
llvm-svn: 329288
2018-04-05 16:32:10 +02:00
|
|
|
MATCHER(Succeeded, "") { return arg.Success(); }
|
|
|
|
MATCHER(Failed, "") { return !arg.Success(); }
|
|
|
|
|
|
|
|
template <typename InfoT>
|
|
|
|
testing::Matcher<const detail::ErrorHolder &> Failed() {
|
|
|
|
return MakeMatcher(new detail::ErrorMatchesMono<InfoT>(None));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename InfoT, typename M>
|
|
|
|
testing::Matcher<const detail::ErrorHolder &> Failed(M Matcher) {
|
|
|
|
return MakeMatcher(new detail::ErrorMatchesMono<InfoT>(
|
2018-04-10 16:11:53 +02:00
|
|
|
testing::SafeMatcherCast<InfoT &>(Matcher)));
|
[Testing/Support]: Better matching of Error failure states
Summary:
The existing Failed() matcher only allowed asserting that the operation
failed, but it was not possible to verify any details of the returned
error.
This patch adds two new matchers, which make this possible:
- Failed<InfoT>() verifies that the operation failed with a single error
of a given type.
- Failed<InfoT>(M) additionally check that the contained error info
object is matched by the nested matcher M.
To make these work, I've changed the implementation of the ErrorHolder
class. Now, instead of just storing the string representation of the
Error, it fetches the ErrorInfo objects and stores then as a list of
shared pointers. This way, ErrorHolder remains copyable, while still
retaining the full information contained in the Error object.
In case the Error object contains two or more errors, the new matchers
will fail to match, instead of trying to match all (or any) of the
individual ErrorInfo objects. This seemed to be the most sensible
behavior for when one wants to match exact error details, but I could be
convinced otherwise...
Reviewers: zturner, lhames
Subscribers: llvm-commits
Differential Revision: https://reviews.llvm.org/D44925
llvm-svn: 329288
2018-04-05 16:32:10 +02:00
|
|
|
}
|
2017-06-14 18:41:50 +02:00
|
|
|
|
2017-12-13 11:00:38 +01:00
|
|
|
template <typename M>
|
|
|
|
detail::ValueMatchesPoly<M> HasValue(M Matcher) {
|
|
|
|
return detail::ValueMatchesPoly<M>(Matcher);
|
2017-06-14 18:41:50 +02:00
|
|
|
}
|
2017-12-13 11:00:38 +01:00
|
|
|
|
2017-06-14 18:41:50 +02:00
|
|
|
} // namespace llvm
|
|
|
|
|
|
|
|
#endif
|