1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
|
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef COMPONENTS_PROTO_EXTRAS_PROTO_MATCHERS_H_
#define COMPONENTS_PROTO_EXTRAS_PROTO_MATCHERS_H_
#include <type_traits>
#include "base/containers/flat_map.h"
#include "base/strings/strcat.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "third_party/protobuf/src/google/protobuf/repeated_ptr_field.h"
namespace google::protobuf {
template <typename T>
class RepeatedField;
template <typename Key, typename T>
class Map;
} // namespace google::protobuf
namespace proto_extras {
// Used to force the compiler to resolve the correct overload of a non-ptr
// repeated field accessor.
template <typename MessageType, typename FieldType>
constexpr auto ResolveRepeatedField(
const ::google::protobuf::RepeatedField<FieldType>& (
MessageType::*func_ptr)() const) {
return func_ptr;
}
// Used to force the compiler to resolve the correct overload of a non-ptr
// repeated field accessor.
template <typename MessageType, typename FieldType>
constexpr auto ResolveRepeatedPtrField(
const ::google::protobuf::RepeatedPtrField<FieldType>& (
MessageType::*func_ptr)() const) {
return func_ptr;
}
MATCHER_P5(HasOptionalField,
property_name,
has_field_function,
field_function,
expected_has_field,
expected_field_value_matcher,
"") {
bool arg_has_field = (arg.*has_field_function)();
if (arg_has_field != expected_has_field) {
*result_listener << "is an object whose property `has_" << property_name
<< "` is `" << arg_has_field << "` but expected `"
<< expected_has_field << "` ";
return false;
}
if (!arg_has_field) {
return true;
}
return testing::ExplainMatchResult(
testing::Property(property_name, field_function,
expected_field_value_matcher),
arg, result_listener);
}
// Note: This could be improved to take a vector of matchers, and there would
// need to be a helper method to transform the repeated field list and matcher
// into a vector of applicable matchers.
MATCHER_P3(HasRepeatedField,
property_name,
field_function,
expected_proto_message,
"") {
const auto& expected_field_list = (expected_proto_message.*field_function)();
bool result = testing::ExplainMatchResult(
testing::Property(property_name, field_function,
testing::ElementsAreArray(expected_field_list)),
arg, result_listener);
return result;
}
// Note: This could be improved to take a vector of matchers, and there would
// need to be a helper method to transform the repeated field list and matcher
// into a vector of applicable matchers.
MATCHER_P4(HasRepeatedField,
property_name,
field_function,
expected_proto_message,
item_matcher_function,
"") {
// Create a matcher per item in the expected list.
const auto& expected_field_list = (expected_proto_message.*field_function)();
using ItemType =
typename std::decay_t<decltype(expected_field_list)>::value_type;
std::vector<testing::Matcher<ItemType>> matchers;
matchers.reserve(expected_field_list.size());
for (const auto& item : expected_field_list) {
matchers.push_back((*item_matcher_function)(item));
}
bool result = testing::ExplainMatchResult(
testing::Property(property_name, field_function,
testing::ElementsAreArray(matchers)),
arg, result_listener);
return result;
}
MATCHER_P3(HasMapField,
property_name,
field_function,
expected_proto_message,
"") {
const auto& expected_field_map = (expected_proto_message.*field_function)();
return testing::ExplainMatchResult(
testing::Property(property_name, field_function,
testing::UnorderedElementsAreArray(expected_field_map)),
arg, result_listener);
}
// Note: This could be improved to take a vector of matchers, and there would
// need to be a helper method to transform a field and matcher into a vector of
// applicable matchers.
MATCHER_P4(HasMapField,
property_name,
field_function,
expected_proto_message,
value_matcher_function,
"") {
const auto& expected_field_map = (expected_proto_message.*field_function)();
// Correctly deduce KeyType and ValueType from the map's value_type.
using MapValueType =
typename std::decay_t<decltype(expected_field_map)>::value_type;
using KeyType = typename MapValueType::first_type;
using ValueType = typename MapValueType::second_type;
// Create a vector of matchers for the expected map entries.
std::vector<testing::Matcher<std::pair<const KeyType, ValueType>>> matchers;
matchers.reserve(expected_field_map.size());
for (const auto& item : expected_field_map) {
matchers.push_back(testing::Pair(testing::Eq(item.first),
(*value_matcher_function)(item.second)));
}
return testing::ExplainMatchResult(
testing::Property(property_name, field_function,
testing::UnorderedElementsAreArray(matchers)),
arg, result_listener);
}
} // namespace proto_extras
#endif // COMPONENTS_PROTO_EXTRAS_PROTO_MATCHERS_H_
|