llvm.org GIT mirror llvm / 4021ab7
[Support] Add a GTest matcher for Optional<T> Reviewers: sammccall Reviewed By: sammccall Subscribers: mgorny, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D61071 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@359174 91177308-0d34-0410-b5e6-96231b3b80d8 Ilya Biryukov 1 year, 7 months ago
3 changed file(s) with 74 addition(s) and 1 deletion(s). Raw diff Collapse all Expand all
88 #ifndef LLVM_TESTING_SUPPORT_SUPPORTHELPERS_H
99 #define LLVM_TESTING_SUPPORT_SUPPORTHELPERS_H
1010
11 #include "llvm/ADT/Optional.h"
1112 #include "llvm/ADT/SmallString.h"
1213 #include "llvm/Support/Error.h"
1314 #include "llvm/Support/raw_os_ostream.h"
15 #include "gmock/gmock-matchers.h"
1416 #include "gtest/gtest-printers.h"
1517
1618 #include
5254 PrintTo(static_cast(Item), Out);
5355 }
5456 }
57
58 template class ValueIsMatcher {
59 public:
60 explicit ValueIsMatcher(InnerMatcher ValueMatcher)
61 : ValueMatcher(ValueMatcher) {}
62
63 template
64 operator ::testing::Matcher &>() const {
65 return ::testing::MakeMatcher(
66 new Impl(::testing::SafeMatcherCast(ValueMatcher)));
67 }
68
69 template
70 class Impl : public ::testing::MatcherInterface &> {
71 public:
72 explicit Impl(const ::testing::Matcher &ValueMatcher)
73 : ValueMatcher(ValueMatcher) {}
74
75 bool MatchAndExplain(const llvm::Optional &Input,
76 testing::MatchResultListener *L) const override {
77 return Input && ValueMatcher.MatchAndExplain(Input.getValue(), L);
78 }
79
80 void DescribeTo(std::ostream *OS) const override {
81 *OS << "has a value that ";
82 ValueMatcher.DescribeTo(OS);
83 }
84 void DescribeNegationTo(std::ostream *OS) const override {
85 *OS << "does not have a value that ";
86 ValueMatcher.DescribeTo(OS);
87 }
88
89 private:
90 testing::Matcher ValueMatcher;
91 };
92
93 private:
94 InnerMatcher ValueMatcher;
95 };
5596 } // namespace detail
5697
98 /// Matches an llvm::Optional with a value that conforms to an inner matcher.
99 /// To match llvm::None you could use Eq(llvm::None).
100 template
101 detail::ValueIsMatcher ValueIs(const InnerMatcher &ValueMatcher) {
102 return detail::ValueIsMatcher(ValueMatcher);
103 }
57104 namespace unittest {
58105 SmallString<128> getInputFileDirectory(const char *Argv0);
59 }
106 } // namespace unittest
60107 } // namespace llvm
61108
62109 #endif
3838 LEB128Test.cpp
3939 LineIteratorTest.cpp
4040 LockFileManagerTest.cpp
41 MatchersTest.cpp
4142 MD5Test.cpp
4243 ManagedStatic.cpp
4344 MathExtrasTest.cpp
0 //===----- unittests/MatchersTest.cpp -------------------------------------===//
1 //
2 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
3 // See https://llvm.org/LICENSE.txt for license information.
4 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5 //
6 //===----------------------------------------------------------------------===//
7
8 #include "llvm/ADT/Optional.h"
9 #include "llvm/Testing/Support/SupportHelpers.h"
10 #include "gmock/gmock-matchers.h"
11
12 using ::testing::_;
13 using ::testing::AllOf;
14 using ::testing::Gt;
15 using ::testing::Lt;
16 using ::testing::Not;
17
18 namespace {
19 TEST(MatchersTest, Optional) {
20 EXPECT_THAT(llvm::Optional(llvm::None), Not(llvm::ValueIs(_)));
21 EXPECT_THAT(llvm::Optional(10), llvm::ValueIs(10));
22 EXPECT_THAT(llvm::Optional(10), llvm::ValueIs(AllOf(Lt(11), Gt(9))));
23 }
24 } // namespace