Home | History | Annotate | Download | only in Support
      1 //===- llvm/Testing/Support/Error.h ---------------------------------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 
     10 #ifndef LLVM_TESTING_SUPPORT_ERROR_H
     11 #define LLVM_TESTING_SUPPORT_ERROR_H
     12 
     13 #include "llvm/ADT/Optional.h"
     14 #include "llvm/Support/Error.h"
     15 #include "llvm/Testing/Support/SupportHelpers.h"
     16 
     17 #include "gmock/gmock.h"
     18 #include <ostream>
     19 
     20 namespace llvm {
     21 namespace detail {
     22 ErrorHolder TakeError(Error Err);
     23 
     24 template <typename T> ExpectedHolder<T> TakeExpected(Expected<T> &Exp) {
     25   llvm::detail::ExpectedHolder<T> Result;
     26   auto &EH = static_cast<llvm::detail::ErrorHolder &>(Result);
     27   EH = TakeError(Exp.takeError());
     28   if (Result.Success)
     29     Result.Value = &(*Exp);
     30   return Result;
     31 }
     32 
     33 template <typename T> ExpectedHolder<T> TakeExpected(Expected<T> &&Exp) {
     34   return TakeExpected(Exp);
     35 }
     36 } // namespace detail
     37 
     38 #define EXPECT_THAT_ERROR(Err, Matcher)                                        \
     39   EXPECT_THAT(llvm::detail::TakeError(Err), Matcher)
     40 #define ASSERT_THAT_ERROR(Err, Matcher)                                        \
     41   ASSERT_THAT(llvm::detail::TakeError(Err), Matcher)
     42 
     43 #define EXPECT_THAT_EXPECTED(Err, Matcher)                                     \
     44   EXPECT_THAT(llvm::detail::TakeExpected(Err), Matcher)
     45 #define ASSERT_THAT_EXPECTED(Err, Matcher)                                     \
     46   ASSERT_THAT(llvm::detail::TakeExpected(Err), Matcher)
     47 
     48 MATCHER(Succeeded, "") { return arg.Success; }
     49 MATCHER(Failed, "") { return !arg.Success; }
     50 
     51 MATCHER_P(HasValue, value,
     52           "succeeded with value " + testing::PrintToString(value)) {
     53   if (!arg.Success) {
     54     *result_listener << "operation failed";
     55     return false;
     56   }
     57 
     58   assert(arg.Value.hasValue());
     59   if (**arg.Value != value) {
     60     *result_listener << "but \"" + testing::PrintToString(**arg.Value) +
     61                             "\" != " + testing::PrintToString(value);
     62     return false;
     63   }
     64 
     65   return true;
     66 }
     67 } // namespace llvm
     68 
     69 #endif
     70