File: proto_matchers.h

package info (click to toggle)
chromium 140.0.7339.127-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,192,880 kB
  • sloc: cpp: 35,093,808; ansic: 7,161,670; javascript: 4,199,694; python: 1,441,797; asm: 949,904; xml: 747,503; pascal: 187,748; perl: 88,691; sh: 88,248; objc: 79,953; sql: 52,714; cs: 44,599; fortran: 24,137; makefile: 22,114; tcl: 15,277; php: 13,980; yacc: 9,000; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (151 lines) | stat: -rw-r--r-- 5,444 bytes parent folder | download | duplicates (3)
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_