/
Matchers.java
1701 lines (1570 loc) · 70.6 KB
/
Matchers.java
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
package org.hamcrest;
import org.hamcrest.collection.ArrayMatching;
import org.hamcrest.core.IsIterableContaining;
import org.hamcrest.core.StringRegularExpression;
import org.hamcrest.text.IsEqualCompressingWhiteSpace;
import java.util.regex.Pattern;
@SuppressWarnings({"unused", "WeakerAccess"})
public class Matchers {
/**
* Creates a matcher that matches if the examined object matches <b>ALL</b> of the specified matchers.
* For example:
* <pre>assertThat("myValue", allOf(startsWith("my"), containsString("Val")))</pre>
*/
public static <T> org.hamcrest.Matcher<T> allOf(java.lang.Iterable<org.hamcrest.Matcher<? super T>> matchers) {
return org.hamcrest.core.AllOf.allOf(matchers);
}
/**
* Creates a matcher that matches if the examined object matches <b>ALL</b> of the specified matchers.
* For example:
* <pre>assertThat("myValue", allOf(startsWith("my"), containsString("Val")))</pre>
*/
@SafeVarargs
public static <T> org.hamcrest.Matcher<T> allOf(org.hamcrest.Matcher<? super T>... matchers) {
return org.hamcrest.core.AllOf.allOf(matchers);
}
/**
* Creates a matcher that matches if the examined object matches <b>ALL</b> of the specified matchers.
* For example:
* <pre>assertThat("myValue", allOf(startsWith("my"), containsString("Val")))</pre>
*/
public static <T> org.hamcrest.Matcher<T> allOf(org.hamcrest.Matcher<? super T> first, org.hamcrest.Matcher<? super T> second) {
return org.hamcrest.core.AllOf.allOf(first, second);
}
/**
* Creates a matcher that matches if the examined object matches <b>ALL</b> of the specified matchers.
* For example:
* <pre>assertThat("myValue", allOf(startsWith("my"), containsString("Val")))</pre>
*/
public static <T> org.hamcrest.Matcher<T> allOf(org.hamcrest.Matcher<? super T> first, org.hamcrest.Matcher<? super T> second, org.hamcrest.Matcher<? super T> third) {
return org.hamcrest.core.AllOf.allOf(first, second, third);
}
/**
* Creates a matcher that matches if the examined object matches <b>ALL</b> of the specified matchers.
* For example:
* <pre>assertThat("myValue", allOf(startsWith("my"), containsString("Val")))</pre>
*/
public static <T> org.hamcrest.Matcher<T> allOf(org.hamcrest.Matcher<? super T> first, org.hamcrest.Matcher<? super T> second, org.hamcrest.Matcher<? super T> third, org.hamcrest.Matcher<? super T> fourth) {
return org.hamcrest.core.AllOf.allOf(first, second, third, fourth);
}
/**
* Creates a matcher that matches if the examined object matches <b>ALL</b> of the specified matchers.
* For example:
* <pre>assertThat("myValue", allOf(startsWith("my"), containsString("Val")))</pre>
*/
public static <T> org.hamcrest.Matcher<T> allOf(org.hamcrest.Matcher<? super T> first, org.hamcrest.Matcher<? super T> second, org.hamcrest.Matcher<? super T> third, org.hamcrest.Matcher<? super T> fourth, org.hamcrest.Matcher<? super T> fifth) {
return org.hamcrest.core.AllOf.allOf(first, second, third, fourth, fifth);
}
/**
* Creates a matcher that matches if the examined object matches <b>ALL</b> of the specified matchers.
* For example:
* <pre>assertThat("myValue", allOf(startsWith("my"), containsString("Val")))</pre>
*/
public static <T> org.hamcrest.Matcher<T> allOf(org.hamcrest.Matcher<? super T> first, org.hamcrest.Matcher<? super T> second, org.hamcrest.Matcher<? super T> third, org.hamcrest.Matcher<? super T> fourth, org.hamcrest.Matcher<? super T> fifth, org.hamcrest.Matcher<? super T> sixth) {
return org.hamcrest.core.AllOf.allOf(first, second, third, fourth, fifth, sixth);
}
/**
* Creates a matcher that matches if the examined object matches <b>ANY</b> of the specified matchers.
* For example:
* <pre>assertThat("myValue", anyOf(startsWith("foo"), containsString("Val")))</pre>
*/
public static <T> org.hamcrest.core.AnyOf<T> anyOf(java.lang.Iterable<org.hamcrest.Matcher<? super T>> matchers) {
return org.hamcrest.core.AnyOf.anyOf(matchers);
}
/**
* Creates a matcher that matches if the examined object matches <b>ANY</b> of the specified matchers.
* For example:
* <pre>assertThat("myValue", anyOf(startsWith("foo"), containsString("Val")))</pre>
*/
@SafeVarargs
public static <T> org.hamcrest.core.AnyOf<T> anyOf(org.hamcrest.Matcher<? super T>... matchers) {
return org.hamcrest.core.AnyOf.anyOf(matchers);
}
/**
* Creates a matcher that matches if the examined object matches <b>ANY</b> of the specified matchers.
* For example:
* <pre>assertThat("myValue", anyOf(startsWith("foo"), containsString("Val")))</pre>
*/
public static <T> org.hamcrest.core.AnyOf<T> anyOf(org.hamcrest.Matcher<? super T> first, org.hamcrest.Matcher<? super T> second) {
return org.hamcrest.core.AnyOf.anyOf(first, second);
}
/**
* Creates a matcher that matches if the examined object matches <b>ANY</b> of the specified matchers.
* For example:
* <pre>assertThat("myValue", anyOf(startsWith("foo"), containsString("Val")))</pre>
*/
public static <T> org.hamcrest.core.AnyOf<T> anyOf(org.hamcrest.Matcher<? super T> first, org.hamcrest.Matcher<? super T> second, org.hamcrest.Matcher<? super T> third) {
return org.hamcrest.core.AnyOf.anyOf(first, second, third);
}
/**
* Creates a matcher that matches if the examined object matches <b>ANY</b> of the specified matchers.
* For example:
* <pre>assertThat("myValue", anyOf(startsWith("foo"), containsString("Val")))</pre>
*/
public static <T> org.hamcrest.core.AnyOf<T> anyOf(org.hamcrest.Matcher<? super T> first, org.hamcrest.Matcher<? super T> second, org.hamcrest.Matcher<? super T> third, org.hamcrest.Matcher<? super T> fourth) {
return org.hamcrest.core.AnyOf.anyOf(first, second, third, fourth);
}
/**
* Creates a matcher that matches if the examined object matches <b>ANY</b> of the specified matchers.
* For example:
* <pre>assertThat("myValue", anyOf(startsWith("foo"), containsString("Val")))</pre>
*/
public static <T> org.hamcrest.core.AnyOf<T> anyOf(org.hamcrest.Matcher<? super T> first, org.hamcrest.Matcher<? super T> second, org.hamcrest.Matcher<? super T> third, org.hamcrest.Matcher<? super T> fourth, org.hamcrest.Matcher<? super T> fifth) {
return org.hamcrest.core.AnyOf.anyOf(first, second, third, fourth, fifth);
}
/**
* Creates a matcher that matches if the examined object matches <b>ANY</b> of the specified matchers.
* For example:
* <pre>assertThat("myValue", anyOf(startsWith("foo"), containsString("Val")))</pre>
*/
public static <T> org.hamcrest.core.AnyOf<T> anyOf(org.hamcrest.Matcher<? super T> first, org.hamcrest.Matcher<? super T> second, org.hamcrest.Matcher<? super T> third, org.hamcrest.Matcher<? super T> fourth, org.hamcrest.Matcher<? super T> fifth, org.hamcrest.Matcher<? super T> sixth) {
return org.hamcrest.core.AnyOf.anyOf(first, second, third, fourth, fifth, sixth);
}
/**
* Creates a matcher that matches when both of the specified matchers match the examined object.
* For example:
* <pre>assertThat("fab", both(containsString("a")).and(containsString("b")))</pre>
*/
public static <LHS> org.hamcrest.core.CombinableMatcher.CombinableBothMatcher<LHS> both(org.hamcrest.Matcher<? super LHS> matcher) {
return org.hamcrest.core.CombinableMatcher.both(matcher);
}
/**
* Creates a matcher that matches when either of the specified matchers match the examined object.
* For example:
* <pre>assertThat("fan", either(containsString("a")).or(containsString("b")))</pre>
*/
public static <LHS> org.hamcrest.core.CombinableMatcher.CombinableEitherMatcher<LHS> either(org.hamcrest.Matcher<? super LHS> matcher) {
return org.hamcrest.core.CombinableMatcher.either(matcher);
}
/**
* Wraps an existing matcher, overriding its description with that specified. All other functions are
* delegated to the decorated matcher, including its mismatch description.
* For example:
* <pre>describedAs("a big decimal equal to %0", equalTo(myBigDecimal), myBigDecimal.toPlainString())</pre>
*
* @param description
* the new description for the wrapped matcher
* @param matcher
* the matcher to wrap
* @param values
* optional values to insert into the tokenized description
*/
public static <T> org.hamcrest.Matcher<T> describedAs(java.lang.String description, org.hamcrest.Matcher<T> matcher, java.lang.Object... values) {
return org.hamcrest.core.DescribedAs.describedAs(description, matcher, values);
}
/**
* Creates a matcher for {@link Iterable}s that only matches when a single pass over the
* examined {@link Iterable} yields items that are all matched by the specified
* <code>itemMatcher</code>.
* For example:
* <pre>assertThat(Arrays.asList("bar", "baz"), everyItem(startsWith("ba")))</pre>
*
* @param itemMatcher
* the matcher to apply to every item provided by the examined {@link Iterable}
*/
public static <U> org.hamcrest.Matcher<java.lang.Iterable<? extends U>> everyItem(org.hamcrest.Matcher<U> itemMatcher) {
return org.hamcrest.core.Every.everyItem(itemMatcher);
}
/**
* Decorates another Matcher, retaining its behaviour, but allowing tests
* to be slightly more expressive.
* For example:
* <pre>assertThat(cheese, is(equalTo(smelly)))</pre>
* instead of:
* <pre>assertThat(cheese, equalTo(smelly))</pre>
*/
public static <T> org.hamcrest.Matcher<T> is(org.hamcrest.Matcher<T> matcher) {
return org.hamcrest.core.Is.is(matcher);
}
/**
* A shortcut to the frequently used <code>is(equalTo(x))</code>.
* For example:
* <pre>assertThat(cheese, is(smelly))</pre>
* instead of:
* <pre>assertThat(cheese, is(equalTo(smelly)))</pre>
*/
public static <T> org.hamcrest.Matcher<T> is(T value) {
return org.hamcrest.core.Is.is(value);
}
/**
* A shortcut to the frequently used <code>is(instanceOf(SomeClass.class))</code>.
* For example:
* <pre>assertThat(cheese, isA(Cheddar.class))</pre>
* instead of:
* <pre>assertThat(cheese, is(instanceOf(Cheddar.class)))</pre>
*/
public static <T> org.hamcrest.Matcher<T> isA(java.lang.Class<?> type) {
return org.hamcrest.core.Is.isA(type);
}
/**
* Creates a matcher that always matches, regardless of the examined object.
*/
public static org.hamcrest.Matcher<java.lang.Object> anything() {
return org.hamcrest.core.IsAnything.anything();
}
/**
* Creates a matcher that always matches, regardless of the examined object, but describes
* itself with the specified {@link String}.
*
* @param description
* a meaningful {@link String} used when describing itself
*/
public static org.hamcrest.Matcher<java.lang.Object> anything(java.lang.String description) {
return org.hamcrest.core.IsAnything.anything(description);
}
/**
* Creates a matcher for {@link Iterable}s that only matches when a single pass over the
* examined {@link Iterable} yields at least one item that is matched by the specified
* <code>itemMatcher</code>. Whilst matching, the traversal of the examined {@link Iterable}
* will stop as soon as a matching item is found.
* For example:
* <pre>assertThat(Arrays.asList("foo", "bar"), hasItem(startsWith("ba")))</pre>
*
* @param itemMatcher
* the matcher to apply to items provided by the examined {@link Iterable}
*/
public static <T> org.hamcrest.Matcher<java.lang.Iterable<? super T>> hasItem(org.hamcrest.Matcher<? super T> itemMatcher) {
return IsIterableContaining.hasItem(itemMatcher);
}
/**
* Creates a matcher for {@link Iterable}s that only matches when a single pass over the
* examined {@link Iterable} yields at least one item that is equal to the specified
* <code>item</code>. Whilst matching, the traversal of the examined {@link Iterable}
* will stop as soon as a matching item is found.
* For example:
* <pre>assertThat(Arrays.asList("foo", "bar"), hasItem("bar"))</pre>
*
* @param item
* the item to compare against the items provided by the examined {@link Iterable}
*/
public static <T> org.hamcrest.Matcher<java.lang.Iterable<? super T>> hasItem(T item) {
return IsIterableContaining.hasItem(item);
}
/**
* Creates a matcher for {@link Iterable}s that matches when consecutive passes over the
* examined {@link Iterable} yield at least one item that is matched by the corresponding
* matcher from the specified <code>itemMatchers</code>. Whilst matching, each traversal of
* the examined {@link Iterable} will stop as soon as a matching item is found.
* For example:
* <pre>assertThat(Arrays.asList("foo", "bar", "baz"), hasItems(endsWith("z"), endsWith("o")))</pre>
*
* @param itemMatchers
* the matchers to apply to items provided by the examined {@link Iterable}
*/
@SafeVarargs
public static <T> org.hamcrest.Matcher<java.lang.Iterable<T>> hasItems(org.hamcrest.Matcher<? super T>... itemMatchers) {
return IsIterableContaining.hasItems(itemMatchers);
}
/**
* Creates a matcher for {@link Iterable}s that matches when consecutive passes over the
* examined {@link Iterable} yield at least one item that is equal to the corresponding
* item from the specified <code>items</code>. Whilst matching, each traversal of the
* examined {@link Iterable} will stop as soon as a matching item is found.
* For example:
* <pre>assertThat(Arrays.asList("foo", "bar", "baz"), hasItems("baz", "foo"))</pre>
*
* @param items
* the items to compare against the items provided by the examined {@link Iterable}
*/
@SafeVarargs
public static <T> org.hamcrest.Matcher<java.lang.Iterable<T>> hasItems(T... items) {
return IsIterableContaining.hasItems(items);
}
/**
* Creates a matcher that matches when the examined object is logically equal to the specified
* <code>operand</code>, as determined by calling the {@link java.lang.Object#equals} method on
* the <b>examined</b> object.
*
* <p>If the specified operand is <code>null</code> then the created matcher will only match if
* the examined object's <code>equals</code> method returns <code>true</code> when passed a
* <code>null</code> (which would be a violation of the <code>equals</code> contract), unless the
* examined object itself is <code>null</code>, in which case the matcher will return a positive
* match.</p>
*
* <p>The created matcher provides a special behaviour when examining <code>Array</code>s, whereby
* it will match if both the operand and the examined object are arrays of the same length and
* contain items that are equal to each other (according to the above rules) <b>in the same
* indexes</b>.</p>
* For example:
* <pre>
* assertThat("foo", equalTo("foo"));
* assertThat(new String[] {"foo", "bar"}, equalTo(new String[] {"foo", "bar"}));
* </pre>
*/
public static <T> org.hamcrest.Matcher<T> equalTo(T operand) {
return org.hamcrest.core.IsEqual.equalTo(operand);
}
/**
* Creates an {@link org.hamcrest.core.IsEqual} matcher that does not enforce the values being
* compared to be of the same static type.
*/
public static org.hamcrest.Matcher<java.lang.Object> equalToObject(java.lang.Object operand) {
return org.hamcrest.core.IsEqual.equalToObject(operand);
}
/**
* Creates a matcher that matches when the examined object is an instance of the specified <code>type</code>,
* as determined by calling the {@link java.lang.Class#isInstance(Object)} method on that type, passing the
* the examined object.
*
* <p>The created matcher forces a relationship between specified type and the examined object, and should be
* used when it is necessary to make generics conform, for example in the JMock clause
* <code>with(any(Thing.class))</code></p>
* For example:
* <pre>assertThat(new Canoe(), instanceOf(Canoe.class));</pre>
*/
public static <T> org.hamcrest.Matcher<T> any(java.lang.Class<T> type) {
return org.hamcrest.core.IsInstanceOf.any(type);
}
/**
* Creates a matcher that matches when the examined object is an instance of the specified <code>type</code>,
* as determined by calling the {@link java.lang.Class#isInstance(Object)} method on that type, passing the
* the examined object.
*
* <p>The created matcher assumes no relationship between specified type and the examined object.</p>
* For example:
* <pre>assertThat(new Canoe(), instanceOf(Paddlable.class));</pre>
*/
public static <T> org.hamcrest.Matcher<T> instanceOf(java.lang.Class<?> type) {
return org.hamcrest.core.IsInstanceOf.instanceOf(type);
}
/**
* Creates a matcher that wraps an existing matcher, but inverts the logic by which
* it will match.
* For example:
* <pre>assertThat(cheese, is(not(equalTo(smelly))))</pre>
*
* @param matcher
* the matcher whose sense should be inverted
*/
public static <T> org.hamcrest.Matcher<T> not(org.hamcrest.Matcher<T> matcher) {
return org.hamcrest.core.IsNot.not(matcher);
}
/**
* A shortcut to the frequently used <code>not(equalTo(x))</code>.
* For example:
* <pre>assertThat(cheese, is(not(smelly)))</pre>
* instead of:
* <pre>assertThat(cheese, is(not(equalTo(smelly))))</pre>
*
* @param value
* the value that any examined object should <b>not</b> equal
*/
public static <T> org.hamcrest.Matcher<T> not(T value) {
return org.hamcrest.core.IsNot.not(value);
}
/**
* A shortcut to the frequently used <code>not(nullValue())</code>.
* For example:
* <pre>assertThat(cheese, is(notNullValue()))</pre>
* instead of:
* <pre>assertThat(cheese, is(not(nullValue())))</pre>
*/
public static org.hamcrest.Matcher<java.lang.Object> notNullValue() {
return org.hamcrest.core.IsNull.notNullValue();
}
/**
* A shortcut to the frequently used <code>not(nullValue(X.class)). Accepts a
* single dummy argument to facilitate type inference.</code>.
* For example:
* <pre>assertThat(cheese, is(notNullValue(X.class)))</pre>
* instead of:
* <pre>assertThat(cheese, is(not(nullValue(X.class))))</pre>
*
* @param type
* dummy parameter used to infer the generic type of the returned matcher
*/
public static <T> org.hamcrest.Matcher<T> notNullValue(java.lang.Class<T> type) {
return org.hamcrest.core.IsNull.notNullValue(type);
}
/**
* Creates a matcher that matches if examined object is <code>null</code>.
* For example:
* <pre>assertThat(cheese, is(nullValue())</pre>
*/
public static org.hamcrest.Matcher<java.lang.Object> nullValue() {
return org.hamcrest.core.IsNull.nullValue();
}
/**
* Creates a matcher that matches if examined object is <code>null</code>. Accepts a
* single dummy argument to facilitate type inference.
* For example:
* <pre>assertThat(cheese, is(nullValue(Cheese.class))</pre>
*
* @param type
* dummy parameter used to infer the generic type of the returned matcher
*/
public static <T> org.hamcrest.Matcher<T> nullValue(java.lang.Class<T> type) {
return org.hamcrest.core.IsNull.nullValue(type);
}
/**
* Creates a matcher that matches only when the examined object is the same instance as
* the specified target object.
*
* @param target
* the target instance against which others should be assessed
*/
public static <T> org.hamcrest.Matcher<T> sameInstance(T target) {
return org.hamcrest.core.IsSame.sameInstance(target);
}
/**
* Creates a matcher that matches only when the examined object is the same instance as
* the specified target object.
*
* @param target
* the target instance against which others should be assessed
*/
public static <T> org.hamcrest.Matcher<T> theInstance(T target) {
return org.hamcrest.core.IsSame.theInstance(target);
}
/**
* Creates a matcher that matches if the examined {@link String} contains the specified
* {@link String} anywhere.
* For example:
* <pre>assertThat("myStringOfNote", containsString("ring"))</pre>
*
* @param substring
* the substring that the returned matcher will expect to find within any examined string
*/
public static Matcher<java.lang.String> containsString(java.lang.String substring) {
return org.hamcrest.core.StringContains.containsString(substring);
}
/**
* Creates a matcher that matches if the examined {@link String} contains the specified
* {@link String} anywhere, ignoring case.
* For example:
* <pre>assertThat("myStringOfNote", containsStringIgnoringCase("Ring"))</pre>
*
* @param substring
* the substring that the returned matcher will expect to find within any examined string
*/
public static Matcher<java.lang.String> containsStringIgnoringCase(java.lang.String substring) {
return org.hamcrest.core.StringContains.containsStringIgnoringCase(substring);
}
/**
* <p>
* Creates a matcher that matches if the examined {@link String} starts with the specified
* {@link String}.
* </p>
* For example:
* <pre>assertThat("myStringOfNote", startsWith("my"))</pre>
*
* @param prefix
* the substring that the returned matcher will expect at the start of any examined string
*/
public static Matcher<java.lang.String> startsWith(java.lang.String prefix) {
return org.hamcrest.core.StringStartsWith.startsWith(prefix);
}
/**
* <p>
* Creates a matcher that matches if the examined {@link String} starts with the specified
* {@link String}, ignoring case
* </p>
* For example:
* <pre>assertThat("myStringOfNote", startsWithIgnoringCase("My"))</pre>
*
* @param prefix
* the substring that the returned matcher will expect at the start of any examined string
*/
public static Matcher<java.lang.String> startsWithIgnoringCase(java.lang.String prefix) {
return org.hamcrest.core.StringStartsWith.startsWithIgnoringCase(prefix);
}
/**
* Creates a matcher that matches if the examined {@link String} ends with the specified
* {@link String}.
* For example:
* <pre>assertThat("myStringOfNote", endsWith("Note"))</pre>
*
* @param suffix
* the substring that the returned matcher will expect at the end of any examined string
*/
public static Matcher<java.lang.String> endsWith(java.lang.String suffix) {
return org.hamcrest.core.StringEndsWith.endsWith(suffix);
}
/**
* Creates a matcher that matches if the examined {@link String} ends with the specified
* {@link String}, ignoring case.
* For example:
* <pre>assertThat("myStringOfNote", endsWithIgnoringCase("note"))</pre>
*
* @param suffix
* the substring that the returned matcher will expect at the end of any examined string
*/
public static Matcher<java.lang.String> endsWithIgnoringCase(java.lang.String suffix) {
return org.hamcrest.core.StringEndsWith.endsWithIgnoringCase(suffix);
}
/**
* Validate a string with a {@link java.util.regex.Pattern}.
*
* <pre>
* assertThat("abc", matchesRegex(Pattern.compile("ˆ[a-z]$"));
* </pre>
*
* @param pattern
* the pattern to be used.
* @return The matcher.
*/
public static Matcher<String> matchesRegex(Pattern pattern) {
return StringRegularExpression.matchesRegex(pattern);
}
/**
* Validate a string with a regex.
*
* <pre>
* assertThat("abc", matchesRegex("ˆ[a-z]+$"));
* </pre>
*
* @param regex
* The regex to be used for the validation.
* @return The matcher.
*/
public static Matcher<String> matchesRegex(String regex) {
return StringRegularExpression.matchesRegex(Pattern.compile(regex));
}
/**
* Creates a matcher that matches arrays whose elements are satisfied by the specified matchers. Matches
* positively only if the number of matchers specified is equal to the length of the examined array and
* each matcher[i] is satisfied by array[i].
* For example:
* <pre>assertThat(new Integer[]{1,2,3}, is(array(equalTo(1), equalTo(2), equalTo(3))))</pre>
*
* @param elementMatchers
* the matchers that the elements of examined arrays should satisfy
*/
@SafeVarargs
public static <T> org.hamcrest.collection.IsArray<T> array(org.hamcrest.Matcher<? super T>... elementMatchers) {
return org.hamcrest.collection.IsArray.array(elementMatchers);
}
/**
* Creates a matcher for arrays that matches when the examined array contains at least one item
* that is matched by the specified <code>elementMatcher</code>. Whilst matching, the traversal
* of the examined array will stop as soon as a matching element is found.
* For example:
* <pre>assertThat(new String[] {"foo", "bar"}, hasItemInArray(startsWith("ba")))</pre>
*
* @param elementMatcher
* the matcher to apply to elements in examined arrays
*/
public static <T> org.hamcrest.Matcher<T[]> hasItemInArray(org.hamcrest.Matcher<? super T> elementMatcher) {
return ArrayMatching.hasItemInArray(elementMatcher);
}
/**
* A shortcut to the frequently used <code>hasItemInArray(equalTo(x))</code>.
* For example:
* <pre>assertThat(hasItemInArray(x))</pre>
* instead of:
* <pre>assertThat(hasItemInArray(equalTo(x)))</pre>
*
* @param element
* the element that should be present in examined arrays
*/
public static <T> org.hamcrest.Matcher<T[]> hasItemInArray(T element) {
return ArrayMatching.hasItemInArray(element);
}
/**
* Creates a matcher for arrays that matches when each item in the examined array is
* logically equal to the corresponding item in the specified items. For a positive match,
* the examined array must be of the same length as the number of specified items.
* For example:
* <pre>assertThat(new String[]{"foo", "bar"}, arrayContaining("foo", "bar"))</pre>
*
* @param items
* the items that must equal the items within an examined array
*/
@SafeVarargs
public static <E> org.hamcrest.Matcher<E[]> arrayContaining(E... items) {
return ArrayMatching.arrayContaining(items);
}
/**
* Creates a matcher for arrays that matches when each item in the examined array satisfies the
* corresponding matcher in the specified matchers. For a positive match, the examined array
* must be of the same length as the number of specified matchers.
* For example:
* <pre>assertThat(new String[]{"foo", "bar"}, arrayContaining(equalTo("foo"), equalTo("bar")))</pre>
*
* @param itemMatchers
* the matchers that must be satisfied by the items in the examined array
*/
@SafeVarargs
public static <E> org.hamcrest.Matcher<E[]> arrayContaining(org.hamcrest.Matcher<? super E>... itemMatchers) {
return ArrayMatching.arrayContaining(itemMatchers);
}
/**
* Creates a matcher for arrays that matches when each item in the examined array satisfies the
* corresponding matcher in the specified list of matchers. For a positive match, the examined array
* must be of the same length as the specified list of matchers.
* For example:
* <pre>assertThat(new String[]{"foo", "bar"}, arrayContaining(Arrays.asList(equalTo("foo"), equalTo("bar"))))</pre>
*
* @param itemMatchers
* a list of matchers, each of which must be satisfied by the corresponding item in an examined array
*/
public static <E> org.hamcrest.Matcher<E[]> arrayContaining(java.util.List<org.hamcrest.Matcher<? super E>> itemMatchers) {
return ArrayMatching.arrayContaining(itemMatchers);
}
/**
* <p>
* Creates an order agnostic matcher for arrays that matches when each item in the
* examined array satisfies one matcher anywhere in the specified matchers.
* For a positive match, the examined array must be of the same length as the number of
* specified matchers.
* </p>
* <p>
* N.B. each of the specified matchers will only be used once during a given examination, so be
* careful when specifying matchers that may be satisfied by more than one entry in an examined
* array.
* </p>
* <p>
* For example:
* </p>
* <pre>assertThat(new String[]{"foo", "bar"}, arrayContainingInAnyOrder(equalTo("bar"), equalTo("foo")))</pre>
*
* @param itemMatchers
* a list of matchers, each of which must be satisfied by an entry in an examined array
*/
@SafeVarargs
public static <E> org.hamcrest.Matcher<E[]> arrayContainingInAnyOrder(org.hamcrest.Matcher<? super E>... itemMatchers) {
return ArrayMatching.arrayContainingInAnyOrder(itemMatchers);
}
/**
* <p>
* Creates an order agnostic matcher for arrays that matches when each item in the
* examined array satisfies one matcher anywhere in the specified collection of matchers.
* For a positive match, the examined array must be of the same length as the specified collection
* of matchers.
* </p>
* <p>
* N.B. each matcher in the specified collection will only be used once during a given
* examination, so be careful when specifying matchers that may be satisfied by more than
* one entry in an examined array.
* </p>
* <p>
* For example:
* </p>
* <pre>assertThat(new String[]{"foo", "bar"}, arrayContainingInAnyOrder(Arrays.asList(equalTo("bar"), equalTo("foo"))))</pre>
*
* @param itemMatchers
* a list of matchers, each of which must be satisfied by an item provided by an examined array
*/
public static <E> org.hamcrest.Matcher<E[]> arrayContainingInAnyOrder(java.util.Collection<org.hamcrest.Matcher<? super E>> itemMatchers) {
return ArrayMatching.arrayContainingInAnyOrder(itemMatchers);
}
/**
* <p>Creates an order agnostic matcher for arrays that matches when each item in the
* examined array is logically equal to one item anywhere in the specified items.
* For a positive match, the examined array must be of the same length as the number of
* specified items.
* </p>
* <p>N.B. each of the specified items will only be used once during a given examination, so be
* careful when specifying items that may be equal to more than one entry in an examined
* array.
* </p>
* <p>
* For example:
* </p>
* <pre>assertThat(new String[]{"foo", "bar"}, arrayContainingInAnyOrder("bar", "foo"))</pre>
*
* @param items
* the items that must equal the entries of an examined array, in any order
*/
@SafeVarargs
public static <E> org.hamcrest.Matcher<E[]> arrayContainingInAnyOrder(E... items) {
return ArrayMatching.arrayContainingInAnyOrder(items);
}
/**
* Creates a matcher for arrays that matches when the <code>length</code> of the array
* satisfies the specified matcher.
* For example:
* <pre>assertThat(new String[]{"foo", "bar"}, arrayWithSize(equalTo(2)))</pre>
*
* @param sizeMatcher
* a matcher for the length of an examined array
*/
public static <E> org.hamcrest.Matcher<E[]> arrayWithSize(org.hamcrest.Matcher<? super java.lang.Integer> sizeMatcher) {
return org.hamcrest.collection.IsArrayWithSize.arrayWithSize(sizeMatcher);
}
/**
* Creates a matcher for arrays that matches when the <code>length</code> of the array
* equals the specified <code>size</code>.
* For example:
* <pre>assertThat(new String[]{"foo", "bar"}, arrayWithSize(2))</pre>
*
* @param size
* the length that an examined array must have for a positive match
*/
public static <E> org.hamcrest.Matcher<E[]> arrayWithSize(int size) {
return org.hamcrest.collection.IsArrayWithSize.arrayWithSize(size);
}
/**
* Creates a matcher for arrays that matches when the <code>length</code> of the array
* is zero.
* For example:
* <pre>assertThat(new String[0], emptyArray())</pre>
*/
public static <E> org.hamcrest.Matcher<E[]> emptyArray() {
return org.hamcrest.collection.IsArrayWithSize.emptyArray();
}
/**
* Creates a matcher for {@link java.util.Map}s that matches when the <code>size()</code> method returns
* a value that satisfies the specified matcher.
* For example:
* <pre>assertThat(myMap, is(aMapWithSize(equalTo(2))))</pre>
*
* @param sizeMatcher
* a matcher for the size of an examined {@link java.util.Map}
*/
public static <K, V> org.hamcrest.Matcher<java.util.Map<? extends K,? extends V>> aMapWithSize(org.hamcrest.Matcher<? super java.lang.Integer> sizeMatcher) {
return org.hamcrest.collection.IsMapWithSize.aMapWithSize(sizeMatcher);
}
/**
* Creates a matcher for {@link java.util.Map}s that matches when the <code>size()</code> method returns
* a value equal to the specified <code>size</code>.
* For example:
* <pre>assertThat(myMap, is(aMapWithSize(2)))</pre>
*
* @param size
* the expected size of an examined {@link java.util.Map}
*/
public static <K, V> org.hamcrest.Matcher<java.util.Map<? extends K,? extends V>> aMapWithSize(int size) {
return org.hamcrest.collection.IsMapWithSize.aMapWithSize(size);
}
/**
* Creates a matcher for {@link java.util.Map}s that matches when the <code>size()</code> method returns
* zero.
* For example:
* <pre>assertThat(myMap, is(anEmptyMap()))</pre>
*/
public static <K, V> org.hamcrest.Matcher<java.util.Map<? extends K,? extends V>> anEmptyMap() {
return org.hamcrest.collection.IsMapWithSize.anEmptyMap();
}
/**
* Creates a matcher for {@link java.util.Collection}s that matches when the <code>size()</code> method returns
* a value that satisfies the specified matcher.
* For example:
* <pre>assertThat(Arrays.asList("foo", "bar"), hasSize(equalTo(2)))</pre>
*
* @param sizeMatcher
* a matcher for the size of an examined {@link java.util.Collection}
*/
public static <E> org.hamcrest.Matcher<java.util.Collection<? extends E>> hasSize(org.hamcrest.Matcher<? super java.lang.Integer> sizeMatcher) {
return org.hamcrest.collection.IsCollectionWithSize.hasSize(sizeMatcher);
}
/**
* Creates a matcher for {@link java.util.Collection}s that matches when the <code>size()</code> method returns
* a value equal to the specified <code>size</code>.
* For example:
* <pre>assertThat(Arrays.asList("foo", "bar"), hasSize(2))</pre>
*
* @param size
* the expected size of an examined {@link java.util.Collection}
*/
public static <E> org.hamcrest.Matcher<java.util.Collection<? extends E>> hasSize(int size) {
return org.hamcrest.collection.IsCollectionWithSize.hasSize(size);
}
/**
* Creates a matcher for {@link java.util.Collection}s matching examined collections whose <code>isEmpty</code>
* method returns <code>true</code>.
* For example:
* <pre>assertThat(new ArrayList<String>(), is(empty()))</pre>
*/
public static <E> org.hamcrest.Matcher<java.util.Collection<? extends E>> empty() {
return org.hamcrest.collection.IsEmptyCollection.empty();
}
/**
* Creates a matcher for {@link java.util.Collection}s matching examined collections whose <code>isEmpty</code>
* method returns <code>true</code>.
* For example:
* <pre>assertThat(new ArrayList<String>(), is(emptyCollectionOf(String.class)))</pre>
*
* @param unusedToForceReturnType
* the type of the collection's content
*/
public static <E> org.hamcrest.Matcher<java.util.Collection<E>> emptyCollectionOf(java.lang.Class<E> unusedToForceReturnType) {
return org.hamcrest.collection.IsEmptyCollection.emptyCollectionOf(unusedToForceReturnType);
}
/**
* Creates a matcher for {@link Iterable}s matching examined iterables that yield no items.
* For example:
* <pre>assertThat(new ArrayList<String>(), is(emptyIterable()))</pre>
*/
public static <E> org.hamcrest.Matcher<java.lang.Iterable<? extends E>> emptyIterable() {
return org.hamcrest.collection.IsEmptyIterable.emptyIterable();
}
/**
* Creates a matcher for {@link Iterable}s matching examined iterables that yield no items.
* For example:
* <pre>assertThat(new ArrayList<String>(), is(emptyIterableOf(String.class)))</pre>
*
* @param unusedToForceReturnType
* the type of the iterable's content
*/
public static <E> org.hamcrest.Matcher<java.lang.Iterable<E>> emptyIterableOf(java.lang.Class<E> unusedToForceReturnType) {
return org.hamcrest.collection.IsEmptyIterable.emptyIterableOf(unusedToForceReturnType);
}
/**
* Creates a matcher for {@link Iterable}s that matches when a single pass over the
* examined {@link Iterable} yields a series of items, each logically equal to the
* corresponding item in the specified items. For a positive match, the examined iterable
* must be of the same length as the number of specified items.
* For example:
* <pre>assertThat(Arrays.asList("foo", "bar"), contains("foo", "bar"))</pre>
*
* @param items
* the items that must equal the items provided by an examined {@link Iterable}
*/
@SafeVarargs
public static <E> org.hamcrest.Matcher<java.lang.Iterable<? extends E>> contains(E... items) {
return org.hamcrest.collection.IsIterableContainingInOrder.contains(items);
}
/**
* Creates a matcher for {@link Iterable}s that matches when a single pass over the
* examined {@link Iterable} yields a single item that satisfies the specified matcher.
* For a positive match, the examined iterable must only yield one item.
* For example:
* <pre>assertThat(Arrays.asList("foo"), contains(equalTo("foo")))</pre>
*
* @param itemMatcher
* the matcher that must be satisfied by the single item provided by an
* examined {@link Iterable}
*/
public static <E> org.hamcrest.Matcher<java.lang.Iterable<? extends E>> contains(org.hamcrest.Matcher<? super E> itemMatcher) {
return org.hamcrest.collection.IsIterableContainingInOrder.contains(itemMatcher);
}
/**
* Creates a matcher for {@link Iterable}s that matches when a single pass over the
* examined {@link Iterable} yields a series of items, each satisfying the corresponding
* matcher in the specified matchers. For a positive match, the examined iterable
* must be of the same length as the number of specified matchers.
* For example:
* <pre>assertThat(Arrays.asList("foo", "bar"), contains(equalTo("foo"), equalTo("bar")))</pre>
*
* @param itemMatchers
* the matchers that must be satisfied by the items provided by an examined {@link Iterable}
*/
@SafeVarargs
public static <E> org.hamcrest.Matcher<java.lang.Iterable<? extends E>> contains(org.hamcrest.Matcher<? super E>... itemMatchers) {
return org.hamcrest.collection.IsIterableContainingInOrder.contains(itemMatchers);
}
/**
* Creates a matcher for {@link Iterable}s that matches when a single pass over the
* examined {@link Iterable} yields a series of items, each satisfying the corresponding
* matcher in the specified list of matchers. For a positive match, the examined iterable
* must be of the same length as the specified list of matchers.
* For example:
* <pre>assertThat(Arrays.asList("foo", "bar"), contains(Arrays.asList(equalTo("foo"), equalTo("bar"))))</pre>
*
* @param itemMatchers
* a list of matchers, each of which must be satisfied by the corresponding item provided by
* an examined {@link Iterable}
*/
public static <E> org.hamcrest.Matcher<java.lang.Iterable<? extends E>> contains(java.util.List<org.hamcrest.Matcher<? super E>> itemMatchers) {
return org.hamcrest.collection.IsIterableContainingInOrder.contains(itemMatchers);
}
/**
* <p>
* Creates an order agnostic matcher for {@link Iterable}s that matches when a single pass over
* the examined {@link Iterable} yields a series of items, each satisfying one matcher anywhere
* in the specified matchers. For a positive match, the examined iterable must be of the same
* length as the number of specified matchers.
* </p>
* <p>
* N.B. each of the specified matchers will only be used once during a given examination, so be
* careful when specifying matchers that may be satisfied by more than one entry in an examined
* iterable.
* </p>
* <p>
* For example:
* </p>
* <pre>assertThat(Arrays.asList("foo", "bar"), containsInAnyOrder(equalTo("bar"), equalTo("foo")))</pre>
*
* @param itemMatchers
* a list of matchers, each of which must be satisfied by an item provided by an examined {@link Iterable}
*/
@SafeVarargs
public static <T> org.hamcrest.Matcher<java.lang.Iterable<? extends T>> containsInAnyOrder(org.hamcrest.Matcher<? super T>... itemMatchers) {
return org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder(itemMatchers);
}
/**
* <p>
* Creates an order agnostic matcher for {@link Iterable}s that matches when a single pass over
* the examined {@link Iterable} yields a series of items, each logically equal to one item
* anywhere in the specified items. For a positive match, the examined iterable
* must be of the same length as the number of specified items.
* </p>
* <p>
* N.B. each of the specified items will only be used once during a given examination, so be
* careful when specifying items that may be equal to more than one entry in an examined
* iterable.
* </p>
* <p>
* For example:
* </p>
* <pre>assertThat(Arrays.asList("foo", "bar"), containsInAnyOrder("bar", "foo"))</pre>
*
* @param items
* the items that must equal the items provided by an examined {@link Iterable} in any order
*/
@SafeVarargs
public static <T> org.hamcrest.Matcher<java.lang.Iterable<? extends T>> containsInAnyOrder(T... items) {
return org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder(items);
}
/**
* <p>
* Creates an order agnostic matcher for {@link Iterable}s that matches when a single pass over
* the examined {@link Iterable} yields a series of items, each satisfying one matcher anywhere
* in the specified collection of matchers. For a positive match, the examined iterable
* must be of the same length as the specified collection of matchers.
* </p>
* <p>
* N.B. each matcher in the specified collection will only be used once during a given
* examination, so be careful when specifying matchers that may be satisfied by more than
* one entry in an examined iterable.
* </p>
* <p>For example:</p>