Skip to content

Commit 4ea70ec

Browse files
committed
[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 llvm-svn: 359174
1 parent 445c22b commit 4ea70ec

File tree

3 files changed

+74
-1
lines changed

3 files changed

+74
-1
lines changed

llvm/include/llvm/Testing/Support/SupportHelpers.h

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99
#ifndef LLVM_TESTING_SUPPORT_SUPPORTHELPERS_H
1010
#define LLVM_TESTING_SUPPORT_SUPPORTHELPERS_H
1111

12+
#include "llvm/ADT/Optional.h"
1213
#include "llvm/ADT/SmallString.h"
1314
#include "llvm/Support/Error.h"
1415
#include "llvm/Support/raw_os_ostream.h"
16+
#include "gmock/gmock-matchers.h"
1517
#include "gtest/gtest-printers.h"
1618

1719
#include <string>
@@ -53,11 +55,56 @@ void PrintTo(const ExpectedHolder<T> &Item, std::ostream *Out) {
5355
PrintTo(static_cast<const ErrorHolder &>(Item), Out);
5456
}
5557
}
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+
};
5697
} // namespace detail
5798

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+
}
58105
namespace unittest {
59106
SmallString<128> getInputFileDirectory(const char *Argv0);
60-
}
107+
} // namespace unittest
61108
} // namespace llvm
62109

63110
#endif

llvm/unittests/Support/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ add_llvm_unittest(SupportTests
3939
LEB128Test.cpp
4040
LineIteratorTest.cpp
4141
LockFileManagerTest.cpp
42+
MatchersTest.cpp
4243
MD5Test.cpp
4344
ManagedStatic.cpp
4445
MathExtrasTest.cpp
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//===----- unittests/MatchersTest.cpp -------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "llvm/ADT/Optional.h"
10+
#include "llvm/Testing/Support/SupportHelpers.h"
11+
#include "gmock/gmock-matchers.h"
12+
13+
using ::testing::_;
14+
using ::testing::AllOf;
15+
using ::testing::Gt;
16+
using ::testing::Lt;
17+
using ::testing::Not;
18+
19+
namespace {
20+
TEST(MatchersTest, Optional) {
21+
EXPECT_THAT(llvm::Optional<int>(llvm::None), Not(llvm::ValueIs(_)));
22+
EXPECT_THAT(llvm::Optional<int>(10), llvm::ValueIs(10));
23+
EXPECT_THAT(llvm::Optional<int>(10), llvm::ValueIs(AllOf(Lt(11), Gt(9))));
24+
}
25+
} // namespace

0 commit comments

Comments
 (0)