Pavel Labath 9fcf6e8c4e [Testing/Support] Make the HasValue matcher composable
Summary:
This makes it possible to run an arbitrary matcher on the value
contained within the Expected<T> object.

To do this, I've needed to fully spell out the matcher, instead of using
the shorthand MATCHER_P macro.

The slight gotcha here is that standard template deduction will fail if
one tries to match HasValue(47) against an Expected<int &> -- the
workaround is to use HasValue(testing::Eq(47)).

The explanations produced by this matcher have changed a bit, since now
we delegate to the nested matcher to print the value. Since these don't
put quotes around the value, I've changed our PrintTo methods to match.

Reviewers: zturner

Subscribers: llvm-commits

Differential Revision: https://reviews.llvm.org/D41065

llvm-svn: 320561
2017-12-13 10:00:38 +00:00

50 lines
1.3 KiB
C++

//===- Testing/Support/SupportHelpers.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_SUPPORTHELPERS_H
#define LLVM_TESTING_SUPPORT_SUPPORTHELPERS_H
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/Error.h"
#include "gtest/gtest-printers.h"
namespace llvm {
namespace detail {
struct ErrorHolder {
bool Success;
std::string Message;
};
template <typename T> struct ExpectedHolder : public ErrorHolder {
ExpectedHolder(ErrorHolder Err, Expected<T> &Exp)
: ErrorHolder(std::move(Err)), Exp(Exp) {}
Expected<T> &Exp;
};
inline void PrintTo(const ErrorHolder &Err, std::ostream *Out) {
*Out << (Err.Success ? "succeeded" : "failed");
if (!Err.Success) {
*Out << " (" << StringRef(Err.Message).trim().str() << ")";
}
}
template <typename T>
void PrintTo(const ExpectedHolder<T> &Item, std::ostream *Out) {
if (Item.Success) {
*Out << "succeeded with value " << ::testing::PrintToString(*Item.Exp);
} else {
PrintTo(static_cast<const ErrorHolder &>(Item), Out);
}
}
} // namespace detail
} // namespace llvm
#endif