Hamcrest-Qt  0.0.1
Hamcrest matchers for C++/Qt
 All Classes Namespaces Functions
allof.h
1 #ifndef HAMCRESTQT_ALLOF_H
2 #define HAMCRESTQT_ALLOF_H
3 
4 #include <QList>
5 #include <QSharedPointer>
6 
7 #include "diagnosingmatcher.h"
8 
9 namespace HamcrestQt {
10 
15 template <typename T>
16 class AllOf : public DiagnosingMatcher<T>
17 {
18 public:
19  explicit AllOf(const QList<QSharedPointer<Matcher<T> > > &m) : matchers(m) {}
20 
21  virtual bool matches(const T &item, Description &mismatch) const
22  {
23  foreach (const QSharedPointer<Matcher<T> > &matcher, matchers) {
24  if (!matcher->matches(item)) {
25  mismatch.appendDescriptionOf(*matcher).appendText(" ");
26  matcher->describeMismatch(item, mismatch);
27  return false;
28  }
29  }
30 
31  return true;
32  }
33 
34  virtual void describeTo(Description &description) const
35  {
36  description.appendList(QStringLiteral("("), QStringLiteral(" and "), QStringLiteral(")"),
37  matchers.begin(), matchers.end());
38  }
39 
40 private:
41  QList<QSharedPointer<Matcher<T> > > matchers;
42 };
43 
50 template <typename T>
51 QSharedPointer<Matcher<T> > allOf(const QList<QSharedPointer<Matcher<T> > > &matchers)
52 {
53  return QSharedPointer<Matcher<T> >(new AllOf<T>(matchers));
54 }
55 
56 template <typename T>
57 QSharedPointer<Matcher<T> > allOf(const QSharedPointer<Matcher<T> > &first,
58  const QSharedPointer<Matcher<T> > &second)
59 {
60  QList<QSharedPointer<Matcher<T> > > matchers;
61  matchers.append(first);
62  matchers.append(second);
63  return allOf(matchers);
64 }
65 
66 template <typename T>
67 QSharedPointer<Matcher<T> > allOf(const QSharedPointer<Matcher<T> > &first,
68  const QSharedPointer<Matcher<T> > &second,
69  const QSharedPointer<Matcher<T> > &third)
70 {
71  QList<QSharedPointer<Matcher<T> > > matchers;
72  matchers.append(first);
73  matchers.append(second);
74  matchers.append(third);
75  return allOf(matchers);
76 }
77 
78 template <typename T>
79 QSharedPointer<Matcher<T> > allOf(const QSharedPointer<Matcher<T> > &first,
80  const QSharedPointer<Matcher<T> > &second,
81  const QSharedPointer<Matcher<T> > &third,
82  const QSharedPointer<Matcher<T> > &fourth)
83 {
84  QList<QSharedPointer<Matcher<T> > > matchers;
85  matchers.append(first);
86  matchers.append(second);
87  matchers.append(third);
88  matchers.append(fourth);
89  return allOf(matchers);
90 }
91 
92 template <typename T>
93 QSharedPointer<Matcher<T> > allOf(const QSharedPointer<Matcher<T> > &first,
94  const QSharedPointer<Matcher<T> > &second,
95  const QSharedPointer<Matcher<T> > &third,
96  const QSharedPointer<Matcher<T> > &fourth,
97  const QSharedPointer<Matcher<T> > &fifth)
98 {
99  QList<QSharedPointer<Matcher<T> > > matchers;
100  matchers.append(first);
101  matchers.append(second);
102  matchers.append(third);
103  matchers.append(fourth);
104  matchers.append(fifth);
105  return allOf(matchers);
106 }
107 
108 template <typename T>
109 QSharedPointer<Matcher<T> > allOf(const QSharedPointer<Matcher<T> > &first,
110  const QSharedPointer<Matcher<T> > &second,
111  const QSharedPointer<Matcher<T> > &third,
112  const QSharedPointer<Matcher<T> > &fourth,
113  const QSharedPointer<Matcher<T> > &fifth,
114  const QSharedPointer<Matcher<T> > &sixth)
115 {
116  QList<QSharedPointer<Matcher<T> > > matchers;
117  matchers.append(first);
118  matchers.append(second);
119  matchers.append(third);
120  matchers.append(fourth);
121  matchers.append(fifth);
122  matchers.append(sixth);
123  return allOf(matchers);
124 }
125 
126 } // namespace HamcrestQt
127 
128 #endif // HAMCRESTQT_ALLOF_H
A description of a Matcher.
Definition: description.h:17
A matcher over acceptable values.
Definition: matcher.h:21
QSharedPointer< Matcher< T > > allOf(const QList< QSharedPointer< Matcher< T > > > &matchers)
Creates a matcher that matches if the examined object matches ALL of the specified matchers...
Definition: allof.h:51
Description & appendList(const QString &start, const QString &separator, const QString &end, Iterator startIterator, Iterator endIterator)
Appends a list of SelfDescribing objects to the description.
Definition: description.h:54
Definition: diagnosingmatcher.h:9
Calculates the logical conjunction of multiple matchers.
Definition: allof.h:16
virtual void describeTo(Description &description) const
Generates a description of the object.
Definition: allof.h:34
virtual Description & appendText(const QString &text)=0
Appends some plain text to the description.
virtual Description & appendDescriptionOf(const SelfDescribing &value)=0
Appends the description of a SelfDescribing value to this description.