LCOV - code coverage report
Current view: top level - include/llvm/Testing/Support - Error.h (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 98 161 60.9 %
Date: 2018-10-20 13:21:21 Functions: 45 108 41.7 %
Legend: Lines: hit not hit

          Line data    Source code
       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         102 : template <typename T> ExpectedHolder<T> TakeExpected(Expected<T> &Exp) {
      25         204 :   return {TakeError(Exp.takeError()), Exp};
      26             : }
      27          28 : 
      28          56 : template <typename T> ExpectedHolder<T> TakeExpected(Expected<T> &&Exp) {
      29           0 :   return TakeExpected(Exp);
      30          24 : }
      31          48 : 
      32             : template <typename T>
      33          15 : class ValueMatchesMono
      34          30 :     : public testing::MatcherInterface<const ExpectedHolder<T> &> {
      35          25 : public:
      36           0 :   explicit ValueMatchesMono(const testing::Matcher<T> &Matcher)
      37           0 :       : Matcher(Matcher) {}
      38          18 : 
      39           0 :   bool MatchAndExplain(const ExpectedHolder<T> &Holder,
      40             :                        testing::MatchResultListener *listener) const override {
      41           0 :     if (!Holder.Success())
      42          19 :       return false;
      43          19 : 
      44           0 :     bool result = Matcher.MatchAndExplain(*Holder.Exp, listener);
      45          19 : 
      46           0 :     if (result)
      47          19 :       return result;
      48             :     *listener << "(";
      49           0 :     Matcher.DescribeNegationTo(listener->stream());
      50          17 :     *listener << ")";
      51             :     return result;
      52          17 :   }
      53             : 
      54           0 :   void DescribeTo(std::ostream *OS) const override {
      55           2 :     *OS << "succeeded with value (";
      56             :     Matcher.DescribeTo(OS);
      57           0 :     *OS << ")";
      58           0 :   }
      59          12 : 
      60           0 :   void DescribeNegationTo(std::ostream *OS) const override {
      61          12 :     *OS << "did not succeed or value (";
      62             :     Matcher.DescribeNegationTo(OS);
      63           0 :     *OS << ")";
      64          10 :   }
      65             : 
      66          10 : private:
      67             :   testing::Matcher<T> Matcher;
      68             : };
      69           2 : 
      70             : template<typename M>
      71             : class ValueMatchesPoly {
      72             : public:
      73           7 :   explicit ValueMatchesPoly(const M &Matcher) : Matcher(Matcher) {}
      74             : 
      75           7 :   template <typename T>
      76           0 :   operator testing::Matcher<const ExpectedHolder<T> &>() const {
      77             :     return MakeMatcher(
      78           7 :         new ValueMatchesMono<T>(testing::SafeMatcherCast<T>(Matcher)));
      79             :   }
      80           7 : 
      81             : private:
      82             :   M Matcher;
      83           0 : };
      84             : 
      85             : template <typename InfoT>
      86             : class ErrorMatchesMono : public testing::MatcherInterface<const ErrorHolder &> {
      87             : public:
      88           4 :   explicit ErrorMatchesMono(Optional<testing::Matcher<InfoT &>> Matcher)
      89           4 :       : Matcher(std::move(Matcher)) {}
      90             : 
      91           4 :   bool MatchAndExplain(const ErrorHolder &Holder,
      92           4 :                        testing::MatchResultListener *listener) const override {
      93           4 :     if (Holder.Success())
      94           4 :       return false;
      95             : 
      96           4 :     if (Holder.Infos.size() > 1) {
      97           4 :       *listener << "multiple errors";
      98           0 :       return false;
      99           0 :     }
     100             : 
     101           0 :     auto &Info = *Holder.Infos[0];
     102           0 :     if (!Info.isA<InfoT>()) {
     103             :       *listener << "Error was not of given type";
     104           0 :       return false;
     105           0 :     }
     106             : 
     107           0 :     if (!Matcher)
     108           0 :       return true;
     109           0 : 
     110           0 :     return Matcher->MatchAndExplain(static_cast<InfoT &>(Info), listener);
     111             :   }
     112           0 : 
     113           0 :   void DescribeTo(std::ostream *OS) const override {
     114           0 :     *OS << "failed with Error of given type";
     115           0 :     if (Matcher) {
     116             :       *OS << " and the error ";
     117           0 :       Matcher->DescribeTo(OS);
     118           0 :     }
     119             :   }
     120             : 
     121             :   void DescribeNegationTo(std::ostream *OS) const override {
     122             :     *OS << "succeeded or did not fail with the error of given type";
     123             :     if (Matcher) {
     124             :       *OS << " or the error ";
     125             :       Matcher->DescribeNegationTo(OS);
     126             :     }
     127             :   }
     128             : 
     129             : private:
     130          19 :   Optional<testing::Matcher<InfoT &>> Matcher;
     131             : };
     132          19 : } // namespace detail
     133             : 
     134           9 : #define EXPECT_THAT_ERROR(Err, Matcher)                                        \
     135             :   EXPECT_THAT(llvm::detail::TakeError(Err), Matcher)
     136           9 : #define ASSERT_THAT_ERROR(Err, Matcher)                                        \
     137             :   ASSERT_THAT(llvm::detail::TakeError(Err), Matcher)
     138           7 : 
     139             : #define EXPECT_THAT_EXPECTED(Err, Matcher)                                     \
     140           7 :   EXPECT_THAT(llvm::detail::TakeExpected(Err), Matcher)
     141             : #define ASSERT_THAT_EXPECTED(Err, Matcher)                                     \
     142           3 :   ASSERT_THAT(llvm::detail::TakeExpected(Err), Matcher)
     143             : 
     144        2867 : MATCHER(Succeeded, "") { return arg.Success(); }
     145         302 : MATCHER(Failed, "") { return !arg.Success(); }
     146             : 
     147             : template <typename InfoT>
     148             : testing::Matcher<const detail::ErrorHolder &> Failed() {
     149             :   return MakeMatcher(new detail::ErrorMatchesMono<InfoT>(None));
     150             : }
     151             : 
     152             : template <typename InfoT, typename M>
     153         123 : testing::Matcher<const detail::ErrorHolder &> Failed(M Matcher) {
     154          11 :   return MakeMatcher(new detail::ErrorMatchesMono<InfoT>(
     155           8 :       testing::SafeMatcherCast<InfoT &>(Matcher)));
     156             : }
     157           8 : 
     158             : template <typename M>
     159           8 : detail::ValueMatchesPoly<M> HasValue(M Matcher) {
     160             :   return detail::ValueMatchesPoly<M>(Matcher);
     161             : }
     162           7 : 
     163             : } // namespace llvm
     164           1 : 
     165             : #endif

Generated by: LCOV version 1.13