/
iutest_matcher.hpp
2494 lines (2210 loc) · 63.7 KB
/
iutest_matcher.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//======================================================================
//-----------------------------------------------------------------------
/**
* @file iutest_matcher.hpp
* @brief iris unit test matcher 定義 ファイル
*
* @author t.shirayanagi
* @par copyright
* Copyright (C) 2014-2018, Takazumi Shirayanagi\n
* This software is released under the new BSD License,
* see LICENSE
*/
//-----------------------------------------------------------------------
//======================================================================
#ifndef INCG_IRIS_IUTEST_MATCHER_HPP_23746E00_1A4B_4778_91AD_45C6DEFEEFA7_
#define INCG_IRIS_IUTEST_MATCHER_HPP_23746E00_1A4B_4778_91AD_45C6DEFEEFA7_
#if IUTEST_HAS_MATCHERS
//======================================================================
// define
/**
* @private
* @{
*/
#define IUTEST_TEST_THAT(actual, matcher, on_failure) \
IUTEST_AMBIGUOUS_ELSE_BLOCKER_ \
if( ::iutest::AssertionResult iutest_ar = matcher(actual) ) { \
} else \
on_failure(::iutest::detail::MatcherAssertionFailureMessage( \
::iutest::PrintToString(actual).c_str(), #matcher, iutest_ar))
/**
* @}
*/
namespace iutest {
namespace detail
{
//======================================================================
// function
/**
* @brief Matcher Assertion Failure Message
*/
inline ::std::string MatcherAssertionFailureMessage(const char* actual, const char* matcher_str
, const AssertionResult& ar)
{
iu_global_format_stringstream strm;
strm << "error: Expected: " << matcher_str
<< "\n Actual: " << actual
<< "\nWhich is: " << ar.message();
return strm.str();
}
//======================================================================
// class
/**
* @brief matcher interface
*/
class IMatcher
{
public:
template<typename T>
struct is_matcher : public iutest_type_traits::is_base_of<IMatcher, T> {};
public:
// IMatcher(const IMatcher &) {}
IMatcher& operator = (const IMatcher&) { return *this; }
virtual ~IMatcher() {}
virtual ::std::string WhichIs() const = 0;
};
inline iu_ostream& operator << (iu_ostream& os, const IMatcher& msg)
{
return os << msg.WhichIs();
}
/**
* @private
* @{
*/
#define DECL_COMPARE_MATCHER(name, op) \
template<typename T>class IUTEST_PP_CAT(name, Matcher): public IMatcher{ \
public: explicit IUTEST_PP_CAT(name, Matcher)(const T& v) : m_expected(v) {}\
IUTEST_PP_CAT(name, Matcher)(const IUTEST_PP_CAT(name, Matcher) & rhs) : m_expected(rhs.m_expected) {}\
::std::string WhichIs() const IUTEST_CXX_OVERRIDE { \
iu_global_format_stringstream strm; \
strm << #name ": " << m_expected; return strm.str(); \
} \
template<typename U>AssertionResult operator ()(const U& actual) const { \
if( actual op m_expected ) return AssertionSuccess(); \
return AssertionFailure() << WhichIs(); \
} \
private: IUTEST_PP_DISALLOW_ASSIGN(IUTEST_PP_CAT(name, Matcher)); \
const T& m_expected; \
}
#define DECL_COMPARE_MATCHER2(name, op) \
class IUTEST_PP_CAT(Twofold, IUTEST_PP_CAT(name, Matcher)): public IMatcher{ \
public: IUTEST_PP_CAT(Twofold, IUTEST_PP_CAT(name, Matcher))() {} \
IUTEST_PP_CAT(Twofold, IUTEST_PP_CAT(name, Matcher))(const IUTEST_PP_CAT(Twofold, IUTEST_PP_CAT(name, Matcher)) &) {}\
::std::string WhichIs() const IUTEST_CXX_OVERRIDE { return #name; } \
template<typename T, typename U>AssertionResult operator () \
(const T& actual, const U& expected) const { \
if( actual op expected ) return AssertionSuccess(); \
return AssertionFailure() << WhichIs() << ": " << actual << " vs " << expected; \
} private: \
IUTEST_PP_DISALLOW_ASSIGN(IUTEST_PP_CAT(Twofold, IUTEST_PP_CAT(name, Matcher)));\
}
IUTEST_PRAGMA_WARN_PUSH()
IUTEST_PRAGMA_WARN_DISABLE_SIGN_COMPARE()
DECL_COMPARE_MATCHER(Ne, !=);
DECL_COMPARE_MATCHER(Le, <=);
DECL_COMPARE_MATCHER(Lt, < );
DECL_COMPARE_MATCHER(Ge, >=);
DECL_COMPARE_MATCHER(Gt, > );
DECL_COMPARE_MATCHER2(Eq, ==);
DECL_COMPARE_MATCHER2(Ne, !=);
DECL_COMPARE_MATCHER2(Le, <=);
DECL_COMPARE_MATCHER2(Lt, < );
DECL_COMPARE_MATCHER2(Ge, >=);
DECL_COMPARE_MATCHER2(Gt, > );
IUTEST_PRAGMA_WARN_POP()
#undef DECL_COMPARE_MATCHER
#undef DECL_COMPARE_MATCHER2
#define DECL_STR_COMPARE_MATCHER(name) \
template<typename T>class IUTEST_PP_CAT(name, Matcher): public IMatcher { \
public: IUTEST_PP_CAT(name, Matcher)(const T& value) : m_expected(value) {} \
template<typename U>AssertionResult operator ()(const U& actual) const { \
if( internal::IUTEST_PP_CAT(name, Helper)::Compare( \
actual, m_expected) ) { return AssertionSuccess(); } \
return AssertionFailure() << WhichIs(); \
} \
::std::string WhichIs() const IUTEST_CXX_OVERRIDE { \
iu_global_format_stringstream strm; strm << #name ": " << m_expected; \
return strm.str(); \
} \
private: IUTEST_PP_DISALLOW_ASSIGN(IUTEST_PP_CAT(name, Matcher)); \
const T& m_expected; \
}
DECL_STR_COMPARE_MATCHER(StrEq);
DECL_STR_COMPARE_MATCHER(StrNe);
DECL_STR_COMPARE_MATCHER(StrCaseEq);
DECL_STR_COMPARE_MATCHER(StrCaseNe);
#undef DECL_COMPARE_MATCHER
/**
* @}
*/
/**
* @brief IsNull matcher
*/
class IsNullMatcher : public IMatcher
{
public:
IsNullMatcher() {}
public:
template<typename U>
AssertionResult operator ()(const U* actual) const
{
if( actual == NULL )
{
return AssertionSuccess();
}
return AssertionFailure() << WhichIs();
}
::std::string WhichIs() const IUTEST_CXX_OVERRIDE
{
return "Is Null";
}
private:
IUTEST_PP_DISALLOW_ASSIGN(IsNullMatcher);
};
/**
* @brief NotNull matcher
*/
class NotNullMatcher : public IMatcher
{
public:
NotNullMatcher() {}
public:
template<typename U>
AssertionResult operator ()(const U* actual) const
{
if( actual != NULL )
{
return AssertionSuccess();
}
return AssertionFailure() << WhichIs();
}
::std::string WhichIs() const IUTEST_CXX_OVERRIDE
{
return "Not Null";
}
private:
IUTEST_PP_DISALLOW_ASSIGN(NotNullMatcher);
};
/**
* @brief Floating point Eq matcher
*/
template<typename T>
class FloatingPointEqMatcher : public IMatcher
{
public:
explicit FloatingPointEqMatcher(const T& value) : m_expected(value) {}
public:
template<typename U>
AssertionResult operator ()(const U& actual) const
{
floating_point<T> f2(actual);
if( m_expected.AlmostEquals(f2) )
{
return AssertionSuccess();
}
return AssertionFailure() << WhichIs();
}
::std::string WhichIs() const IUTEST_CXX_OVERRIDE
{
iu_global_format_stringstream strm;
strm << "Eq: " << PrintToString(m_expected);
return strm.str();
}
private:
IUTEST_PP_DISALLOW_ASSIGN(FloatingPointEqMatcher);
floating_point<T> m_expected;
};
/**
* @brief Floating point Eq matcher (NanSensitive)
*/
template<typename T>
class NanSensitiveFloatingPointEqMatcher : public IMatcher
{
public:
explicit NanSensitiveFloatingPointEqMatcher(const T& value) : m_expected(value) {}
public:
template<typename U>
AssertionResult operator ()(const U& actual) const
{
floating_point<T> f2(actual);
if( m_expected.NanSensitiveAlmostEquals(f2) )
{
return AssertionSuccess();
}
return AssertionFailure() << WhichIs();
}
::std::string WhichIs() const IUTEST_CXX_OVERRIDE
{
iu_global_format_stringstream strm;
strm << "NanSensitive Eq: " << PrintToString(m_expected);
return strm.str();
}
private:
IUTEST_PP_DISALLOW_ASSIGN(NanSensitiveFloatingPointEqMatcher);
floating_point<T> m_expected;
};
/**
* @brief StartsWith matcher
*/
template<typename T>
class StartsWithMatcher : public IMatcher
{
public:
explicit StartsWithMatcher(T str) : m_str(str) {}
public:
template<typename U>
AssertionResult operator ()(const U& actual) const
{
if( StartsWith(actual, m_str) )
{
return AssertionSuccess();
}
return AssertionFailure() << WhichIs();
}
public:
::std::string WhichIs() const IUTEST_CXX_OVERRIDE
{
iu_global_format_stringstream strm;
strm << "StartsWith: " << m_str;
return strm.str();
}
private:
static bool StartsWith(const char* actual, const char* start)
{
return strstr(actual, start) == actual;
}
static bool StartsWith(const ::std::string& actual, const char* start)
{
const char* p = actual.c_str();
return StartsWith(p, start);
}
static bool StartsWith(const char* actual, const ::std::string& start)
{
const char* p = start.c_str();
return StartsWith(actual, p);
}
static bool StartsWith(const ::std::string& actual, const ::std::string& start)
{
const char* p = start.c_str();
return StartsWith(actual, p);
}
private:
IUTEST_PP_DISALLOW_ASSIGN(StartsWithMatcher);
T m_str;
};
/**
* @brief Has substr matcher
*/
template<typename T>
class HasSubstrMatcher : public IMatcher
{
public:
explicit HasSubstrMatcher(T expected) : m_expected(expected) {}
HasSubstrMatcher(const HasSubstrMatcher & rhs) : m_expected(rhs.m_expected) {}
public:
template<typename U>
AssertionResult operator ()(const U& actual) const
{
if( HasSubstr(actual, m_expected) )
{
return AssertionSuccess();
}
return AssertionFailure() << WhichIs();
}
public:
::std::string WhichIs() const IUTEST_CXX_OVERRIDE
{
iu_global_format_stringstream strm;
strm << "HasSubstr: " << m_expected;
return strm.str();
}
private:
static bool HasSubstr(const char* actual, const char* expected)
{
return strstr(actual, expected) != NULL;
}
static bool HasSubstr(const ::std::string& actual, const char* expected)
{
const char* p = actual.c_str();
return HasSubstr(p, expected);
}
static bool HasSubstr(const char* actual, const ::std::string& expected)
{
const char* p = expected.c_str();
return HasSubstr(actual, p);
}
static bool HasSubstr(const ::std::string& actual, const ::std::string& expected)
{
const char* p = expected.c_str();
return HasSubstr(actual, p);
}
private:
IUTEST_PP_DISALLOW_ASSIGN(HasSubstrMatcher);
T m_expected;
};
/**
* @brief EndsWith matcher
*/
template<typename T>
class EndsWithMatcher : public IMatcher
{
public:
explicit EndsWithMatcher(T str) : m_str(str) {}
public:
template<typename U>
AssertionResult operator ()(const U& actual) const
{
if( EndsWith(actual, m_str) )
{
return AssertionSuccess();
}
return AssertionFailure() << WhichIs();
}
public:
::std::string WhichIs() const IUTEST_CXX_OVERRIDE
{
iu_global_format_stringstream strm;
strm << "EndsWith: " << m_str;
return strm.str();
}
private:
static bool EndsWith(const char* actual, const char* end)
{
const size_t len = strlen(end);
const size_t actual_len = strlen(actual);
if( len > actual_len )
{
return false;
}
const char* p = actual + actual_len - 1;
const char* q = end + len - 1;
for( size_t i=0; i < len; ++i, --p, --q )
{
if( *p != *q )
{
return false;
}
}
return true;
}
static bool EndsWith(const ::std::string& actual, const char* end)
{
const char* p = actual.c_str();
return EndsWith(p, end);
}
static bool EndsWith(const char* actual, const ::std::string& end)
{
const char* p = end.c_str();
return EndsWith(actual, p);
}
static bool EndsWith(const ::std::string& actual, const ::std::string& end)
{
const char* p = end.c_str();
return EndsWith(actual, p);
}
private:
IUTEST_PP_DISALLOW_ASSIGN(EndsWithMatcher);
T m_str;
};
/**
* @brief Equals matcher
*/
template<typename T>
class EqMatcher : public IMatcher
{
public:
explicit EqMatcher(const T& expected) : m_expected(expected) {}
public:
template<typename U>
AssertionResult operator ()(const U& actual) const
{
if( Equals(actual, m_expected) )
{
return AssertionSuccess();
}
return AssertionFailure() << WhichIs();
}
public:
::std::string WhichIs() const IUTEST_CXX_OVERRIDE
{
iu_global_format_stringstream strm;
strm << "Eq: " << m_expected;
return strm.str();
}
private:
template<typename A, typename B>
static bool Equals(const A& actual, const B& expected)
{
IUTEST_PRAGMA_WARN_PUSH()
IUTEST_PRAGMA_WARN_DISABLE_SIGN_COMPARE()
return actual == expected;
IUTEST_PRAGMA_WARN_POP()
}
static bool Equals(const char* actual, const char* expected)
{
return strcmp(actual, expected) == 0;
}
static bool Equals(const ::std::string& actual, const char* expected)
{
const char* p = actual.c_str();
return Equals(p, expected);
}
static bool Equals(const ::std::string& actual, const ::std::string& expected)
{
const char* p = expected.c_str();
return Equals(actual, p);
}
private:
IUTEST_PP_DISALLOW_ASSIGN(EqMatcher);
const T& m_expected;
};
/**
* @brief TypedEq matcher
*/
template<typename T>
class TypedEqMatcher : public EqMatcher<T>
{
public:
explicit TypedEqMatcher(T expected) : EqMatcher<T>(m_expected), m_expected(expected) {}
public:
AssertionResult operator ()(const T& actual)
{
return EqMatcher<T>::operator ()(actual);
}
template<typename U>
AssertionResult operator ()(const U&) const;
private:
IUTEST_PP_DISALLOW_ASSIGN(TypedEqMatcher);
T m_expected;
};
/**
* @brief Cast to matcher
*/
#if !defined(IUTEST_NO_SFINAE)
template<typename T>
T& CastToMatcher(T& matcher
, typename detail::enable_if_t< IMatcher::is_matcher<T> >::type*& = detail::enabler::value)
{
return matcher;
}
/** @overload */
template<typename T>
EqMatcher<T> CastToMatcher(const T& value
, typename detail::disable_if_t< IMatcher::is_matcher<T> >::type*& = detail::enabler::value)
{
return EqMatcher<T>(value);
}
#else
template<typename T>
T& CastToMatcher(T& matcher)
{
return matcher;
}
#endif
/**
* @brief Contains matcher
*/
template<typename T>
class ContainsMatcher : public IMatcher
{
public:
explicit ContainsMatcher(const T& expected) : m_expected(expected) {}
public:
template<typename U>
AssertionResult operator ()(const U& actual)
{
IUTEST_USING_BEGIN_END();
if( Contains(begin(actual), end(actual)) )
{
return AssertionSuccess();
}
return AssertionFailure() << WhichIs();
}
public:
::std::string WhichIs() const IUTEST_CXX_OVERRIDE
{
iu_global_format_stringstream strm;
strm << "Contains: " << m_expected;
return strm.str();
}
private:
template<typename Ite>
bool Contains(Ite begin, Ite end)
{
for( Ite it = begin; it != end; ++it )
{
if( CastToMatcher(m_expected)(*it) )
{
return true;
}
}
return false;
}
private:
IUTEST_PP_DISALLOW_ASSIGN(ContainsMatcher);
T m_expected;
};
/**
* @brief Each matcher
*/
template<typename T>
class EachMatcher : public IMatcher
{
public:
explicit EachMatcher(const T& expected) : m_expected(expected) {}
public:
template<typename U>
AssertionResult operator ()(const U& actual)
{
IUTEST_USING_BEGIN_END();
if( Each(begin(actual), end(actual)) )
{
return AssertionSuccess();
}
return AssertionFailure() << WhichIs();
}
public:
::std::string WhichIs() const IUTEST_CXX_OVERRIDE
{
iu_global_format_stringstream strm;
strm << "Each: " << m_expected;
return strm.str();
}
private:
template<typename Ite>
bool Each(Ite begin, Ite end)
{
for( Ite it = begin; it != end; ++it )
{
if( !CastToMatcher(m_expected)(*it) )
{
return false;
}
}
return true;
}
private:
IUTEST_PP_DISALLOW_ASSIGN(EachMatcher);
T m_expected;
};
/**
* @brief ContainerEq matcher
*/
template<typename T>
class ContainerEqMatcher : public IMatcher
{
public:
explicit ContainerEqMatcher(const T& expected) : m_expected(expected) {}
public:
template<typename U>
AssertionResult operator ()(const U& actual)
{
IUTEST_USING_BEGIN_END();
if( Check(begin(m_expected), end(m_expected)
, begin(actual), end(actual)) )
{
return AssertionSuccess();
}
return AssertionFailure() << WhichIs();
}
public:
::std::string WhichIs() const IUTEST_CXX_OVERRIDE
{
iu_global_format_stringstream strm;
strm << "ContainerEq: " << PrintToString(m_expected);
strm << " (" << m_whichIs << ")";
return strm.str();
}
private:
template<typename Ite1, typename Ite2>
bool Check(Ite1 b1, Ite1 e1, Ite2 b2, Ite2 e2)
{
size_t elem=0;
bool result = true;
Message ar;
for( elem=0; b1 != e1 && b2 != e2; ++b1, ++b2, ++elem )
{
if( !internal::EqHelper<false>::Compare("", "", *b1, *b2) )
{
result = false;
ar << "\nMismatch in a position " << elem << ": "
<< ::iutest::internal::FormatForComparisonFailureMessage(*b1, *b2)
<< " vs " << ::iutest::internal::FormatForComparisonFailureMessage(*b2, *b1);
}
}
if( b1 != e1 || b2 != e2 )
{
const size_t elem1 = elem + ::std::distance(b1, e1);
const size_t elem2 = elem + ::std::distance(b2, e2);
result = false;
ar << "\nMismatch element : " << elem1 << " vs " << elem2;
}
m_whichIs = ar.GetString();
return result;
}
private:
IUTEST_PP_DISALLOW_ASSIGN(ContainerEqMatcher);
const T& m_expected;
::std::string m_whichIs;
};
/**
* @brief Pointwise matcher
*/
template<typename M, typename T>
class PointwiseMatcher : public IMatcher
{
public:
PointwiseMatcher(const M& matcher, const T& expected)
: m_matcher(matcher), m_expected(expected) {}
public:
template<typename U>
AssertionResult operator ()(const U& actual)
{
IUTEST_USING_BEGIN_END();
if( Check(begin(m_expected), end(m_expected)
, begin(actual), end(actual)) )
{
return AssertionSuccess();
}
return AssertionFailure() << WhichIs();
}
public:
::std::string WhichIs() const IUTEST_CXX_OVERRIDE
{
iu_global_format_stringstream strm;
strm << "Pointwise: " << m_matcher << ": " << PrintToString(m_expected);
strm << " (" << m_whichIs << ")";
return strm.str();
}
private:
template<typename Ite1, typename Ite2>
bool Check(Ite1 b1, Ite1 e1, Ite2 b2, Ite2 e2)
{
size_t elem=0;
bool result = true;
Message ar;
for( elem=0; b1 != e1 && b2 != e2; ++b1, ++b2, ++elem )
{
const AssertionResult r = m_matcher(*b2, *b1);
if( r.failed() )
{
result = false;
ar << "\nMismatch in a position " << elem << ": " << r.message();
}
}
if( b1 != e1 || b2 != e2 )
{
const size_t elem1 = elem + ::std::distance(b1, e1);
const size_t elem2 = elem + ::std::distance(b2, e2);
result = false;
ar << "\nMismatch element : " << elem1 << " vs " << elem2;
}
m_whichIs = ar.GetString();
return result;
}
private:
IUTEST_PP_DISALLOW_ASSIGN(PointwiseMatcher);
M m_matcher;
const T& m_expected;
::std::string m_whichIs;
};
/**
* @brief IsEmpty matcher
*/
class IsEmptyMatcher : public IMatcher
{
public:
IsEmptyMatcher() {}
public:
template<typename U>
AssertionResult operator ()(const U& actual)
{
if( (actual).empty() )
{
return AssertionSuccess();
}
return AssertionFailure() << WhichIs();
}
public:
::std::string WhichIs() const IUTEST_CXX_OVERRIDE
{
return "Is Empty";
}
private:
IUTEST_PP_DISALLOW_ASSIGN(IsEmptyMatcher);
};
/**
* @brief SizeIs matcher
*/
template<typename T>
class SizeIsMatcher : public IMatcher
{
public:
explicit SizeIsMatcher(const T& expected) : m_expected(expected) {}
public:
template<typename U>
AssertionResult operator ()(const U& actual)
{
if( Check(actual) )
{
return AssertionSuccess();
}
return AssertionFailure() << WhichIs();
}
public:
::std::string WhichIs() const IUTEST_CXX_OVERRIDE
{
iu_global_format_stringstream strm;
strm << "Size is: " << m_expected;
return strm.str();
}
private:
template<typename Container>
bool Check(const Container& actual)
{
return static_cast<bool>(CastToMatcher(m_expected)(actual.size()));
}
#if !defined(IUTEST_NO_FUNCTION_TEMPLATE_ORDERING)
template<typename U, size_t SIZE>
bool Check(const U(&)[SIZE])
{
return static_cast<bool>(CastToMatcher(m_expected)(SIZE));
}
#endif
private:
IUTEST_PP_DISALLOW_ASSIGN(SizeIsMatcher);
T m_expected;
};
/**
* @brief At matcher
*/
template<typename T>
class AtMatcher : public IMatcher
{
public:
AtMatcher(size_t index, const T& expected) : m_index(index), m_expected(expected) {}
public:
template<typename U>
AssertionResult operator ()(const U& actual)
{
if( CastToMatcher(m_expected)(actual[m_index]) )
{
return AssertionSuccess();
}
return AssertionFailure() << WhichIs();
}
public:
::std::string WhichIs() const IUTEST_CXX_OVERRIDE
{
iu_global_format_stringstream strm;
strm << "At " << m_index << ": " << m_expected;
return strm.str();
}
private:
IUTEST_PP_DISALLOW_ASSIGN(AtMatcher);
size_t m_index;
T m_expected;
};
/**
* @brief ElementsAreArray matcher
*/
template<typename T>
class ElementsAreArrayMatcher : public IMatcher
{
public:
template<typename It>
ElementsAreArrayMatcher(It begin, It end, bool expected_elem_count=true)
: m_expected_elem_count(expected_elem_count)
{
m_expected.insert(m_expected.end(), begin, end);
}
public:
template<typename U>
AssertionResult operator ()(const U& actual)
{
IUTEST_USING_BEGIN_END();
return Check(begin(actual), end(actual));
}
public:
::std::string WhichIs() const IUTEST_CXX_OVERRIDE
{
return WhichIs(PrintToString(m_expected));
}
::std::string WhichIs(const ::std::string& msg) const
{
iu_global_format_stringstream strm;
if( m_expected_elem_count )
{
strm << "ElementsAreArray: ";
}
else
{
strm << "ElementsAreArrayForward: ";
}
strm << msg;
return strm.str();
}
private:
template<typename Ite>
AssertionResult Check(Ite actual_begin, Ite actual_end)
{
const size_t actual_cnt = ::std::distance(actual_begin, actual_end);
const size_t expected_cnt = m_expected.size();
if( actual_cnt < expected_cnt )
{
iu_global_format_stringstream stream;
stream << "actual argument[" << actual_cnt << "] is less than " << expected_cnt;
return AssertionFailure() << WhichIs(stream.str());
}
if( m_expected_elem_count && actual_cnt > expected_cnt )
{
iu_global_format_stringstream stream;
stream << "actual argument[" << actual_cnt << "] is greater than " << expected_cnt;
return AssertionFailure() << WhichIs(stream.str());
}
Ite it_a=actual_begin;
typename ::std::vector<T>::iterator it_e=m_expected.begin();
for( int i=0; it_e != m_expected.end(); ++it_e, ++it_a, ++i )
{
if( *it_a != *it_e )
{
return AssertionFailure() << WhichIs();
}
}
return AssertionSuccess();
}
private:
IUTEST_PP_DISALLOW_ASSIGN(ElementsAreArrayMatcher);
::std::vector<T> m_expected;
bool m_expected_elem_count;
};
#if IUTEST_HAS_MATCHER_ELEMENTSARE
/**
* @brief ElementsAre matcher base class
*/
class ElementsAreMatcherBase : public IMatcher
{
protected:
template<typename T, typename U>
static AssertionResult Check(T& matchers, const U& actual)
{
IUTEST_USING_BEGIN_END();
return Check<0, tuples::tuple_size<T>::value - 1>(begin(actual), end(actual), matchers);
}
template<int N, typename T>
static ::std::string WhichIs(const T& matchers)
{
::std::string str = "ElementsAre: {";
str += WhichIs_<T, N, tuples::tuple_size<T>::value-1>(matchers);
str += "}";
return str;
}