Commit cdb0a10d authored by Joe Mason's avatar Joe Mason Committed by Commit Bot

Support gmock matchers in mojo StructPtr

gmock's Pointee matcher dereferences raw or smart pointers. It depends
on smart pointers defining element_type to recognize what type they
hold. This patch adds element_type to StructPtr and InlineStructPtr so
gmock will recognize them as smart pointers.

R=rockot

Change-Id: I06333a31c0a958fd24784e1ee2244d0232267bba
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2403583Reviewed-by: default avatarKen Rockot <rockot@google.com>
Commit-Queue: Joe Mason <joenotcharles@chromium.org>
Cr-Commit-Position: refs/heads/master@{#805915}
parent b3a62810
......@@ -35,6 +35,10 @@ class StructPtr {
public:
using Struct = S;
// Exposing StructPtr<S>::element_type allows gmock's Pointee matcher to
// dereference StructPtr's.
using element_type = S;
StructPtr() = default;
StructPtr(std::nullptr_t) {}
......@@ -122,6 +126,10 @@ class InlinedStructPtr {
public:
using Struct = S;
// Exposing InlinedStructPtr<S>::element_type allows gmock's Pointee matcher
// to dereference InlinedStructPtr's.
using element_type = S;
InlinedStructPtr() = default;
InlinedStructPtr(std::nullptr_t) {}
......
......@@ -11,6 +11,7 @@
#include "mojo/public/cpp/system/message_pipe.h"
#include "mojo/public/interfaces/bindings/tests/test_export2.mojom.h"
#include "mojo/public/interfaces/bindings/tests/test_structs.mojom.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace mojo {
......@@ -542,5 +543,24 @@ TEST_F(StructTest, EnumNestedStructTest) {
EXPECT_EQ(EnumNestedStruct::StructEnum::SECOND, output->local_enum_state_);
}
TEST_F(StructTest, Matcher) {
// Pointee will dereference StructPtr<S> or InlinedStructPtr<S> to get S.
// This test ensures that the matcher compiles and works.
using ::testing::Pointee;
using ::testing::AllOf;
using ::testing::Field;
mojo::InlinedStructPtr<Rect> rect = MakeRect();
EXPECT_THAT(
rect, Pointee(AllOf(Field(&Rect::x, 1), Field(&Rect::y, 2),
Field(&Rect::width, 10), Field(&Rect::height, 20))));
mojo::StructPtr<MultiVersionStruct> multi = MakeMultiVersionStruct();
EXPECT_THAT(multi, Pointee(AllOf(Field(&MultiVersionStruct::f_int32, 123),
Field(&MultiVersionStruct::f_rect,
Pointee(Field(&Rect::x, 5))))));
}
} // namespace test
} // namespace mojo
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment