From d8612dc3b5becdda32a7af334c61b4b3f7069715 Mon Sep 17 00:00:00 2001 From: FranckRJ Date: Sat, 11 May 2024 12:59:41 +0200 Subject: [PATCH] Minor changes. --- include/fakeit/api_macros.hpp | 8 +++--- include/mockutils/DynamicProxy.hpp | 6 +++-- tests/multiple_translation_units_stub.cpp | 17 +++++++------ tests/multiple_translation_units_stub.h | 16 +++++++----- .../multiple_translation_units_stub_test.cpp | 25 +++++++++++++------ 5 files changed, 45 insertions(+), 27 deletions(-) diff --git a/include/fakeit/api_macros.hpp b/include/fakeit/api_macros.hpp index ae89b756..dcf2fb56 100644 --- a/include/fakeit/api_macros.hpp +++ b/include/fakeit/api_macros.hpp @@ -48,16 +48,16 @@ (mock).template stub(CONST_OVERLOADED_METHOD_PTR( mock , method, prototype )).setMethodDetails(#mock,#method) #define RefOverloadedMethod(mock, method, prototype) \ - (mock).template stub<__COUNTER__>(REF_OVERLOADED_METHOD_PTR( mock , method, prototype )).setMethodDetails(#mock,#method) + (mock).template stub(REF_OVERLOADED_METHOD_PTR( mock , method, prototype )).setMethodDetails(#mock,#method) #define ConstRefOverloadedMethod(mock, method, prototype) \ - (mock).template stub<__COUNTER__>(CONST_REF_OVERLOADED_METHOD_PTR( mock , method, prototype )).setMethodDetails(#mock,#method) + (mock).template stub(CONST_REF_OVERLOADED_METHOD_PTR( mock , method, prototype )).setMethodDetails(#mock,#method) #define RValRefOverloadedMethod(mock, method, prototype) \ - (mock).template stub<__COUNTER__>(R_VAL_REF_OVERLOADED_METHOD_PTR( mock , method, prototype )).setMethodDetails(#mock,#method) + (mock).template stub(R_VAL_REF_OVERLOADED_METHOD_PTR( mock , method, prototype )).setMethodDetails(#mock,#method) #define ConstRValRefOverloadedMethod(mock, method, prototype) \ - (mock).template stub<__COUNTER__>(CONST_R_VAL_REF_OVERLOADED_METHOD_PTR( mock , method, prototype )).setMethodDetails(#mock,#method) + (mock).template stub(CONST_R_VAL_REF_OVERLOADED_METHOD_PTR( mock , method, prototype )).setMethodDetails(#mock,#method) #define Verify(...) \ Verify( __VA_ARGS__ ).setFileInfo(__FILE__, __LINE__, __func__) diff --git a/include/mockutils/DynamicProxy.hpp b/include/mockutils/DynamicProxy.hpp index 5708c14c..62d28857 100644 --- a/include/mockutils/DynamicProxy.hpp +++ b/include/mockutils/DynamicProxy.hpp @@ -34,7 +34,9 @@ namespace fakeit { { unsigned int offset = 0; for (; offset < _offsets.size(); offset++) { - if (_offsets[offset] == id) { + // "_methodMocks[offset] != nullptr" checked to guarantee that "_offsets[offset]" was really set and + // doesn't just contain the default id. + if (_offsets[offset] == id && _methodMocks[offset] != nullptr) { break; } } @@ -62,7 +64,7 @@ namespace fakeit { DynamicProxy(C &inst) : _instancePtr(&inst), _methodMocks(VTUtils::getVTSize()), - _offsets(VTUtils::getVTSize(), std::numeric_limits::max()), // TODO: should be size_t ? + _offsets(VTUtils::getVTSize(), std::numeric_limits::max()), _invocationHandlers(_methodMocks, _offsets) { _originalVt.copyFrom(VirtualTable::getVTable(*_instancePtr)); _originalVt.setCookie(InvocationHandlerCollection::VtCookieIndex, &_invocationHandlers); diff --git a/tests/multiple_translation_units_stub.cpp b/tests/multiple_translation_units_stub.cpp index 1997e91b..74be8d90 100644 --- a/tests/multiple_translation_units_stub.cpp +++ b/tests/multiple_translation_units_stub.cpp @@ -1,12 +1,15 @@ #include "multiple_translation_units_stub.h" +namespace multiple_tu { -void stubFunc2(fakeit::Mock& mock) -{ - fakeit::When(Method(mock, func2)).AlwaysReturn("String"); -} + void stubFunc(fakeit::Mock& mock) + { + fakeit::When(Method(mock, func)).AlwaysReturn(3); + } + + void stubFunc2(fakeit::Mock& mock) + { + fakeit::When(Method(mock, func2)).AlwaysReturn("String"); + } -void stubFunc(fakeit::Mock& mock) -{ - fakeit::When(Method(mock, func)).AlwaysReturn(3); } diff --git a/tests/multiple_translation_units_stub.h b/tests/multiple_translation_units_stub.h index 084cf17b..747a4f41 100644 --- a/tests/multiple_translation_units_stub.h +++ b/tests/multiple_translation_units_stub.h @@ -4,10 +4,14 @@ #include "fakeit.hpp" -struct SomeInterface { - virtual int func() = 0; - virtual std::string func2() = 0; -}; +namespace multiple_tu { -void stubFunc2(fakeit::Mock& mock); -void stubFunc(fakeit::Mock& mock); + struct SomeInterface { + virtual int func() = 0; + virtual std::string func2() = 0; + }; + + void stubFunc(fakeit::Mock& mock); + void stubFunc2(fakeit::Mock& mock); + +} diff --git a/tests/multiple_translation_units_stub_test.cpp b/tests/multiple_translation_units_stub_test.cpp index b0764352..4435816d 100644 --- a/tests/multiple_translation_units_stub_test.cpp +++ b/tests/multiple_translation_units_stub_test.cpp @@ -3,22 +3,31 @@ #include "multiple_translation_units_stub.h" using namespace fakeit; +using namespace multiple_tu; struct MultipleTranslationUnitsStub : tpunit::TestFixture { - MultipleTranslationUnitsStub() - : tpunit::TestFixture( + + MultipleTranslationUnitsStub() : + tpunit::TestFixture( TEST(MultipleTranslationUnitsStub::NoCollidingIds) - ) - {} + ) + { + } + + // TODO: more tests to be sure I didn't broke something by changing stuff. + // TODO: add option to revert to old ID generation and test that tests crash. void NoCollidingIds() { Mock mock; - SomeInterface &i = mock.get(); - stubFunc2(mock); - When(Method(mock, func)).Return(1); + stubFunc(mock); + When(Method(mock, func2)).Return("Something"); + + SomeInterface &i = mock.get(); - i.func2(); // Uncatchable write access violation if ids collide + // Uncatchable write access violation if ids collide. + i.func(); + i.func2(); } } __MultipleTranslationUnitsStub;