mirror of
https://github.com/RPCS3/llvm-mirror.git
synced 2024-11-24 11:42:57 +01:00
7470585f23
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 llvm-svn: 305395
70 lines
2.1 KiB
C++
70 lines
2.1 KiB
C++
//===- 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) {
|
|
llvm::detail::ExpectedHolder<T> Result;
|
|
auto &EH = static_cast<llvm::detail::ErrorHolder &>(Result);
|
|
EH = TakeError(Exp.takeError());
|
|
if (Result.Success)
|
|
Result.Value = &(*Exp);
|
|
return Result;
|
|
}
|
|
|
|
template <typename T> ExpectedHolder<T> TakeExpected(const Expected<T> &Exp) {
|
|
return TakeExpected(const_cast<Expected<T> &>(Exp));
|
|
}
|
|
} // 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)
|
|
|
|
MATCHER(Succeeded, "") { return arg.Success; }
|
|
MATCHER(Failed, "") { return !arg.Success; }
|
|
|
|
MATCHER_P(HasValue, value,
|
|
"succeeded with value " + testing::PrintToString(value)) {
|
|
if (!arg.Success) {
|
|
*result_listener << "operation failed";
|
|
return false;
|
|
}
|
|
|
|
assert(arg.Value.hasValue());
|
|
if (**arg.Value != value) {
|
|
*result_listener << "but \"" + testing::PrintToString(**arg.Value) +
|
|
"\" != " + testing::PrintToString(value);
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
} // namespace llvm
|
|
|
|
#endif
|