Permalink
Browse files

Moved failure message boilerplate into the base matcher class.

  • Loading branch information...
1 parent 71600e9 commit ca99d24fac7c0f7b20bd03f1277ee176c25efe1c Adam Milligan committed Apr 23, 2011
Showing with 26 additions and 27 deletions.
  1. +6 −1 Source/Matchers/Base.h
  2. +6 −2 Source/Matchers/Base.mm
  3. +3 −4 Source/Matchers/BeTruthy.h
  4. +3 −6 Source/Matchers/BeTruthy.mm
  5. +8 −14 Source/Matchers/Equal.h
@@ -16,6 +16,10 @@ namespace Cedar { namespace Matchers {
virtual ~Base() = 0;
// Allow default copy ctor.
+ NSString * failure_message() const;
+ NSString * negative_failure_message() const;
+
+ protected:
template<typename U>
NSString * string_for(const U &) const;
NSString * string_for(const char value) const;
@@ -26,7 +30,8 @@ namespace Cedar { namespace Matchers {
template<typename U>
void build_failure_message_start(const U &) const;
- NSString * failure_message_start() const;
+
+ virtual NSString * failure_message_end() const = 0;
private:
mutable NSString *valueString_;
@@ -29,8 +29,12 @@
return this->string_for(static_cast<const id &>(value));
}
- NSString * Base::failure_message_start() const {
- return [NSString stringWithFormat:@"Expected <%@> to", valueString_];
+ NSString * Base::failure_message() const {
+ return [NSString stringWithFormat:@"Expected <%@> to %@", valueString_, this->failure_message_end()];
+ }
+
+ NSString * Base::negative_failure_message() const {
+ return [NSString stringWithFormat:@"Expected <%@> to not %@", valueString_, this->failure_message_end()];
}
}}
@@ -6,7 +6,7 @@
#include <sstream>
namespace Cedar { namespace Matchers {
- class BeTruthy : Base {
+ class BeTruthy : public Base {
private:
BeTruthy & operator=(const BeTruthy &);
@@ -15,11 +15,10 @@ namespace Cedar { namespace Matchers {
~BeTruthy();
// Allow default copy ctor.
+ virtual NSString * failure_message_end() const;
+
template<typename U>
bool matches(const U &) const;
-
- NSString * failure_message() const;
- NSString * negative_failure_message() const;
};
BeTruthy be_truthy();
@@ -12,12 +12,9 @@ BeTruthy be_truthy() {
BeTruthy::~BeTruthy() {
}
- NSString * BeTruthy::failure_message() const {
- return [NSString stringWithFormat:@"%@ evaluate to true", this->failure_message_start()];
- }
-
- NSString * BeTruthy::negative_failure_message() const {
- return [NSString stringWithFormat:@"%@ not evaluate to true", this->failure_message_start()];
+ /*virtual*/ NSString *
+ BeTruthy::failure_message_end() const {
+ return @"evaluate to true";
}
}}
@@ -7,7 +7,7 @@
namespace Cedar { namespace Matchers {
template<typename T>
- class Equal : private Base {
+ class Equal : public Base {
private:
Equal<T> & operator=(const Equal<T> &);
@@ -16,15 +16,14 @@ namespace Cedar { namespace Matchers {
~Equal();
// Allow default copy ctor.
+ virtual NSString * failure_message_end() const;
+
template<typename U>
bool matches(const U &) const;
bool matches(const id &) const;
bool matches(NSObject * const &) const;
bool matches(NSString * const &) const;
- NSString * failure_message() const;
- NSString * negative_failure_message() const;
-
private:
const T & expectedValue_;
};
@@ -43,23 +42,18 @@ namespace Cedar { namespace Matchers {
Equal<T>::~Equal() {
}
+ template<typename T>
+ /*virtual*/ NSString * Equal<T>::failure_message_end() const {
+ return [NSString stringWithFormat:@"equal <%@>", this->string_for(expectedValue_)];
+ }
+
#pragma mark Generic
template<typename T> template<typename U>
bool Equal<T>::matches(const U & actualValue) const {
this->build_failure_message_start(actualValue);
return expectedValue_ == actualValue;
}
- template<typename T>
- NSString * Equal<T>::failure_message() const {
- return [NSString stringWithFormat:@"%@ equal <%@>", this->failure_message_start(), this->string_for(expectedValue_)];
- }
-
- template<typename T>
- NSString * Equal<T>::negative_failure_message() const {
- return [NSString stringWithFormat:@"%@ not equal <%@>", this->failure_message_start(), this->string_for(expectedValue_)];
- }
-
#pragma mark id
template<typename T>
bool Equal<T>::matches(const id & actualValue) const {

0 comments on commit ca99d24

Please sign in to comment.