forked from cel-expr/cel-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_data_util.h
More file actions
165 lines (136 loc) · 5.56 KB
/
Copy pathtest_data_util.h
File metadata and controls
165 lines (136 loc) · 5.56 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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#ifndef THIRD_PARTY_CEL_CPP_TESTUTIL_TEST_DATA_UTIL_H_
#define THIRD_PARTY_CEL_CPP_TESTUTIL_TEST_DATA_UTIL_H_
#include "google/api/expr/v1beta1/eval.pb.h"
#include "google/api/expr/v1beta1/value.pb.h"
#include "google/protobuf/message.h"
#include "google/protobuf/util/message_differencer.h"
#include "absl/strings/match.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "absl/time/time.h"
#include "absl/types/span.h"
#include "common/type.h"
#include "internal/types.h"
#include "testutil/test_data_util.h"
#include "testdata/test_value.pb.h"
namespace google {
namespace api {
namespace expr {
namespace v1beta1 {
/**
* Initializes a message differencer to support value equivelance checks
* for v1beta1::Value.
*/
void InitValueDifferencer(google::protobuf::util::MessageDifferencer* differencer);
} // namespace v1beta1
namespace testutil {
/**
* Returns the set of all pairs of different test values.
*/
std::vector<std::pair<testdata::TestValue, testdata::TestValue>> AllPairs(
const testdata::TestValues& value_cases);
/** Converts a nullptr to a TestValue. */
testdata::TestValue NewValue(std::nullptr_t value);
/** Converts a bool to a TestValue. */
testdata::TestValue NewValue(bool value);
/** Converts a double to a TestValue. */
testdata::TestValue NewValue(double value, absl::string_view name = "");
/** Converts a int64_t to a TestValue. */
testdata::TestValue NewValue(int64_t value, absl::string_view name = "");
/** Converts a unit64_t to a TestValue. */
testdata::TestValue NewValue(uint64_t value, absl::string_view name = "");
/** Converts a string_view to a TestValue. */
testdata::TestValue NewValue(absl::string_view value,
absl::string_view name = "");
/** Converts a string literal to a TestValue. */
testdata::TestValue NewValue(const char* value, absl::string_view name = "");
/** Converts a nullptr to a TestValue. */
testdata::TestValue NewValue(const google::protobuf::Message& value,
absl::string_view name = "");
/** Converts a duration to a TestValue. */
testdata::TestValue NewValue(absl::Duration value, absl::string_view name = "");
/** Converts a time to a TestValue. */
testdata::TestValue NewValue(absl::Time value, absl::string_view name = "");
/** Converts a string_view of bytes to a TestValue. */
testdata::TestValue NewBytesValue(absl::string_view value,
absl::string_view name = "");
testdata::TestValue NewValue(const expr::common::Type& type);
// Helpers to remove overload ambiguity.
template <typename T>
expr::internal::specialize_ift<expr::internal::is_float<T>, testdata::TestValue>
NewValue(T&& value, absl::string_view name = "") {
return NewValue(static_cast<double>(value), name);
}
template <typename T>
expr::internal::specialize_ift<expr::internal::is_int<T>, testdata::TestValue>
NewValue(T&& value, absl::string_view name = "") {
return NewValue(static_cast<int64_t>(value), name);
}
template <typename T>
expr::internal::specialize_ift<expr::internal::is_uint<T>, testdata::TestValue>
NewValue(T&& value, absl::string_view name = "") {
return NewValue(static_cast<uint64_t>(value), name);
}
namespace internal {
void AppendToList(const testdata::TestValue& test_value,
testdata::TestValue* list_value);
void AppendToList(testdata::TestValue* list_value);
template <typename T, typename... Args>
void AppendToList(testdata::TestValue* list_value, T&& next,
Args&&... remaining) {
AppendToList(NewValue(std::forward<T>(next)), list_value);
AppendToList(list_value, std::forward<Args>(remaining)...);
}
void AddToMap(const testdata::TestValue& key, const testdata::TestValue& value,
testdata::TestValue* map_value);
void AddToMap(testdata::TestValue* map_value);
template <typename K, typename V, typename... Args>
void AddToMap(testdata::TestValue* map_value, K&& next_key, V&& next_value,
Args&&... remaining) {
AddToMap(NewValue(std::forward<K>(next_key)),
NewValue(std::forward<V>(next_value)), map_value);
AddToMap(map_value, std::forward<Args>(remaining)...);
}
void StartList(testdata::TestValue* list_value);
void EndList(testdata::TestValue* list_value);
void StartMap(testdata::TestValue* map_value);
void EndMap(testdata::TestValue* map_value);
} // namespace internal
/**
* Converts arguments into a list TestValue. For example:
*
* TestValue list = NewListValue("elem1", "elem2", 3, 4.0, ...);
*/
template <typename... Args>
testdata::TestValue NewListValue(Args&&... values) {
testdata::TestValue value;
value.set_name("list");
internal::StartList(&value);
internal::AppendToList(&value, std::forward<Args>(values)...);
internal::EndList(&value);
return value;
}
/**
* Converts arguments into a map TestValue. For example:
*
* TestValue map = NewMapValue("key1", "value1", 2, 2.0, ...);
*/
template <typename... Args>
testdata::TestValue NewMapValue(Args&&... values) {
testdata::TestValue value;
value.set_name("map");
internal::StartMap(&value);
internal::AddToMap(&value, std::forward<Args>(values)...);
internal::EndMap(&value);
return value;
}
/** Creates a new test value with the given name. */
testdata::TestValue WithName(testdata::TestValue value, absl::string_view name);
/** Merges to equivalent test values into a single value. */
testdata::TestValue Merge(const absl::Span<const testdata::TestValue>& values,
absl::string_view name = "");
} // namespace testutil
} // namespace expr
} // namespace api
} // namespace google
#endif // THIRD_PARTY_CEL_CPP_TESTUTIL_TEST_DATA_UTIL_H_