Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
message_matchers.h
Go to the documentation of this file.
1// Copyright 2010-2024 Google LLC
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14#ifndef OR_TOOLS_BASE_MESSAGE_MATCHERS_H_
15#define OR_TOOLS_BASE_MESSAGE_MATCHERS_H_
16
17#include <memory>
18
19#include "absl/strings/string_view.h"
20#include "gmock/gmock-matchers.h"
21#include "gmock/gmock.h"
22#include "google/protobuf/message.h"
23#include "google/protobuf/util/message_differencer.h"
24
25namespace testing {
26namespace internal {
27// Utilities.
28// How to compare two fields (equal vs. equivalent).
29typedef ::google::protobuf::util::MessageDifferencer::MessageFieldComparison
31
32// How to compare two floating-points (exact vs. approximate).
33typedef ::google::protobuf::util::DefaultFieldComparator::FloatComparison
35
36// How to compare repeated fields (whether the order of elements matters).
37typedef ::google::protobuf::util::MessageDifferencer::RepeatedFieldComparison
39
40// Whether to compare all fields (full) or only fields present in the
41// expected protobuf (partial).
42typedef ::google::protobuf::util::MessageDifferencer::Scope
44
46 ::google::protobuf::util::MessageDifferencer::EQUAL;
48 ::google::protobuf::util::MessageDifferencer::EQUIVALENT;
50 ::google::protobuf::util::DefaultFieldComparator::EXACT;
52 ::google::protobuf::util::DefaultFieldComparator::APPROXIMATE;
54 ::google::protobuf::util::MessageDifferencer::AS_LIST;
56 ::google::protobuf::util::MessageDifferencer::AS_SET;
58 ::google::protobuf::util::MessageDifferencer::FULL;
60 ::google::protobuf::util::MessageDifferencer::PARTIAL;
61
62// Options for comparing two protobufs.
94
95// Whether the protobuf must be initialized.
96const bool kMustBeInitialized = true;
97const bool kMayBeUninitialized = false;
98
100 public:
101 using is_gtest_matcher = void;
102 using MessageType = ::google::protobuf::Message;
103
105 : message_(CloneMessage(message)) {}
106
108 : message_(CloneMessage(message)) {}
109
111 testing::MatchResultListener*) const {
112 return EqualsMessage(*message_, m);
113 }
115 testing::MatchResultListener*) const {
116 return EqualsMessage(*message_, *m);
117 }
118
119 void DescribeTo(std::ostream* os) const {
120 *os << "has the same serialization as " << ExpectedMessageDescription();
121 }
122
123 void DescribeNegationTo(std::ostream* os) const {
124 *os << "does not have the same serialization as "
125 << ExpectedMessageDescription();
126 }
127
128 private:
129 std::unique_ptr<MessageType> CloneMessage(const MessageType& message) {
130 std::unique_ptr<MessageType> clone(message.New());
131 clone->CheckTypeAndMergeFrom(message);
132 return clone;
133 }
134
135 bool EqualsMessage(const ::google::protobuf::Message& m_1,
136 const ::google::protobuf::Message& m_2) const {
137 std::string s_1, s_2;
138 m_1.SerializeToString(&s_1);
139 m_2.SerializeToString(&s_2);
140 return s_1 == s_2;
141 }
142
143 std::string ExpectedMessageDescription() const {
144 return message_->DebugString();
145 }
146
147 const std::shared_ptr<MessageType> message_;
148};
149
150using PolymorphicProtoMatcher = PolymorphicMatcher<ProtoMatcher>;
151} // namespace internal
152
154 const ::google::protobuf::Message& message) {
156}
157
158// for Pointwise
160 const auto& a = ::testing::get<0>(arg);
161 const auto& b = ::testing::get<1>(arg);
162 return ::testing::ExplainMatchResult(EqualsProto(b), a, result_listener);
163}
164
165// Constructs a matcher that matches the argument if
166// argument.Equivalent(x) or argument->Equivalent(x) returns true.
168 const ::google::protobuf::Message& x) {
171 return MakePolymorphicMatcher(
173}
174
175} // namespace testing
176
177#endif // OR_TOOLS_BASE_MESSAGE_MATCHERS_H_
ProtoMatcher(const MessageType &message, bool, ProtoComparison &)
bool MatchAndExplain(const MessageType *m, testing::MatchResultListener *) const
void DescribeNegationTo(std::ostream *os) const
ProtoMatcher(const MessageType &message)
bool MatchAndExplain(const MessageType &m, testing::MatchResultListener *) const
::google::protobuf::Message MessageType
void DescribeTo(std::ostream *os) const
int64_t b
Definition table.cc:45
int64_t a
Definition table.cc:44
::google::protobuf::util::MessageDifferencer::Scope ProtoComparisonScope
const ProtoFieldComparison kProtoEqual
const ProtoFieldComparison kProtoEquiv
::google::protobuf::util::DefaultFieldComparator::FloatComparison ProtoFloatComparison
How to compare two floating-points (exact vs. approximate).
const ProtoComparisonScope kProtoPartial
const ProtoFloatComparison kProtoExact
PolymorphicMatcher< ProtoMatcher > PolymorphicProtoMatcher
::google::protobuf::util::MessageDifferencer::MessageFieldComparison ProtoFieldComparison
const RepeatedFieldComparison kProtoCompareRepeatedFieldsIgnoringOrdering
::google::protobuf::util::MessageDifferencer::RepeatedFieldComparison RepeatedFieldComparison
How to compare repeated fields (whether the order of elements matters).
const bool kMayBeUninitialized
const ProtoComparisonScope kProtoFull
const RepeatedFieldComparison kProtoCompareRepeatedFieldsRespectOrdering
const ProtoFloatComparison kProtoApproximate
const bool kMustBeInitialized
Whether the protobuf must be initialized.
internal::ProtoMatcher EqualsProto(const ::google::protobuf::Message &message)
internal::PolymorphicProtoMatcher EquivToProto(const ::google::protobuf::Message &x)
MATCHER(EqualsProto, "")
for Pointwise
const Variable x
Definition qp_tests.cc:127
Options for comparing two protobufs.
std::vector< std::string > unordered_fields
RepeatedFieldComparison repeated_field_comp
std::vector< std::string > ignore_fields
std::vector< std::string > ignore_field_paths
std::string message
Definition trace.cc:397