Hamcrest-Qt  0.0.1
Hamcrest matchers for C++/Qt
 All Classes Namespaces Functions
matcherassert.h
1 #ifndef HAMCRESTQT_MATCHERASSERT_H
2 #define HAMCRESTQT_MATCHERASSERT_H
3 
4 #include <QList>
5 #include <qtestcase.h>
6 #include "matcher.h"
7 #include "stringdescription.h"
8 
9 namespace HamcrestQt {
10 
12 {
13 public:
15  {
16  public:
17  virtual ~AssertionListener() {}
18  virtual void assertionError(const QString &message) = 0;
19  };
20 
21  static void addAssertionListener(MatcherAssert::AssertionListener *listener)
22  {
23  listeners.append(listener);
24  }
25 
26  template <typename T>
27  static bool assertThat(const QString &reason, const T &actual, const Matcher<T> &matcher, const char *file, int line)
28  {
29  if (!matcher.matches(actual)) {
30  StringDescription description;
31  description.appendText(reason)
32  .appendText("\nExpected: ")
33  .appendDescriptionOf(matcher)
34  .appendText("\n but: ");
35  matcher.describeMismatch(actual, description);
36 
37  // notify listeners
38  notifyAssertionListener(description.toString());
39 
40  // assertion failed
41  QTest::qFail(description.toString().toLatin1(), file, line);
42  return false;
43  }
44 
45  return true;
46  }
47 
48  template <typename T>
49  static bool assertThat(const QString &reason, const T &actual, const QSharedPointer<Matcher<T> > &matcher, const char *file, int line)
50  {
51  return assertThat(reason, actual, *matcher, file, line);
52  }
53 
54  template <typename T>
55  static bool assertThat(const T &actual, const Matcher<T> &matcher, const char *file, int line)
56  {
57  return assertThat(QStringLiteral(""), actual, matcher, file, line);
58  }
59 
60  template <typename T>
61  static bool assertThat(const T &actual, const QSharedPointer<Matcher<T> > &matcher, const char *file, int line)
62  {
63  return assertThat(QStringLiteral(""), actual, *matcher, file, line);
64  }
65 
66  static bool assertThat(const QString &reason, bool assertion, const char *file, int line)
67  {
68  if (!assertion) {
69  // notify listeners
70  notifyAssertionListener(reason);
71 
72  // assertion failed
73  QTest::qFail(reason.toLatin1(), file, line);
74  return false;
75  }
76 
77  return true;
78  }
79 
80 private:
81  static void notifyAssertionListener(const QString &message)
82  {
83  foreach (MatcherAssert::AssertionListener *listener, listeners) {
84  listener->assertionError(message);
85  }
86  }
87 
88  static QList<MatcherAssert::AssertionListener*> listeners;
89 };
90 
91 
92 #define ASSERT_THAT(actual, matcher) \
93 do {\
94  if (!HamcrestQt::MatcherAssert::assertThat(actual, matcher, __FILE__, __LINE__))\
95  return;\
96 } while (0)
97 
98 
99 #define ASSERT_THAT_MSG(reason, actual, matcher) \
100 do {\
101  if (!HamcrestQt::MatcherAssert::assertThat(reason, actual, matcher, __FILE__, __LINE__))\
102  return;\
103 } while (0)
104 
105 } // namespace HamcrestQt
106 
107 #endif // HAMCRESTQT_MATCHERASSERT_H
virtual QString toString() const
Returns the description as a string.
Definition: stringdescription.cpp:11
A matcher over acceptable values.
Definition: matcher.h:21
virtual Description & appendText(const QString &text)
Appends some plain text to the description.
Definition: basedescription.cpp:7
A Description that is stored as a string.
Definition: stringdescription.h:13
Definition: matcherassert.h:11
virtual bool matches(const T &item) const =0
Evaluates the matcher for argument item.
virtual void describeMismatch(const T &item, Description &mismatchDescription) const =0
Generate a description of why the matcher has not accepted the item.
virtual Description & appendText(const QString &text)=0
Appends some plain text to the description.
Definition: matcherassert.h:14
virtual Description & appendDescriptionOf(const SelfDescribing &value)=0
Appends the description of a SelfDescribing value to this description.