diff --git a/include/fakeit/Behavior.hpp b/include/fakeit/Behavior.hpp index b0678fa9..e90f6a64 100644 --- a/include/fakeit/Behavior.hpp +++ b/include/fakeit/Behavior.hpp @@ -16,6 +16,7 @@ #include "mockutils/DefaultValue.hpp" #include "fakeit/FakeitExceptions.hpp" +#include "fakeit/FakeIt.hpp" namespace fakeit { @@ -67,7 +68,9 @@ struct CallForever: public Behavior { template struct ThrowUnexpectedMethodCall: public Behavior { virtual R invoke(arglist&... args) override { - throw UnexpectedMethodCallException(); + UnexpectedMethodCallException e; + FakeIt::log(e); + throw e; } virtual bool isDone() override { diff --git a/include/fakeit/DefaultErrorFormatter.hpp b/include/fakeit/DefaultErrorFormatter.hpp deleted file mode 100644 index 9ef9108e..00000000 --- a/include/fakeit/DefaultErrorFormatter.hpp +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright (c) 2014 Eran Pe'er. - * - * This program is made available under the terms of the MIT License. - * - * Created on Mar 10, 2014 - */ - -#ifndef DefaultErrorFormatter_h__ -#define DefaultErrorFormatter_h__ - -#include -#include "fakeit/ErrorFormatter.hpp" - -namespace fakeit { - -//struct DefaultErrorFormatter: public virtual ErrorFormatter { -// virtual ~DefaultErrorFormatter() = default; -// -// virtual std::string buildNoOtherInvocationsVerificationErrorMsg( // -// std::vector& allIvocations, // -// std::vector& unverifedIvocations) override { -// -// auto format = std::string("found ") + std::to_string(unverifedIvocations.size()) + " non verified invocations.\n"; -// return format + toString(unverifedIvocations); -// } -// -// virtual std::string buildExactVerificationErrorMsg( -// std::vector& expectedPattern, -// std::vector& actualSequence, -// int expectedInvocationCount, int count) override { -// return std::string("expected ") + std::to_string(expectedInvocationCount) + " but was " + std::to_string(count); -// } -// -// virtual std::string buildAtLeastVerificationErrorMsg( -// std::vector& expectedPattern, -// std::vector& actualSequence, -// int expectedInvocationCount, int count) override { -// return std::string("Expected invocation sequence could not be found in actual invocation order"); -// } -//private: -//private: -// std::string toString(std::vector& unverifedIvocations) { -// std::string formattedString; -// for (auto invocation : unverifedIvocations) { -// formattedString += invocation->format(); -// formattedString += '\n'; -// } -// return formattedString; -// } -//}static defaultErrorFormatter; - -} -#endif /* DefaultErrorFormatter_h__ */ diff --git a/include/fakeit/DefaultLogger.hpp b/include/fakeit/DefaultLogger.hpp new file mode 100644 index 00000000..6e670c9c --- /dev/null +++ b/include/fakeit/DefaultLogger.hpp @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2014 Eran Pe'er. + * + * This program is made available under the terms of the MIT License. + * + * Created on Mar 10, 2014 + */ + +#ifndef DefaultLogger_h__ +#define DefaultLogger_h__ + +#include "fakeit/Logger.hpp" + +namespace fakeit { + +struct DefaultLogger: public fakeit::Logger { + + virtual void log(UnexpectedMethodCallException& e) override { + } + + virtual void log(SequenceVerificationException& e) override { + } + + virtual void log(NoMoreInvocationsVerificationException& e) override { + } + +}; +} + +#endif //Logger_h__ diff --git a/include/fakeit/DomainObjects.hpp b/include/fakeit/DomainObjects.hpp index 85f9258c..8f8fc636 100644 --- a/include/fakeit/DomainObjects.hpp +++ b/include/fakeit/DomainObjects.hpp @@ -13,7 +13,7 @@ namespace fakeit { -template +template struct MockObject { virtual C & get() = 0; }; diff --git a/include/fakeit/ErrorFormatter.hpp b/include/fakeit/ErrorFormatter.hpp deleted file mode 100644 index 0e03557e..00000000 --- a/include/fakeit/ErrorFormatter.hpp +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright (c) 2014 Eran Pe'er. - * - * This program is made available under the terms of the MIT License. - * - * Created on Mar 10, 2014 - */ - -#ifndef ErrorFormatter_h__ -#define ErrorFormatter_h__ - -#include -#include "fakeit/ActualInvocation.hpp" -#include "fakeit/Sequence.hpp" - -namespace fakeit { - -//struct ErrorFormatter { -// virtual ~ErrorFormatter() = default; -// -// virtual std::string buildNoOtherInvocationsVerificationErrorMsg( // -// std::vector& allIvocations, // -// std::vector& unverifedIvocations) = 0; -// -// virtual std::string buildExactVerificationErrorMsg(std::vector& expectedPattern, std::vector& actualSequence, -// int expectedInvocationCount, int count)= 0; -// -// virtual std::string buildAtLeastVerificationErrorMsg(std::vector& expectedPattern, -// std::vector& actualSequence, int expectedInvocationCount, int count)= 0; -//}; - -} - -#endif //ErrorFormatter_h__ diff --git a/include/fakeit/FakeIt.hpp b/include/fakeit/FakeIt.hpp new file mode 100644 index 00000000..8c90a331 --- /dev/null +++ b/include/fakeit/FakeIt.hpp @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2014 Eran Pe'er. + * + * This program is made available under the terms of the MIT License. + * + * Created on Mar 10, 2014 + */ + +#ifndef FakeIt_h__ +#define FakeIt_h__ + +#include "fakeit/DefaultLogger.hpp" + +namespace fakeit { + +struct FakeIt { + + static void log(UnexpectedMethodCallException& e){ + getLogger().log(e); + } + + static void log(SequenceVerificationException& e){ + getLogger().log(e); + } + + static void log(NoMoreInvocationsVerificationException& e){ + getLogger().log(e); + } + + static Logger& getLogger(){ + static DefaultLogger logger; + return logger; + } +}; + +} + + +#endif // diff --git a/include/fakeit/FakeitExceptions.hpp b/include/fakeit/FakeitExceptions.hpp index 17cb31be..760d9e93 100644 --- a/include/fakeit/FakeitExceptions.hpp +++ b/include/fakeit/FakeitExceptions.hpp @@ -11,23 +11,24 @@ #include #include "fakeit/Sequence.hpp" -#include "fakeit/DomainObjects.hpp" #include "mockutils/Formatter.hpp" namespace fakeit { -class FakeitException { -}; +struct FakeitException { -struct UnexpectedMethodCallException: public FakeitException { - UnexpectedMethodCallException() { - } + virtual std::string what() const = 0; - friend std::ostream & operator<<(std::ostream &os, const UnexpectedMethodCallException& val) { - os << std::string("UnexpectedMethodCallException: could not find any recorded behavior to support this method call"); + friend std::ostream & operator<<(std::ostream &os, const FakeitException& val) { + os << val.what(); return os; } +}; +struct UnexpectedMethodCallException: public FakeitException { + virtual std::string what() const override { + return std::string("UnexpectedMethodCallException: could not find any recorded behavior to support this method call"); + } }; enum class VerificationType { @@ -35,10 +36,6 @@ enum class VerificationType { }; struct VerificationException: public FakeitException { - - VerificationException() { - } - virtual VerificationType verificationType() const = 0; }; @@ -61,10 +58,9 @@ struct NoMoreInvocationsVerificationException: public VerificationException { return _unverifedIvocations; } - friend std::ostream & operator<<(std::ostream &os, const NoMoreInvocationsVerificationException& val) { - os << std::string("VerificationException: expected no more invocations but found ") // - .append(std::to_string(val.unverifedIvocations().size())); - return os; + virtual std::string what() const override { + return std::string("VerificationException: expected no more invocations but found ") // + .append(std::to_string(unverifedIvocations().size())); } private: @@ -103,13 +99,12 @@ struct SequenceVerificationException: public VerificationException { return _actualCount; } - friend std::ostream & operator<<(std::ostream &os, const SequenceVerificationException& val) { - os << std::string("VerificationException: expected ") // - .append(val.verificationType() == fakeit::VerificationType::Exact ? "exactly " : "at least ") // - .append(std::to_string(val.expectedCount())) // + virtual std::string what() const override { + return std::string("VerificationException: expected ") // + .append(verificationType() == fakeit::VerificationType::Exact ? "exactly " : "at least ") // + .append(std::to_string(expectedCount())) // .append(" invocations but was ") // - .append(std::to_string(val.actualCount())); - return os; + .append(std::to_string(actualCount())); } private: @@ -119,7 +114,5 @@ struct SequenceVerificationException: public VerificationException { const int _actualCount; }; - - } #endif // FakeitExceptions_h__ diff --git a/include/fakeit/Logger.hpp b/include/fakeit/Logger.hpp new file mode 100644 index 00000000..256a9d36 --- /dev/null +++ b/include/fakeit/Logger.hpp @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2014 Eran Pe'er. + * + * This program is made available under the terms of the MIT License. + * + * Created on Mar 10, 2014 + */ + +#ifndef Logger_h__ +#define Logger_h__ + +#include "fakeit/FakeitExceptions.hpp" + +namespace fakeit { + +struct Logger { + virtual ~Logger() = default; + + virtual void log(UnexpectedMethodCallException& e) = 0; + + virtual void log(SequenceVerificationException& e) = 0; + + virtual void log(NoMoreInvocationsVerificationException& e) = 0; + +}; +} +#endif //Logger_h__ diff --git a/include/fakeit/MethodMock.hpp b/include/fakeit/MethodMock.hpp index 1dd7144e..4b602294 100644 --- a/include/fakeit/MethodMock.hpp +++ b/include/fakeit/MethodMock.hpp @@ -211,7 +211,9 @@ struct MethodMock: public virtual MethodInvocationHandler, public args...) }; auto methodInvocationMock = getMethodInvocationMockForActualArgs(*actualInvoaction); if (!methodInvocationMock) { - throw UnexpectedMethodCallException(); + UnexpectedMethodCallException e; + FakeIt::log(e); + throw e; } auto matcher = methodInvocationMock->getMatcher(); actualInvoaction->setActualMatcher(matcher); diff --git a/include/fakeit/Mock.hpp b/include/fakeit/Mock.hpp index 31450407..e9a94b90 100644 --- a/include/fakeit/Mock.hpp +++ b/include/fakeit/Mock.hpp @@ -17,19 +17,27 @@ #include "mockutils/DynamicProxy.hpp" #include "fakeit/StubbingImpl.hpp" #include "fakeit/DomainObjects.hpp" +#include "fakeit/DefaultLogger.hpp" namespace fakeit { using namespace fakeit; +static DefaultLogger logger; + template class Mock: private MockObject, public virtual ActualInvocationsSource { private: DynamicProxy proxy; - C* instance;bool isSpy; + C* instance; // + bool isSpy; void unmocked() { - throw UnexpectedMethodCallException { }; + class UnmockedMethodInvocation: public UnexpectedMethodCallException { + + } e; + FakeIt::log(e); + throw e; } static C* createFakeInstance() { @@ -100,28 +108,12 @@ class Mock: private MockObject, public virtual ActualInvocationsSource { std::shared_ptr>(new MethodStubbingContextImpl(*this, vMethod))); } - void Stub() { - } - template DataMemberStubbingRoot stubDataMember(DATA_TYPE C::*member, const arglist&... ctorargs) { proxy.stubDataMember(member, ctorargs...); return DataMemberStubbingRoot(); } -public: - - static_assert(std::is_polymorphic::value, "Can only mock a polymorphic type"); - - Mock() : - Mock(*(createFakeInstance())) { - isSpy = false; - } - - Mock(C &obj) : - proxy { obj }, instance(&obj), isSpy(true) { - } - /** * Return all actual invocations of this mock. */ @@ -133,6 +125,20 @@ class Mock: private MockObject, public virtual ActualInvocationsSource { } } + Mock(C &obj, bool isSpy) : + proxy { obj }, instance(&obj), isSpy(isSpy) { + } + +public: + + static_assert(std::is_polymorphic::value, "Can only mock a polymorphic type"); + + Mock() : Mock(*(createFakeInstance()), false) { + } + + Mock(C &obj) :Mock(obj, true) { + } + virtual ~Mock() { if (isSpy) return; @@ -162,12 +168,6 @@ class Mock: private MockObject, public virtual ActualInvocationsSource { return stubDataMember(member, ctorargs...); } - template::value>::type> - void Stub(H head, M ... tail) { - When(head); - Stub(tail...); - } - template::value>::type> FunctionStubbingRoot operator [](R (C::*vMethod)(arglist...) const) { auto methodWithoutConstVolatile = reinterpret_cast(vMethod); diff --git a/include/fakeit/SpyFunctor.hpp b/include/fakeit/SpyFunctor.hpp index 87466bf9..03a13abc 100644 --- a/include/fakeit/SpyFunctor.hpp +++ b/include/fakeit/SpyFunctor.hpp @@ -20,13 +20,13 @@ class SpyFunctor { } template - void spy(MethodStubbingBase& root) { - C& obj = root.getMockObject().get(); - auto methodFromOriginalVT = root.getOriginalMethod(); + void spy(MethodStubbingBase& context) { + C& obj = context.get(); + auto methodFromOriginalVT = context.getOriginalMethod(); std::shared_ptr> ptr { new ReturnDelegateValue(obj, methodFromOriginalVT) }; - root.AppendAction(ptr); - root.apply(); + context.AppendAction(ptr); + context.apply(); } public: diff --git a/include/fakeit/StubbingImpl.hpp b/include/fakeit/StubbingImpl.hpp index 16d4f8fa..ef2fd178 100644 --- a/include/fakeit/StubbingImpl.hpp +++ b/include/fakeit/StubbingImpl.hpp @@ -23,7 +23,7 @@ #include "fakeit/Stubbing.hpp" #include "fakeit/Sequence.hpp" #include "fakeit/ActualInvocation.hpp" -#include "fakeit/ErrorFormatter.hpp" +#include "fakeit/Logger.hpp" namespace fakeit { @@ -66,8 +66,14 @@ class MethodStubbingBase: public RecordedMethodInvocation, // return stubbingContext->getOriginalMethod(); } - MockObject& getMockObject() { - return stubbingContext->getMock(); +// virtual R invoke(arglist&... args) { +// C& instance = getMockObject().get(); +// auto vMethod = getOriginalMethod(); +// return ((&instance)->*vMethod)(args...); +// } + + C& get() { + return stubbingContext->getMock().get(); } std::shared_ptr> buildInitialMethodBody() { diff --git a/include/fakeit/UsingFunctor.hpp b/include/fakeit/UsingFunctor.hpp index f7339181..0fd68d5b 100644 --- a/include/fakeit/UsingFunctor.hpp +++ b/include/fakeit/UsingFunctor.hpp @@ -10,12 +10,12 @@ #define UsingFunctor_hpp_ #include -#include #include "fakeit/StubbingImpl.hpp" #include "fakeit/Stubbing.hpp" #include "fakeit/Sequence.hpp" #include "fakeit/SortInvocations.hpp" +#include "fakeit/FakeIt.hpp" namespace fakeit { @@ -79,7 +79,8 @@ class UsingFunctor { std::set involvedMocks; std::vector expectedPattern; - int expectedInvocationCount;bool _isActive; + int expectedInvocationCount; + bool _isActive; static inline int AT_LEAST_ONCE() { return -1; @@ -211,7 +212,7 @@ class UsingFunctor { ExactVerificationException e(expectedPattern, actualSequence, expectedInvocationCount, count); - std::cout << e << std::endl; + fakeit::FakeIt::log(e); throw e; } @@ -228,7 +229,7 @@ class UsingFunctor { }; AtLeastVerificationException e(expectedPattern, actualSequence, -expectedInvocationCount, count); - std::cout << e << std::endl; + FakeIt::log(e); throw e; }