Skip to content

Commit

Permalink
add basic formatting of verification exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
eranpeer committed Jun 21, 2014
1 parent 1861c27 commit 6f70cca
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 20 deletions.
30 changes: 27 additions & 3 deletions include/fakeit/FakeitExceptions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <functional>
#include "fakeit/Sequence.hpp"
#include "fakeit/DomainObjects.hpp"
#include "mockutils/Formatter.hpp"

namespace fakeit {

Expand All @@ -21,6 +22,12 @@ class FakeitException {
struct UnexpectedMethodCallException: public FakeitException {
UnexpectedMethodCallException() {
}

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");
return os;
}

};

enum class VerificationType {
Expand All @@ -32,7 +39,7 @@ struct VerificationException: public FakeitException {
VerificationException() {
}

virtual VerificationType verificationType() = 0;
virtual VerificationType verificationType() const = 0;
};

struct NoMoreInvocationsVerificationException: public VerificationException {
Expand All @@ -42,7 +49,7 @@ struct NoMoreInvocationsVerificationException: public VerificationException {
VerificationException(), _allIvocations(allIvocations), _unverifedIvocations(unverifedIvocations) { //
}

virtual VerificationType verificationType() override {
virtual VerificationType verificationType() const override {
return VerificationType::NoMoreInvocatoins;
}

Expand All @@ -54,11 +61,18 @@ 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;
}

private:
const std::vector<Invocation*> _allIvocations;
const std::vector<Invocation*> _unverifedIvocations;
};


struct SequenceVerificationException: public VerificationException {
SequenceVerificationException( //
std::vector<Sequence*>& expectedPattern, //
Expand Down Expand Up @@ -89,13 +103,23 @@ 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())) //
.append(" invocations but was ") //
.append(std::to_string(val.actualCount()));
return os;
}

private:
const std::vector<Sequence*> _expectedPattern;
const std::vector<Invocation*> _actualSequence;
const int _expectedCount;
const int _actualCount;
};

}


}
#endif // FakeitExceptions_h__
3 changes: 0 additions & 3 deletions include/fakeit/Invocation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@
#include <iostream>
#include <sstream>

#include "mockutils/TuplePrinter.hpp"
#include "mockutils/Macros.hpp"

#include "fakeit/DomainObjects.hpp"

namespace fakeit {
Expand Down
14 changes: 10 additions & 4 deletions include/fakeit/UsingFunctor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#define UsingFunctor_hpp_

#include <set>
#include <iostream>

#include "fakeit/StubbingImpl.hpp"
#include "fakeit/Stubbing.hpp"
Expand Down Expand Up @@ -203,12 +204,15 @@ class UsingFunctor {
SequenceVerificationException(expectedPattern, actualSequence, expectedCount, actualCount) {
}

virtual VerificationType verificationType() override {
virtual VerificationType verificationType()const override {
return VerificationType::Exact;
}
};

throw ExactVerificationException(expectedPattern, actualSequence, expectedInvocationCount, count);

ExactVerificationException e(expectedPattern, actualSequence, expectedInvocationCount, count);
std::cout << e << std::endl;
throw e;
}

void throwAtLeastVerificationException(std::vector<Invocation*> actualSequence, int count) {
Expand All @@ -218,12 +222,14 @@ class UsingFunctor {
SequenceVerificationException(expectedPattern, actualSequence, expectedCount, actualCount) {
}

virtual VerificationType verificationType() override {
virtual VerificationType verificationType()const override {
return VerificationType::AtLeast;
}
};

throw AtLeastVerificationException(expectedPattern, actualSequence, -expectedInvocationCount, count);
AtLeastVerificationException e(expectedPattern, actualSequence, -expectedInvocationCount, count);
std::cout << e << std::endl;
throw e;
}

};
Expand Down
6 changes: 6 additions & 0 deletions include/mockutils/Formatter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ template<class T> struct Formatter {
}
};

template<> struct Formatter<bool> {
static std::string format(const bool& val) {
return val? "true":"false";
}
};

template<> struct Formatter<int> {
static std::string format(const int& val) {
return std::to_string(val);
Expand Down
13 changes: 9 additions & 4 deletions tests/stubbing_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,22 @@ struct BasicStubbing: tpunit::TestFixture {
virtual void proc(int) = 0;
};

template <typename T> std::string to_string(T& val){
std::stringstream stream;
stream << val;
return stream.str();
}

void calling_an_unstubbed_method_should_raise_UnmockedMethodCallException() {
Mock<SomeInterface> mock;
SomeInterface &i = mock.get();
try {
i.func(1);
FAIL();
} catch(UnexpectedMethodCallException&)
} catch(UnexpectedMethodCallException& e)
{
// std::string expected("Unexpected method call. Could not find recorded implementation to support method call");
// std::string actual(e.what());
// ASSERT_EQUAL(expected,actual);
std::string expected("UnexpectedMethodCallException: could not find any recorded behavior to support this method call");
ASSERT_EQUAL(expected,to_string(e));
}
}

Expand Down
22 changes: 16 additions & 6 deletions tests/verification_error_formatting_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ struct ErrorFormattingTests: tpunit::TestFixture {
{
}

template <typename T> std::string to_string(T& val){
std::stringstream stream;
stream << val;
return stream.str();
}

void check_atleast_verification_exception() {
Mock<SomeInterface> mock;
try {
Expand All @@ -63,9 +69,9 @@ struct ErrorFormattingTests: tpunit::TestFixture {
ASSERT_EQUAL(1, e.expectedCount());
ASSERT_EQUAL(0, e.actualSequence().size());
ASSERT_EQUAL(1, e.expectedPattern().size());
// std::string expected("Expected invocation sequence could not be found in actual invocation order");
// std::string actual(e.what());
// ASSERT_EQUAL(expected, actual);

std::string expected{"VerificationException: expected at least 1 invocations but was 0"};
ASSERT_EQUAL(expected,to_string(e));
}
}

Expand All @@ -80,9 +86,9 @@ struct ErrorFormattingTests: tpunit::TestFixture {
ASSERT_EQUAL(1, e.expectedCount());
ASSERT_EQUAL(0, e.actualSequence().size());
ASSERT_EQUAL(1, e.expectedPattern().size());
// std::string expected("Expected invocation sequence could not be found in actual invocation order");
// std::string actual(e.what());
// ASSERT_EQUAL(expected, actual);

std::string expected{"VerificationException: expected exactly 1 invocations but was 0"};
ASSERT_EQUAL(expected,to_string(e));
}
}

Expand All @@ -98,6 +104,10 @@ struct ErrorFormattingTests: tpunit::TestFixture {
ASSERT_EQUAL(VerificationType::NoMoreInvocatoins, e.verificationType());
ASSERT_EQUAL(2, e.allIvocations().size());
ASSERT_EQUAL(1, e.unverifedIvocations().size());

std::string expected{"VerificationException: expected no more invocations but found 1"};
ASSERT_EQUAL(expected,to_string(e));

}
}

Expand Down

0 comments on commit 6f70cca

Please sign in to comment.