Skip to content

Commit db9955d

Browse files
committed
OSS export.
PiperOrigin-RevId: 400433800
1 parent fee5212 commit db9955d

9 files changed

Lines changed: 242 additions & 9 deletions

base/BUILD

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

33
package(default_visibility = ["//visibility:public"])
44

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+
],
19+
)
20+
521
cc_library(
622
name = "status_macros",
723
hdrs = [

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_

bazel/deps.bzl

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,21 @@ def base_deps():
2828

2929
http_archive(
3030
name = "com_googlesource_code_re2",
31-
strip_prefix = "re2-master",
32-
urls = ["https://github.com/google/re2/archive/master.zip"],
31+
strip_prefix = "re2-main",
32+
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,

eval/compiler/flat_expr_builder_comprehensions_test.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ namespace runtime {
2525

2626
namespace {
2727

28+
using google::api::expr::v1alpha1::CheckedExpr;
2829
using google::api::expr::v1alpha1::Expr;
2930
using google::api::expr::v1alpha1::SourceInfo;
3031
using testing::HasSubstr;

eval/eval/container_access_step_test.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ TEST_F(ContainerAccessStepTest, TestInvalidContainerType) {
312312
ASSERT_TRUE(result.IsError());
313313
EXPECT_THAT(*result.ErrorOrDie(),
314314
StatusIs(absl::StatusCode::kInvalidArgument,
315-
HasSubstr("Invalid container type: 'int64_t'")));
315+
HasSubstr("Invalid container type: 'int64")));
316316
}
317317

318318
INSTANTIATE_TEST_SUITE_P(CombinedContainerTest,

eval/public/builtin_func_registrar_test.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
namespace google::api::expr::runtime {
2626
namespace {
2727

28+
using google::api::expr::v1alpha1::Expr;
29+
using google::api::expr::v1alpha1::SourceInfo;
30+
2831
using ::google::api::expr::internal::MakeGoogleApiDurationMax;
2932
using ::google::api::expr::internal::MakeGoogleApiDurationMin;
3033
using testing::HasSubstr;

eval/public/extension_func_test.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,8 +431,9 @@ TEST_F(ExtensionTest, TestGetDate) {
431431
CelValue result;
432432
absl::CivilSecond date(2015, 2, 3, 4, 5, 6);
433433
absl::CivilSecond normal_date(2015, 2, 3);
434-
absl::TimeZone time_zone = absl::LocalTimeZone();
434+
absl::TimeZone time_zone;
435435
std::string time_zonestr = "America/Los_Angeles";
436+
absl::LoadTimeZone(time_zonestr, &time_zone);
436437

437438
absl::Time expected_val = absl::FromCivil(normal_date, time_zone);
438439
absl::Time input_val = absl::FromCivil(date, time_zone);

0 commit comments

Comments
 (0)