forked from cel-expr/cel-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.h
More file actions
102 lines (83 loc) · 3.22 KB
/
Copy pathutil.h
File metadata and controls
102 lines (83 loc) · 3.22 KB
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
#ifndef THIRD_PARTY_CEL_CPP_TESTUTIL_EXPECT_SAME_TYPE_H_
#define THIRD_PARTY_CEL_CPP_TESTUTIL_EXPECT_SAME_TYPE_H_
#include <string>
#include "google/protobuf/message.h"
#include "google/protobuf/text_format.h"
#include "gmock/gmock.h"
namespace google {
namespace api {
namespace expr {
namespace testutil {
// A helper class that causes the compiler to print a helpful error when
// they template args don't match.
template <typename E, typename A>
struct ExpectSameType;
template <typename T>
struct ExpectSameType<T, T> {};
// Creates a proto message of type T from a textual representation.
template <typename T>
T CreateProto(const std::string& textual_proto);
/**
* Simple implementation of a proto matcher comparing string representations.
*
* IMPORTANT: Only use this for protos whose textual representation is
* deterministic (that may not be the case for the map collection type).
*/
class ProtoStringMatcher {
public:
explicit inline ProtoStringMatcher(const std::string& expected)
: expected_(expected) {}
explicit inline ProtoStringMatcher(const google::protobuf::Message& expected)
: expected_(expected.DebugString()) {}
template <typename Message>
bool MatchAndExplain(const Message& p,
::testing::MatchResultListener* /* listener */) const;
template <typename Message>
bool MatchAndExplain(const Message* p,
::testing::MatchResultListener* /* listener */) const;
inline void DescribeTo(::std::ostream* os) const { *os << expected_; }
inline void DescribeNegationTo(::std::ostream* os) const {
*os << "not equal to expected message: " << expected_;
}
private:
const std::string expected_;
};
// Polymorphic matcher to compare any two protos.
inline ::testing::PolymorphicMatcher<ProtoStringMatcher> EqualsProto(
const std::string& x) {
return ::testing::MakePolymorphicMatcher(ProtoStringMatcher(x));
}
// Polymorphic matcher to compare any two protos.
inline ::testing::PolymorphicMatcher<ProtoStringMatcher> EqualsProto(
const google::protobuf::Message& x) {
return ::testing::MakePolymorphicMatcher(ProtoStringMatcher(x));
}
template <typename T>
T CreateProto(const std::string& textual_proto) {
T proto;
google::protobuf::TextFormat::ParseFromString(textual_proto, &proto);
return proto;
}
template <typename Message>
bool ProtoStringMatcher::MatchAndExplain(
const Message& p, ::testing::MatchResultListener* /* listener */) const {
// Need to CreateProto and then print as std::string so that the formatting
// matches exactly.
return p.SerializeAsString() ==
CreateProto<Message>(expected_).SerializeAsString();
}
template <typename Message>
bool ProtoStringMatcher::MatchAndExplain(
const Message* p, ::testing::MatchResultListener* /* listener */) const {
// Need to CreateProto and then print as std::string so that the formatting
// matches exactly.
std::unique_ptr<google::protobuf::Message> value;
value.reset(p->New());
google::protobuf::TextFormat::ParseFromString(expected_, value.get());
return p->SerializeAsString() == value->SerializeAsString();
}
} // namespace testutil
} // namespace expr
} // namespace api
} // namespace google
#endif // THIRD_PARTY_CEL_CPP_TESTUTIL_EXPECT_SAME_TYPE_H_