Googletest export

Add a `Pointer` matcher as an analog to `Pointee`.

Similar to `Pointee`, `Pointer` works with either raw or smart pointers and
allows creating a matcher like Pointer(Eq(foo)) for smart pointers.

PiperOrigin-RevId: 346164768
This commit is contained in:
Abseil Team
2020-12-07 16:17:26 -05:00
committed by Andy Getz
parent 7bf5057a04
commit a02a591605
5 changed files with 131 additions and 31 deletions

View File

@@ -124,20 +124,6 @@ TEST(ConvertIdentifierNameToWordsTest, WorksWhenNameIsMixture) {
ConvertIdentifierNameToWords("_Chapter11Section_1_"));
}
TEST(PointeeOfTest, WorksForSmartPointers) {
EXPECT_TRUE(
(std::is_same<int, PointeeOf<std::unique_ptr<int>>::type>::value));
EXPECT_TRUE(
(std::is_same<std::string,
PointeeOf<std::shared_ptr<std::string>>::type>::value));
}
TEST(PointeeOfTest, WorksForRawPointers) {
EXPECT_TRUE((std::is_same<int, PointeeOf<int*>::type>::value));
EXPECT_TRUE((std::is_same<const char, PointeeOf<const char*>::type>::value));
EXPECT_TRUE((std::is_void<PointeeOf<void*>::type>::value));
}
TEST(GetRawPointerTest, WorksForSmartPointers) {
const char* const raw_p1 = new const char('a'); // NOLINT
const std::unique_ptr<const char> p1(raw_p1);

View File

@@ -3728,6 +3728,65 @@ TEST(PointeeTest, ReferenceToNonConstRawPointer) {
EXPECT_FALSE(m.Matches(p));
}
TEST(PointeeTest, SmartPointer) {
const Matcher<std::unique_ptr<int>> m = Pointee(Ge(0));
std::unique_ptr<int> n(new int(1));
EXPECT_TRUE(m.Matches(n));
}
TEST(PointeeTest, SmartPointerToConst) {
const Matcher<std::unique_ptr<const int>> m = Pointee(Ge(0));
// There's no implicit conversion from unique_ptr<int> to const
// unique_ptr<const int>, so we must pass a unique_ptr<const int> into the
// matcher.
std::unique_ptr<const int> n(new int(1));
EXPECT_TRUE(m.Matches(n));
}
TEST(PointerTest, RawPointer) {
int n = 1;
const Matcher<int*> m = Pointer(Eq(&n));
EXPECT_TRUE(m.Matches(&n));
int* p = nullptr;
EXPECT_FALSE(m.Matches(p));
EXPECT_FALSE(m.Matches(nullptr));
}
TEST(PointerTest, RawPointerToConst) {
int n = 1;
const Matcher<const int*> m = Pointer(Eq(&n));
EXPECT_TRUE(m.Matches(&n));
int* p = nullptr;
EXPECT_FALSE(m.Matches(p));
EXPECT_FALSE(m.Matches(nullptr));
}
TEST(PointerTest, SmartPointer) {
std::unique_ptr<int> n(new int(10));
int* raw_n = n.get();
const Matcher<std::unique_ptr<int>> m = Pointer(Eq(raw_n));
EXPECT_TRUE(m.Matches(n));
}
TEST(PointerTest, SmartPointerToConst) {
std::unique_ptr<const int> n(new int(10));
const int* raw_n = n.get();
const Matcher<std::unique_ptr<const int>> m = Pointer(Eq(raw_n));
// There's no implicit conversion from unique_ptr<int> to const
// unique_ptr<const int>, so we must pass a unique_ptr<const int> into the
// matcher.
std::unique_ptr<const int> p(new int(10));
EXPECT_FALSE(m.Matches(p));
}
MATCHER_P(FieldIIs, inner_matcher, "") {
return ExplainMatchResult(inner_matcher, arg.i, result_listener);
}