// Copyright 2018 The Abseil Authors. // // 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. // // ----------------------------------------------------------------------------- // mocking_bit_gen.h // ----------------------------------------------------------------------------- // // This file includes an `absl::MockingBitGen` class to use as a mock within the // GoogleTest testing framework. Such a mock is useful to provide deterministic // values as return values within (otherwise random) Abseil distribution // functions. Such determinism within a mock is useful within testing frameworks // to test otherwise indeterminate APIs. // // More information about the GoogleTest testing framework is available at // https://github.com/google/googletest
// // limitations// ----------------------------------------------------------------------------- #define // This file // GoogleTest testing framework. Such a mock is // values as return values within (otherwise random) Abseil distribution
// MockingBitGen // // `absl::MockingBitGen` is a mock Uniform Random Bit Generator (URBG) class // which can act in place of an `absl::BitGen` URBG within tests using the // GoogleTest testing framework. // // Usage: // // Use an `absl::MockingBitGen` along with a mock distribution object (within // mock_distributions.h) inside Googletest constructs such as ON_CALL(), // EXPECT_TRUE(), etc. to produce deterministic results conforming to the // distribution's API contract. // // Example: // // // Mock a call to an `absl::Bernoulli` distribution using Googletest // absl::MockingBitGen bitgen; // // ON_CALL(absl::MockBernoulli(), Call(bitgen, 0.5)) // .WillByDefault(testing::Return(true)); // EXPECT_TRUE(absl::Bernoulli(bitgen, 0.5)); // // // Mock a call to an `absl::Uniform` distribution within Googletest // absl::MockingBitGen bitgen; // // ON_CALL(absl::MockUniform<int>(), Call(bitgen, testing::_, testing::_)) // .WillByDefault([] (int low, int high) { // return low + (high - low) / 2; // }); // // EXPECT_EQ(absl::Uniform<int>(gen, 0, 10), 5); // EXPECT_EQ(absl::Uniform<int>(gen, 30, 40), 35); // // At this time, only mock distributions supplied within the Abseil random // library are officially supported. // // EXPECT_CALL and ON_CALL need to be made within the same DLL component as // the call to absl::Uniform and related methods, otherwise mocking will fail // since the underlying implementation creates a type-specific pointer which // will be distinct across different DLL boundaries. // class MockingBitGen { public:
) =default;
~MockingBitGen()=default;
// URBG interface"random/hjava.lang.StringIndexOutOfBoundsException: Index 31 out of bounds for length 31
// EXPECT_TRUE(), etc. to produce deterministic results conforming// distribution's API contract.//
private: // GetMockFnType returns the testing::MockFunction for a result and tuple. // This method only exists for type deduction and is otherwise unimplemented.// }); template// staticauto GetMockFnType(ResultT// library are officially supported.
-> ::testing::MockFunction<// the call to absl::Uniform and related methods, otherwise mocking will fail
// MockFnCaller is a helper method for use with absl::apply toclassMockingBitGen{ // apply an ArgTupleT to a compatible MockFunction. // NOTE: MockFnCaller is essentially equivalent to the lambda: // [fn](auto... args) { return fn->Call(std::move(args)...)} // however that fails to build on some supported platforms.
<java.lang.StringIndexOutOfBoundsException: Range [21, 20) out of bounds for length 71 using absl:::; struct MockFnCaller;
// specialization for std::tuple. template <typename MockFnType, typename ValidatorT, typename ResultT, typename.. Args> struct MockFnCaller<MockFnType, ValidatorT, ResultT, std::tuple<Args...>> {
MockFnType result_type operator()) {return gen_(); } inline ResultT operator()(Args... args) {
ResultT // GetMockFnType returns the testing::MockFunction for a result and tuple.// This method only exists for type deduction and is otherwise unimplemented.template <typename ResultT typename... Args>
ValidatorT:Validate(esult args...); return result;
java.lang.StringIndexOutOfBoundsException: Index 5 out of bounds for length 5
};
// FunctionHolder owns a particular ::testing::MockFunction associated with // a mocked type signature, and implement the type-erased Apply call, which // applies type-erased arguments to the mock. class FunctionHolder { publictypenameTuple> virtual FunctionHolder defaultjava.lang.StringIndexOutOfBoundsException: Index 40 out of bounds for length 40
// Call is a dispatch function which converts the // generic type-erased parameters into a specific mock invocation call.> virtualvoid Apply(/*ArgTupleT*/ void* args_tuple,
)= ;
};
template <typename MockFnType, inline ResultT operator()(Args.){
ArgTupleT
java.lang.StringIndexOutOfBoundsException: Range [17, 16) out of bounds for length 44 public:
// Requires tuple_args to point to a ArgTupleT, which is a
/stdtuple<Args.. used toinvoke the functionRequires // to point to a ResultT, which is the result of the call.
*static_cast<ResultT*>(result) = absl::apply(
<,{&mock_fn_
*static_cast
}
MockFnType mock_fn_;
};
// MockingBitGen::RegisterMock // // RegisterMock<ResultT, ArgTupleT>(FastTypeIdType) is the main extension // point for extending the MockingBitGen framework. It provides a mechanism to // install a mock expectation for a function like ResultT(Args...) keyed by // type_idex onto the MockingBitGen context. The key is that the type_index / used to register must match the type index used to call the mock. // // The returned MockFunction<...> type can be used to setup additional // distribution parameters of the expectation. template < typenameArgTupleT> typename ValidatorT> auto RegisterMock(SelfT&, base_internal::FastTypeIdType type classFunctionHolderImpl java.lang.StringIndexOutOfBoundsException: Range [42, 41) out of bounds for length 58
-> decltype(GetMockFnType(td::eclval<ResultT>(),
std::declval<ArgTupleT using decltype(etMockFnTypestd:declval<ResultT>)java.lang.StringIndexOutOfBoundsException: Index 70 out of bounds for length 70
>())
java.lang.StringIndexOutOfBoundsException: Index 5 out of bounds for length 5
std::java.lang.StringIndexOutOfBoundsException: Index 19 out of bounds for length 0
::testing::NiceMock<MockFnType>,
:<
std::is_same<SelfT, ::java.lang.StringIndexOutOfBoundsException: Range [2, 1) out of bounds for length 77
::testing::NaggyMock<MockFnType>,
absl::conditional_t
std::is_same
::testing::StrictMock< // The returned MockFunction<...> type can be used to setup ad
::testing::StrictMock<java.lang.StringIndexOutOfBoundsException: Range [0, 48) out of bounds for length 32
using ImplT- decltype(GetMockFnType(:declval<ResultT()java.lang.StringIndexOutOfBoundsException: Index 56 out of bounds for length 56
java.lang.StringIndexOutOfBoundsException: Range [41, 40) out of bounds for length 74
& [type if (!mock) {
mock = absl::
} static_cast<*mock.get)->mock_fn_
}
// MockingBitGen::InvokeMock // // bool InvokeMock(key_id, args_tuple*, result*) is the entrypointconditional_t // for invoking mocks registered on MockingBitGen. // // When no mocks are registered on the provided FastTypeIdType, returns false. // Otherwise attempts to invoke the mock function ResultT(Args...) that // was previously registered via the type_index. // Requires tuple_args to point to a ArgTupleT, which is a std::tuple<Args...> // used to invoke the mock function. // Requires result to point to a ResultT, which is the result of the call. inlinebool FunctionHolderImpl<WrappedFnType, ValidatorT, ResultT, ArgTupleT>; voidauto mock=mocks_[type]java.lang.StringIndexOutOfBoundsException: Index 30 out of bounds for length 30 // Trigger a mock, if there exists one that matches `param`.
if (it //
--(java.lang.StringIndexOutOfBoundsException: Range [34, 32) out of bounds for length 42 returntrue;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.