|
9 | 9 | #ifndef LLVM_TESTING_SUPPORT_SUPPORTHELPERS_H
|
10 | 10 | #define LLVM_TESTING_SUPPORT_SUPPORTHELPERS_H
|
11 | 11 |
|
| 12 | +#include "llvm/ADT/Optional.h" |
12 | 13 | #include "llvm/ADT/SmallString.h"
|
13 | 14 | #include "llvm/Support/Error.h"
|
14 | 15 | #include "llvm/Support/raw_os_ostream.h"
|
| 16 | +#include "gmock/gmock-matchers.h" |
15 | 17 | #include "gtest/gtest-printers.h"
|
16 | 18 |
|
17 | 19 | #include <string>
|
@@ -53,11 +55,56 @@ void PrintTo(const ExpectedHolder<T> &Item, std::ostream *Out) {
|
53 | 55 | PrintTo(static_cast<const ErrorHolder &>(Item), Out);
|
54 | 56 | }
|
55 | 57 | }
|
| 58 | + |
| 59 | +template <class InnerMatcher> class ValueIsMatcher { |
| 60 | +public: |
| 61 | + explicit ValueIsMatcher(InnerMatcher ValueMatcher) |
| 62 | + : ValueMatcher(ValueMatcher) {} |
| 63 | + |
| 64 | + template <class T> |
| 65 | + operator ::testing::Matcher<const llvm::Optional<T> &>() const { |
| 66 | + return ::testing::MakeMatcher( |
| 67 | + new Impl<T>(::testing::SafeMatcherCast<T>(ValueMatcher))); |
| 68 | + } |
| 69 | + |
| 70 | + template <class T> |
| 71 | + class Impl : public ::testing::MatcherInterface<const llvm::Optional<T> &> { |
| 72 | + public: |
| 73 | + explicit Impl(const ::testing::Matcher<T> &ValueMatcher) |
| 74 | + : ValueMatcher(ValueMatcher) {} |
| 75 | + |
| 76 | + bool MatchAndExplain(const llvm::Optional<T> &Input, |
| 77 | + testing::MatchResultListener *L) const override { |
| 78 | + return Input && ValueMatcher.MatchAndExplain(Input.getValue(), L); |
| 79 | + } |
| 80 | + |
| 81 | + void DescribeTo(std::ostream *OS) const override { |
| 82 | + *OS << "has a value that "; |
| 83 | + ValueMatcher.DescribeTo(OS); |
| 84 | + } |
| 85 | + void DescribeNegationTo(std::ostream *OS) const override { |
| 86 | + *OS << "does not have a value that "; |
| 87 | + ValueMatcher.DescribeTo(OS); |
| 88 | + } |
| 89 | + |
| 90 | + private: |
| 91 | + testing::Matcher<T> ValueMatcher; |
| 92 | + }; |
| 93 | + |
| 94 | +private: |
| 95 | + InnerMatcher ValueMatcher; |
| 96 | +}; |
56 | 97 | } // namespace detail
|
57 | 98 |
|
| 99 | +/// Matches an llvm::Optional<T> with a value that conforms to an inner matcher. |
| 100 | +/// To match llvm::None you could use Eq(llvm::None). |
| 101 | +template <class InnerMatcher> |
| 102 | +detail::ValueIsMatcher<InnerMatcher> ValueIs(const InnerMatcher &ValueMatcher) { |
| 103 | + return detail::ValueIsMatcher<InnerMatcher>(ValueMatcher); |
| 104 | +} |
58 | 105 | namespace unittest {
|
59 | 106 | SmallString<128> getInputFileDirectory(const char *Argv0);
|
60 |
| -} |
| 107 | +} // namespace unittest |
61 | 108 | } // namespace llvm
|
62 | 109 |
|
63 | 110 | #endif
|
0 commit comments