Skip to content
This repository was archived by the owner on Dec 8, 2021. It is now read-only.

Commit c1a0fa6

Browse files
authored
refactor: copy IsProtoEqual from spanner (#179)
Create a new library to host common testing utilities that depend on Protobuf, as the GCS library does not depend on protobuf and we do not want to depend on protobuf for its tests.
1 parent 80fcf05 commit c1a0fa6

File tree

8 files changed

+253
-5
lines changed

8 files changed

+253
-5
lines changed

google/cloud/BUILD

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ cc_library(
8282
load(":google_cloud_cpp_grpc_utils_unit_tests.bzl", "google_cloud_cpp_grpc_utils_unit_tests")
8383

8484
[cc_test(
85-
name = "google_cloud_cpp_grpc_utils_" + test.replace("/", "_").replace(".cc", ""),
85+
name = test.replace("/", "_").replace(".cc", ""),
8686
srcs = [test],
8787
deps = [
8888
":google_cloud_cpp_grpc_utils",

google/cloud/testing_util/BUILD

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,41 @@ cc_library(
2727
"@com_google_googletest//:gtest",
2828
],
2929
)
30+
31+
load(":google_cloud_cpp_testing_unit_tests.bzl", "google_cloud_cpp_testing_unit_tests")
32+
33+
[cc_test(
34+
name = test.replace("/", "_").replace(".cc", ""),
35+
srcs = [test],
36+
deps = [
37+
":google_cloud_cpp_testing",
38+
"//google/cloud:google_cloud_cpp_common",
39+
"@com_google_googletest//:gtest_main",
40+
],
41+
) for test in google_cloud_cpp_testing_unit_tests]
42+
43+
load(":google_cloud_cpp_testing_grpc.bzl", "google_cloud_cpp_testing_grpc_hdrs", "google_cloud_cpp_testing_grpc_srcs")
44+
45+
cc_library(
46+
name = "google_cloud_cpp_testing_grpc",
47+
srcs = google_cloud_cpp_testing_grpc_srcs,
48+
hdrs = google_cloud_cpp_testing_grpc_hdrs,
49+
deps = [
50+
"//google/cloud:google_cloud_cpp_common",
51+
"@com_google_googletest//:gtest",
52+
"@com_google_protobuf//:protobuf",
53+
],
54+
)
55+
56+
load(":google_cloud_cpp_testing_grpc_unit_tests.bzl", "google_cloud_cpp_testing_grpc_unit_tests")
57+
58+
[cc_test(
59+
name = test.replace("/", "_").replace(".cc", ""),
60+
srcs = [test],
61+
deps = [
62+
":google_cloud_cpp_testing_grpc",
63+
"//google/cloud:google_cloud_cpp_common",
64+
"@com_google_googletest//:gtest_main",
65+
"@com_google_protobuf//:protobuf",
66+
],
67+
) for test in google_cloud_cpp_testing_grpc_unit_tests]

google/cloud/testing_util/CMakeLists.txt

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,41 @@ if (BUILD_TESTING OR GOOGLE_CLOUD_CPP_TESTING_UTIL_ENABLE_INSTALL)
6262
add_test(NAME ${target} COMMAND ${target})
6363
endforeach ()
6464

65+
find_package(googleapis)
66+
add_library(google_cloud_cpp_testing_grpc is_proto_equal.cc
67+
is_proto_equal.h)
68+
target_link_libraries(
69+
google_cloud_cpp_testing_grpc
70+
PUBLIC google_cloud_cpp_common protobuf::libprotobuf GTest::gmock
71+
PRIVATE google_cloud_cpp_common_options)
72+
73+
create_bazel_config(google_cloud_cpp_testing_grpc YEAR 2020)
74+
75+
set(google_cloud_cpp_testing_grpc_unit_tests is_proto_equal_test.cc)
76+
77+
export_list_to_bazel("google_cloud_cpp_testing_grpc_unit_tests.bzl"
78+
"google_cloud_cpp_testing_grpc_unit_tests" YEAR 2020)
79+
80+
foreach (fname ${google_cloud_cpp_testing_grpc_unit_tests})
81+
string(REPLACE "/" "_" target ${fname})
82+
string(REPLACE ".cc" "" target ${target})
83+
add_executable(${target} ${fname})
84+
target_link_libraries(
85+
${target}
86+
PRIVATE google_cloud_cpp_testing_grpc
87+
google_cloud_cpp_testing
88+
google_cloud_cpp_common
89+
protobuf::libprotobuf
90+
GTest::gmock_main
91+
GTest::gmock
92+
GTest::gtest
93+
google_cloud_cpp_common_options)
94+
if (MSVC)
95+
target_compile_options(${target} PRIVATE "/bigobj")
96+
endif ()
97+
add_test(NAME ${target} COMMAND ${target})
98+
endforeach ()
99+
65100
# Export the CMake targets to make it easy to create configuration files.
66101
install(
67102
EXPORT google_cloud_cpp_testing-targets
@@ -70,7 +105,7 @@ if (BUILD_TESTING OR GOOGLE_CLOUD_CPP_TESTING_UTIL_ENABLE_INSTALL)
70105
# Install the libraries and headers in the locations determined by
71106
# GNUInstallDirs
72107
install(
73-
TARGETS google_cloud_cpp_testing
108+
TARGETS google_cloud_cpp_testing google_cloud_cpp_testing_grpc
74109
EXPORT google_cloud_cpp_testing-targets
75110
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
76111
COMPONENT google_cloud_cpp_runtime
@@ -82,7 +117,7 @@ if (BUILD_TESTING OR GOOGLE_CLOUD_CPP_TESTING_UTIL_ENABLE_INSTALL)
82117
# With CMake-3.12 and higher we could avoid this separate command (and the
83118
# duplication).
84119
install(
85-
TARGETS google_cloud_cpp_testing
120+
TARGETS google_cloud_cpp_testing google_cloud_cpp_testing_grpc
86121
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
87122
COMPONENT google_cloud_cpp_development
88123
NAMELINK_ONLY
@@ -100,13 +135,22 @@ if (BUILD_TESTING OR GOOGLE_CLOUD_CPP_TESTING_UTIL_ENABLE_INSTALL)
100135
"Google Cloud C++ Client Library Testing Utilities")
101136
set(GOOGLE_CLOUD_CPP_PC_DESCRIPTION
102137
"Testing Utilities used by the Google Cloud C++ Client Libraries.")
103-
set(GOOGLE_CLOUD_CPP_PC_LIBS "-lgoogle_cloud_cpp_testing")
104138

105-
# Create and install the pkg-config files.
139+
# Create and install the pkg-config files. First for testing_utils:
140+
set(GOOGLE_CLOUD_CPP_PC_LIBS "-lgoogle_cloud_cpp_testing")
141+
set(GOOGLE_CLOUD_CPP_PC_REQUIRES "google_cloud_cpp_common")
106142
configure_file("${PROJECT_SOURCE_DIR}/google/cloud/config.pc.in"
107143
"google_cloud_cpp_testing.pc" @ONLY)
108144
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/google_cloud_cpp_testing.pc"
109145
DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
146+
# Then for testing_utils_grpc:
147+
set(GOOGLE_CLOUD_CPP_PC_LIBS "-lgoogle_cloud_cpp_testing_grpc")
148+
set(GOOGLE_CLOUD_CPP_PC_REQUIRES
149+
"google_cloud_cpp_testing google_cloud_cpp_common")
150+
configure_file("${PROJECT_SOURCE_DIR}/google/cloud/config.pc.in"
151+
"google_cloud_cpp_testing_grpc.pc" @ONLY)
152+
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/google_cloud_cpp_testing_grpc.pc"
153+
DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
110154

111155
# Create and install the CMake configuration files.
112156
configure_file("config.cmake.in" "google_cloud_cpp_testing-config.cmake"
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Copyright 2020 Google LLC
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+
# http://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+
# DO NOT EDIT -- GENERATED BY CMake -- Change the CMakeLists.txt file if needed
16+
17+
"""Automatically generated source lists for google_cloud_cpp_testing_grpc - DO NOT EDIT."""
18+
19+
google_cloud_cpp_testing_grpc_hdrs = [
20+
"is_proto_equal.h",
21+
]
22+
23+
google_cloud_cpp_testing_grpc_srcs = [
24+
"is_proto_equal.cc",
25+
]
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Copyright 2020 Google LLC
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+
# http://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+
# DO NOT EDIT -- GENERATED BY CMake -- Change the CMakeLists.txt file if needed
16+
17+
"""Automatically generated unit tests list - DO NOT EDIT."""
18+
19+
google_cloud_cpp_testing_grpc_unit_tests = [
20+
"is_proto_equal_test.cc",
21+
]
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright 2020 Google LLC
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+
// http://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+
#include "google/cloud/testing_util/is_proto_equal.h"
16+
#include <google/protobuf/util/message_differencer.h>
17+
18+
namespace google {
19+
namespace cloud {
20+
inline namespace GOOGLE_CLOUD_CPP_NS {
21+
namespace testing_util {
22+
23+
optional<std::string> CompareProtos(google::protobuf::Message const& arg,
24+
google::protobuf::Message const& value) {
25+
std::string delta;
26+
google::protobuf::util::MessageDifferencer differencer;
27+
differencer.ReportDifferencesToString(&delta);
28+
auto const result = differencer.Compare(arg, value);
29+
if (result) return {};
30+
return delta;
31+
}
32+
33+
} // namespace testing_util
34+
} // namespace GOOGLE_CLOUD_CPP_NS
35+
} // namespace cloud
36+
} // namespace google
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright 2020 Google LLC
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+
// http://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+
#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_TESTING_UTIL_IS_PROTO_EQUAL_H
16+
#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_TESTING_UTIL_IS_PROTO_EQUAL_H
17+
18+
#include "google/cloud/optional.h"
19+
#include "google/cloud/version.h"
20+
#include <google/protobuf/message.h>
21+
#include <gmock/gmock.h>
22+
23+
namespace google {
24+
namespace cloud {
25+
inline namespace GOOGLE_CLOUD_CPP_NS {
26+
namespace testing_util {
27+
28+
optional<std::string> CompareProtos(google::protobuf::Message const& arg,
29+
google::protobuf::Message const& value);
30+
31+
MATCHER_P(IsProtoEqual, value, "Checks whether protos are equal") {
32+
optional<std::string> delta = CompareProtos(arg, value);
33+
if (delta.has_value()) {
34+
*result_listener << "\n" << *delta;
35+
}
36+
return !delta.has_value();
37+
}
38+
39+
} // namespace testing_util
40+
} // namespace GOOGLE_CLOUD_CPP_NS
41+
} // namespace cloud
42+
} // namespace google
43+
44+
#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_TESTING_UTIL_IS_PROTO_EQUAL_H
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright 2020 Google LLC
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+
// http://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+
#include "google/cloud/testing_util/is_proto_equal.h"
16+
#include <google/protobuf/wrappers.pb.h>
17+
#include <gmock/gmock.h>
18+
19+
namespace google {
20+
namespace cloud {
21+
inline namespace GOOGLE_CLOUD_CPP_NS {
22+
namespace testing_util {
23+
namespace {
24+
25+
using ::testing::Not;
26+
27+
TEST(IsProtoEqual, Basic) {
28+
::google::protobuf::StringValue actual;
29+
actual.set_value("Hello World");
30+
::google::protobuf::StringValue not_actual;
31+
32+
EXPECT_THAT(actual, IsProtoEqual(actual));
33+
EXPECT_THAT(actual, Not(IsProtoEqual(not_actual)));
34+
}
35+
36+
} // namespace
37+
} // namespace testing_util
38+
} // namespace GOOGLE_CLOUD_CPP_NS
39+
} // namespace cloud
40+
} // namespace google

0 commit comments

Comments
 (0)