Google OR-Tools v9.15
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
proto_matcher.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2# Copyright 2010-2025 Google LLC
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"""A matcher that tests if protos meet MathOpts definition of equivalence.
16
17This is designed to be used with unittest.mock, which is canonical for mocking
18in Google Python (e.g., see stubby codelabs).
19
20See normalize.py for technical definitions.
21
22The matcher can be used as a replacement for
23google3.net.proto2.contrib.pyutil.matcher.Proto2Matcher
24but supports a much smaller set of features.
25"""
26
27import copy
28
29from google.protobuf import message
30from ortools.math_opt.python import normalize
31
32
34 """Matcher that checks if protos are equivalent in the MathOpt sense.
35
36 See normalize.py for technical definitions.
37 """
38
39 def __init__(self, expected: message.Message):
40 self._expected = copy.deepcopy(expected)
41 normalize.math_opt_normalize_proto(self._expected)
42
43 def __eq__(self, actual: message.Message) -> bool:
44 actual = copy.deepcopy(actual)
45 normalize.math_opt_normalize_proto(actual)
46 return str(actual) == str(self._expected)
47
48 def __ne__(self, other: message.Message) -> bool:
49 return not self == other