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

Commit

Permalink
refactor: copy IsProtoEqual from spanner (#179)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
coryan authored Feb 14, 2020
1 parent 80fcf05 commit c1a0fa6
Show file tree
Hide file tree
Showing 8 changed files with 253 additions and 5 deletions.
2 changes: 1 addition & 1 deletion google/cloud/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ cc_library(
load(":google_cloud_cpp_grpc_utils_unit_tests.bzl", "google_cloud_cpp_grpc_utils_unit_tests")

[cc_test(
name = "google_cloud_cpp_grpc_utils_" + test.replace("/", "_").replace(".cc", ""),
name = test.replace("/", "_").replace(".cc", ""),
srcs = [test],
deps = [
":google_cloud_cpp_grpc_utils",
Expand Down
38 changes: 38 additions & 0 deletions google/cloud/testing_util/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,41 @@ cc_library(
"@com_google_googletest//:gtest",
],
)

load(":google_cloud_cpp_testing_unit_tests.bzl", "google_cloud_cpp_testing_unit_tests")

[cc_test(
name = test.replace("/", "_").replace(".cc", ""),
srcs = [test],
deps = [
":google_cloud_cpp_testing",
"//google/cloud:google_cloud_cpp_common",
"@com_google_googletest//:gtest_main",
],
) for test in google_cloud_cpp_testing_unit_tests]

load(":google_cloud_cpp_testing_grpc.bzl", "google_cloud_cpp_testing_grpc_hdrs", "google_cloud_cpp_testing_grpc_srcs")

cc_library(
name = "google_cloud_cpp_testing_grpc",
srcs = google_cloud_cpp_testing_grpc_srcs,
hdrs = google_cloud_cpp_testing_grpc_hdrs,
deps = [
"//google/cloud:google_cloud_cpp_common",
"@com_google_googletest//:gtest",
"@com_google_protobuf//:protobuf",
],
)

load(":google_cloud_cpp_testing_grpc_unit_tests.bzl", "google_cloud_cpp_testing_grpc_unit_tests")

[cc_test(
name = test.replace("/", "_").replace(".cc", ""),
srcs = [test],
deps = [
":google_cloud_cpp_testing_grpc",
"//google/cloud:google_cloud_cpp_common",
"@com_google_googletest//:gtest_main",
"@com_google_protobuf//:protobuf",
],
) for test in google_cloud_cpp_testing_grpc_unit_tests]
52 changes: 48 additions & 4 deletions google/cloud/testing_util/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,41 @@ if (BUILD_TESTING OR GOOGLE_CLOUD_CPP_TESTING_UTIL_ENABLE_INSTALL)
add_test(NAME ${target} COMMAND ${target})
endforeach ()

find_package(googleapis)
add_library(google_cloud_cpp_testing_grpc is_proto_equal.cc
is_proto_equal.h)
target_link_libraries(
google_cloud_cpp_testing_grpc
PUBLIC google_cloud_cpp_common protobuf::libprotobuf GTest::gmock
PRIVATE google_cloud_cpp_common_options)

create_bazel_config(google_cloud_cpp_testing_grpc YEAR 2020)

set(google_cloud_cpp_testing_grpc_unit_tests is_proto_equal_test.cc)

export_list_to_bazel("google_cloud_cpp_testing_grpc_unit_tests.bzl"
"google_cloud_cpp_testing_grpc_unit_tests" YEAR 2020)

foreach (fname ${google_cloud_cpp_testing_grpc_unit_tests})
string(REPLACE "/" "_" target ${fname})
string(REPLACE ".cc" "" target ${target})
add_executable(${target} ${fname})
target_link_libraries(
${target}
PRIVATE google_cloud_cpp_testing_grpc
google_cloud_cpp_testing
google_cloud_cpp_common
protobuf::libprotobuf
GTest::gmock_main
GTest::gmock
GTest::gtest
google_cloud_cpp_common_options)
if (MSVC)
target_compile_options(${target} PRIVATE "/bigobj")
endif ()
add_test(NAME ${target} COMMAND ${target})
endforeach ()

# Export the CMake targets to make it easy to create configuration files.
install(
EXPORT google_cloud_cpp_testing-targets
Expand All @@ -70,7 +105,7 @@ if (BUILD_TESTING OR GOOGLE_CLOUD_CPP_TESTING_UTIL_ENABLE_INSTALL)
# Install the libraries and headers in the locations determined by
# GNUInstallDirs
install(
TARGETS google_cloud_cpp_testing
TARGETS google_cloud_cpp_testing google_cloud_cpp_testing_grpc
EXPORT google_cloud_cpp_testing-targets
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
COMPONENT google_cloud_cpp_runtime
Expand All @@ -82,7 +117,7 @@ if (BUILD_TESTING OR GOOGLE_CLOUD_CPP_TESTING_UTIL_ENABLE_INSTALL)
# With CMake-3.12 and higher we could avoid this separate command (and the
# duplication).
install(
TARGETS google_cloud_cpp_testing
TARGETS google_cloud_cpp_testing google_cloud_cpp_testing_grpc
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
COMPONENT google_cloud_cpp_development
NAMELINK_ONLY
Expand All @@ -100,13 +135,22 @@ if (BUILD_TESTING OR GOOGLE_CLOUD_CPP_TESTING_UTIL_ENABLE_INSTALL)
"Google Cloud C++ Client Library Testing Utilities")
set(GOOGLE_CLOUD_CPP_PC_DESCRIPTION
"Testing Utilities used by the Google Cloud C++ Client Libraries.")
set(GOOGLE_CLOUD_CPP_PC_LIBS "-lgoogle_cloud_cpp_testing")

# Create and install the pkg-config files.
# Create and install the pkg-config files. First for testing_utils:
set(GOOGLE_CLOUD_CPP_PC_LIBS "-lgoogle_cloud_cpp_testing")
set(GOOGLE_CLOUD_CPP_PC_REQUIRES "google_cloud_cpp_common")
configure_file("${PROJECT_SOURCE_DIR}/google/cloud/config.pc.in"
"google_cloud_cpp_testing.pc" @ONLY)
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/google_cloud_cpp_testing.pc"
DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
# Then for testing_utils_grpc:
set(GOOGLE_CLOUD_CPP_PC_LIBS "-lgoogle_cloud_cpp_testing_grpc")
set(GOOGLE_CLOUD_CPP_PC_REQUIRES
"google_cloud_cpp_testing google_cloud_cpp_common")
configure_file("${PROJECT_SOURCE_DIR}/google/cloud/config.pc.in"
"google_cloud_cpp_testing_grpc.pc" @ONLY)
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/google_cloud_cpp_testing_grpc.pc"
DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")

# Create and install the CMake configuration files.
configure_file("config.cmake.in" "google_cloud_cpp_testing-config.cmake"
Expand Down
25 changes: 25 additions & 0 deletions google/cloud/testing_util/google_cloud_cpp_testing_grpc.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# DO NOT EDIT -- GENERATED BY CMake -- Change the CMakeLists.txt file if needed

"""Automatically generated source lists for google_cloud_cpp_testing_grpc - DO NOT EDIT."""

google_cloud_cpp_testing_grpc_hdrs = [
"is_proto_equal.h",
]

google_cloud_cpp_testing_grpc_srcs = [
"is_proto_equal.cc",
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# DO NOT EDIT -- GENERATED BY CMake -- Change the CMakeLists.txt file if needed

"""Automatically generated unit tests list - DO NOT EDIT."""

google_cloud_cpp_testing_grpc_unit_tests = [
"is_proto_equal_test.cc",
]
36 changes: 36 additions & 0 deletions google/cloud/testing_util/is_proto_equal.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "google/cloud/testing_util/is_proto_equal.h"
#include <google/protobuf/util/message_differencer.h>

namespace google {
namespace cloud {
inline namespace GOOGLE_CLOUD_CPP_NS {
namespace testing_util {

optional<std::string> CompareProtos(google::protobuf::Message const& arg,
google::protobuf::Message const& value) {
std::string delta;
google::protobuf::util::MessageDifferencer differencer;
differencer.ReportDifferencesToString(&delta);
auto const result = differencer.Compare(arg, value);
if (result) return {};
return delta;
}

} // namespace testing_util
} // namespace GOOGLE_CLOUD_CPP_NS
} // namespace cloud
} // namespace google
44 changes: 44 additions & 0 deletions google/cloud/testing_util/is_proto_equal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_TESTING_UTIL_IS_PROTO_EQUAL_H
#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_TESTING_UTIL_IS_PROTO_EQUAL_H

#include "google/cloud/optional.h"
#include "google/cloud/version.h"
#include <google/protobuf/message.h>
#include <gmock/gmock.h>

namespace google {
namespace cloud {
inline namespace GOOGLE_CLOUD_CPP_NS {
namespace testing_util {

optional<std::string> CompareProtos(google::protobuf::Message const& arg,
google::protobuf::Message const& value);

MATCHER_P(IsProtoEqual, value, "Checks whether protos are equal") {
optional<std::string> delta = CompareProtos(arg, value);
if (delta.has_value()) {
*result_listener << "\n" << *delta;
}
return !delta.has_value();
}

} // namespace testing_util
} // namespace GOOGLE_CLOUD_CPP_NS
} // namespace cloud
} // namespace google

#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_TESTING_UTIL_IS_PROTO_EQUAL_H
40 changes: 40 additions & 0 deletions google/cloud/testing_util/is_proto_equal_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "google/cloud/testing_util/is_proto_equal.h"
#include <google/protobuf/wrappers.pb.h>
#include <gmock/gmock.h>

namespace google {
namespace cloud {
inline namespace GOOGLE_CLOUD_CPP_NS {
namespace testing_util {
namespace {

using ::testing::Not;

TEST(IsProtoEqual, Basic) {
::google::protobuf::StringValue actual;
actual.set_value("Hello World");
::google::protobuf::StringValue not_actual;

EXPECT_THAT(actual, IsProtoEqual(actual));
EXPECT_THAT(actual, Not(IsProtoEqual(not_actual)));
}

} // namespace
} // namespace testing_util
} // namespace GOOGLE_CLOUD_CPP_NS
} // namespace cloud
} // namespace google

0 comments on commit c1a0fa6

Please sign in to comment.