Pact C++ Consumer DSL  0.0.7
DSL to support writing consumer tests in C++
matchers.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <string>
4 #include <unordered_map>
5 #include <memory>
6 #include <optional>
7 #include <boost/throw_exception.hpp>
8 #include <sstream>
9 #include <vector>
10 
11 namespace pact_consumer::matchers {
12 
13  class IMatcher {
14  public:
15  using Ptr = std::shared_ptr<IMatcher>;
16  virtual std::string getJson() const = 0;
17  virtual std::string as_example() const { return ""; };
18  virtual std::string as_regex() const { return as_example(); };
19  };
20 
21  class ObjectMatcher : public IMatcher {
22  public:
23  ObjectMatcher(const std::unordered_map<std::string, IMatcher::Ptr>& i_fields) : fields { i_fields } {};
24 
25  virtual std::string getJson() const;
26 
27  private:
28  std::unordered_map<std::string, IMatcher::Ptr> fields;
29  };
30 
31  class IntegerMatcher : public IMatcher {
32  public:
34  IntegerMatcher(long v) : value { v } {};
35  IntegerMatcher(int v) : value { v } {};
36 
37  virtual std::string getJson() const;
38 
39  private:
40  std::optional<long> value;
41  };
42 
43  class DecimalMatcher : public IMatcher {
44  public:
46  DecimalMatcher(float v) : value { v } {};
47  DecimalMatcher(double v) : value { v } {};
48 
49  virtual std::string getJson() const;
50 
51  private:
52  std::optional<double> value;
53  };
54 
55  template<typename T>
56  class TypeMatcher : public IMatcher {
57  public:
58  TypeMatcher(T v) : value { v } {};
59 
60  virtual std::string getJson() const;
61 
62  private:
63  T value;
64  };
65 
66  template<typename T>
67  class NumberMatcher : public IMatcher {
68  public:
70  NumberMatcher(T v) : value { v } {};
71 
72  virtual std::string getJson() const;
73 
74  private:
75  std::optional<T> value;
76  };
77 
78  template<typename T>
79  class EqualsMatcher : public IMatcher {
80  public:
81  EqualsMatcher(T v) : value { v } {};
82 
83  virtual std::string getJson() const;
84 
85  virtual std::string as_example() const {
86  std::ostringstream stringStream;
87  stringStream << value;
88  return stringStream.str();
89  };
90 
91  private:
92  T value;
93  };
94 
95  class DateTimeMatcher : public IMatcher {
96  public:
97  DateTimeMatcher(const std::string& f) : format { f } {};
98  DateTimeMatcher(const std::string& f, const std::string& e) : format { f }, example { e } {};
99 
100  virtual std::string getJson() const;
101 
102  private:
103  std::string format, example;
104  };
105 
106  class DateMatcher : public IMatcher {
107  public:
108  DateMatcher(const std::string& f) : format { f } {};
109  DateMatcher(const std::string& f, const std::string& e) : format { f }, example { e } {};
110 
111  virtual std::string getJson() const;
112 
113  private:
114  std::string format, example;
115  };
116 
117  class TimeMatcher : public IMatcher {
118  public:
119  TimeMatcher(const std::string& f) : format { f } {};
120  TimeMatcher(const std::string& f, const std::string& e) : format { f }, example { e } {};
121 
122  virtual std::string getJson() const;
123 
124  private:
125  std::string format, example;
126  };
127 
128  class RegexMatcher : public IMatcher {
129  public:
130  RegexMatcher(const std::string& r) : regex { r } {};
131  RegexMatcher(const std::string& r, const std::string& e) : regex { r }, example { e } {};
132 
133  virtual std::string getJson() const;
134 
135  virtual std::string as_example() const { return example; };
136  virtual std::string as_regex() const { return regex; };
137 
138  private:
139  std::string regex, example;
140  };
141 
142  class EachlikeMatcher : public IMatcher {
143  public:
144  EachlikeMatcher(IMatcher::Ptr t) : obj { t } {};
145  EachlikeMatcher(int e, IMatcher::Ptr t) : obj { t }, examples { e } {};
146  EachlikeMatcher(int e, int m, IMatcher::Ptr t) : obj { t }, examples { e }, min { m } {};
147  EachlikeMatcher(int e, int m, int mx, IMatcher::Ptr t) : obj { t }, examples { e }, min { m }, max { mx } {};
148 
149  virtual std::string getJson() const;
150 
151  private:
152  int examples = 1;
153  std::optional<int> min;
154  std::optional<int> max;
155  IMatcher::Ptr obj;
156  };
157 
159  public:
160  ArrayContainsMatcher(const std::vector<IMatcher::Ptr>& i_variants) : variants { i_variants } {};
161 
162  virtual std::string getJson() const;
163 
164  private:
165  std::vector<IMatcher::Ptr> variants;
166  };
167 
168  class HexadecimalMatcher : public IMatcher {
169  public:
171  HexadecimalMatcher(const std::string& hex) : example { hex } {};
172 
173  virtual std::string getJson() const;
174 
175  private:
176  std::string example;
177  };
178 
179  class IPAddressMatcher : public IMatcher {
180  public:
182  IPAddressMatcher(const std::string& address) : example { address } {};
183 
184  virtual std::string getJson() const;
185 
186  private:
187  std::string example;
188  };
189 
190  class UuidMatcher : public IMatcher {
191  public:
193  UuidMatcher(const std::string& uuid) : example { uuid } {};
194 
195  virtual std::string getJson() const;
196 
197  private:
198  std::string example;
199  };
200 
201  class IncludesMatcher : public IMatcher {
202  public:
203  IncludesMatcher(const std::string& v) : value { v } {};
204 
205  virtual std::string getJson() const;
206 
207  private:
208  std::string value;
209  };
210 
211  class NullMatcher : public IMatcher {
212  public:
213  virtual std::string getJson() const;
214  };
215 
216  class UrlMatcher : public IMatcher {
217  public:
218  UrlMatcher(const std::string& b, const std::vector<IMatcher::Ptr>& f) : basePath { b }, pathFragments { f } {};
219 
220  virtual std::string getJson() const;
221 
222  private:
223  std::string basePath;
224  std::vector<IMatcher::Ptr> pathFragments;
225  };
226 
230  IMatcher::Ptr Object(const std::unordered_map<std::string, IMatcher::Ptr>& fields);
231 
235  IMatcher::Ptr Integer(long value);
236 
240  IMatcher::Ptr Integer(int value);
241 
246 
250  IMatcher::Ptr Like(long value);
254  IMatcher::Ptr Like(int value);
258  IMatcher::Ptr Like(double value);
262  IMatcher::Ptr Like(const std::string& value);
266  IMatcher::Ptr Like(const char *value);
270  IMatcher::Ptr Like(bool value);
271 
277  IMatcher::Ptr DateTime(const std::string& format, const std::string& example);
278 
283  IMatcher::Ptr DateTime(const std::string& format);
284 
290  IMatcher::Ptr Date(const std::string& format, const std::string& example);
291 
296  IMatcher::Ptr Date(const std::string& format);
297 
303  IMatcher::Ptr Time(const std::string& format, const std::string& example);
304 
309  IMatcher::Ptr Time(const std::string& format);
310 
314  IMatcher::Ptr Matching(const std::string& regex, const std::string& example);
315 
319  IMatcher::Ptr Matching(const std::string& regex);
320 
325 
330  IMatcher::Ptr EachLike(int examples, const IMatcher::Ptr obj);
331 
336 
341  IMatcher::Ptr AtLeastOneLike(int examples, const IMatcher::Ptr obj);
342 
347  IMatcher::Ptr HexValue(const std::string& example);
348 
353 
358  IMatcher::Ptr IPAddress(const std::string& example);
359 
364 
369  IMatcher::Ptr Numeric(int example);
370 
375  IMatcher::Ptr Numeric(long example);
376 
381  IMatcher::Ptr Numeric(float example);
382 
387  IMatcher::Ptr Numeric(double example);
388 
393 
398  IMatcher::Ptr Decimal(float example);
399 
404  IMatcher::Ptr Decimal(double example);
405 
411 
416  IMatcher::Ptr Uuid(const std::string& example);
417 
422 
427  IMatcher::Ptr AtMostLike(int max, const IMatcher::Ptr obj);
428 
434  IMatcher::Ptr AtMostLike(int max, int examples, const IMatcher::Ptr obj);
435 
440  IMatcher::Ptr MinArrayLike(int min, const IMatcher::Ptr obj);
441 
447  IMatcher::Ptr MinArrayLike(int min, int examples, const IMatcher::Ptr obj);
448 
454  IMatcher::Ptr ConstrainedArrayLike(int min, int max, const IMatcher::Ptr obj);
455 
462  IMatcher::Ptr ConstrainedArrayLike(int min, int max, int examples, const IMatcher::Ptr obj);
463 
468  IMatcher::Ptr EqualTo(int value);
469 
474  IMatcher::Ptr EqualTo(long value);
475 
480  IMatcher::Ptr EqualTo(float value);
481 
486  IMatcher::Ptr EqualTo(double value);
487 
492  IMatcher::Ptr EqualTo(const std::string& value);
493 
498  IMatcher::Ptr EqualTo(const char *value);
499 
504  IMatcher::Ptr EqualTo(bool value);
505 
510  IMatcher::Ptr IncludesStr(const std::string& value);
511 
516 
522  IMatcher::Ptr Url(const std::string& basePath, const std::vector<IMatcher::Ptr>& pathFragments);
523 
528  IMatcher::Ptr Url(const std::vector<IMatcher::Ptr>& pathFragments);
529 
537  IMatcher::Ptr ArrayContaining(const std::vector<IMatcher::Ptr>& variants);
538 }
ArrayContainsMatcher(const std::vector< IMatcher::Ptr > &i_variants)
Definition: matchers.h:160
virtual std::string getJson() const
Definition: matchers.cpp:537
Definition: matchers.h:106
DateMatcher(const std::string &f, const std::string &e)
Definition: matchers.h:109
virtual std::string getJson() const
Definition: matchers.cpp:312
DateMatcher(const std::string &f)
Definition: matchers.h:108
virtual std::string getJson() const
Definition: matchers.cpp:291
DateTimeMatcher(const std::string &f, const std::string &e)
Definition: matchers.h:98
DateTimeMatcher(const std::string &f)
Definition: matchers.h:97
DecimalMatcher(float v)
Definition: matchers.h:46
DecimalMatcher()
Definition: matchers.h:45
virtual std::string getJson() const
Definition: matchers.cpp:248
DecimalMatcher(double v)
Definition: matchers.h:47
EachlikeMatcher(int e, IMatcher::Ptr t)
Definition: matchers.h:145
virtual std::string getJson() const
Definition: matchers.cpp:382
EachlikeMatcher(int e, int m, IMatcher::Ptr t)
Definition: matchers.h:146
EachlikeMatcher(int e, int m, int mx, IMatcher::Ptr t)
Definition: matchers.h:147
EachlikeMatcher(IMatcher::Ptr t)
Definition: matchers.h:144
virtual std::string getJson() const
Definition: matchers.cpp:284
virtual std::string as_example() const
Definition: matchers.h:85
EqualsMatcher(T v)
Definition: matchers.h:81
HexadecimalMatcher(const std::string &hex)
Definition: matchers.h:171
HexadecimalMatcher()
Definition: matchers.h:170
virtual std::string getJson() const
Definition: matchers.cpp:414
Definition: matchers.h:13
std::shared_ptr< IMatcher > Ptr
Definition: matchers.h:15
virtual std::string getJson() const =0
virtual std::string as_example() const
Definition: matchers.h:17
virtual std::string as_regex() const
Definition: matchers.h:18
IPAddressMatcher(const std::string &address)
Definition: matchers.h:182
virtual std::string getJson() const
Definition: matchers.cpp:443
IPAddressMatcher()
Definition: matchers.h:181
IncludesMatcher(const std::string &v)
Definition: matchers.h:203
virtual std::string getJson() const
Definition: matchers.cpp:492
IntegerMatcher()
Definition: matchers.h:33
IntegerMatcher(int v)
Definition: matchers.h:35
IntegerMatcher(long v)
Definition: matchers.h:34
virtual std::string getJson() const
Definition: matchers.cpp:236
Definition: matchers.h:211
virtual std::string getJson() const
Definition: matchers.cpp:499
NumberMatcher()
Definition: matchers.h:69
NumberMatcher(T v)
Definition: matchers.h:70
virtual std::string getJson() const
Definition: matchers.cpp:270
ObjectMatcher(const std::unordered_map< std::string, IMatcher::Ptr > &i_fields)
Definition: matchers.h:23
virtual std::string getJson() const
Definition: matchers.cpp:226
Definition: matchers.h:128
RegexMatcher(const std::string &r, const std::string &e)
Definition: matchers.h:131
virtual std::string as_regex() const
Definition: matchers.h:136
RegexMatcher(const std::string &r)
Definition: matchers.h:130
virtual std::string as_example() const
Definition: matchers.h:135
virtual std::string getJson() const
Definition: matchers.cpp:354
Definition: matchers.h:117
TimeMatcher(const std::string &f)
Definition: matchers.h:119
virtual std::string getJson() const
Definition: matchers.cpp:333
TimeMatcher(const std::string &f, const std::string &e)
Definition: matchers.h:120
Definition: matchers.h:56
TypeMatcher(T v)
Definition: matchers.h:58
virtual std::string getJson() const
Definition: matchers.cpp:262
Definition: matchers.h:216
virtual std::string getJson() const
Definition: matchers.cpp:505
UrlMatcher(const std::string &b, const std::vector< IMatcher::Ptr > &f)
Definition: matchers.h:218
Definition: matchers.h:190
UuidMatcher()
Definition: matchers.h:192
UuidMatcher(const std::string &uuid)
Definition: matchers.h:193
virtual std::string getJson() const
Definition: matchers.cpp:472
Definition: matchers.cpp:8
IMatcher::Ptr NullValue()
Definition: matchers.cpp:210
IMatcher::Ptr MinArrayLike(int min, const IMatcher::Ptr obj)
Definition: matchers.cpp:162
IMatcher::Ptr Uuid(const std::string &example)
Definition: matchers.cpp:146
IMatcher::Ptr Date(const std::string &format, const std::string &example)
Definition: matchers.cpp:58
IMatcher::Ptr AtLeastOneLike(const IMatcher::Ptr obj)
Definition: matchers.cpp:90
IMatcher::Ptr Url(const std::string &basePath, const std::vector< IMatcher::Ptr > &pathFragments)
Definition: matchers.cpp:214
IMatcher::Ptr EachLike(const IMatcher::Ptr obj)
Definition: matchers.cpp:82
IMatcher::Ptr Like(long value)
Definition: matchers.cpp:26
IMatcher::Ptr Integer(long value)
Definition: matchers.cpp:14
IMatcher::Ptr IncludesStr(const std::string &value)
Definition: matchers.cpp:206
IMatcher::Ptr Time(const std::string &format, const std::string &example)
Definition: matchers.cpp:66
IMatcher::Ptr Numeric(int example)
Definition: matchers.cpp:114
IMatcher::Ptr HexValue(const std::string &example)
Definition: matchers.cpp:98
IMatcher::Ptr Object(const std::unordered_map< std::string, IMatcher::Ptr > &fields)
Definition: matchers.cpp:10
IMatcher::Ptr DateTime(const std::string &format, const std::string &example)
Definition: matchers.cpp:50
IMatcher::Ptr AtMostLike(int max, const IMatcher::Ptr obj)
Definition: matchers.cpp:154
IMatcher::Ptr ArrayContaining(const std::vector< IMatcher::Ptr > &variants)
Definition: matchers.cpp:222
IMatcher::Ptr ConstrainedArrayLike(int min, int max, const IMatcher::Ptr obj)
Definition: matchers.cpp:170
IMatcher::Ptr Matching(const std::string &regex, const std::string &example)
Definition: matchers.cpp:74
IMatcher::Ptr IPAddress(const std::string &example)
Definition: matchers.cpp:106
IMatcher::Ptr Decimal(float example)
Definition: matchers.cpp:134
IMatcher::Ptr EqualTo(int value)
Definition: matchers.cpp:178