diff options
author | Abseil Team <absl-team@google.com> | 2020-12-07 21:17:26 (GMT) |
---|---|---|
committer | Andy Getz <durandal@google.com> | 2020-12-07 23:04:12 (GMT) |
commit | a02a591605dfef9addde49634bf010dbe8f95c50 (patch) | |
tree | 6258f1a86109731e61e328b5eac6cdce89146f85 /googlemock/test | |
parent | 7bf5057a04eebb12cb731bfd6b3a19c510dc5087 (diff) | |
download | googletest-a02a591605dfef9addde49634bf010dbe8f95c50.zip googletest-a02a591605dfef9addde49634bf010dbe8f95c50.tar.gz googletest-a02a591605dfef9addde49634bf010dbe8f95c50.tar.bz2 |
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
Diffstat (limited to 'googlemock/test')
-rw-r--r-- | googlemock/test/gmock-internal-utils_test.cc | 14 | ||||
-rw-r--r-- | googlemock/test/gmock-matchers_test.cc | 59 |
2 files changed, 59 insertions, 14 deletions
diff --git a/googlemock/test/gmock-internal-utils_test.cc b/googlemock/test/gmock-internal-utils_test.cc index 8019f4a..0d15e8f 100644 --- a/googlemock/test/gmock-internal-utils_test.cc +++ b/googlemock/test/gmock-internal-utils_test.cc @@ -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); diff --git a/googlemock/test/gmock-matchers_test.cc b/googlemock/test/gmock-matchers_test.cc index e692319..8084e29 100644 --- a/googlemock/test/gmock-matchers_test.cc +++ b/googlemock/test/gmock-matchers_test.cc @@ -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); } |