Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(mocks): provide access to call options in client tests #11050

Merged
merged 3 commits into from
Mar 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions google/cloud/google_cloud_cpp_common.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,9 @@ install(
# for these, a regular library would not work on macOS (where the library needs
# at least one .o file).
add_library(google_cloud_cpp_mocks INTERFACE)
set(google_cloud_cpp_mocks_hdrs # cmake-format: sort
mocks/mock_stream_range.h)
set(google_cloud_cpp_mocks_hdrs
# cmake-format: sort
mocks/current_options.h mocks/mock_stream_range.h)
export_list_to_bazel("google_cloud_cpp_mocks.bzl" "google_cloud_cpp_mocks_hdrs"
YEAR "2022")
target_link_libraries(
Expand Down Expand Up @@ -367,6 +368,7 @@ if (BUILD_TESTING)
internal/utility_test.cc
kms_key_name_test.cc
log_test.cc
mocks/current_options_test.cc
mocks/mock_stream_range_test.cc
options_test.cc
polling_policy_test.cc
Expand Down
1 change: 1 addition & 0 deletions google/cloud/google_cloud_cpp_common_unit_tests.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ google_cloud_cpp_common_unit_tests = [
"internal/utility_test.cc",
"kms_key_name_test.cc",
"log_test.cc",
"mocks/current_options_test.cc",
"mocks/mock_stream_range_test.cc",
"options_test.cc",
"polling_policy_test.cc",
Expand Down
1 change: 1 addition & 0 deletions google/cloud/google_cloud_cpp_mocks.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@
"""Automatically generated unit tests list - DO NOT EDIT."""

google_cloud_cpp_mocks_hdrs = [
"mocks/current_options.h",
"mocks/mock_stream_range.h",
]
51 changes: 51 additions & 0 deletions google/cloud/mocks/current_options.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright 2023 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
//
// https://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_MOCKS_CURRENT_OPTIONS_H
#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_MOCKS_CURRENT_OPTIONS_H

#include "google/cloud/options.h"

namespace google {
namespace cloud {
namespace mocks {
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN

/**
* Retrieve options used in a client call.
*
* This would be used to verify configuration options from within a
* MockConnection. It provides a way for applications to test the difference
* between `client.Foo(request, options)` and `client.Foo(request)`.
*
* @code
* TEST(Foo, CallOptions) {
* auto mock = std::make_shared<MockConnection>();
* EXPECT_CALL(*mock, Foo).WillOnce([] {
* auto const& options = google::cloud::mocks::CurrentOptions();
devbww marked this conversation as resolved.
Show resolved Hide resolved
* EXPECT_THAT(options, ...);
* });
* auto client = Client(mock);
* MyFunctionThatCallsFoo(client);
* }
* @endcode
*/
inline Options const& CurrentOptions() { return internal::CurrentOptions(); }

GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
} // namespace mocks
} // namespace cloud
} // namespace google

#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_MOCKS_CURRENT_OPTIONS_H
47 changes: 47 additions & 0 deletions google/cloud/mocks/current_options_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright 2023 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
//
// https://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/mocks/current_options.h"
#include "google/cloud/testing_util/status_matchers.h"
#include <gmock/gmock.h>

namespace google {
namespace cloud {
namespace mocks {
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
namespace {

TEST(CurrentOptionsTest, Basic) {
struct IntOption {
using Type = int;
};

EXPECT_FALSE(CurrentOptions().has<IntOption>());
{
internal::OptionsSpan span(Options{}.set<IntOption>(1));
EXPECT_EQ(CurrentOptions().get<IntOption>(), 1);
{
internal::OptionsSpan span(Options{}.set<IntOption>(2));
EXPECT_EQ(CurrentOptions().get<IntOption>(), 2);
}
EXPECT_EQ(CurrentOptions().get<IntOption>(), 1);
}
EXPECT_FALSE(CurrentOptions().has<IntOption>());
}

} // namespace
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
} // namespace mocks
} // namespace cloud
} // namespace google
2 changes: 2 additions & 0 deletions google/cloud/mocks/mock_stream_range_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ namespace google {
namespace cloud {
namespace mocks {
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
namespace {

using ::google::cloud::testing_util::StatusIs;
using ::testing::ElementsAre;
Expand Down Expand Up @@ -69,6 +70,7 @@ TEST(MakeStreamRangeTest, ValuesThenStatus) {
EXPECT_THAT(result.final_status, StatusIs(StatusCode::kAborted));
}

} // namespace
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
} // namespace mocks
} // namespace cloud
Expand Down