Google Cloud C++ Client  2.7.0
C++ Client Library for Google Cloud Platform
mock_stream_range.h
Go to the documentation of this file.
1 // Copyright 2022 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 // 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 #ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_MOCKS_MOCK_STREAM_RANGE_H
16 #define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_MOCKS_MOCK_STREAM_RANGE_H
17 
18 #include "google/cloud/status.h"
19 #include "google/cloud/stream_range.h"
20 #include <vector>
21 
22 namespace google {
23 namespace cloud {
24 /**
25  * Contains helpers for testing the Google Cloud C++ Client Libraries.
26  *
27  * The symbols defined in this namespace are offered for public consumption.
28  * They are not just for maintainers of the client library.
29  */
30 namespace mocks {
32 
33 /**
34  * Construct a `StreamRange<T>` for use in tests.
35  *
36  * @param values The successfully returned `T` values.
37  * @param final_status The final Status of the range. Defaults to an OK Status.
38  *
39  * @code
40  * auto sr = MakeStreamRange<T>({t1, t2});
41  * for (StatusOr<int> const& v : sr) {
42  * // Yields t1 -> t2
43  * }
44  *
45  * sr = MakeStreamRange<T>({t1, t2}, BadStatus());
46  * for (StatusOr<int> const& v : sr) {
47  * // Yields t1 -> t2 -> BadStatus()
48  * }
49  * @endcode
50  */
51 template <typename T>
52 StreamRange<T> MakeStreamRange(std::vector<T> values,
53  Status final_status = {}) {
54  auto iter = values.begin();
55  auto reader =
56  [v = std::move(values), it = std::move(iter),
57  s = std::move(final_status)]() mutable -> absl::variant<Status, T> {
58  if (it == v.end()) return s;
59  return *it++;
60  };
61  return internal::MakeStreamRange<T>(std::move(reader));
62 }
63 
65 } // namespace mocks
66 } // namespace cloud
67 } // namespace google
68 
69 #endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_MOCKS_MOCK_STREAM_RANGE_H