• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2019 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 // EqualsProto is only available in google3 version of protobuf for Messages.
16 // This compat-header provides a simple EqualsProto matcher for MessageLite
17 // in the non-google3 version of protobufs, and includes the existing
18 // EqualsProto matcher in the google3 version, based on the build flags.
19 
20 #ifndef ICING_PORTABLE_EQUALS_PROTO_H_
21 #define ICING_PORTABLE_EQUALS_PROTO_H_
22 
23 #include "gmock/gmock.h"          // IWYU pragma: export
24 #include <google/protobuf/message_lite.h>  // IWYU pragma: export
25 
26 #if defined(__ANDROID__) || defined(__APPLE__)
27 namespace icing {
28 namespace lib {
29 namespace portable_equals_proto {
30 // We need this matcher because MessageLite does not support reflection.
31 // Hence, there is no better way to compare two protos of an arbitrary type.
32 // This matcher enables comparing non-google3 protos on, e.g., Android, with
33 // a known caveat that it is unable to provide detailed difference information.
34 MATCHER_P(EqualsProto, other, "Compare MessageLite by serialized string") {
35   return ::testing::ExplainMatchResult(::testing::Eq(other.SerializeAsString()),
36                                        arg.SerializeAsString(),
37                                        result_listener);
38 }  // MATCHER_P
39 }  // namespace portable_equals_proto
40 }  // namespace lib
41 }  // namespace icing
42 #else
43 namespace icing {
44 namespace lib {
45 namespace portable_equals_proto {
46 // Leverage the powerful google3 matcher when available, for human readable
47 // differences.
48 using ::testing::EqualsProto;
49 }  // namespace portable_equals_proto
50 }  // namespace lib
51 }  // namespace icing
52 #endif  // defined(__ANDROID__) || defined(__APPLE__)
53 
54 #endif  // ICING_PORTABLE_EQUALS_PROTO_H_
55