Skip to content

Commit

Permalink
add logging basic support.
Browse files Browse the repository at this point in the history
  • Loading branch information
eranpeer committed Jul 8, 2014
1 parent 6f70cca commit ba8c1df
Show file tree
Hide file tree
Showing 13 changed files with 164 additions and 151 deletions.
5 changes: 4 additions & 1 deletion include/fakeit/Behavior.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "mockutils/DefaultValue.hpp"
#include "fakeit/FakeitExceptions.hpp"
#include "fakeit/FakeIt.hpp"

namespace fakeit {

Expand Down Expand Up @@ -67,7 +68,9 @@ struct CallForever: public Behavior<R, arglist...> {
template<typename R, typename ... arglist>
struct ThrowUnexpectedMethodCall: public Behavior<R, arglist...> {
virtual R invoke(arglist&... args) override {
throw UnexpectedMethodCallException();
UnexpectedMethodCallException e;
FakeIt::log(e);
throw e;
}

virtual bool isDone() override {
Expand Down
54 changes: 0 additions & 54 deletions include/fakeit/DefaultErrorFormatter.hpp

This file was deleted.

30 changes: 30 additions & 0 deletions include/fakeit/DefaultLogger.hpp
Original file line number Diff line number Diff line change
@@ -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__
2 changes: 1 addition & 1 deletion include/fakeit/DomainObjects.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace fakeit {

template <typename C>
template<typename C>
struct MockObject {
virtual C & get() = 0;
};
Expand Down
34 changes: 0 additions & 34 deletions include/fakeit/ErrorFormatter.hpp

This file was deleted.

39 changes: 39 additions & 0 deletions include/fakeit/FakeIt.hpp
Original file line number Diff line number Diff line change
@@ -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 //
41 changes: 17 additions & 24 deletions include/fakeit/FakeitExceptions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,31 @@

#include <functional>
#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 {
Exact, AtLeast, NoMoreInvocatoins
};

struct VerificationException: public FakeitException {

VerificationException() {
}

virtual VerificationType verificationType() const = 0;
};

Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -119,7 +114,5 @@ struct SequenceVerificationException: public VerificationException {
const int _actualCount;
};



}
#endif // FakeitExceptions_h__
27 changes: 27 additions & 0 deletions include/fakeit/Logger.hpp
Original file line number Diff line number Diff line change
@@ -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__
4 changes: 3 additions & 1 deletion include/fakeit/MethodMock.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,9 @@ struct MethodMock: public virtual MethodInvocationHandler<R, arglist...>, public
args...) };
auto methodInvocationMock = getMethodInvocationMockForActualArgs(*actualInvoaction);
if (!methodInvocationMock) {
throw UnexpectedMethodCallException();
UnexpectedMethodCallException e;
FakeIt::log(e);
throw e;
}
auto matcher = methodInvocationMock->getMatcher();
actualInvoaction->setActualMatcher(matcher);
Expand Down
Loading

0 comments on commit ba8c1df

Please sign in to comment.