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 namespace icing { 27 namespace lib { 28 namespace portable_equals_proto { 29 // We need this matcher because MessageLite does not support reflection. 30 // Hence, there is no better way to compare two protos of an arbitrary type. 31 // This matcher enables comparing non-google3 protos on, e.g., Android, with 32 // a known caveat that it is unable to provide detailed difference information. 33 #if defined(__ANDROID__) || defined(__APPLE__) 34 35 MATCHER_P(EqualsProto, other, "Compare MessageLite by serialized string") { 36 return ::testing::ExplainMatchResult(::testing::Eq(other.SerializeAsString()), 37 arg.SerializeAsString(), 38 result_listener); 39 } 40 41 MATCHER(EqualsProto, "") { 42 return ::testing::ExplainMatchResult(EqualsProto(std::get<1>(arg)), 43 std::get<0>(arg), result_listener); 44 } 45 46 #else 47 48 // Leverage the powerful google3 matcher when available, for human readable 49 // differences. 50 using ::testing::EqualsProto; 51 52 #endif // defined(__ANDROID__) || defined(__APPLE__) 53 54 } // namespace portable_equals_proto 55 } // namespace lib 56 } // namespace icing 57 58 #endif // ICING_PORTABLE_EQUALS_PROTO_H_ 59