Skip to content

Commit ec05665

Browse files
Neural-Link Teamtensorflow-copybara
authored andcommitted
Add a few macros for testing.
PiperOrigin-RevId: 366291052
1 parent f0a43fa commit ec05665

File tree

4 files changed

+112
-2
lines changed

4 files changed

+112
-2
lines changed

research/carls/testing/BUILD

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,15 @@ carls_cc_proto_library(
4747
cc_library(
4848
name = "test_helper",
4949
testonly = 1,
50+
srcs = ["test_helper.cc"],
5051
hdrs = ["test_helper.h"],
5152
deps = [
5253
"@com_github_google_glog//:glog",
54+
"@com_github_grpc_grpc//:grpc++",
55+
"@com_google_absl//absl/status",
5356
"@com_google_googletest//:gtest",
5457
"@com_google_protobuf//:protobuf",
58+
"@tensorflow_includes//:includes",
5559
],
5660
)
5761

@@ -61,7 +65,11 @@ cc_test(
6165
deps = [
6266
":test_helper",
6367
":test_proto2_cc_proto",
68+
"//research/carls/base:status_helper",
69+
"@com_github_grpc_grpc//:grpc++",
70+
"@com_google_absl//absl/status",
6471
"@com_google_googletest//:gtest_main",
72+
"@tensorflow_includes//:includes",
6573
],
6674
)
6775

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/* Copyright 2021 Google LLC. All Rights Reserved.
2+
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+
https://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+
16+
#include "research/carls/testing/test_helper.h"
17+
18+
#include "grpcpp/support/status.h" // net
19+
#include "tensorflow/core/platform/status.h"
20+
21+
namespace carls {
22+
namespace internal {
23+
24+
template <>
25+
std::string GetErrorMessage(const absl::Status& status) {
26+
return std::string(status.message());
27+
}
28+
29+
template <>
30+
std::string GetErrorMessage(const grpc::Status& status) {
31+
return status.error_message();
32+
}
33+
34+
template <>
35+
std::string GetErrorMessage(const tensorflow::Status& status) {
36+
return status.error_message();
37+
}
38+
39+
} // namespace internal
40+
} // namespace carls

research/carls/testing/test_helper.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,15 @@ limitations under the License.
2121
#include "google/protobuf/text_format.h" // proto import
2222
#include "gmock/gmock.h"
2323
#include "gtest/gtest.h"
24+
#include "absl/status/status.h"
2425

2526
namespace carls {
27+
namespace internal {
28+
29+
template <typename StatusType>
30+
std::string GetErrorMessage(const StatusType& status);
31+
32+
} // namespace internal
2633

2734
// A simple implementation of a proto matcher comparing string representations.
2835
class ProtoStringMatcher {
@@ -57,6 +64,24 @@ inline ::testing::PolymorphicMatcher<ProtoStringMatcher> EqualsProto(
5764
return ::testing::MakePolymorphicMatcher(ProtoStringMatcher(proto));
5865
}
5966

67+
// Macros for testing the results of functions that return Status or
68+
// StatusOr<T> (for any type T).
69+
#undef EXPECT_OK
70+
#define EXPECT_OK(expression) EXPECT_TRUE(expression.ok())
71+
72+
#define EXPECT_NOT_OK(expression) EXPECT_FALSE(expression.ok())
73+
74+
#define EXPECT_ERROR(expression, err_msg) \
75+
ASSERT_FALSE(expression.ok()); \
76+
EXPECT_EQ(err_msg, internal::GetErrorMessage(expression));
77+
78+
#undef ASSERT_OK
79+
#define ASSERT_OK(expression) ASSERT_TRUE(expression.ok())
80+
81+
#define ASSERT_ERROR(expression, err_msg) \
82+
ASSERT_FALSE(expression.ok()); \
83+
ASSERT_EQ(err_msg, internal::GetErrorMessage(expression));
84+
6085
} // namespace carls
6186

6287
#endif // NEURAL_STRUCTURED_LEARNING_RESEARCH_CARLS_TESTING_TEST_HELPER_H_

research/carls/testing/test_helper_test.cc

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@ limitations under the License.
1515

1616
#include "research/carls/testing/test_helper.h"
1717

18+
#include "grpcpp/support/status.h" // net
19+
#include "absl/status/status.h"
20+
#include "research/carls/base/status_helper.h"
1821
#include "research/carls/testing/test_proto2.pb.h" // proto to pb
22+
#include "tensorflow/core/platform/status.h"
23+
#include "tensorflow/core/protobuf/error_codes.pb.h" // proto to pb
1924

2025
namespace carls {
2126

@@ -28,9 +33,41 @@ TEST(TestHelperTest, EqualsProto) {
2833
TEST(TestHelperTest, EqualsProtoText) {
2934
TestBaseProto2Def proto;
3035
proto.set_name("Audrey");
31-
EXPECT_THAT(proto, EqualsProto<TestBaseProto2Def>(R"(
36+
EXPECT_THAT(proto, EqualsProto<TestBaseProto2Def>(R"pb(
3237
name: "Audrey"
33-
)"));
38+
)pb"));
39+
}
40+
41+
TEST(TestHelperTest, AbslStatusChecks) {
42+
EXPECT_OK(absl::OkStatus());
43+
EXPECT_NOT_OK(absl::InternalError("First error."));
44+
EXPECT_ERROR(absl::InternalError("First error."), "First error.");
45+
ASSERT_OK(absl::OkStatus());
46+
ASSERT_ERROR(absl::InternalError("Second error."), "Second error.");
47+
}
48+
49+
TEST(TestHelperTest, GrpcStatusChecks) {
50+
EXPECT_OK(grpc::Status::OK);
51+
EXPECT_OK(ToGrpcStatus(absl::OkStatus()));
52+
EXPECT_NOT_OK(ToGrpcStatus(absl::InternalError("First error.")));
53+
EXPECT_ERROR(ToGrpcStatus(absl::InternalError("First error.")),
54+
"First error.");
55+
ASSERT_OK(grpc::Status::OK);
56+
ASSERT_ERROR(ToGrpcStatus(absl::InternalError("Second error.")),
57+
"Second error.");
58+
}
59+
60+
TEST(TestHelperTest, TensoFlowStatusChecks) {
61+
EXPECT_OK(tensorflow::Status::OK());
62+
EXPECT_NOT_OK(
63+
tensorflow::Status(tensorflow::error::INVALID_ARGUMENT, "First error."));
64+
EXPECT_ERROR(
65+
tensorflow::Status(tensorflow::error::INVALID_ARGUMENT, "First error."),
66+
"First error.");
67+
ASSERT_OK(tensorflow::Status::OK());
68+
ASSERT_ERROR(
69+
tensorflow::Status(tensorflow::error::INVALID_ARGUMENT, "Second error."),
70+
"Second error.");
3471
}
3572

3673
} // namespace carls

0 commit comments

Comments
 (0)