Skip to content

Commit ba8c1df

Browse files
committed
add logging basic support.
1 parent 6f70cca commit ba8c1df

13 files changed

+164
-151
lines changed

include/fakeit/Behavior.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include "mockutils/DefaultValue.hpp"
1818
#include "fakeit/FakeitExceptions.hpp"
19+
#include "fakeit/FakeIt.hpp"
1920

2021
namespace fakeit {
2122

@@ -67,7 +68,9 @@ struct CallForever: public Behavior<R, arglist...> {
6768
template<typename R, typename ... arglist>
6869
struct ThrowUnexpectedMethodCall: public Behavior<R, arglist...> {
6970
virtual R invoke(arglist&... args) override {
70-
throw UnexpectedMethodCallException();
71+
UnexpectedMethodCallException e;
72+
FakeIt::log(e);
73+
throw e;
7174
}
7275

7376
virtual bool isDone() override {

include/fakeit/DefaultErrorFormatter.hpp

Lines changed: 0 additions & 54 deletions
This file was deleted.

include/fakeit/DefaultLogger.hpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright (c) 2014 Eran Pe'er.
3+
*
4+
* This program is made available under the terms of the MIT License.
5+
*
6+
* Created on Mar 10, 2014
7+
*/
8+
9+
#ifndef DefaultLogger_h__
10+
#define DefaultLogger_h__
11+
12+
#include "fakeit/Logger.hpp"
13+
14+
namespace fakeit {
15+
16+
struct DefaultLogger: public fakeit::Logger {
17+
18+
virtual void log(UnexpectedMethodCallException& e) override {
19+
}
20+
21+
virtual void log(SequenceVerificationException& e) override {
22+
}
23+
24+
virtual void log(NoMoreInvocationsVerificationException& e) override {
25+
}
26+
27+
};
28+
}
29+
30+
#endif //Logger_h__

include/fakeit/DomainObjects.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
namespace fakeit {
1515

16-
template <typename C>
16+
template<typename C>
1717
struct MockObject {
1818
virtual C & get() = 0;
1919
};

include/fakeit/ErrorFormatter.hpp

Lines changed: 0 additions & 34 deletions
This file was deleted.

include/fakeit/FakeIt.hpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright (c) 2014 Eran Pe'er.
3+
*
4+
* This program is made available under the terms of the MIT License.
5+
*
6+
* Created on Mar 10, 2014
7+
*/
8+
9+
#ifndef FakeIt_h__
10+
#define FakeIt_h__
11+
12+
#include "fakeit/DefaultLogger.hpp"
13+
14+
namespace fakeit {
15+
16+
struct FakeIt {
17+
18+
static void log(UnexpectedMethodCallException& e){
19+
getLogger().log(e);
20+
}
21+
22+
static void log(SequenceVerificationException& e){
23+
getLogger().log(e);
24+
}
25+
26+
static void log(NoMoreInvocationsVerificationException& e){
27+
getLogger().log(e);
28+
}
29+
30+
static Logger& getLogger(){
31+
static DefaultLogger logger;
32+
return logger;
33+
}
34+
};
35+
36+
}
37+
38+
39+
#endif //

include/fakeit/FakeitExceptions.hpp

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,34 +11,31 @@
1111

1212
#include <functional>
1313
#include "fakeit/Sequence.hpp"
14-
#include "fakeit/DomainObjects.hpp"
1514
#include "mockutils/Formatter.hpp"
1615

1716
namespace fakeit {
1817

19-
class FakeitException {
20-
};
18+
struct FakeitException {
2119

22-
struct UnexpectedMethodCallException: public FakeitException {
23-
UnexpectedMethodCallException() {
24-
}
20+
virtual std::string what() const = 0;
2521

26-
friend std::ostream & operator<<(std::ostream &os, const UnexpectedMethodCallException& val) {
27-
os << std::string("UnexpectedMethodCallException: could not find any recorded behavior to support this method call");
22+
friend std::ostream & operator<<(std::ostream &os, const FakeitException& val) {
23+
os << val.what();
2824
return os;
2925
}
26+
};
3027

28+
struct UnexpectedMethodCallException: public FakeitException {
29+
virtual std::string what() const override {
30+
return std::string("UnexpectedMethodCallException: could not find any recorded behavior to support this method call");
31+
}
3132
};
3233

3334
enum class VerificationType {
3435
Exact, AtLeast, NoMoreInvocatoins
3536
};
3637

3738
struct VerificationException: public FakeitException {
38-
39-
VerificationException() {
40-
}
41-
4239
virtual VerificationType verificationType() const = 0;
4340
};
4441

@@ -61,10 +58,9 @@ struct NoMoreInvocationsVerificationException: public VerificationException {
6158
return _unverifedIvocations;
6259
}
6360

64-
friend std::ostream & operator<<(std::ostream &os, const NoMoreInvocationsVerificationException& val) {
65-
os << std::string("VerificationException: expected no more invocations but found ") //
66-
.append(std::to_string(val.unverifedIvocations().size()));
67-
return os;
61+
virtual std::string what() const override {
62+
return std::string("VerificationException: expected no more invocations but found ") //
63+
.append(std::to_string(unverifedIvocations().size()));
6864
}
6965

7066
private:
@@ -103,13 +99,12 @@ struct SequenceVerificationException: public VerificationException {
10399
return _actualCount;
104100
}
105101

106-
friend std::ostream & operator<<(std::ostream &os, const SequenceVerificationException& val) {
107-
os << std::string("VerificationException: expected ") //
108-
.append(val.verificationType() == fakeit::VerificationType::Exact ? "exactly " : "at least ") //
109-
.append(std::to_string(val.expectedCount())) //
102+
virtual std::string what() const override {
103+
return std::string("VerificationException: expected ") //
104+
.append(verificationType() == fakeit::VerificationType::Exact ? "exactly " : "at least ") //
105+
.append(std::to_string(expectedCount())) //
110106
.append(" invocations but was ") //
111-
.append(std::to_string(val.actualCount()));
112-
return os;
107+
.append(std::to_string(actualCount()));
113108
}
114109

115110
private:
@@ -119,7 +114,5 @@ struct SequenceVerificationException: public VerificationException {
119114
const int _actualCount;
120115
};
121116

122-
123-
124117
}
125118
#endif // FakeitExceptions_h__

include/fakeit/Logger.hpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright (c) 2014 Eran Pe'er.
3+
*
4+
* This program is made available under the terms of the MIT License.
5+
*
6+
* Created on Mar 10, 2014
7+
*/
8+
9+
#ifndef Logger_h__
10+
#define Logger_h__
11+
12+
#include "fakeit/FakeitExceptions.hpp"
13+
14+
namespace fakeit {
15+
16+
struct Logger {
17+
virtual ~Logger() = default;
18+
19+
virtual void log(UnexpectedMethodCallException& e) = 0;
20+
21+
virtual void log(SequenceVerificationException& e) = 0;
22+
23+
virtual void log(NoMoreInvocationsVerificationException& e) = 0;
24+
25+
};
26+
}
27+
#endif //Logger_h__

include/fakeit/MethodMock.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,9 @@ struct MethodMock: public virtual MethodInvocationHandler<R, arglist...>, public
211211
args...) };
212212
auto methodInvocationMock = getMethodInvocationMockForActualArgs(*actualInvoaction);
213213
if (!methodInvocationMock) {
214-
throw UnexpectedMethodCallException();
214+
UnexpectedMethodCallException e;
215+
FakeIt::log(e);
216+
throw e;
215217
}
216218
auto matcher = methodInvocationMock->getMatcher();
217219
actualInvoaction->setActualMatcher(matcher);

0 commit comments

Comments
 (0)