@@ -52,13 +52,53 @@ namespace random_internal {
52
52
template <typename >
53
53
struct DistributionCaller ;
54
54
class MockHelpers ;
55
+ } // namespace random_internal
55
56
56
- // Implements MockingBitGen with an option to turn on extra validation.
57
- template <bool EnableValidation>
58
- class MockingBitGenImpl {
57
+ // MockingBitGen
58
+ //
59
+ // `absl::MockingBitGen` is a mock Uniform Random Bit Generator (URBG) class
60
+ // which can act in place of an `absl::BitGen` URBG within tests using the
61
+ // Googletest testing framework.
62
+ //
63
+ // Usage:
64
+ //
65
+ // Use an `absl::MockingBitGen` along with a mock distribution object (within
66
+ // mock_distributions.h) inside Googletest constructs such as ON_CALL(),
67
+ // EXPECT_TRUE(), etc. to produce deterministic results conforming to the
68
+ // distribution's API contract.
69
+ //
70
+ // Example:
71
+ //
72
+ // // Mock a call to an `absl::Bernoulli` distribution using Googletest
73
+ // absl::MockingBitGen bitgen;
74
+ //
75
+ // ON_CALL(absl::MockBernoulli(), Call(bitgen, 0.5))
76
+ // .WillByDefault(testing::Return(true));
77
+ // EXPECT_TRUE(absl::Bernoulli(bitgen, 0.5));
78
+ //
79
+ // // Mock a call to an `absl::Uniform` distribution within Googletest
80
+ // absl::MockingBitGen bitgen;
81
+ //
82
+ // ON_CALL(absl::MockUniform<int>(), Call(bitgen, testing::_, testing::_))
83
+ // .WillByDefault([] (int low, int high) {
84
+ // return low + (high - low) / 2;
85
+ // });
86
+ //
87
+ // EXPECT_EQ(absl::Uniform<int>(gen, 0, 10), 5);
88
+ // EXPECT_EQ(absl::Uniform<int>(gen, 30, 40), 35);
89
+ //
90
+ // At this time, only mock distributions supplied within the Abseil random
91
+ // library are officially supported.
92
+ //
93
+ // EXPECT_CALL and ON_CALL need to be made within the same DLL component as
94
+ // the call to absl::Uniform and related methods, otherwise mocking will fail
95
+ // since the underlying implementation creates a type-specific pointer which
96
+ // will be distinct across different DLL boundaries.
97
+ //
98
+ class MockingBitGen {
59
99
public:
60
- MockingBitGenImpl () = default ;
61
- ~MockingBitGenImpl () = default ;
100
+ MockingBitGen () = default ;
101
+ ~MockingBitGen () = default ;
62
102
63
103
// URBG interface
64
104
using result_type = absl::BitGen::result_type;
@@ -138,25 +178,23 @@ class MockingBitGenImpl {
138
178
typename ValidatorT>
139
179
auto RegisterMock (SelfT&, base_internal::FastTypeIdType type, ValidatorT)
140
180
-> decltype(GetMockFnType(std::declval<ResultT>(),
141
- std::declval<ArgTupleT>()))& {
142
- using ActualValidatorT =
143
- std::conditional_t <EnableValidation, ValidatorT, NoOpValidator>;
181
+ std::declval<ArgTupleT>())) & {
144
182
using MockFnType = decltype (GetMockFnType (std::declval<ResultT>(),
145
183
std::declval<ArgTupleT>()));
146
184
147
185
using WrappedFnType = absl::conditional_t <
148
- std::is_same<SelfT, ::testing::NiceMock<MockingBitGenImpl >>::value,
186
+ std::is_same<SelfT, ::testing::NiceMock<MockingBitGen >>::value,
149
187
::testing::NiceMock<MockFnType>,
150
188
absl::conditional_t <
151
- std::is_same<SelfT, ::testing::NaggyMock<MockingBitGenImpl >>::value,
189
+ std::is_same<SelfT, ::testing::NaggyMock<MockingBitGen >>::value,
152
190
::testing::NaggyMock<MockFnType>,
153
191
absl::conditional_t <
154
192
std::is_same<SelfT,
155
- ::testing::StrictMock<MockingBitGenImpl >>::value,
193
+ ::testing::StrictMock<MockingBitGen >>::value,
156
194
::testing::StrictMock<MockFnType>, MockFnType>>>;
157
195
158
196
using ImplT =
159
- FunctionHolderImpl<WrappedFnType, ActualValidatorT , ResultT, ArgTupleT>;
197
+ FunctionHolderImpl<WrappedFnType, ValidatorT , ResultT, ArgTupleT>;
160
198
auto & mock = mocks_[type];
161
199
if (!mock) {
162
200
mock = absl::make_unique<ImplT>();
@@ -196,58 +234,6 @@ class MockingBitGenImpl {
196
234
// InvokeMock
197
235
};
198
236
199
- } // namespace random_internal
200
-
201
- // MockingBitGen
202
- //
203
- // `absl::MockingBitGen` is a mock Uniform Random Bit Generator (URBG) class
204
- // which can act in place of an `absl::BitGen` URBG within tests using the
205
- // Googletest testing framework.
206
- //
207
- // Usage:
208
- //
209
- // Use an `absl::MockingBitGen` along with a mock distribution object (within
210
- // mock_distributions.h) inside Googletest constructs such as ON_CALL(),
211
- // EXPECT_TRUE(), etc. to produce deterministic results conforming to the
212
- // distribution's API contract.
213
- //
214
- // Example:
215
- //
216
- // // Mock a call to an `absl::Bernoulli` distribution using Googletest
217
- // absl::MockingBitGen bitgen;
218
- //
219
- // ON_CALL(absl::MockBernoulli(), Call(bitgen, 0.5))
220
- // .WillByDefault(testing::Return(true));
221
- // EXPECT_TRUE(absl::Bernoulli(bitgen, 0.5));
222
- //
223
- // // Mock a call to an `absl::Uniform` distribution within Googletest
224
- // absl::MockingBitGen bitgen;
225
- //
226
- // ON_CALL(absl::MockUniform<int>(), Call(bitgen, testing::_, testing::_))
227
- // .WillByDefault([] (int low, int high) {
228
- // return low + (high - low) / 2;
229
- // });
230
- //
231
- // EXPECT_EQ(absl::Uniform<int>(gen, 0, 10), 5);
232
- // EXPECT_EQ(absl::Uniform<int>(gen, 30, 40), 35);
233
- //
234
- // At this time, only mock distributions supplied within the Abseil random
235
- // library are officially supported.
236
- //
237
- // EXPECT_CALL and ON_CALL need to be made within the same DLL component as
238
- // the call to absl::Uniform and related methods, otherwise mocking will fail
239
- // since the underlying implementation creates a type-specific pointer which
240
- // will be distinct across different DLL boundaries.
241
- //
242
- using MockingBitGen = random_internal::MockingBitGenImpl<true >;
243
-
244
- // UnvalidatedMockingBitGen
245
- //
246
- // UnvalidatedMockingBitGen is a variant of MockingBitGen which does no extra
247
- // validation.
248
- using UnvalidatedMockingBitGen ABSL_DEPRECATED (" Use MockingBitGen instead" ) =
249
- random_internal::MockingBitGenImpl<false>;
250
-
251
237
ABSL_NAMESPACE_END
252
238
} // namespace absl
253
239
0 commit comments