Skip to content

Commit 00be9c6

Browse files
authored
Merge pull request #130 from google/cpp-sync
OSS Export
2 parents 4b70f64 + db9955d commit 00be9c6

139 files changed

Lines changed: 5384 additions & 6727 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

base/BUILD

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,21 @@
11
licenses(["notice"]) # Apache v2.0
22

3-
package(
4-
default_visibility = ["//visibility:public"],
3+
package(default_visibility = ["//visibility:public"])
4+
5+
cc_library(
6+
name = "testing",
7+
testonly = True,
8+
srcs = [
9+
"testing.cc",
10+
],
11+
hdrs = [
12+
"testing.h",
13+
],
14+
deps = [
15+
"@com_google_absl//absl/status",
16+
"@com_google_absl//absl/status:statusor",
17+
"@com_google_googletest//:gtest_main",
18+
],
519
)
620

721
cc_library(

base/status_macros.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,27 @@
3434
} while (false)
3535
#endif
3636

37-
#if !defined(ASSIGN_OR_RETURN)
3837
#define CEL_CONCAT_(x, y) x##y
3938
#define CEL_CONCAT(x, y) CEL_CONCAT_(x, y)
39+
40+
#if !defined(ASSIGN_OR_RETURN)
4041
#define ASSIGN_OR_RETURN(lhs, rexpr) \
4142
auto CEL_CONCAT(_statusor, __LINE__) = \
4243
static_cast<decltype(rexpr)&&>(rexpr); \
4344
RETURN_IF_ERROR(CEL_CONCAT(_statusor, __LINE__).status()); \
4445
lhs = std::move(CEL_CONCAT(_statusor, __LINE__).value());
4546
#endif
4647

48+
#if !defined(ASSERT_OK_AND_ASSIGN)
49+
#define ASSERT_OK_AND_ASSIGN(lhs, rexpr) \
50+
ASSERT_OK_AND_ASSIGN_IMPL(CEL_CONCAT(_statusor, __LINE__), lhs, rexpr)
51+
52+
#define ASSERT_OK_AND_ASSIGN_IMPL(statusor, lhs, rexpr) \
53+
auto statusor = (rexpr); \
54+
ASSERT_TRUE(statusor.status().ok()) << statusor.status(); \
55+
lhs = std::move(statusor.value())
56+
#endif
57+
4758
template <typename To, typename From> // use like this: down_cast<T*>(foo);
4859
inline To down_cast(From* f) { // so we only accept pointers
4960
static_assert(

base/testing.cc

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include "base/testing.h"
2+
3+
namespace cel_base {
4+
namespace testing {
5+
6+
void StatusIsMatcherCommonImpl::DescribeTo(std::ostream* os) const {
7+
*os << ", has a status code that ";
8+
code_matcher_.DescribeTo(os);
9+
*os << ", and has an error message that ";
10+
message_matcher_.DescribeTo(os);
11+
}
12+
13+
void StatusIsMatcherCommonImpl::DescribeNegationTo(std::ostream* os) const {
14+
*os << ", or has a status code that ";
15+
code_matcher_.DescribeNegationTo(os);
16+
*os << ", or has an error message that ";
17+
message_matcher_.DescribeNegationTo(os);
18+
}
19+
20+
bool StatusIsMatcherCommonImpl::MatchAndExplain(
21+
const absl::Status& status,
22+
::testing::MatchResultListener* result_listener) const {
23+
::testing::StringMatchResultListener inner_listener;
24+
25+
inner_listener.Clear();
26+
if (!code_matcher_.MatchAndExplain(status.code(), &inner_listener)) {
27+
*result_listener << (inner_listener.str().empty()
28+
? "whose status code is wrong"
29+
: "which has a status code " +
30+
inner_listener.str());
31+
return false;
32+
}
33+
34+
if (!message_matcher_.Matches(std::string(status.message()))) {
35+
*result_listener << "whose error message is wrong";
36+
return false;
37+
}
38+
39+
return true;
40+
}
41+
42+
} // namespace testing
43+
} // namespace cel_base

base/testing.h

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
/*
2+
* Copyright 2021 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef THIRD_PARTY_CEL_CPP_BASE_TESTING_H_
18+
#define THIRD_PARTY_CEL_CPP_BASE_TESTING_H_
19+
20+
#include <ostream>
21+
#include <string>
22+
#include <type_traits>
23+
24+
#include "gmock/gmock.h" // IWYU pragma: export
25+
#include "gtest/gtest.h"
26+
#include "absl/status/status.h"
27+
#include "absl/status/statusor.h"
28+
29+
namespace cel_base {
30+
namespace testing {
31+
32+
inline const absl::Status& GetStatus(const absl::Status& status) {
33+
return status;
34+
}
35+
36+
template <typename T>
37+
inline const absl::Status& GetStatus(const absl::StatusOr<T>& status) {
38+
return status.status();
39+
}
40+
41+
// StatusIs() is a polymorphic matcher. This class is the common
42+
// implementation of it shared by all types T where StatusIs() can be
43+
// used as a Matcher<T>.
44+
class StatusIsMatcherCommonImpl {
45+
public:
46+
StatusIsMatcherCommonImpl(
47+
::testing::Matcher<absl::StatusCode> code_matcher,
48+
::testing::Matcher<const std::string&> message_matcher)
49+
: code_matcher_(std::move(code_matcher)),
50+
message_matcher_(std::move(message_matcher)) {}
51+
52+
void DescribeTo(std::ostream* os) const;
53+
54+
void DescribeNegationTo(std::ostream* os) const;
55+
56+
bool MatchAndExplain(const absl::Status& status,
57+
::testing::MatchResultListener* result_listener) const;
58+
59+
private:
60+
const ::testing::Matcher<absl::StatusCode> code_matcher_;
61+
const ::testing::Matcher<const std::string&> message_matcher_;
62+
};
63+
64+
// Monomorphic implementation of matcher StatusIs() for a given type
65+
// T. T can be Status, StatusOr<>, or a reference to either of them.
66+
template <typename T>
67+
class MonoStatusIsMatcherImpl : public ::testing::MatcherInterface<T> {
68+
public:
69+
explicit MonoStatusIsMatcherImpl(StatusIsMatcherCommonImpl common_impl)
70+
: common_impl_(std::move(common_impl)) {}
71+
72+
void DescribeTo(std::ostream* os) const override {
73+
common_impl_.DescribeTo(os);
74+
}
75+
76+
void DescribeNegationTo(std::ostream* os) const override {
77+
common_impl_.DescribeNegationTo(os);
78+
}
79+
80+
bool MatchAndExplain(
81+
T actual_value,
82+
::testing::MatchResultListener* result_listener) const override {
83+
return common_impl_.MatchAndExplain(GetStatus(actual_value),
84+
result_listener);
85+
}
86+
87+
private:
88+
StatusIsMatcherCommonImpl common_impl_;
89+
};
90+
91+
// Implements StatusIs() as a polymorphic matcher.
92+
class StatusIsMatcher {
93+
public:
94+
StatusIsMatcher(::testing::Matcher<absl::StatusCode> code_matcher,
95+
::testing::Matcher<const std::string&> message_matcher)
96+
: common_impl_(std::move(code_matcher), std::move(message_matcher)) {}
97+
98+
// Converts this polymorphic matcher to a monomorphic matcher of the given
99+
// type. T can be StatusOr<>, Status, or a reference to either of them.
100+
template <typename T>
101+
operator ::testing::Matcher<T>() const { // NOLINT
102+
return ::testing::MakeMatcher(new MonoStatusIsMatcherImpl<T>(common_impl_));
103+
}
104+
105+
private:
106+
const StatusIsMatcherCommonImpl common_impl_;
107+
};
108+
109+
// Monomorphic implementation of matcher IsOk() for a given type T.
110+
// T can be Status, StatusOr<>, or a reference to either of them.
111+
template <typename T>
112+
class MonoIsOkMatcherImpl : public ::testing::MatcherInterface<T> {
113+
public:
114+
void DescribeTo(std::ostream* os) const override { *os << "is OK"; }
115+
void DescribeNegationTo(std::ostream* os) const override {
116+
*os << "is not OK";
117+
}
118+
bool MatchAndExplain(T actual_value,
119+
::testing::MatchResultListener*) const override {
120+
return GetStatus(actual_value).ok();
121+
}
122+
};
123+
124+
// Implements IsOk() as a polymorphic matcher.
125+
class IsOkMatcher {
126+
public:
127+
template <typename T>
128+
operator ::testing::Matcher<T>() const { // NOLINT
129+
return ::testing::MakeMatcher(new MonoIsOkMatcherImpl<T>());
130+
}
131+
};
132+
133+
// Returns a gMock matcher that matches a Status or StatusOr<> whose status code
134+
// matches code_matcher, and whose error message matches message_matcher.
135+
template <typename StatusCodeMatcher>
136+
StatusIsMatcher StatusIs(
137+
StatusCodeMatcher&& code_matcher,
138+
::testing::Matcher<const std::string&> message_matcher) {
139+
return StatusIsMatcher(std::forward<StatusCodeMatcher>(code_matcher),
140+
std::move(message_matcher));
141+
}
142+
143+
// Returns a gMock matcher that matches a Status or StatusOr<> whose status code
144+
// matches code_matcher.
145+
template <typename StatusCodeMatcher>
146+
StatusIsMatcher StatusIs(StatusCodeMatcher&& code_matcher) {
147+
return StatusIs(std::forward<StatusCodeMatcher>(code_matcher), ::testing::_);
148+
}
149+
150+
// Returns a gMock matcher that matches a Status or StatusOr<> which is OK.
151+
inline IsOkMatcher IsOk() { return IsOkMatcher(); }
152+
153+
} // namespace testing
154+
} // namespace cel_base
155+
156+
#endif // THIRD_PARTY_CEL_CPP_BASE_TESTING_H_

base/unilib.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace UniLib {
66

77
// Detects whether a string is valid UTF-8.
88
bool IsStructurallyValid(absl::string_view str) {
9-
if (str.size() == 0) {
9+
if (str.empty()) {
1010
return true;
1111
}
1212
const char *s = &str[0];

bazel/deps.bzl

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,17 @@ def base_deps():
3232
urls = ["https://github.com/google/re2/archive/main.zip"],
3333
)
3434

35-
PROTOBUF_VERSION = "3.14.0"
35+
PROTOBUF_VERSION = "3.18.0"
36+
PROTOBUF_SHA = "14e8042b5da37652c92ef6a2759e7d2979d295f60afd7767825e3de68c856c54"
3637
http_archive(
3738
name = "com_google_protobuf",
39+
sha256 = PROTOBUF_SHA,
3840
strip_prefix = "protobuf-" + PROTOBUF_VERSION,
3941
urls = ["https://github.com/protocolbuffers/protobuf/archive/v" + PROTOBUF_VERSION + ".tar.gz"],
4042
)
4143

42-
GOOGLEAPIS_GIT_SHA = "be480e391cc88a75cf2a81960ef79c80d5012068" # Jul 24, 2019
43-
GOOGLEAPIS_SHA = "c1969e5b72eab6d9b6cfcff748e45ba57294aeea1d96fd04cd081995de0605c2"
44+
GOOGLEAPIS_GIT_SHA = "77066268d1fd5d72278afc2aef1ebc1d2112cca6" # Oct 01, 2021
45+
GOOGLEAPIS_SHA = "dca75efd11a6295618dba919ad52fe551ba8bb85778d331a38c2bca282234296"
4446
http_archive(
4547
name = "com_google_googleapis",
4648
sha256 = GOOGLEAPIS_SHA,
@@ -103,7 +105,7 @@ def cel_spec_deps():
103105
],
104106
)
105107

106-
CEL_SPEC_GIT_SHA = "95fe21a64063d63482a4b1b3159c07b5b7b64d77" # 11/23/2020
108+
CEL_SPEC_GIT_SHA = "c9ae91b24fdaf869d7c59a9f64863249a6a2905e" # 9/22/2021
107109
http_archive(
108110
name = "com_google_cel_spec",
109111
strip_prefix = "cel-spec-" + CEL_SPEC_GIT_SHA,

common/BUILD

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ cc_test(
147147
srcs = ["escaping_test.cc"],
148148
deps = [
149149
":escaping",
150-
"@com_google_googletest//:gtest_main",
150+
"//base:testing",
151151
],
152152
)
153153

@@ -192,6 +192,7 @@ cc_test(
192192
deps = [
193193
":custom_object",
194194
":value",
195+
"//base:testing",
195196
"//internal:status_util",
196197
"//internal:types",
197198
"//internal:value_internal",
@@ -200,7 +201,6 @@ cc_test(
200201
"@com_google_absl//absl/strings",
201202
"@com_google_googleapis//google/rpc:status_cc_proto",
202203
"@com_google_googleapis//google/type:money_cc_proto",
203-
"@com_google_googletest//:gtest_main",
204204
"@com_google_protobuf//:protobuf",
205205
],
206206
)
@@ -223,13 +223,38 @@ cc_library(
223223
],
224224
)
225225

226+
cc_library(
227+
name = "overflow",
228+
srcs = ["overflow.cc"],
229+
hdrs = ["overflow.h"],
230+
deps = [
231+
"//base:status_macros",
232+
"@com_google_absl//absl/status",
233+
"@com_google_absl//absl/status:statusor",
234+
"@com_google_absl//absl/strings",
235+
"@com_google_absl//absl/time",
236+
],
237+
)
238+
226239
cc_test(
227240
name = "converters_test",
228241
srcs = ["converters_test.cc"],
229242
deps = [
230243
":converters",
231244
":value",
245+
"//base:testing",
232246
"@com_google_absl//absl/memory",
233-
"@com_google_googletest//:gtest_main",
247+
],
248+
)
249+
250+
cc_test(
251+
name = "overflow_test",
252+
srcs = ["overflow_test.cc"],
253+
deps = [
254+
":overflow",
255+
"//base:testing",
256+
"@com_google_absl//absl/functional:function_ref",
257+
"@com_google_absl//absl/status",
258+
"@com_google_absl//absl/time",
234259
],
235260
)

common/escaping_test.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "common/escaping.h"
22

3-
#include "gmock/gmock.h"
3+
#include "base/testing.h"
44
#include "gtest/gtest.h"
55

66
namespace google {

0 commit comments

Comments
 (0)