/
Matcher.scala
3219 lines (2960 loc) · 121 KB
/
Matcher.scala
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
/*
* Copyright 2001-2013 Artima, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.scalatest.matchers
import org.scalactic._
import org.scalatest.enablers._
import org.scalatest.matchers.dsl._
import org.scalatest.FailureMessages
import MatchersHelper.andMatchersAndApply
import MatchersHelper.orMatchersAndApply
import org.scalatest.Resources
import scala.collection.GenTraversable
import scala.reflect.ClassTag
import scala.util.matching.Regex
import TripleEqualsSupport.Spread
import TripleEqualsSupport.TripleEqualsInvocation
/**
* Trait extended by objects that can match a value of the specified type. The value to match is
* passed to the matcher's <code>apply</code> method. The result is a <code>MatchResult</code>.
* A matcher is, therefore, a function from the specified type, <code>T</code>, to a <code>MatchResult</code>.
* <p></p> <!-- needed otherwise the heading below shows up in the wrong place. dumb scaladoc algo -->
*
* <h2>Creating custom matchers</h2>
*
* <p>
* If none of the built-in matcher syntax satisfies a particular need you have, you can create
* custom <code>Matcher</code>s that allow
* you to place your own syntax directly after <code>should</code>. For example, although you can ensure that a <code>java.io.File</code> has a name
* that ends with a particular extension like this:
* </p>
*
* <pre class="stHighlighted">
* file.getName should endWith (<span class="stQuotedString">".txt"</span>)
* </pre>
*
* <p>
* You might prefer
* to create a custom <code>Matcher[java.io.File]</code>
* named <code>endWithExtension</code>, so you could write expressions like:
* </p>
*
* <pre class="stHighlighted">
* file should endWithExtension (<span class="stQuotedString">"txt"</span>)
* file should not endWithExtension <span class="stQuotedString">"txt"</span>
* file should (exist and endWithExtension (<span class="stQuotedString">"txt"</span>))
* </pre>
*
* <p>
* One good way to organize custom matchers is to place them inside one or more
* traits that you can then mix into the suites that need them. Here's an example:
* </p>
*
* <pre class="stHighlighted">
* <span class="stReserved">import</span> org.scalatest._
* <span class="stReserved">import</span> matchers._
* <br/><span class="stReserved">trait</span> <span class="stType">CustomMatchers</span> {
* <br/> <span class="stReserved">class</span> <span class="stType">FileEndsWithExtensionMatcher</span>(expectedExtension: <span class="stType">String</span>) <span class="stReserved">extends</span> <span class="stType">Matcher[java.io.File]</span> {
* <br/> <span class="stReserved">def</span> apply(left: <span class="stType">java.io.File</span>) = {
* <span class="stReserved">val</span> name = left.getName
* <span class="stType">MatchResult</span>(
* name.endsWith(expectedExtension),
* s<span class="stQuotedString">"""File $name did not end with extension "$expectedExtension""""</span>,
* s<span class="stQuotedString">"""File $name ended with extension "$expectedExtension""""</span>
* )
* }
* }
* <br/> <span class="stReserved">def</span> endWithExtension(expectedExtension: <span class="stType">String</span>) = <span class="stReserved">new</span> <span class="stType">FileEndsWithExtensionMatcher</span>(expectedExtension)
* }
* <br/><span class="stLineComment">// Make them easy to import with:</span>
* <span class="stLineComment">// import CustomMatchers._</span>
* <span class="stReserved">object</span> <span class="stType">CustomMatchers</span> <span class="stReserved">extends</span> <span class="stType">CustomMatchers</span>
* </pre>
*
* <p>
* Note: the <code>CustomMatchers</code> companion object exists to make it easy to bring the
* matchers defined in this trait into scope via importing, instead of mixing in the trait. The ability
* to import them is useful, for example, when you want to use the matchers defined in a trait in the Scala interpreter console.
* </p>
*
* <p>
* This trait contains one matcher class, <code>FileEndsWithExtensionMatcher</code>, and a <code>def</code> named <code>endWithExtension</code> that returns a new
* instance of <code>FileEndsWithExtensionMatcher</code>. Because the class extends <code>Matcher[java.io.File]</code>,
* the compiler will only allow it be used to match against instances of <code>java.io.File</code>. A matcher must declare an
* <code>apply</code> method that takes the type decared in <code>Matcher</code>'s type parameter, in this case <code>java.io.File</code>.
* The apply method will return a <code>MatchResult</code> whose <code>matches</code> field will indicate whether the match succeeded.
* The <code>failureMessage</code> field will provide a programmer-friendly error message indicating, in the event of a match failure, what caused
* the match to fail.
* </p>
*
* <p>
* The <code>FileEndsWithExtensionMatcher</code> matcher in this example determines success by determining if the passed <code>java.io.File</code> ends with
* the desired extension. It does this in the first argument passed to the <code>MatchResult</code> factory method:
* </p>
*
* <pre class="stHighlighted">
* name.endsWith(expectedExtension)
* </pre>
*
* <p>
* In other words, if the file name has the expected extension, this matcher matches.
* The next argument to <code>MatchResult</code>'s factory method produces the failure message string:
* </p>
*
* <pre class="stHighlighted">
* s<span class="stQuotedString">"""File $name did not end with extension "$expectedExtension""""</span>,
* </pre>
*
* <p>
* For example, consider this matcher expression:
* </p>
*
* <pre class="stHighlighted">
* <span class="stReserved">import</span> org.scalatest._
* <span class="stReserved">import</span> Matchers._
* <span class="stReserved">import</span> java.io.File
* <span class="stReserved">import</span> CustomMatchers._
* <br/><span class="stReserved">new</span> <span class="stType">File</span>(<span class="stQuotedString">"essay.text"</span>) should endWithExtension (<span class="stQuotedString">"txt"</span>)
* </pre>
*
* <p>
* Because the passed <code>java.io.File</code> has the name <code>essay.text</code>, but the expected extension is <code>"txt"</code>, the failure
* message would be:
* </p>
*
* <pre>
* File essay.text did not have extension "txt"
* </pre>
*
* <p>
* For more information on the fields in a <code>MatchResult</code>, including the subsequent field (or fields) that follow the failure message,
* please see the documentation for <a href="MatchResult.html"><code>MatchResult</code></a>.
* </p>
*
* <a name="otherways"></a>
* <h2>Creating dynamic matchers</h2>
*
* <p>
* There are other ways to create new matchers besides defining one as shown above. For example, you might check that a file is hidden like this:
* </p>
*
* <pre class="stHighlighted">
* <span class="stReserved">new</span> <span class="stType">File</span>(<span class="stQuotedString">"secret.txt"</span>) should be (<span class="stQuotedString">'hidden</span>)
* </pre>
*
* <p>
* If you wanted to get rid of the tick mark, you could simply define <code>hidden</code> like this:
* </p>
*
* <pre class="stHighlighted">
* <span class="stReserved">val</span> hidden = <span class="stQuotedString">'hidden</span>
* </pre>
*
* <p>
* Now you can check that an file is hidden without the tick mark:
* </p>
*
* <pre class="stHighlighted">
* <span class="stReserved">new</span> <span class="stType">File</span>(<span class="stQuotedString">"secret.txt"</span>) should be (hidden)
* </pre>
*
* <p>
* You could get rid of the parens with by using <code>shouldBe</code>:
* </p>
*
* <pre class="stHighlighted">
* <span class="stReserved">new</span> <span class="stType">File</span>(<span class="stQuotedString">"secret.txt"</span>) shouldBe hidden
* </pre>
*
* <h2>Creating matchers using logical operators</h2>
*
* <p>
* You can also use ScalaTest matchers' logical operators to combine existing matchers into new ones, like this:
* </p>
*
* <pre class="stHighlighted">
* <span class="stReserved">val</span> beWithinTolerance = be >= <span class="stLiteral">0</span> and be <= <span class="stLiteral">10</span>
* </pre>
*
* <p>
* Now you could check that a number is within the tolerance (in this case, between 0 and 10, inclusive), like this:
* </p>
*
* <pre class="stHighlighted">
* num should beWithinTolerance
* </pre>
*
* <p>
* When defining a full blown matcher, one shorthand is to use one of the factory methods in <code>Matcher</code>'s companion
* object. For example, instead of writing this:
* </p>
*
* <pre class="stHighlighted">
* <span class="stReserved">val</span> beOdd =
* <span class="stReserved">new</span> <span class="stType">Matcher[Int]</span> {
* <span class="stReserved">def</span> apply(left: <span class="stType">Int</span>) =
* <span class="stType">MatchResult</span>(
* left % <span class="stLiteral">2</span> == <span class="stLiteral">1</span>,
* left + <span class="stQuotedString">" was not odd"</span>,
* left + <span class="stQuotedString">" was odd"</span>
* )
* }
* </pre>
*
* <p>
* You could alternately write this:
* </p>
*
* <pre class="stHighlighted">
* <span class="stReserved">val</span> beOdd =
* <span class="stType">Matcher</span> { (left: <span class="stType">Int</span>) =>
* <span class="stType">MatchResult</span>(
* left % <span class="stLiteral">2</span> == <span class="stLiteral">1</span>,
* left + <span class="stQuotedString">" was not odd"</span>,
* left + <span class="stQuotedString">" was odd"</span>
* )
* }
* </pre>
*
* <p>
* Either way you define the <code>beOdd</code> matcher, you could use it like this:
* </p>
*
* <pre class="stHighlighted">
* <span class="stLiteral">3</span> should beOdd
* <span class="stLiteral">4</span> should not (beOdd)
* </pre>
*
* <a name="composingMatchers"></a>
* <h2>Composing matchers</h2>
*
* <p>
* You can also compose matchers. For example, the <code>endWithExtension</code> matcher from the example above
* can be more easily created by composing a function with the existing <code>endWith</code> matcher:
* </p>
*
* <pre class="stREPL">
* scala> import org.scalatest._
* import org.scalatest._
*
* scala> import Matchers._
* import Matchers._
*
* scala> import java.io.File
* import java.io.File
*
* scala> def endWithExtension(ext: String) = endWith(ext) compose { (f: File) => f.getPath }
* endWithExtension: (ext: String)org.scalatest.matchers.Matcher[java.io.File]
* </pre>
*
* <p>
* Now you have a <code>Matcher[File]</code> whose <code>apply</code> method first
* invokes the converter function to convert the passed <code>File</code> to a <code>String</code>,
* then passes the resulting <code>String</code> to <code>endWith</code>. Thus, you could use this version
* <code>endWithExtension</code> like the previous one:
* </p>
*
* <pre class="stREPL">
* scala> new File("output.txt") should endWithExtension("txt")
* </pre>
*
* <p>
* In addition, by composing twice, you can modify the type of both sides of a match statement
* with the same function, like this:
* </p>
*
* <pre class="stREPL">
* scala> val f = be > (_: Int)
* f: Int => org.scalatest.matchers.Matcher[Int] = <function1>
*
* scala> val g = (_: String).toInt
* g: String => Int = <function1>
*
* scala> val beAsIntsGreaterThan = (f compose g) andThen (_ compose g)
* beAsIntsGreaterThan: String => org.scalatest.matchers.Matcher[String] = <function1>
*
* scala> "8" should beAsIntsGreaterThan ("7")
* </pre>
*
* <p>
* At thsi point, however, the error message for the <code>beAsIntsGreaterThan</code>
* gives no hint that the <code>Int</code>s being compared were parsed from <code>String</code>s:
* </p>
*
* <pre class="stREPL">
* scala> "7" should beAsIntsGreaterThan ("8")
* org.scalatest.exceptions.TestFailedException: 7 was not greater than 8
* </pre>
*
* <p>
* To modify error message, you can use trait <a href="MatcherProducers.html"><code>MatcherProducers</code></a>, which
* also provides a <code>composeTwice</code> method that performs the <code>compose</code> ...
* <code>andThen</code> ... <code>compose</code> operation:
* </p>
*
* <pre class="stREPL">
* scala> import matchers._
* import matchers._
*
* scala> import MatcherProducers._
* import MatcherProducers._
*
* scala> val beAsIntsGreaterThan = f composeTwice g // means: (f compose g) andThen (_ compose g)
* beAsIntsGreaterThan: String => org.scalatest.matchers.Matcher[String] = <function1>
*
* scala> "8" should beAsIntsGreaterThan ("7")
* </pre>
*
* <p>
* Of course, the error messages is still the same:
* </p>
*
* <pre class="stREPL">
* scala> "7" should beAsIntsGreaterThan ("8")
* org.scalatest.exceptions.TestFailedException: 7 was not greater than 8
* </pre>
*
* <p>
* To modify the error messages, you can use <code>mapResult</code> from <code>MatcherProducers</code>. Here's an example:
* </p>
*
* <pre class="stREPL">
* scala> val beAsIntsGreaterThan =
* f composeTwice g mapResult { mr =>
* mr.copy(
* failureMessageArgs =
* mr.failureMessageArgs.map((LazyArg(_) { "\"" + _.toString + "\".toInt"})),
* negatedFailureMessageArgs =
* mr.negatedFailureMessageArgs.map((LazyArg(_) { "\"" + _.toString + "\".toInt"})),
* midSentenceFailureMessageArgs =
* mr.midSentenceFailureMessageArgs.map((LazyArg(_) { "\"" + _.toString + "\".toInt"})),
* midSentenceNegatedFailureMessageArgs =
* mr.midSentenceNegatedFailureMessageArgs.map((LazyArg(_) { "\"" + _.toString + "\".toInt"}))
* )
* }
* beAsIntsGreaterThan: String => org.scalatest.matchers.Matcher[String] = <function1>
* </pre>
*
* <p>
* The <code>mapResult</code> method takes a function that accepts a <code>MatchResult</code> and produces a new
* <code>MatchResult</code>, which can contain modified arguments and modified error messages. In this example,
* the error messages are being modified by wrapping the old arguments in <a href="LazyArg.html"><code>LazyArg</code></a>
* instances that lazily apply the given prettification functions to the <code>toString</code> result of the old args.
* Now the error message is clearer:
* </p>
*
* <pre class="stREPL">
* scala> "7" should beAsIntsGreaterThan ("8")
* org.scalatest.exceptions.TestFailedException: "7".toInt was not greater than "8".toInt
* </pre>
*
* <h2>Matcher's variance</h2>
*
* <p>
* <code>Matcher</code> is contravariant in its type parameter, <code>T</code>, to make its use more flexible.
* As an example, consider the hierarchy:
* </p>
*
* <pre class="stHighlighted">
* <span class="stReserved">class</span> <span class="stType">Fruit</span>
* <span class="stReserved">class</span> <span class="stType">Orange</span> <span class="stReserved">extends</span> <span class="stType">Fruit</span>
* <span class="stReserved">class</span> <span class="stType">ValenciaOrange</span> <span class="stReserved">extends</span> <span class="stType">Orange</span>
* </pre>
*
* <p>
* Given an orange:
* </p>
*
* <pre class="stHighlighted">
* <span class="stReserved">val</span> orange = <span class="stType">Orange</span>
* </pre>
*
* <p>
* The expression "<code>orange should</code>" will, via an implicit conversion in <code>Matchers</code>,
* result in an object that has a <code>should</code>
* method that takes a <code>Matcher[Orange]</code>. If the static type of the matcher being passed to <code>should</code> is
* <code>Matcher[Valencia]</code> it shouldn't (and won't) compile. The reason it shouldn't compile is that
* the left value is an <code>Orange</code>, but not necessarily a <code>Valencia</code>, and a
* <code>Matcher[Valencia]</code> only knows how to match against a <code>Valencia</code>. The reason
* it won't compile is given that <code>Matcher</code> is contravariant in its type parameter, <code>T</code>, a
* <code>Matcher[Valencia]</code> is <em>not</em> a subtype of <code>Matcher[Orange]</code>.
* </p>
*
* <p>
* By contrast, if the static type of the matcher being passed to <code>should</code> is <code>Matcher[Fruit]</code>,
* it should (and will) compile. The reason it <em>should</em> compile is that given the left value is an <code>Orange</code>,
* it is also a <code>Fruit</code>, and a <code>Matcher[Fruit]</code> knows how to match against <code>Fruit</code>s.
* The reason it <em>will</em> compile is that given that <code>Matcher</code> is contravariant in its type parameter, <code>T</code>, a
* <code>Matcher[Fruit]</code> is indeed a subtype of <code>Matcher[Orange]</code>.
* </p>
*
* @author Bill Venners
*/
trait Matcher[-T] extends Function1[T, MatchResult] { outerInstance =>
/**
* Check to see if the specified object, <code>left</code>, matches, and report the result in
* the returned <code>MatchResult</code>. The parameter is named <code>left</code>, because it is
* usually the value to the left of a <code>should</code> or <code>must</code> invocation. For example,
* in:
*
* <pre class="stHighlighted">
* list should equal (<span class="stType">List</span>(<span class="stLiteral">1</span>, <span class="stLiteral">2</span>, <span class="stLiteral">3</span>))
* </pre>
*
* The <code>equal (List(1, 2, 3))</code> expression results in a matcher that holds a reference to the
* right value, <code>List(1, 2, 3)</code>. The <code>should</code> method invokes <code>apply</code>
* on this matcher, passing in <code>list</code>, which is therefore the "<code>left</code>" value. The
* matcher will compare the <code>list</code> (the <code>left</code> value) with <code>List(1, 2, 3)</code> (the right
* value), and report the result in the returned <code>MatchResult</code>.
*
* @param left the value against which to match
* @return the <code>MatchResult</code> that represents the result of the match
*/
def apply(left: T): MatchResult
/**
* Compose this matcher with the passed function, returning a new matcher.
*
* <p>
* This method overrides <code>compose</code> on <code>Function1</code> to
* return a more specific function type of <code>Matcher</code>. For example, given
* a <code>beOdd</code> matcher defined like this:
* </p>
*
* <pre class="stHighlighted">
* <span class="stReserved">val</span> beOdd =
* <span class="stReserved">new</span> <span class="stType">Matcher[Int]</span> {
* <span class="stReserved">def</span> apply(left: <span class="stType">Int</span>) =
* <span class="stType">MatchResult</span>(
* left % <span class="stLiteral">2</span> == <span class="stLiteral">1</span>,
* left + <span class="stQuotedString">" was not odd"</span>,
* left + <span class="stQuotedString">" was odd"</span>
* )
* }
* </pre>
*
* <p>
* You could use <code>beOdd</code> like this:
* </p>
*
* <pre class="stHighlighted">
* <span class="stLiteral">3</span> should beOdd
* <span class="stLiteral">4</span> should not (beOdd)
* </pre>
*
* <p>
* If for some odd reason, you wanted a <code>Matcher[String]</code> that
* checked whether a string, when converted to an <code>Int</code>,
* was odd, you could make one by composing <code>beOdd</code> with
* a function that converts a string to an <code>Int</code>, like this:
* </p>
*
* <pre class="stHighlighted">
* <span class="stReserved">val</span> beOddAsInt = beOdd compose { (s: <span class="stType">String</span>) => s.toInt }
* </pre>
*
* <p>
* Now you have a <code>Matcher[String]</code> whose <code>apply</code> method first
* invokes the converter function to convert the passed string to an <code>Int</code>,
* then passes the resulting <code>Int</code> to <code>beOdd</code>. Thus, you could use
* <code>beOddAsInt</code> like this:
* </p>
*
* <pre class="stHighlighted">
* <span class="stQuotedString">"3"</span> should beOddAsInt
* <span class="stQuotedString">"4"</span> should not (beOddAsInt)
* </pre>
*/
override def compose[U](g: U => T): Matcher[U] =
new Matcher[U] {
def apply(u: U) = outerInstance.apply(g(u))
}
// TODO: mention not short circuited, and the precendence is even between and and or
/**
* Returns a matcher whose <code>apply</code> method returns a <code>MatchResult</code>
* that represents the logical-and of the results of the wrapped and the passed matcher applied to
* the same value.
*
* <p>
* The reason <code>and</code> has an upper bound on its type parameter is so that the <code>Matcher</code>
* resulting from an invocation of <code>and</code> will have the correct type parameter. If you call
* <code>and</code> on a <code>Matcher[Orange]</code>, passing in a <code>Matcher[Valencia]</code>,
* the result will have type <code>Matcher[Valencia]</code>. This is correct because both a
* <code>Matcher[Orange]</code> and a <code>Matcher[Valencia]</code> know how to match a
* <code>Valencia</code> (but a <code>Matcher[Valencia]</code> doesn't know how to
* match any old <code>Orange</code>). If you call
* <code>and</code> on a <code>Matcher[Orange]</code>, passing in a <code>Matcher[Fruit]</code>,
* the result will have type <code>Matcher[Orange]</code>. This is also correct because both a
* <code>Matcher[Orange]</code> and a <code>Matcher[Fruit]</code> know how to match an
* <code>Orange</code> (but a <code>Matcher[Orange]</code> doesn't know how to
* match any old <code>Fruit</code>).
* </p>
*
* @param the matcher to logical-and with this matcher
* @return a matcher that performs the logical-and of this and the passed matcher
*/
def and[U <: T](rightMatcher: Matcher[U]): Matcher[U] =
new Matcher[U] {
def apply(left: U): MatchResult = {
andMatchersAndApply(left, outerInstance, rightMatcher)
}
override def toString: String = "(" + Prettifier.default(outerInstance) + ") and (" + Prettifier.default(rightMatcher) + ")"
}
import scala.language.higherKinds
/**
* Returns a <code>MatcherFactory</code> whose <code>matcher</code> method returns a <code>Matcher</code>,
* which has <code>apply</code> method that returns a <code>MatchResult</code> that represents the logical-and
* of the results of the wrapped and the passed <code>MatcherFactory</code> applied to the same value.
*
* @param rightMatcherFactory1 the <code>MatcherFactory</code> to logical-and with this <code>MatcherFactory</code>
* @return a <code>MatcherFactory</code> that performs the logical-and of this and the passed <code>MatcherFactory</code>
*/
def and[U, TC1[_]](rightMatcherFactory1: MatcherFactory1[U, TC1]): MatcherFactory1[T with U, TC1] =
new MatcherFactory1[T with U, TC1] {
def matcher[V <: T with U : TC1]: Matcher[V] = {
new Matcher[V] {
def apply(left: V): MatchResult = {
val rightMatcher = rightMatcherFactory1.matcher
andMatchersAndApply(left, outerInstance, rightMatcher)
}
}
}
override def toString: String = "(" + Prettifier.default(outerInstance) + ") and (" + Prettifier.default(rightMatcherFactory1) + ")"
}
/**
* Returns a matcher whose <code>apply</code> method returns a <code>MatchResult</code>
* that represents the logical-or of the results of this and the passed matcher applied to
* the same value.
*
* <p>
* The reason <code>or</code> has an upper bound on its type parameter is so that the <code>Matcher</code>
* resulting from an invocation of <code>or</code> will have the correct type parameter. If you call
* <code>or</code> on a <code>Matcher[Orange]</code>, passing in a <code>Matcher[Valencia]</code>,
* the result will have type <code>Matcher[Valencia]</code>. This is correct because both a
* <code>Matcher[Orange]</code> and a <code>Matcher[Valencia]</code> know how to match a
* <code>Valencia</code> (but a <code>Matcher[Valencia]</code> doesn't know how to
* match any old <code>Orange</code>). If you call
* <code>or</code> on a <code>Matcher[Orange]</code>, passing in a <code>Matcher[Fruit]</code>,
* the result will have type <code>Matcher[Orange]</code>. This is also correct because both a
* <code>Matcher[Orange]</code> and a <code>Matcher[Fruit]</code> know how to match an
* <code>Orange</code> (but a <code>Matcher[Orange]</code> doesn't know how to
* match any old <code>Fruit</code>).
* </p>
*
* @param rightMatcher the matcher to logical-or with this matcher
* @return a matcher that performs the logical-or of this and the passed matcher
*/
def or[U <: T](rightMatcher: Matcher[U]): Matcher[U] =
new Matcher[U] {
def apply(left: U): MatchResult = {
orMatchersAndApply(left, outerInstance, rightMatcher)
}
override def toString: String = "(" + Prettifier.default(outerInstance) + ") or (" + Prettifier.default(rightMatcher) + ")"
}
/**
* Returns a <code>MatcherFactory</code> whose <code>matcher</code> method returns a <code>Matcher</code>,
* which has <code>apply</code> method that returns a <code>MatchResult</code> that represents the logical-or
* of the results of the wrapped and the passed <code>MatcherFactory</code> applied to the same value.
*
* @param rightMatcherFactory1 the <code>MatcherFactory</code> to logical-or with this <code>MatcherFactory</code>
* @return a <code>MatcherFactory</code> that performs the logical-or of this and the passed <code>MatcherFactory</code>
*/
def or[U, TC1[_]](rightMatcherFactory1: MatcherFactory1[U, TC1]): MatcherFactory1[T with U, TC1] =
new MatcherFactory1[T with U, TC1] {
def matcher[V <: T with U : TC1]: Matcher[V] = {
new Matcher[V] {
def apply(left: V): MatchResult = {
val rightMatcher = rightMatcherFactory1.matcher
orMatchersAndApply(left, outerInstance, rightMatcher)
}
override def toString: String = "(" + Prettifier.default(outerInstance) + ") or (" + Prettifier.default(rightMatcherFactory1) + ")"
}
}
override def toString: String = "(" + Prettifier.default(outerInstance) + ") or (" + Prettifier.default(rightMatcherFactory1) + ")"
}
/**
* This class is part of the ScalaTest matchers DSL. Please see the documentation for <a href="../Matchers.html"><code>Matchers</code></a> for an overview of
* the matchers DSL.
*
* @author Bill Venners
*/
final class AndHaveWord {
/**
* This method enables the following syntax:
*
* <pre class="stHighlighted">
* aMatcher and have length (<span class="stLiteral">3</span> - <span class="stLiteral">1</span>)
* ^
* </pre>
**/
def length(expectedLength: Long): MatcherFactory1[T, Length] = and(MatcherWords.have.length(expectedLength))
/**
* This method enables the following syntax:
*
* <pre class="stHighlighted">
* aMatcher and have size (<span class="stLiteral">3</span> - <span class="stLiteral">1</span>)
* ^
* </pre>
**/
def size(expectedSize: Long): MatcherFactory1[T, Size] = and(MatcherWords.have.size(expectedSize))
/**
* This method enables the following syntax:
*
* <pre class="stHighlighted">
* aMatcher and have message (<span class="stQuotedString">"A message from Mars"</span>)
* ^
* </pre>
**/
def message(expectedMessage: String): MatcherFactory1[T, Messaging] = and(MatcherWords.have.message(expectedMessage))
}
/**
* This method enables the following syntax:
*
* <pre class="stHighlighted">
* aMatcher and have size (<span class="stLiteral">3</span> - <span class="stLiteral">1</span>)
* ^
* </pre>
**/
def and(haveWord: HaveWord): AndHaveWord = new AndHaveWord
/**
* This class is part of the ScalaTest matchers DSL. Please see the documentation for <a href="../Matchers.html"><code>Matchers</code></a> for an overview of
* the matchers DSL.
*
* @author Bill Venners
*/
final class AndContainWord(prettifier: Prettifier, pos: source.Position) {
/**
* This method enables the following syntax:
*
* <pre class="stHighlighted">
* aMatcher and contain (<span class="stLiteral">3</span> - <span class="stLiteral">1</span>)
* ^
* </pre>
**/
def apply[U](expectedElement: Any): MatcherFactory1[T with U, Containing] = outerInstance.and(MatcherWords.contain(expectedElement))
/**
* This method enables the following syntax:
*
* <pre class="stHighlighted">
* aMatcher and contain key (<span class="stQuotedString">"one"</span>)
* ^
* </pre>
**/
def key(expectedKey: Any): MatcherFactory1[T, KeyMapping] = outerInstance.and(MatcherWords.contain.key(expectedKey))
/**
* This method enables the following syntax:
*
* <pre class="stHighlighted">
* aMatcher and contain value (<span class="stLiteral">1</span>)
* ^
* </pre>
**/
def value(expectedValue: Any): MatcherFactory1[T, ValueMapping] = outerInstance.and(MatcherWords.contain.value(expectedValue))
/**
* This method enables the following syntax:
*
* <pre class="stHighlighted">
* aMatcher and contain theSameElementsAs <span class="stType">List</span>(<span class="stLiteral">1</span>, <span class="stLiteral">2</span>, <span class="stLiteral">3</span>)
* ^
* </pre>
**/
def theSameElementsAs(right: GenTraversable[_]): MatcherFactory1[T, Aggregating] =
outerInstance.and(MatcherWords.contain.theSameElementsAs(right))
/**
* This method enables the following syntax:
*
* <pre class="stHighlighted">
* aMatcher and contain theSameElementsInOrderAs <span class="stType">List</span>(<span class="stLiteral">1</span>, <span class="stLiteral">2</span>, <span class="stLiteral">3</span>)
* ^
* </pre>
**/
def theSameElementsInOrderAs(right: GenTraversable[_]): MatcherFactory1[T, Sequencing] =
outerInstance.and(MatcherWords.contain.theSameElementsInOrderAs(right))
/**
* This method enables the following syntax:
*
* <pre class="stHighlighted">
* aMatcher and contain inOrderOnly (<span class="stLiteral">1</span>, <span class="stLiteral">2</span>, <span class="stLiteral">3</span>)
* ^
* </pre>
**/
def inOrderOnly(firstEle: Any, secondEle: Any, remainingEles: Any*): MatcherFactory1[T, Sequencing] =
outerInstance.and(MatcherWords.contain.inOrderOnly(firstEle, secondEle, remainingEles.toList: _*)(prettifier, pos))
/**
* This method enables the following syntax:
*
* <pre class="stHighlighted">
* aMatcher and contain allOf (<span class="stLiteral">1</span>, <span class="stLiteral">2</span>, <span class="stLiteral">3</span>)
* ^
* </pre>
**/
def allOf(firstEle: Any, secondEle: Any, remainingEles: Any*): MatcherFactory1[T, Aggregating] =
outerInstance.and(MatcherWords.contain.allOf(firstEle, secondEle, remainingEles.toList: _*)(prettifier, pos))
/**
* This method enables the following syntax:
*
* <pre class="stHighlighted">
* aMatcher and contain allElementsOf <span class="stType">List</span>(<span class="stLiteral">1</span>, <span class="stLiteral">2</span>, <span class="stLiteral">3</span>)
* ^
* </pre>
**/
def allElementsOf(elements: GenTraversable[Any]): MatcherFactory1[T, Aggregating] =
outerInstance.and(MatcherWords.contain.allElementsOf(elements))
/**
* This method enables the following syntax:
*
* <pre class="stHighlighted">
* aMatcher and contain inOrder (<span class="stLiteral">1</span>, <span class="stLiteral">2</span>, <span class="stLiteral">3</span>)
* ^
* </pre>
**/
def inOrder(firstEle: Any, secondEle: Any, remainingEles: Any*): MatcherFactory1[T, Sequencing] =
outerInstance.and(MatcherWords.contain.inOrder(firstEle, secondEle, remainingEles.toList: _*)(prettifier, pos))
/**
* This method enables the following syntax:
*
* <pre class="stHighlighted">
* aMatcher and contain inOrderElementsOf <span class="stType">List</span>(<span class="stLiteral">1</span>, <span class="stLiteral">2</span>, <span class="stLiteral">3</span>)
* ^
* </pre>
**/
def inOrderElementsOf(elements: GenTraversable[Any]): MatcherFactory1[T, Sequencing] =
outerInstance.and(MatcherWords.contain.inOrderElementsOf(elements))
/**
* This method enables the following syntax:
*
* <pre class="stHighlighted">
* aMatcher and contain oneOf (<span class="stLiteral">1</span>, <span class="stLiteral">2</span>, <span class="stLiteral">3</span>)
* ^
* </pre>
**/
def oneOf(firstEle: Any, secondEle: Any, remainingEles: Any*): MatcherFactory1[T, Containing] =
outerInstance.and(MatcherWords.contain.oneOf(firstEle, secondEle, remainingEles.toList: _*)(prettifier, pos))
/**
* This method enables the following syntax:
*
* <pre class="stHighlighted">
* aMatcher and contain oneElementOf <span class="stType">List</span>(<span class="stLiteral">1</span>, <span class="stLiteral">2</span>, <span class="stLiteral">3</span>)
* ^
* </pre>
**/
def oneElementOf(elements: GenTraversable[Any]): MatcherFactory1[T, Containing] =
outerInstance.and(MatcherWords.contain.oneElementOf(elements))
/**
* This method enables the following syntax:
*
* <pre class="stHighlighted">
* aMatcher and contain atLeastOneOf (<span class="stLiteral">1</span>, <span class="stLiteral">2</span>, <span class="stLiteral">3</span>)
* ^
* </pre>
**/
def atLeastOneOf(firstEle: Any, secondEle: Any, remainingEles: Any*): MatcherFactory1[T, Aggregating] =
outerInstance.and(MatcherWords.contain.atLeastOneOf(firstEle, secondEle, remainingEles.toList: _*)(prettifier, pos))
/**
* This method enables the following syntax:
*
* <pre class="stHighlighted">
* aMatcher and contain atLeastOneElementOf (<span class="stLiteral">1</span>, <span class="stLiteral">2</span>, <span class="stLiteral">3</span>)
* ^
* </pre>
**/
def atLeastOneElementOf(elements: GenTraversable[Any]): MatcherFactory1[T, Aggregating] =
outerInstance.and(MatcherWords.contain.atLeastOneElementOf(elements))
/**
* This method enables the following syntax:
*
* <pre class="stHighlighted">
* aMatcher and contain only (<span class="stLiteral">1</span>, <span class="stLiteral">2</span>, <span class="stLiteral">3</span>)
* ^
* </pre>
**/
def only(right: Any*): MatcherFactory1[T, Aggregating] =
outerInstance.and(MatcherWords.contain.only(right.toList: _*)(prettifier, pos))
/**
* This method enables the following syntax:
*
* <pre class="stHighlighted">
* aMatcher and contain noneOf (<span class="stLiteral">1</span>, <span class="stLiteral">2</span>, <span class="stLiteral">3</span>)
* ^
* </pre>
**/
def noneOf(firstEle: Any, secondEle: Any, remainingEles: Any*): MatcherFactory1[T, Containing] =
outerInstance.and(MatcherWords.contain.noneOf(firstEle, secondEle, remainingEles.toList: _*)(prettifier, pos))
/**
* This method enables the following syntax:
*
* <pre class="stHighlighted">
* aMatcher and contain noElementsOf (<span class="stLiteral">1</span>, <span class="stLiteral">2</span>, <span class="stLiteral">3</span>)
* ^
* </pre>
**/
def noElementsOf(elements: GenTraversable[Any]): MatcherFactory1[T, Containing] =
outerInstance.and(MatcherWords.contain.noElementsOf(elements))
/**
* This method enables the following syntax:
*
* <pre class="stHighlighted">
* aMatcher and contain atMostOneOf (<span class="stLiteral">1</span>, <span class="stLiteral">2</span>, <span class="stLiteral">3</span>)
* ^
* </pre>
**/
def atMostOneOf(firstEle: Any, secondEle: Any, remainingEles: Any*): MatcherFactory1[T, Aggregating] =
outerInstance.and(MatcherWords.contain.atMostOneOf(firstEle, secondEle, remainingEles.toList: _*)(prettifier, pos))
/**
* This method enables the following syntax:
*
* <pre class="stHighlighted">
* aMatcher and contain atMostOneElementOf <span class="stType">List</span>(<span class="stLiteral">1</span>, <span class="stLiteral">2</span>, <span class="stLiteral">3</span>)
* ^
* </pre>
**/
def atMostOneElementOf(elements: GenTraversable[Any]): MatcherFactory1[T, Aggregating] =
outerInstance.and(MatcherWords.contain.atMostOneElementOf(elements))
}
/**
* This method enables the following syntax:
*
* <pre class="stHighlighted">
* aMatcher and contain key (<span class="stQuotedString">"one"</span>)
* ^
* </pre>
**/
def and(containWord: ContainWord)(implicit prettifier: Prettifier, pos: source.Position): AndContainWord = new AndContainWord(prettifier, pos)
/**
* This class is part of the ScalaTest matchers DSL. Please see the documentation for <a href="../Matchers.html"><code>Matchers</code></a> for an overview of
* the matchers DSL.
*
* @author Bill Venners
*/
final class AndBeWord {
// SKIP-SCALATESTJS,NATIVE-START
/**
* This method enables the following syntax:
*
* <pre class="stHighlighted">
* aMatcher and be a (<span class="stQuotedString">'file</span>)
* ^
* </pre>
**/
def a(symbol: Symbol): Matcher[T with AnyRef] = and(MatcherWords.be.a(symbol))
// SKIP-SCALATESTJS,NATIVE-END
/**
* This method enables the following syntax, where <code>file</code> is a <a href="BePropertyMatcher.html"><code>BePropertyMatcher</code></a>:
*
* <pre class="stHighlighted">
* aMatcher and be a (file)
* ^
* </pre>
**/
def a[U](bePropertyMatcher: BePropertyMatcher[U]): Matcher[T with AnyRef with U] = and(MatcherWords.be.a(bePropertyMatcher))
/**
* This method enables the following syntax, where <code>positiveNumber</code> and <code>validNumber</code> are <a href="AMatcher.html"><code>AMatcher</code></a>:
*
* <pre class="stHighlighted">
* aMatcher and be a (validNumber)
* ^
* </pre>
**/
def a[U](aMatcher: AMatcher[U]): Matcher[T with U] = and(MatcherWords.be.a(aMatcher))
// SKIP-SCALATESTJS,NATIVE-START
/**
* This method enables the following syntax:
*
* <pre class="stHighlighted">
* aMatcher and be an (<span class="stQuotedString">'apple</span>)
* ^
* </pre>
**/
def an(symbol: Symbol): Matcher[T with AnyRef] = and(MatcherWords.be.an(symbol))
// SKIP-SCALATESTJS,NATIVE-END
/**
* This method enables the following syntax, where <code>apple</code> is a <a href="BePropertyMatcher.html"><code>BePropertyMatcher</code></a>:
*
* <pre class="stHighlighted">
* aMatcher and be an (apple)
* ^
* </pre>
**/
def an[U](bePropertyMatcher: BePropertyMatcher[U]): Matcher[T with AnyRef with U] = and(MatcherWords.be.an(bePropertyMatcher))
/**
* This method enables the following syntax, where <code>integerNumber</code> is an <a href="AnMatcher.html"><code>AnMatcher</code></a>:
*
* <pre class="stHighlighted">
* aMatcher and be an (integerNumber)
* ^
* </pre>
**/
def an[U](anMatcher: AnMatcher[U]): Matcher[T with U] = and(MatcherWords.be.an(anMatcher))
/**
* This method enables the following syntax:
*
* <pre class="stHighlighted">
* aMatcher and be theSameInstanceAs (string)
* ^
* </pre>
**/
def theSameInstanceAs(anyRef: AnyRef): Matcher[T with AnyRef] = and(MatcherWords.be.theSameInstanceAs(anyRef))
/**
* This method enables the following syntax, where <code>fraction</code> refers to a <code>PartialFunction</code>:
*
* <pre class="stHighlighted">
* aMatcher and be definedAt (<span class="stLiteral">8</span>)
* ^
* </pre>
**/
def definedAt[A, U <: PartialFunction[A, _]](right: A): Matcher[T with U] = and(MatcherWords.be.definedAt(right))
}
/**
* This method enables the following syntax:
*
* <pre class="stHighlighted">
* aMatcher and be a (<span class="stQuotedString">'file</span>)
* ^
* </pre>
**/
def and(beWord: BeWord): AndBeWord = new AndBeWord
/**
* This class is part of the ScalaTest matchers DSL. Please see the documentation for <a href="../Matchers.html"><code>Matchers</code></a> for an overview of
* the matchers DSL.
*
* @author Bill Venners
*/
final class AndFullyMatchWord {
/**
* This method enables the following syntax:
*
* <pre class="stHighlighted">
* aMatcher and fullyMatch regex (decimal)
* ^
* </pre>
**/
def regex(regexString: String): Matcher[T with String] = and(MatcherWords.fullyMatch.regex(regexString))
/**
* This method enables the following syntax:
*
* <pre class="stHighlighted">
* aMatcher and fullyMatch regex (<span class="stQuotedString">"a(b*)c"</span> withGroup <span class="stQuotedString">"bb"</span>)
* ^
* </pre>
**/
def regex(regexWithGroups: RegexWithGroups): Matcher[T with String] = and(MatcherWords.fullyMatch.regex(regexWithGroups))