/
AnyFlatSpec.scala
1545 lines (1541 loc) · 90.7 KB
/
AnyFlatSpec.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.flatspec
import org.scalatest.{Finders, Suite}
/**
* Facilitates a “behavior-driven” style of development (BDD), in which tests
* are combined with text that specifies the behavior the tests verify.
* </p>
*
* <table><tr><td class="usage">
* <strong>Recommended Usage</strong>:
* Class <code>AnyFlatSpec</code> is a good first step for teams wishing to move from xUnit to BDD, because its structure is flat like xUnit, so simple and familiar,
* but the test names must be written in a specification style: “X should Y,” “A must B,” <em>etc. </em>
* </td></tr></table>
*
* <p>
* Trait <code>AnyFlatSpec</code> is so named because
* your specification text and tests line up flat against the left-side indentation level, with no nesting needed.
* Here's an example <code>AnyFlatSpec</code>:
* </p>
*
* <pre class="stHighlighted">
* <span class="stReserved">package</span> org.scalatest.examples.flatspec
* <br/><span class="stReserved">import</span> org.scalatest.flatspec.AnyFlatSpec
* <br/><span class="stReserved">class</span> <span class="stType">SetSpec</span> <span class="stReserved">extends</span> <span class="stType">AnyFlatSpec</span> {
* <br/> behavior of <span class="stQuotedString">"An empty Set"</span>
* <br/> it should <span class="stQuotedString">"have size 0"</span> in {
* assert(Set.empty.size === <span class="stLiteral">0</span>)
* }
* <br/> it should <span class="stQuotedString">"produce NoSuchElementException when head is invoked"</span> in {
* assertThrows[<span class="stType">NoSuchElementException</span>] {
* Set.empty.head
* }
* }
* }
* </pre>
*
* <p>
* Note: you can use <code>must</code> or <code>can</code> as well as <code>should</code> in a <code>AnyFlatSpec</code>. For example, instead of
* <code>it should "have</code>..., you could write <code>it must "have</code>... or <code>it can "have</code>....
* </p>
*
* <p>
* Instead of using a <code>behavior of</code> clause, you can alternatively use a shorthand syntax in which you replace
* the first <code>it</code> with the subject string, like this:
* </p>
*
* <pre class="stHighlighted">
* <span class="stReserved">package</span> org.scalatest.examples.flatspec
* <br/><span class="stReserved">import</span> org.scalatest.flatspec.AnyFlatSpec
* <br/><span class="stReserved">class</span> <span class="stType">SetSpec</span> <span class="stReserved">extends</span> <span class="stType">AnyFlatSpec</span> {
* <br/> <span class="stQuotedString">"An empty Set"</span> should <span class="stQuotedString">"have size 0"</span> in {
* assert(Set.empty.size === <span class="stLiteral">0</span>)
* }
* <br/> it should <span class="stQuotedString">"produce NoSuchElementException when head is invoked"</span> in {
* assertThrows[<span class="stType">NoSuchElementException</span>] {
* Set.empty.head
* }
* }
* }
* </pre>
*
* <p>
* Running either of the two previous versions of <code>SetSpec</code> in the Scala interpreter would yield:
* </p>
*
* <pre class="stREPL">
* <span class="stGreen">An empty Set
* - should have size 0
* - should produce NoSuchElementException when head is invoked</span>
* </pre>
*
* <p>
* In a <code>AnyFlatSpec</code> you write a one (or more) sentence specification for each bit of behavior you wish to
* specify and test. Each specification sentence has a
* "subject," which is sometimes called the <em>system under test</em> (or SUT). The
* subject is the entity being specified and tested and also serves as the subject of the sentences you write for each test.
* Often you will want to write multiple tests for the same subject. In a <code>AnyFlatSpec</code>, you name the subject once,
* with a <code>behavior of</code> clause or its shorthand, then write tests for that subject with <code>it should</code>/<code>must</code>/<code>can "do something"</code> phrases.
* Each <code>it</code> refers to the most recently declared subject. For example, the four tests shown in this snippet are all testing
* a stack that contains one item:
* </p>
*
* <pre class="stHighlighted">
* behavior of <span class="stQuotedString">"A Stack (with one item)"</span>
* <br/>it should <span class="stQuotedString">"be non-empty"</span> in {}
* <br/>it should <span class="stQuotedString">"return the top item on peek"</span> in {}
* <br/>it should <span class="stQuotedString">"not remove the top item on peek"</span> in {}
* <br/>it should <span class="stQuotedString">"remove the top item on pop"</span> in {}
* </pre>
*
* <p>
* The same is true if the tests are written using the shorthand notation:
* </p>
*
* <pre class="stHighlighted">
* <span class="stQuotedString">"A Stack (with one item)"</span> should <span class="stQuotedString">"be non-empty"</span> in {}
* <br/>it should <span class="stQuotedString">"return the top item on peek"</span> in {}
* <br/>it should <span class="stQuotedString">"not remove the top item on peek"</span> in {}
* <br/>it should <span class="stQuotedString">"remove the top item on pop"</span> in {}
* </pre>
*
* <p>
* In a <code>AnyFlatSpec</code>, therefore, to figure out what "<code>it</code>" means, you just scan vertically until you find the most
* recent use of <code>behavior of</code> or the shorthand notation.
* </p>
*
* <p>
* Because sometimes the subject could be plural, you can alternatively use <code>they</code> instead of <code>it</code>:
* </p>
*
* <pre class="stHighlighted">
* <span class="stQuotedString">"The combinators"</span> should <span class="stQuotedString">"be easy to learn"</span> in {}
* <br/>they should <span class="stQuotedString">"be efficient"</span> in {}
* <br/>they should <span class="stQuotedString">"do something cool"</span> in {}
* </pre>
*
* <p>
* A <code>AnyFlatSpec</code>'s lifecycle has two phases: the <em>registration</em> phase and the
* <em>ready</em> phase. It starts in registration phase and enters ready phase the first time
* <code>run</code> is called on it. It then remains in ready phase for the remainder of its lifetime.
* </p>
*
* <p>
* Tests can only be registered while the <code>AnyFlatSpec</code> is
* in its registration phase. Any attempt to register a test after the <code>AnyFlatSpec</code> has
* entered its ready phase, <em>i.e.</em>, after <code>run</code> has been invoked on the <code>AnyFlatSpec</code>,
* will be met with a thrown <a href="../exceptions/TestRegistrationClosedException.html"><code>TestRegistrationClosedException</code></a>. The recommended style
* of using <code>AnyFlatSpec</code> is to register tests during object construction as is done in all
* the examples shown here. If you keep to the recommended style, you should never see a
* <code>TestRegistrationClosedException</code>.
* </p>
*
* <a name="ignoredTests"></a><h2>Ignored tests</h2>
*
* To support the common use case of temporarily disabling a test, with the
* good intention of resurrecting the test at a later time, <code>AnyFlatSpec</code> provides a method
* <code>ignore</code> that can be used instead of <code>it</code> or <code>they</code> to register a test. For example, to temporarily
* disable the test with the name <code>"An empty Set should produce NoSuchElementException when head is invoked"</code>, just
* change “<code>it</code>” into “<code>ignore</code>,” like this:
* </p>
*
* <pre class="stHighlighted">
* <span class="stReserved">package</span> org.scalatest.examples.flatspec.ignore
* <br/><span class="stReserved">import</span> org.scalatest.flatspec.AnyFlatSpec
* <br/><span class="stReserved">class</span> <span class="stType">SetSpec</span> <span class="stReserved">extends</span> <span class="stType">AnyFlatSpec</span> {
* <br/> <span class="stQuotedString">"An empty Set"</span> should <span class="stQuotedString">"have size 0"</span> in {
* assert(Set.empty.size === <span class="stLiteral">0</span>)
* }
* <br/> ignore should <span class="stQuotedString">"produce NoSuchElementException when head is invoked"</span> in {
* assertThrows[<span class="stType">NoSuchElementException</span>] {
* Set.empty.head
* }
* }
* }
* </pre>
*
* <p>
* If you run this version of <code>SetSpec</code> with:
* </p>
*
* <pre class="stREPL">
* scala> org.scalatest.run(new SetSpec)
* </pre>
*
* <p>
* It will run only the first test and report that the second test was ignored:
* </p>
*
* <pre class="stREPL">
* <span class="stGreen">An empty Set</span>
* <span class="stGreen">- should have size 0</span>
* <span class="stYellow">- should produce NoSuchElementException when head is invoked !!! IGNORED !!!</span>
* </pre>
*
* <p>
* When using shorthand notation, you won't have an <code>it</code> to change into <code>ignore</code> for
* the first test of each new subject. To ignore such tests, you must instead change <code>in</code> to <code>ignore</code>.
* For example, to temporarily disable the test with the name <code>"An empty Set should have size 0"</code>,
* change “<code>in</code>” into “<code>ignore</code>” like this:
* </p>
*
* <pre class="stHighlighted">
* <span class="stReserved">package</span> org.scalatest.examples.flatspec.ignoreafter
* <br/><span class="stReserved">import</span> org.scalatest.flatspec.AnyFlatSpec
* <br/><span class="stReserved">class</span> <span class="stType">SetSpec</span> <span class="stReserved">extends</span> <span class="stType">AnyFlatSpec</span> {
* <br/> <span class="stQuotedString">"An empty Set"</span> should <span class="stQuotedString">"have size 0"</span> ignore {
* assert(Set.empty.size === <span class="stLiteral">0</span>)
* }
* <br/> it should <span class="stQuotedString">"produce NoSuchElementException when head is invoked"</span> in {
* assertThrows[<span class="stType">NoSuchElementException</span>] {
* Set.empty.head
* }
* }
* }
* </pre>
*
* <p>
* If you run this version of <code>StackSpec</code> with:
* </p>
*
* <pre class="stREPL">
* scala> org.scalatest.run(new SetSpec)
* </pre>
*
* <p>
* It will run only the second test and report that the first test was ignored:
* </p>
*
* <pre class="stREPL">
* <span class="stGreen">An empty Set</span>
* <span class="stYellow">- should have size 0 !!! IGNORED !!!</span>
* <span class="stGreen">- should produce NoSuchElementException when head is invoked</span>
* </pre>
*
* <p>
* If you wish to temporarily ignore an entire suite of tests, you can (on the JVM, not Scala.js) annotate the test class with <code>@Ignore</code>, like this:
* </p>
*
* <pre class="stHighlighted">
* <span class="stReserved">package</span> org.scalatest.examples.flatspec.ignoreall
* <br/><span class="stReserved">import</span> org.scalatest._
* <br/>@<span class="stType">Ignore</span>
* <span class="stReserved">class</span> <span class="stType">SetSpec</span> <span class="stReserved">extends</span> <span class="stType">flatspec.AnyFlatSpec</span> {
* <br/> <span class="stQuotedString">"An empty Set"</span> should <span class="stQuotedString">"have size 0"</span> in {
* assert(Set.empty.size === <span class="stLiteral">0</span>)
* }
* <br/> it should <span class="stQuotedString">"produce NoSuchElementException when head is invoked"</span> in {
* assertThrows[<span class="stType">NoSuchElementException</span>] {
* Set.empty.head
* }
* }
* }
* </pre>
*
* <p>
* When you mark a test class with a tag annotation, ScalaTest will mark each test defined in that class with that tag.
* Thus, marking the <code>SetSpec</code> in the above example with the <code>@Ignore</code> tag annotation means that both tests
* in the class will be ignored. If you run the above <code>SetSpec</code> in the Scala interpreter, you'll see:
* </p>
*
* <pre class="stREPL">
* scala> org.scalatest.run(new SetSpec)
* <span class="stGreen">SetSpec:
* An empty Set</span>
* <span class="stYellow">- should have size 0 !!! IGNORED !!!
* - should produce NoSuchElementException when head is invoked !!! IGNORED !!!</span>
* </pre>
*
* <p>
* Note that marking a test class as ignored won't prevent it from being discovered by ScalaTest. Ignored classes
* will be discovered and run, and all their tests will be reported as ignored. This is intended to keep the ignored
* class visible, to encourage the developers to eventually fix and “un-ignore” it. If you want to
* prevent a class from being discovered at all (on the JVM, not Scala.js), use the <a href="../DoNotDiscover.html"><code>DoNotDiscover</code></a> annotation instead.
* </p>
*
* <a name="informers"></a><h2>Informers</h2>
*
* <p>
* One of the parameters to <code>AnyFlatSpec</code>'s <code>run</code> method is a <a href="../Reporter.html"><code>Reporter</code></a>, which
* will collect and report information about the running suite of tests.
* Information about suites and tests that were run, whether tests succeeded or failed,
* and tests that were ignored will be passed to the <code>Reporter</code> as the suite runs.
* Most often the reporting done by default by <code>AnyFlatSpec</code>'s methods will be sufficient, but
* occasionally you may wish to provide custom information to the <code>Reporter</code> from a test.
* For this purpose, an <a href="../Informer.html"><code>Informer</code></a> that will forward information to the current <code>Reporter</code>
* is provided via the <code>info</code> parameterless method.
* You can pass the extra information to the <code>Informer</code> via its <code>apply</code> method.
* The <code>Informer</code> will then pass the information to the <code>Reporter</code> via an <a href="../events/InfoProvided.html"><code>InfoProvided</code></a> event.
* </p>
*
* <p>
* One use case for the <code>Informer</code> is to pass more information about a specification to the reporter. For example,
* the <a href="../GivenWhenThen.html"><code>GivenWhenThen</code></a> trait provides methods that use the implicit <code>info</code> provided by <code>FlatSpec</code>
* to pass such information to the reporter. Here's an example:
* </p>
*
* <pre class="stHighlighted">
* <span class="stReserved">package</span> org.scalatest.examples.flatspec.info
* <br/><span class="stReserved">import</span> collection.mutable
* <span class="stReserved">import</span> org.scalatest._
* <br/><span class="stReserved">class</span> <span class="stType">SetSpec</span> <span class="stReserved">extends</span> <span class="stType">flatspec.AnyFlatSpec</span> <span class="stReserved">with</span> <span class="stType">GivenWhenThen</span> {
* <br/> <span class="stQuotedString">"A mutable Set"</span> should <span class="stQuotedString">"allow an element to be added"</span> in {
* <span class="stType">Given</span>(<span class="stQuotedString">"an empty mutable Set"</span>)
* <span class="stReserved">val</span> set = mutable.Set.empty[<span class="stType">String</span>]
* <br/> <span class="stType">When</span>(<span class="stQuotedString">"an element is added"</span>)
* set += <span class="stQuotedString">"clarity"</span>
* <br/> <span class="stType">Then</span>(<span class="stQuotedString">"the Set should have size 1"</span>)
* assert(set.size === <span class="stLiteral">1</span>)
* <br/> <span class="stType">And</span>(<span class="stQuotedString">"the Set should contain the added element"</span>)
* assert(set.contains(<span class="stQuotedString">"clarity"</span>))
* <br/> info(<span class="stQuotedString">"That's all folks!"</span>)
* }
* }
* </pre>
*
* <p>
* If you run this <code>AnyFlatSpec</code> from the interpreter, you will see the following output:
* </p>
*
* <pre class="stREPL">
* scala> org.scalatest.run(new SetSpec)
* <span class="stGreen">SetSpec:
* A mutable Set
* - should allow an element to be added
* + Given an empty mutable Set
* + When an element is added
* + Then the Set should have size 1
* + And the Set should contain the added element
* + That's all folks! </span>
* </pre>
*
* <a name="documenters"></a><h2>Documenters</h2>
*
* <p>
* <code>AnyFlatSpec</code> also provides a <code>markup</code> method that returns a <a href="../Documenter.html"><code>Documenter</code></a>, which allows you to send
* to the <code>Reporter</code> text formatted in <a href="http://daringfireball.net/projects/markdown/" target="_blank">Markdown syntax</a>.
* You can pass the extra information to the <code>Documenter</code> via its <code>apply</code> method.
* The <code>Documenter</code> will then pass the information to the <code>Reporter</code> via an <a href="../events/MarkupProvided.html"><code>MarkupProvided</code></a> event.
* </p>
*
* <p>
* Here's an example <code>AnyFlatSpec</code> that uses <code>markup</code>:
* </p>
*
* <pre class="stHighlighted">
* <span class="stReserved">package</span> org.scalatest.examples.flatspec.markup
* <br/><span class="stReserved">import</span> collection.mutable
* <span class="stReserved">import</span> org.scalatest._
* <br/><span class="stReserved">class</span> <span class="stType">SetSpec</span> <span class="stReserved">extends</span> <span class="stType">flatspec.AnyFlatSpec</span> <span class="stReserved">with</span> <span class="stType">GivenWhenThen</span> {
* <br/> markup { <span class="stQuotedString">"""</span>
* <span class="stQuotedString"></span>
* <span class="stQuotedString">Mutable Set</span>
* <span class="stQuotedString">———--</span>
* <span class="stQuotedString"></span>
* <span class="stQuotedString">A set is a collection that contains no duplicate elements.</span>
* <span class="stQuotedString"></span>
* <span class="stQuotedString">To implement a concrete mutable set, you need to provide implementations</span>
* <span class="stQuotedString">of the following methods:</span>
* <span class="stQuotedString"></span>
* <span class="stQuotedString">def contains(elem: A): Boolean</span>
* <span class="stQuotedString">def iterator: Iterator[A]</span>
* <span class="stQuotedString">def += (elem: A): this.type</span>
* <span class="stQuotedString">def -= (elem: A): this.type</span>
* <span class="stQuotedString"></span>
* <span class="stQuotedString">If you wish that methods like `take`,</span>
* <span class="stQuotedString">`drop`, `filter` return the same kind of set,</span>
* <span class="stQuotedString">you should also override:</span>
* <span class="stQuotedString"></span>
* <span class="stQuotedString">def empty: This</span>
* <span class="stQuotedString"></span>
* <span class="stQuotedString">It is also good idea to override methods `foreach` and</span>
* <span class="stQuotedString">`size` for efficiency.</span>
* <span class="stQuotedString"></span>
* <span class="stQuotedString">"""</span> }
* <br/> <span class="stQuotedString">"A mutable Set"</span> should <span class="stQuotedString">"allow an element to be added"</span> in {
* <span class="stType">Given</span>(<span class="stQuotedString">"an empty mutable Set"</span>)
* <span class="stReserved">val</span> set = mutable.Set.empty[<span class="stType">String</span>]
* <br/> <span class="stType">When</span>(<span class="stQuotedString">"an element is added"</span>)
* set += <span class="stQuotedString">"clarity"</span>
* <br/> <span class="stType">Then</span>(<span class="stQuotedString">"the Set should have size 1"</span>)
* assert(set.size === <span class="stLiteral">1</span>)
* <br/> <span class="stType">And</span>(<span class="stQuotedString">"the Set should contain the added element"</span>)
* assert(set.contains(<span class="stQuotedString">"clarity"</span>))
* <br/> markup(<span class="stQuotedString">"This test finished with a **bold** statement!"</span>)
* }
* }
* </pre>
*
* <p>
* Although all of ScalaTest's built-in reporters will display the markup text in some form,
* the HTML reporter will format the markup information into HTML. Thus, the main purpose of <code>markup</code> is to
* add nicely formatted text to HTML reports. Here's what the above <code>SetSpec</code> would look like in the HTML reporter:
* </p>
*
* <img class="stScreenShot" src="../../../lib/flatSpec.gif">
*
* <a name="notifiersAlerters"></a><h2>Notifiers and alerters</h2>
*
* <p>
* ScalaTest records text passed to <code>info</code> and <code>markup</code> during tests, and sends the recorded text in the <code>recordedEvents</code> field of
* test completion events like <code>TestSucceeded</code> and <code>TestFailed</code>. This allows string reporters (like the standard out reporter) to show
* <code>info</code> and <code>markup</code> text <em>after</em> the test name in a color determined by the outcome of the test. For example, if the test fails, string
* reporters will show the <code>info</code> and <code>markup</code> text in red. If a test succeeds, string reporters will show the <code>info</code>
* and <code>markup</code> text in green. While this approach helps the readability of reports, it means that you can't use <code>info</code> to get status
* updates from long running tests.
* </p>
*
* <p>
* To get immediate (<em>i.e.</em>, non-recorded) notifications from tests, you can use <code>note</code> (a <a href="../Notifier.html"><code>Notifier</code></a>) and <code>alert</code>
* (an <a href="../Alerter.html"><code>Alerter</code></a>). Here's an example showing the differences:
* </p>
*
* <pre class="stHighlighted">
* <span class="stReserved">package</span> org.scalatest.examples.flatspec.note
* <br/><span class="stReserved">import</span> collection.mutable
* <span class="stReserved">import</span> org.scalatest._
* <br/><span class="stReserved">class</span> <span class="stType">SetSpec</span> <span class="stReserved">extends</span> <span class="stType">flatspec.AnyFlatSpec</span> {
* <br/> <span class="stQuotedString">"A mutable Set"</span> should <span class="stQuotedString">"allow an element to be added"</span> in {
* <br/> info(<span class="stQuotedString">"info is recorded"</span>)
* markup(<span class="stQuotedString">"markup is *also* recorded"</span>)
* note(<span class="stQuotedString">"notes are sent immediately"</span>)
* alert(<span class="stQuotedString">"alerts are also sent immediately"</span>)
* <br/> <span class="stReserved">val</span> set = mutable.Set.empty[<span class="stType">String</span>]
* set += <span class="stQuotedString">"clarity"</span>
* assert(set.size === <span class="stLiteral">1</span>)
* assert(set.contains(<span class="stQuotedString">"clarity"</span>))
* }
* }
* </pre>
*
* <p>
* Because <code>note</code> and <code>alert</code> information is sent immediately, it will appear <em>before</em> the test name in string reporters, and its color will
* be unrelated to the ultimate outcome of the test: <code>note</code> text will always appear in green, <code>alert</code> text will always appear in yellow.
* Here's an example:
* </p>
*
* <pre class="stREPL">
* scala> org.scalatest.run(new SetSpec)
* <span class="stGreen">SetSpec:
* A mutable Set
* + notes are sent immediately</span>
* <span class="stYellow">+ alerts are also sent immediately</span>
* <span class="stGreen">- should allow an element to be added
* + info is recorded
* + markup is *also* recorded</span>
* </pre>
*
* <p>
* Another example is <a href="../tools/Runner$.html#slowpokeNotifications">slowpoke notifications</a>.
* If you find a test is taking a long time to complete, but you're not sure which test, you can enable
* slowpoke notifications. ScalaTest will use an <code>Alerter</code> to fire an event whenever a test has been running
* longer than a specified amount of time.
* </p>
*
* <p>
* In summary, use <code>info</code> and <code>markup</code> for text that should form part of the specification output. Use
* <code>note</code> and <code>alert</code> to send status notifications. (Because the HTML reporter is intended to produce a
* readable, printable specification, <code>info</code> and <code>markup</code> text will appear in the HTML report, but
* <code>note</code> and <code>alert</code> text will not.)
* </p>
*
* <a name="pendingTests"></a><h2>Pending tests</h2>
*
* <p>
* A <em>pending test</em> is one that has been given a name but is not yet implemented. The purpose of
* pending tests is to facilitate a style of testing in which documentation of behavior is sketched
* out before tests are written to verify that behavior (and often, before the behavior of
* the system being tested is itself implemented). Such sketches form a kind of specification of
* what tests and functionality to implement later.
* </p>
*
* <p>
* To support this style of testing, a test can be given a name that specifies one
* bit of behavior required by the system being tested. The test can also include some code that
* sends more information about the behavior to the reporter when the tests run. At the end of the test,
* it can call method <code>pending</code>, which will cause it to complete abruptly with <a href="../exceptions/TestPendingException.html"><code>TestPendingException</code></a>.
* </p>
*
* <p>
* Because tests in ScalaTest can be designated as pending with <code>TestPendingException</code>, both the test name and any information
* sent to the reporter when running the test can appear in the report of a test run. (In other words,
* the code of a pending test is executed just like any other test.) However, because the test completes abruptly
* with <code>TestPendingException</code>, the test will be reported as pending, to indicate
* the actual test, and possibly the functionality it is intended to test, has not yet been implemented.
* You can mark tests as pending in <code>FlatSpec</code> like this:
* </p>
*
* <pre class="stHighlighted">
* <span class="stReserved">package</span> org.scalatest.examples.flatspec.pending
* <br/><span class="stReserved">import</span> org.scalatest._
* <br/><span class="stReserved">class</span> <span class="stType">SetSpec</span> <span class="stReserved">extends</span> <span class="stType">flatspec.AnyFlatSpec</span> {
* <br/> <span class="stQuotedString">"An empty Set"</span> should <span class="stQuotedString">"have size 0"</span> in (pending)
* <br/> it should <span class="stQuotedString">"produce NoSuchElementException when head is invoked"</span> in {
* assertThrows[<span class="stType">NoSuchElementException</span>] {
* Set.empty.head
* }
* }
* }
* </pre>
*
* <p>
* If you run this version of <code>AnyFlatSpec</code> with:
* </p>
*
* <pre class="stREPL">
* scala> org.scalatest.run(new SetSpec)
* </pre>
*
* <p>
* It will run both tests but report that <code>An empty Set should have size 0</code> is pending. You'll see:
* </p>
*
* <pre class="stREPL">
* <span class="stGreen">An empty Set</span>
* <span class="stYellow">- should have size 0 (pending)</span>
* <span class="stGreen">- should produce NoSuchElementException when head is invoked</span>
* </pre>
*
* <p>
* One difference between an ignored test and a pending one is that an ignored test is intended to be used during a
* significant refactorings of the code under test, when tests break and you don't want to spend the time to fix
* all of them immediately. You can mark some of those broken tests as ignored temporarily, so that you can focus the red
* bar on just failing tests you actually want to fix immediately. Later you can go back and fix the ignored tests.
* In other words, by ignoring some failing tests temporarily, you can more easily notice failed tests that you actually
* want to fix. By contrast, a pending test is intended to be used before a test and/or the code under test is written.
* Pending indicates you've decided to write a test for a bit of behavior, but either you haven't written the test yet, or
* have only written part of it, or perhaps you've written the test but don't want to implement the behavior it tests
* until after you've implemented a different bit of behavior you realized you need first. Thus ignored tests are designed
* to facilitate refactoring of existing code whereas pending tests are designed to facilitate the creation of new code.
* </p>
*
* <p>
* One other difference between ignored and pending tests is that ignored tests are implemented as a test tag that is
* excluded by default. Thus an ignored test is never executed. By contrast, a pending test is implemented as a
* test that throws <code>TestPendingException</code> (which is what calling the <code>pending</code> method does). Thus
* the body of pending tests are executed up until they throw <code>TestPendingException</code>. The reason for this difference
* is that it enables your unfinished test to send <code>InfoProvided</code> messages to the reporter before it completes
* abruptly with <code>TestPendingException</code>, as shown in the previous example on <code>Informer</code>s
* that used the <code>GivenWhenThen</code> trait. For example, the following snippet in a <code>AnyFlatSpec</code>:
* </p>
*
* <pre class="stHighlighted">
* <span class="stQuotedString">"The Scala language"</span> must <span class="stQuotedString">"add correctly"</span> in {
* <span class="stType">Given</span>(<span class="stQuotedString">"two integers"</span>)
* <span class="stType">When</span>(<span class="stQuotedString">"they are added"</span>)
* <span class="stType">Then</span>(<span class="stQuotedString">"the result is the sum of the two numbers"</span>)
* pending
* }
* <span class="stLineComment">// ...</span>
* </pre>
*
* <p>
* Would yield the following output when run in the interpreter:
* </p>
*
* <pre class="stREPL">
* <span class="stGreen">The Scala language</span>
* <span class="stYellow">- must add correctly (pending)
* + Given two integers
* + When they are added
* + Then the result is the sum of the two numbers</span>
* </pre>
*
* <a name="taggingTests"></a><h2>Tagging tests</h2>
*
* A <code>AnyFlatSpec</code>'s tests may be classified into groups by <em>tagging</em> them with string names.
* As with any suite, when executing a <code>AnyFlatSpec</code>, groups of tests can
* optionally be included and/or excluded. To tag a <code>AnyFlatSpec</code>'s tests,
* you pass objects that extend class <code>org.scalatest.Tag</code> to methods
* that register tests. Class <code>Tag</code> takes one parameter, a string name. If you have
* created tag annotation interfaces as described in the <a href="../Tag.html"><code>Tag</code> documentation</a>, then you
* will probably want to use tag names on your test functions that match. To do so, simply
* pass the fully qualified names of the tag interfaces to the <code>Tag</code> constructor. For example, if you've
* defined a tag annotation interface with fully qualified name,
* <code>com.mycompany.tags.DbTest</code>, then you could
* create a matching tag for <code>AnyFlatSpec</code>s like this:
* </p>
*
* <pre class="stHighlighted">
* <span class="stReserved">package</span> org.scalatest.examples.flatspec.tagging
* <br/><span class="stReserved">import</span> org.scalatest.Tag
* <br/><span class="stReserved">object</span> <span class="stType">DbTest</span> <span class="stReserved">extends</span> <span class="stType">Tag</span>(<span class="stQuotedString">"com.mycompany.tags.DbTest"</span>)
* </pre>
*
* <p>
* Given these definitions, you could place <code>AnyFlatSpec</code> tests into groups with tags like this:
* </p>
*
* <pre class="stHighlighted">
* <span class="stReserved">import</span> org.scalatest.flatspec.AnyFlatSpec
* <span class="stReserved">import</span> org.scalatest.tagobjects.Slow
* <br/><span class="stReserved">class</span> <span class="stType">SetSpec</span> <span class="stReserved">extends</span> <span class="stType">AnyFlatSpec</span> {
* <br/> behavior of <span class="stQuotedString">"An empty Set"</span>
* <br/> it should <span class="stQuotedString">"have size 0"</span> taggedAs(<span class="stType">Slow</span>) in {
* assert(Set.empty.size === <span class="stLiteral">0</span>)
* }
* <br/> it should <span class="stQuotedString">"produce NoSuchElementException when head is invoked"</span> taggedAs(<span class="stType">Slow</span>, <span class="stType">DbTest</span>) in {
* assertThrows[<span class="stType">NoSuchElementException</span>] {
* Set.empty.head
* }
* }
* }
* </pre>
*
* <p>
* This code marks both tests with the <code>org.scalatest.tags.Slow</code> tag,
* and the second test with the <code>com.mycompany.tags.DbTest</code> tag.
* </p>
*
* <p>
* The <code>run</code> method takes a <a href="../Filter.html"><code>Filter</code></a>, whose constructor takes an optional
* <code>Set[String]</code> called <code>tagsToInclude</code> and a <code>Set[String]</code> called
* <code>tagsToExclude</code>. If <code>tagsToInclude</code> is <code>None</code>, all tests will be run
* except those those belonging to tags listed in the
* <code>tagsToExclude</code> <code>Set</code>. If <code>tagsToInclude</code> is defined, only tests
* belonging to tags mentioned in the <code>tagsToInclude</code> set, and not mentioned in <code>tagsToExclude</code>,
* will be run.
* </p>
*
* <p>
* It is recommended, though not required, that you create a corresponding tag annotation when you
* create a <code>Tag</code> object. A tag annotation (on the JVM, not Scala.js) allows you to tag all the tests of a <code>AnyFlatSpec</code> in
* one stroke by annotating the class. For more information and examples, see the
* <a href="../Tag.html">documentation for class <code>Tag</code></a>. On Scala.js, to tag all tests of a suite, you'll need to
* tag each test individually at the test site.
* </p>
*
* <a name="sharedFixtures"></a>
* <h2>Shared fixtures</h2>
*
* <p>
* A test <em>fixture</em> is composed of the objects and other artifacts (files, sockets, database
* connections, <em>etc.</em>) tests use to do their work.
* When multiple tests need to work with the same fixtures, it is important to try and avoid
* duplicating the fixture code across those tests. The more code duplication you have in your
* tests, the greater drag the tests will have on refactoring the actual production code.
* </p>
*
* <p>
* ScalaTest recommends three techniques to eliminate such code duplication:
* </p>
*
* <ul>
* <li>Refactor using Scala</li>
* <li>Override <code>withFixture</code></li>
* <li>Mix in a <em>before-and-after</em> trait</li>
* </ul>
*
* <p>Each technique is geared towards helping you reduce code duplication without introducing
* instance <code>var</code>s, shared mutable objects, or other dependencies between tests. Eliminating shared
* mutable state across tests will make your test code easier to reason about and more amenable for parallel
* test execution.</p><p>The following sections
* describe these techniques, including explaining the recommended usage
* for each. But first, here's a table summarizing the options:</p>
*
* <table style="border-collapse: collapse; border: 1px solid black">
*
* <tr>
* <td colspan="2" style="background-color: #CCCCCC; border-width: 1px; padding: 3px; padding-top: 7px; border: 1px solid black; text-align: left">
* <strong>Refactor using Scala when different tests need different fixtures.</strong>
* </td>
* </tr>
*
* <tr>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: right">
* <a href="#getFixtureMethods">get-fixture methods</a>
* </td>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: left">
* The <em>extract method</em> refactor helps you create a fresh instances of mutable fixture objects in each test
* that needs them, but doesn't help you clean them up when you're done.
* </td>
* </tr>
*
* <tr>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: right">
* <a href="#fixtureContextObjects">fixture-context objects</a>
* </td>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: left">
* By placing fixture methods and fields into traits, you can easily give each test just the newly created
* fixtures it needs by mixing together traits. Use this technique when you need <em>different combinations
* of mutable fixture objects in different tests</em>, and don't need to clean up after.
* </td>
* </tr>
*
* <tr>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: right">
* <a href="#loanFixtureMethods">loan-fixture methods</a>
* </td>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: left">
* Factor out dupicate code with the <em>loan pattern</em> when different tests need different fixtures <em>that must be cleaned up afterwards</em>.
* </td>
* </tr>
*
* <tr>
* <td colspan="2" style="background-color: #CCCCCC; border-width: 1px; padding: 3px; padding-top: 7px; border: 1px solid black; text-align: left">
* <strong>Override <code>withFixture</code> when most or all tests need the same fixture.</strong>
* </td>
* </tr>
*
* <tr>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: right">
* <a href="#withFixtureNoArgTest">
* <code>withFixture(NoArgTest)</code></a>
* </td>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: left">
* <p>
* The recommended default approach when most or all tests need the same fixture treatment. This general technique
* allows you, for example, to perform side effects at the beginning and end of all or most tests,
* transform the outcome of tests, retry tests, make decisions based on test names, tags, or other test data.
* Use this technique unless:
* </p>
* <dl>
* <dd style="display: list-item; list-style-type: disc; margin-left: 1.2em;">Different tests need different fixtures (refactor using Scala instead)</dd>
* <dd style="display: list-item; list-style-type: disc; margin-left: 1.2em;">An exception in fixture code should abort the suite, not fail the test (use a <em>before-and-after</em> trait instead)</dd>
* <dd style="display: list-item; list-style-type: disc; margin-left: 1.2em;">You have objects to pass into tests (override <code>withFixture(<em>One</em>ArgTest)</code> instead)</dd>
* </dl>
* </td>
* </tr>
*
* <tr>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: right">
* <a href="#withFixtureOneArgTest">
* <code>withFixture(OneArgTest)</code>
* </a>
* </td>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: left">
* Use when you want to pass the same fixture object or objects as a parameter into all or most tests.
* </td>
* </tr>
*
* <tr>
* <td colspan="2" style="background-color: #CCCCCC; border-width: 1px; padding: 3px; padding-top: 7px; border: 1px solid black; text-align: left">
* <strong>Mix in a before-and-after trait when you want an aborted suite, not a failed test, if the fixture code fails.</strong>
* </td>
* </tr>
*
* <tr>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: right">
* <a href="#beforeAndAfter"><code>BeforeAndAfter</code></a>
* </td>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: left">
* Use this boilerplate-buster when you need to perform the same side-effects before and/or after tests, rather than at the beginning or end of tests.
* </td>
* </tr>
*
* <tr>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: right">
* <a href="#composingFixtures"><code>BeforeAndAfterEach</code></a>
* </td>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: left">
* Use when you want to <em>stack traits</em> that perform the same side-effects before and/or after tests, rather than at the beginning or end of tests.
* </td>
* </tr>
*
* </table>
*
* <a name="getFixtureMethods"></a>
* <h4>Calling get-fixture methods</h4>
*
* <p>
* If you need to create the same mutable fixture objects in multiple tests, and don't need to clean them up after using them, the simplest approach is to write one or
* more <em>get-fixture</em> methods. A get-fixture method returns a new instance of a needed fixture object (or a holder object containing
* multiple fixture objects) each time it is called. You can call a get-fixture method at the beginning of each
* test that needs the fixture, storing the returned object or objects in local variables. Here's an example:
* </p>
*
* <pre class="stHighlighted">
* <span class="stReserved">package</span> org.scalatest.examples.flatspec.getfixture
* <br/><span class="stReserved">import</span> org.scalatest.flatspec.AnyFlatSpec
* <span class="stReserved">import</span> collection.mutable.ListBuffer
* <br/><span class="stReserved">class</span> <span class="stType">ExampleSpec</span> <span class="stReserved">extends</span> <span class="stType">AnyFlatSpec</span> {
* <br/> <span class="stReserved">class</span> <span class="stType">Fixture</span> {
* <span class="stReserved">val</span> builder = <span class="stReserved">new</span> <span class="stType">StringBuilder</span>(<span class="stQuotedString">"ScalaTest is "</span>)
* <span class="stReserved">val</span> buffer = <span class="stReserved">new</span> <span class="stType">ListBuffer[String]</span>
* }
* <br/> <span class="stReserved">def</span> fixture = <span class="stReserved">new</span> <span class="stType">Fixture</span>
* <br/> <span class="stQuotedString">"Testing"</span> should <span class="stQuotedString">"be easy"</span> in {
* <span class="stReserved">val</span> f = fixture
* f.builder.append(<span class="stQuotedString">"easy!"</span>)
* assert(f.builder.toString === <span class="stQuotedString">"ScalaTest is easy!"</span>)
* assert(f.buffer.isEmpty)
* f.buffer += <span class="stQuotedString">"sweet"</span>
* }
* <br/> it should <span class="stQuotedString">"be fun"</span> in {
* <span class="stReserved">val</span> f = fixture
* f.builder.append(<span class="stQuotedString">"fun!"</span>)
* assert(f.builder.toString === <span class="stQuotedString">"ScalaTest is fun!"</span>)
* assert(f.buffer.isEmpty)
* }
* }
* </pre>
*
* <p>
* The “<code>f.</code>” in front of each use of a fixture object provides a visual indication of which objects
* are part of the fixture, but if you prefer, you can import the the members with “<code>import f._</code>” and use the names directly.
* </p>
*
* <p>
* If you need to configure fixture objects differently in different tests, you can pass configuration into the get-fixture method. For example, if you could pass
* in an initial value for a mutable fixture object as a parameter to the get-fixture method.
* </p>
*
* <a name="fixtureContextObjects"></a>
* <h4>Instantiating fixture-context objects </h4>
*
* <p>
* An alternate technique that is especially useful when different tests need different combinations of fixture objects is to define the fixture objects as instance variables
* of <em>fixture-context objects</em> whose instantiation forms the body of tests. Like get-fixture methods, fixture-context objects are only
* appropriate if you don't need to clean up the fixtures after using them.
* </p>
*
* <p>
* To use this technique, you define instance variables intialized with fixture objects in traits and/or classes, then in each test instantiate an object that
* contains just the fixture objects needed by the test. Traits allow you to mix together just the fixture objects needed by each test, whereas classes
* allow you to pass data in via a constructor to configure the fixture objects. Here's an example in which fixture objects are partitioned into two traits
* and each test just mixes together the traits it needs:
* </p>
*
* <pre class="stHighlighted">
* <span class="stReserved">package</span> org.scalatest.examples.flatspec.fixturecontext
* <br/><span class="stReserved">import</span> collection.mutable.ListBuffer
* <span class="stReserved">import</span> org.scalatest.flatspec.AnyFlatSpec
* <br/><span class="stReserved">class</span> <span class="stType">ExampleSpec</span> <span class="stReserved">extends</span> <span class="stType">AnyFlatSpec</span> {
* <br/> <span class="stReserved">trait</span> <span class="stType">Builder</span> {
* <span class="stReserved">val</span> builder = <span class="stReserved">new</span> <span class="stType">StringBuilder</span>(<span class="stQuotedString">"ScalaTest is "</span>)
* }
* <br/> <span class="stReserved">trait</span> <span class="stType">Buffer</span> {
* <span class="stReserved">val</span> buffer = <span class="stType">ListBuffer</span>(<span class="stQuotedString">"ScalaTest"</span>, <span class="stQuotedString">"is"</span>)
* }
* <br/> <span class="stLineComment">// This test needs the StringBuilder fixture</span>
* <span class="stQuotedString">"Testing"</span> should <span class="stQuotedString">"be productive"</span> in <span class="stReserved">new</span> <span class="stType">Builder</span> {
* builder.append(<span class="stQuotedString">"productive!"</span>)
* assert(builder.toString === <span class="stQuotedString">"ScalaTest is productive!"</span>)
* }
* <br/> <span class="stLineComment">// This test needs the ListBuffer[String] fixture</span>
* <span class="stQuotedString">"Test code"</span> should <span class="stQuotedString">"be readable"</span> in <span class="stReserved">new</span> <span class="stType">Buffer</span> {
* buffer += (<span class="stQuotedString">"readable!"</span>)
* assert(buffer === <span class="stType">List</span>(<span class="stQuotedString">"ScalaTest"</span>, <span class="stQuotedString">"is"</span>, <span class="stQuotedString">"readable!"</span>))
* }
* <br/> <span class="stLineComment">// This test needs both the StringBuilder and ListBuffer</span>
* it should <span class="stQuotedString">"be clear and concise"</span> in <span class="stReserved">new</span> <span class="stType">Builder</span> <span class="stReserved">with</span> <span class="stType">Buffer</span> {
* builder.append(<span class="stQuotedString">"clear!"</span>)
* buffer += (<span class="stQuotedString">"concise!"</span>)
* assert(builder.toString === <span class="stQuotedString">"ScalaTest is clear!"</span>)
* assert(buffer === <span class="stType">List</span>(<span class="stQuotedString">"ScalaTest"</span>, <span class="stQuotedString">"is"</span>, <span class="stQuotedString">"concise!"</span>))
* }
* }
* </pre>
*
* <a name="withFixtureNoArgTest"></a>
* <h4>Overriding <code>withFixture(NoArgTest)</code></h4>
*
* <p>
* Although the get-fixture method and fixture-context object approaches take care of setting up a fixture at the beginning of each
* test, they don't address the problem of cleaning up a fixture at the end of the test. If you just need to perform a side-effect at the beginning or end of
* a test, and don't need to actually pass any fixture objects into the test, you can override <code>withFixture(NoArgTest)</code>, one of ScalaTest's
* lifecycle methods defined in trait <a href="../Suite.html"><code>Suite</code></a>.
* </p>
*
* <p>
* Trait <code>Suite</code>'s implementation of <code>runTest</code> passes a no-arg test function to <code>withFixture(NoArgTest)</code>. It is <code>withFixture</code>'s
* responsibility to invoke that test function. <code>Suite</code>'s implementation of <code>withFixture</code> simply
* invokes the function, like this:
* </p>
*
* <pre class="stHighlighted">
* <span class="stLineComment">// Default implementation in trait Suite</span>
* <span class="stReserved">protected</span> <span class="stReserved">def</span> withFixture(test: <span class="stType">NoArgTest</span>) = {
* test()
* }
* </pre>
*
* <p>
* You can, therefore, override <code>withFixture</code> to perform setup before and/or cleanup after invoking the test function. If
* you have cleanup to perform, you should invoke the test function inside a <code>try</code> block and perform the cleanup in
* a <code>finally</code> clause, in case an exception propagates back through <code>withFixture</code>. (If a test fails because of an exception,
* the test function invoked by withFixture will result in a [[org.scalatest.Failed <code>Failed</code>]] wrapping the exception. Nevertheless,
* best practice is to perform cleanup in a finally clause just in case an exception occurs.)
* </p>
*
* <p>
* The <code>withFixture</code> method is designed to be stacked, and to enable this, you should always call the <code>super</code> implementation
* of <code>withFixture</code>, and let it invoke the test function rather than invoking the test function directly. That is to say, instead of writing
* “<code>test()</code>”, you should write “<code>super.withFixture(test)</code>”, like this:
* </p>
*
* <pre class="stHighlighted">
* <span class="stLineComment">// Your implementation</span>
* <span class="stReserved">override</span> <span class="stReserved">def</span> withFixture(test: <span class="stType">NoArgTest</span>) = {
* <span class="stLineComment">// Perform setup</span>
* <span class="stReserved">try</span> <span class="stReserved">super</span>.withFixture(test) <span class="stLineComment">// Invoke the test function</span>
* <span class="stReserved">finally</span> {
* <span class="stLineComment">// Perform cleanup</span>
* }
* }
* </pre>
*
* <p>
* Here's an example in which <code>withFixture(NoArgTest)</code> is used to take a snapshot of the working directory if a test fails, and
* send that information to the reporter:
* </p>
*
* <pre class="stHighlighted">
* <span class="stReserved">package</span> org.scalatest.examples.flatspec.noargtest
* <br/><span class="stReserved">import</span> java.io.File
* <span class="stReserved">import</span> org.scalatest._
* <br/><span class="stReserved">class</span> <span class="stType">ExampleSpec</span> <span class="stReserved">extends</span> <span class="stType">flatspec.AnyFlatSpec</span> {
* <br/> <span class="stReserved">override</span> <span class="stReserved">def</span> withFixture(test: <span class="stType">NoArgTest</span>) = {
* <br/> <span class="stReserved">super</span>.withFixture(test) <span class="stReserved">match</span> {
* <span class="stReserved">case</span> failed: <span class="stType">Failed</span> =>
* <span class="stReserved">val</span> currDir = <span class="stReserved">new</span> <span class="stType">File</span>(<span class="stQuotedString">"."</span>)
* <span class="stReserved">val</span> fileNames = currDir.list()
* info(<span class="stQuotedString">"Dir snapshot: "</span> + fileNames.mkString(<span class="stQuotedString">", "</span>))
* failed
* <span class="stReserved">case</span> other => other
* }
* }
* <br/> <span class="stQuotedString">"This test"</span> should <span class="stQuotedString">"succeed"</span> in {
* assert(<span class="stLiteral">1</span> + <span class="stLiteral">1</span> === <span class="stLiteral">2</span>)
* }
* <br/> it should <span class="stQuotedString">"fail"</span> in {
* assert(<span class="stLiteral">1</span> + <span class="stLiteral">1</span> === <span class="stLiteral">3</span>)
* }
* }
* </pre>
*
* <p>
* Running this version of <code>ExampleSuite</code> in the interpreter in a directory with two files, <code>hello.txt</code> and <code>world.txt</code>
* would give the following output:
* </p>
*
* <pre class="stREPL">
* scala> org.scalatest.run(new ExampleSuite)
* <span class="stGreen">ExampleSuite:
* This test
* - should succeed</span>
* <span class="stRed">- should fail *** FAILED ***
* 2 did not equal 3 (<console>:33)
* + Dir snapshot: hello.txt, world.txt </span>
* </pre>
*
* <p>
* Note that the <a href="../Suite$NoArgTest.html"><code>NoArgTest</code></a> passed to <code>withFixture</code>, in addition to
* an <code>apply</code> method that executes the test, also includes [[org.scalatest.TestData <code>TestData</code>]] such as the test name and the <a href="../ConfigMap.html">config
* map</a> passed to <code>runTest</code>. Thus you can also use the test name and configuration objects in your <code>withFixture</code>
* implementation.
* </p>
*
* <a name="loanFixtureMethods"></a>
* <h4>Calling loan-fixture methods</h4>
*
* <p>
* If you need to both pass a fixture object into a test <em>and</em> perform cleanup at the end of the test, you'll need to use the <em>loan pattern</em>.
* If different tests need different fixtures that require cleanup, you can implement the loan pattern directly by writing <em>loan-fixture</em> methods.
* A loan-fixture method takes a function whose body forms part or all of a test's code. It creates a fixture, passes it to the test code by invoking the
* function, then cleans up the fixture after the function returns.
* </p>
*
* <p>
* The following example shows three tests that use two fixtures, a database and a file. Both require cleanup after, so each is provided via a
* loan-fixture method. (In this example, the database is simulated with a <code>StringBuffer</code>.)
* </p>
*
* <pre class="stHighlighted">
* <span class="stReserved">package</span> org.scalatest.examples.flatspec.loanfixture
* <br/><span class="stReserved">import</span> java.util.concurrent.ConcurrentHashMap
* <br/><span class="stReserved">object</span> <span class="stType">DbServer</span> { <span class="stLineComment">// Simulating a database server</span>
* <span class="stReserved">type</span> <span class="stType">Db</span> = <span class="stType">StringBuffer</span>
* <span class="stReserved">private</span> <span class="stReserved">val</span> databases = <span class="stReserved">new</span> <span class="stType">ConcurrentHashMap[String, Db]</span>
* <span class="stReserved">def</span> createDb(name: <span class="stType">String</span>): <span class="stType">Db</span> = {
* <span class="stReserved">val</span> db = <span class="stReserved">new</span> <span class="stType">StringBuffer</span>
* databases.put(name, db)
* db
* }
* <span class="stReserved">def</span> removeDb(name: <span class="stType">String</span>) {
* databases.remove(name)
* }
* }
* <br/><span class="stReserved">import</span> org.scalatest.flatspec.AnyFlatSpec
* <span class="stReserved">import</span> DbServer._
* <span class="stReserved">import</span> java.util.UUID.randomUUID
* <span class="stReserved">import</span> java.io._
* <br/><span class="stReserved">class</span> <span class="stType">ExampleSpec</span> <span class="stReserved">extends</span> <span class="stType">flatspec.AnyFlatSpec</span> {
* <br/> <span class="stReserved">def</span> withDatabase(testCode: <span class="stType">Db</span> => <span class="stType">Any</span>) {
* <span class="stReserved">val</span> dbName = randomUUID.toString
* <span class="stReserved">val</span> db = createDb(dbName) <span class="stLineComment">// create the fixture</span>
* <span class="stReserved">try</span> {
* db.append(<span class="stQuotedString">"ScalaTest is "</span>) <span class="stLineComment">// perform setup</span>
* testCode(db) <span class="stLineComment">// "loan" the fixture to the test</span>
* }
* <span class="stReserved">finally</span> removeDb(dbName) <span class="stLineComment">// clean up the fixture</span>
* }
* <br/> <span class="stReserved">def</span> withFile(testCode: (<span class="stType">File</span>, <span class="stType">FileWriter</span>) => <span class="stType">Any</span>) {
* <span class="stReserved">val</span> file = File.createTempFile(<span class="stQuotedString">"hello"</span>, <span class="stQuotedString">"world"</span>) <span class="stLineComment">// create the fixture</span>
* <span class="stReserved">val</span> writer = <span class="stReserved">new</span> <span class="stType">FileWriter</span>(file)
* <span class="stReserved">try</span> {
* writer.write(<span class="stQuotedString">"ScalaTest is "</span>) <span class="stLineComment">// set up the fixture</span>
* testCode(file, writer) <span class="stLineComment">// "loan" the fixture to the test</span>
* }
* <span class="stReserved">finally</span> writer.close() <span class="stLineComment">// clean up the fixture</span>
* }
* <br/> <span class="stLineComment">// This test needs the file fixture</span>
* <span class="stQuotedString">"Testing"</span> should <span class="stQuotedString">"be productive"</span> in withFile { (file, writer) =>
* writer.write(<span class="stQuotedString">"productive!"</span>)