Files
archived-llvm/include/llvm/Testing/Support/SupportHelpers.h
Zachary Turner 83a953d909 [gtest] Create a shared include directory for gtest utilities.
Many times unit tests for different libraries would like to use
the same helper functions for checking common types of errors.

This patch adds a common library with helpers for testing things
in Support, and introduces helpers in here for integrating the
llvm::Error and llvm::Expected<T> classes with gtest and gmock.

Normally, we would just be able to write:

   EXPECT_THAT(someFunction(), succeeded());

but due to some quirks in llvm::Error's move semantics, gmock
doesn't make this easy, so two macros EXPECT_THAT_ERROR() and
EXPECT_THAT_EXPECTED() are introduced to gloss over the difficulties.
Consider this an exception, and possibly only temporary as we
look for ways to improve this.

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

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@305395 91177308-0d34-0410-b5e6-96231b3b80d8
2017-06-14 16:41:50 +00:00

47 lines
1.2 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"
namespace llvm {
namespace detail {
struct ErrorHolder {
bool Success;
std::string Message;
};
template <typename T> struct ExpectedHolder : public ErrorHolder {
Optional<T *> Value;
};
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.Value)
<< "\"";
} else {
PrintTo(static_cast<const ErrorHolder &>(Item), Out);
}
}
} // namespace detail
} // namespace llvm
#endif