/
AsyncFunSpec.scala
2118 lines (2115 loc) · 133 KB
/
AsyncFunSpec.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-2014 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.funspec
import org.scalatest.Suite
/**
* Enables testing of asynchronous code without blocking,
* using a style consistent with traditional <code>AnyFunSpec</code> tests.
*
* <table><tr><td class="usage">
* <strong>Recommended Usage</strong>:
* <code>AsyncFunSpec</code> is intended to enable users of <a href="AnyFunSpec.html"><code>AnyFunSpec</code></a>
* to write non-blocking asynchronous tests that are consistent with their traditional <code>AnyFunSpec</code> tests.
* <em>Note: <code>AsyncFunSpec</code> is intended for use in special situations where non-blocking asynchronous
* testing is needed, with class <code>AnyFunSpec</code> used for general needs.</em>
* </td></tr></table>
*
* <p>
* Given a <code>Future</code> returned by the code you are testing,
* you need not block until the <code>Future</code> completes before
* performing assertions against its value. You can instead map those
* assertions onto the <code>Future</code> and return the resulting
* <code>Future[Assertion]</code> to ScalaTest. The test will complete
* asynchronously, when the <code>Future[Assertion]</code> completes.
* </p>
*
* <p>
* Here's an example <code>AsyncFunSpec</code>:
* </p>
*
* <pre class="stHighlighted">
* <span class="stReserved">package</span> org.scalatest.examples.asyncfunspec
* <br/><span class="stReserved">import</span> org.scalatest.funspec.AsyncFunSpec
* <span class="stReserved">import</span> scala.concurrent.Future
* <br/><span class="stReserved">class</span> <span class="stType">AddSpec</span> <span class="stReserved">extends</span> <span class="stType">AsyncFunSpec</span> {
* <br/> <span class="stReserved">def</span> addSoon(addends: <span class="stType">Int</span>*): <span class="stType">Future[Int]</span> = <span class="stType">Future</span> { addends.sum }
* <br/> describe(<span class="stQuotedString">"addSoon"</span>) {
* it(<span class="stQuotedString">"will eventually compute a sum of passed Ints"</span>) {
* <span class="stReserved">val</span> futureSum: <span class="stType">Future[Int]</span> = addSoon(<span class="stLiteral">1</span>, <span class="stLiteral">2</span>)
* <span class="stLineComment">// You can map assertions onto a Future, then return</span>
* <span class="stLineComment">// the resulting Future[Assertion] to ScalaTest:</span>
* futureSum map { sum => assert(sum == <span class="stLiteral">3</span>) }
* }
* }
* <br/> <span class="stReserved">def</span> addNow(addends: <span class="stType">Int</span>*): <span class="stType">Int</span> = addends.sum
* <br/> describe(<span class="stQuotedString">"addNow"</span>) {
* it(<span class="stQuotedString">"will immediately compute a sum of passed Ints"</span>) {
* <span class="stReserved">val</span> sum: <span class="stType">Int</span> = addNow(<span class="stLiteral">1</span>, <span class="stLiteral">2</span>)
* <span class="stLineComment">// You can also write synchronous tests. The body</span>
* <span class="stLineComment">// must have result type Assertion:</span>
* assert(sum == <span class="stLiteral">3</span>)
* }
* }
* }
* </pre>
*
* <p>
* An <code>AsyncFunSpec</code> contains <em>describe clauses</em> and tests. You define a describe clause
* with <code>describe</code>, and a test with either <code>it</code> or <code>they</code>.
* <code>describe</code>, <code>it</code>, and <code>they</code> are methods, defined in
* <code>AsyncFunSpec</code>, which will be invoked
* by the primary constructor of <code>AddSpec</code>.
* A describe clause names, or gives more information about, the <em>subject</em> (class or other entity) you are specifying
* and testing. In the previous example, <code>"addSoon"</code> and <code>"addNow"</code> are
* the subjects under specification and test. With each test you provide a string (the <em>spec text</em>) that specifies
* one bit of behavior of the subject, and a block of code that tests that behavior.
* You place the spec text between the parentheses, followed by the test code between curly
* braces. The test code will be wrapped up as a function passed as a by-name parameter to
* <code>it</code> (or <code>they</code>), which will register the test for later execution.
* </p>
*
* <p>
* Note: the <code>they</code> method is intended for use when the subject is plural, for example:
* </p>
*
* <pre class="stHighlighted">
* describe(<span class="stQuotedString">"The combinators"</span>) {
* they(<span class="stQuotedString">"should be easy to learn"</span>) { succeed }
* they(<span class="stQuotedString">"should be efficient"</span>) { succeed }
* they(<span class="stQuotedString">"should do something cool"</span>) { succeed }
* }
* </pre>
*
* <p>
* Starting with version 3.0.0, ScalaTest assertions and matchers have result type <code>Assertion</code>.
* The result type of the first test in the example above, therefore, is <code>Future[Assertion]</code>.
* For clarity, here's the relevant code in a REPL session:
* </p>
*
* <pre class="stREPL">
* scala> import org.scalatest._
* import org.scalatest._
*
* scala> import Assertions._
* import Assertions._
*
* scala> import scala.concurrent.Future
* import scala.concurrent.Future
*
* scala> import scala.concurrent.ExecutionContext
* import scala.concurrent.ExecutionContext
*
* scala> implicit val executionContext = ExecutionContext.Implicits.global
* executionContext: scala.concurrent.ExecutionContextExecutor = scala.concurrent.impl.ExecutionContextImpl@26141c5b
*
* scala> def addSoon(addends: Int*): Future[Int] = Future { addends.sum }
* addSoon: (addends: Int*)scala.concurrent.Future[Int]
*
* scala> val futureSum: Future[Int] = addSoon(1, 2)
* futureSum: scala.concurrent.Future[Int] = scala.concurrent.impl.Promise$DefaultPromise@721f47b2
*
* scala> futureSum map { sum => assert(sum == 3) }
* res0: scala.concurrent.Future[org.scalatest.Assertion] = scala.concurrent.impl.Promise$DefaultPromise@3955cfcb
* </pre>
*
* <p>
* The second test has result type <code>Assertion</code>:
* </p>
*
* <pre class="stREPL">
* scala> def addNow(addends: Int*): Int = addends.sum
* addNow: (addends: Int*)Int
*
* scala> val sum: Int = addNow(1, 2)
* sum: Int = 3
*
* scala> assert(sum == 3)
* res1: org.scalatest.Assertion = Succeeded
* </pre>
*
* <p>
* When <code>AddSpec</code> is constructed, the second test will be implicitly converted to
* <code>Future[Assertion]</code> and registered. The implicit conversion is from <code>Assertion</code>
* to <code>Future[Assertion]</code>, so you must end synchronous tests in some ScalaTest assertion
* or matcher expression. If a test would not otherwise end in type <code>Assertion</code>, you can
* place <code>succeed</code> at the end of the test. <code>succeed</code>, a field in trait <code>Assertions</code>,
* returns the <code>Succeeded</code> singleton:
* </p>
*
* <pre class="stREPL">
* scala> succeed
* res2: org.scalatest.Assertion = Succeeded
* </pre>
*
* <p>
* Thus placing <code>succeed</code> at the end of a test body will satisfy the type checker:
* </p>
*
* <pre class="stHighlighted">
* it(<span class="stQuotedString">"will immediately compute a sum of passed Ints"</span>) {
* <span class="stReserved">val</span> sum: <span class="stType">Int</span> = addNow(<span class="stLiteral">1</span>, <span class="stLiteral">2</span>)
* assert(sum == <span class="stLiteral">3</span>)
* println(<span class="stQuotedString">"hi"</span>) <span class="stLineComment">// println has result type Unit</span>
* succeed <span class="stLineComment">// succeed has result type Assertion</span>
* }
* </pre>
*
* <p>
* An <code>AsyncFunSpec</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 with the <code>it</code> method while the <code>AsyncFunSpec</code> is
* in its registration phase. Any attempt to register a test after the <code>AsyncFunSpec</code> has
* entered its ready phase, <em>i.e.</em>, after <code>run</code> has been invoked on the <code>AsyncFunSpec</code>,
* will be met with a thrown <code>TestRegistrationClosedException</code>. The recommended style
* of using <code>AsyncFunSpec</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="asyncExecutionModel"></a><h2>Asynchronous execution model</h2>
*
* <p>
* <code>AsyncFunSpec</code> extends <a href="../AsyncTestSuite.html"><code>AsyncTestSuite</code></a>, which provides an
* implicit <code>scala.concurrent.ExecutionContext</code>
* named <code>executionContext</code>. This
* execution context is used by <code>AsyncFunSpec</code> to
* transform the <code>Future[Assertion]</code>s returned by each test
* into the <a href="../FutureOutcome.html"><code>FutureOutcome</code></a> returned by the <code>test</code> function
* passed to <code>withFixture</code>.
* This <code>ExecutionContext</code> is also intended to be used in the tests,
* including when you map assertions onto futures.
* </p>
*
* <p>
* On both the JVM and Scala.js, the default execution context provided by ScalaTest's asynchronous
* testing styles confines execution to a single thread per test. On JavaScript, where single-threaded
* execution is the only possibility, the default execution context is
* <code>scala.scalajs.concurrent.JSExecutionContext.Implicits.queue</code>. On the JVM,
* the default execution context is a <em>serial execution context</em> provided by ScalaTest itself.
* </p>
*
* <p>
* When ScalaTest's serial execution context is called upon to execute a task, that task is recorded
* in a queue for later execution. For example, one task that will be placed in this queue is the
* task that transforms the <code>Future[Assertion]</code> returned by an asynchronous test body
* to the <code>FutureOutcome</code> returned from the <code>test</code> function.
* Other tasks that will be queued are any transformations of, or callbacks registered on, <code>Future</code>s that occur
* in your test body, including any assertions you map onto <code>Future</code>s. Once the test body returns,
* the thread that executed the test body will execute the tasks in that queue one after another, in the order they
* were enqueued.
* </p>
*
* <p>
* ScalaTest provides its serial execution context as the default on the JVM for three reasons. First, most often
* running both tests and suites in parallel does not give a significant performance boost compared to
* just running suites in parallel. Thus parallel execution of <code>Future</code> transformations within
* individual tests is not generally needed for performance reasons.
* </p>
*
* <p>
* Second, if multiple threads are operating in the same suite
* concurrently, you'll need to make sure access to any mutable fixture objects by multiple threads is synchronized.
* Although access to mutable state along
* the same linear chain of <code>Future</code> transformations need not be synchronized,
* this does not hold true for callbacks, and in general it is easy to make a mistake. Simply put: synchronizing access to
* shared mutable state is difficult and error prone.
* Because ScalaTest's default execution context on the JVM confines execution of <code>Future</code> transformations
* and call backs to a single thread, you need not (by default) worry about synchronizing access to mutable state
* in your asynchronous-style tests.
* </p>
*
* <p>
* Third, asynchronous-style tests need not be complete when the test body returns, because the test body returns
* a <code>Future[Assertion]</code>. This <code>Future[Assertion]</code> will often represent a test that has not yet
* completed. As a result, when using a more traditional execution context backed by a thread-pool, you could
* potentially start many more tests executing concurrently than there are threads in the thread pool. The more
* concurrently execute tests you have competing for threads from the same limited thread pool, the more likely it
* will be that tests will intermitently fail due to timeouts.
* </p>
*
* <p>
* Using ScalaTest's serial execution context on the JVM will ensure the same thread that produced the <code>Future[Assertion]</code>
* returned from a test body is also used to execute any tasks given to the execution context while executing the test
* body—<em>and that thread will not be allowed to do anything else until the test completes.</em>
* If the serial execution context's task queue ever becomes empty while the <code>Future[Assertion]</code> returned by
* that test's body has not yet completed, the thread will <em>block</em> until another task for that test is enqueued. Although
* it may seem counter-intuitive, this blocking behavior means the total number of tests allowed to run concurrently will be limited
* to the total number of threads executing suites. This fact means you can tune the thread pool such that maximum performance
* is reached while avoiding (or at least, reducing the likelihood of) tests that fail due to timeouts because of thread competition.
* </p>
*
* <p>
* This thread confinement strategy does mean, however, that when you are using the default execution context on the JVM, you
* must be sure to <em>never block</em> in the test body waiting for a task to be completed by the
* execution context. If you block, your test will never complete. This kind of problem will be obvious, because the test will
* consistently hang every time you run it. (If a test is hanging, and you're not sure which one it is,
* enable <a href="Runner.scala#slowpokeNotifications">slowpoke notifications</a>.) If you really do
* want to block in your tests, you may wish to just use a
* traditional <a href="AnyFunSpec.html"><code>AnyFunSpec</code></a> with
* <a href="../concurrent/ScalaFutures.html"><code>ScalaFutures</code></a> instead. Alternatively, you could override
* the <code>executionContext</code> and use a traditional <code>ExecutionContext</code> backed by a thread pool. This
* will enable you to block in an asynchronous-style test on the JVM, but you'll need to worry about synchronizing access to
* shared mutable state.
* </p>
*
* <p>
* To use a different execution context, just override <code>executionContext</code>. For example, if you prefer to use
* the <code>runNow</code> execution context on Scala.js instead of the default <code>queue</code>, you would write:
* </p>
*
* <pre class="stHighlighted">
* <span class="stLineComment">// on Scala.js</span>
* <span class="stReserved">implicit</span> <span class="stReserved">override</span> <span class="stReserved">def</span> executionContext =
* org.scalatest.concurrent.TestExecutionContext.runNow
* </pre>
*
* <p>
* If you prefer on the JVM to use the global execution context, which is backed by a thread pool, instead of ScalaTest's default
* serial execution contex, which confines execution to a single thread, you would write:
* </p>
*
* <pre class="stHighlighted">
* <span class="stLineComment">// on the JVM (and also compiles on Scala.js, giving</span>
* <span class="stLineComment">// you the queue execution context)</span>
* <span class="stReserved">implicit</span> <span class="stReserved">override</span> <span class="stReserved">def</span> executionContext =
* scala.concurrent.ExecutionContext.Implicits.global
* </pre>
*
* <a name="serialAndParallel"></a><h2>Serial and parallel test execution</h2>
*
* <p>
* By default (unless you mix in <code>ParallelTestExecution</code>), tests in an <code>AsyncFunSpec</code> will be executed one after
* another, <em>i.e.</em>, serially. This is true whether those tests return <code>Assertion</code> or <code>Future[Assertion]</code>,
* no matter what threads are involved. This default behavior allows
* you to re-use a shared fixture, such as an external database that needs to be cleaned
* after each test, in multiple tests in async-style suites. This is implemented by registering each test, other than the first test, to run
* as a <em>continuation</em> after the previous test completes.
* </p>
*
* <p>
* If you want the tests of an <code>AsyncFunSpec</code> to be executed in parallel, you
* must mix in <code>ParallelTestExecution</code> and enable parallel execution of tests in your build.
* You enable parallel execution in <a href="../tools/Runner$.html"><code>Runner</code></a> with the <code>-P</code> command line flag.
* In the ScalaTest Maven Plugin, set <code>parallel</code> to <code>true</code>.
* In <code>sbt</code>, parallel execution is the default, but to be explicit you can write:
*
* <pre>
* parallelExecution in Test := true // the default in sbt
* </pre>
*
* On the JVM, if both <a href="../ParallelTestExecution.html"><code>ParallelTestExecution</code></a> is mixed in and
* parallel execution is enabled in the build, tests in an async-style suite will be started in parallel, using threads from
* the <a href="../Distributor"><code>Distributor</code></a>, and allowed to complete in parallel, using threads from the
* <code>executionContext</code>. If you are using ScalaTest's serial execution context, the JVM default, asynchronous tests will
* run in parallel very much like traditional (such as <a href="AnyFunSpec.html"><code>AnyFunSpec</code></a>) tests run in
* parallel: 1) Because <code>ParallelTestExecution</code> extends
* <code>OneInstancePerTest</code>, each test will run in its own instance of the test class, you need not worry about synchronizing
* access to mutable instance state shared by different tests in the same suite.
* 2) Because the serial execution context will confine the execution of each test to the single thread that executes the test body,
* you need not worry about synchronizing access to shared mutable state accessed by transformations and callbacks of <code>Future</code>s
* inside the test.
* </p>
*
* <p>
* If <a href="../ParallelTestExecution.html"><code>ParallelTestExecution</code></a> is mixed in but
* parallel execution of suites is <em>not</em> enabled, asynchronous tests on the JVM will be started sequentially, by the single thread
* that invoked <code>run</code>, but without waiting for one test to complete before the next test is started. As a result,
* asynchronous tests will be allowed to <em>complete</em> in parallel, using threads
* from the <code>executionContext</code>. If you are using the serial execution context, however, you'll see
* the same behavior you see when parallel execution is disabled and a traditional suite that mixes in <code>ParallelTestExecution</code>
* is executed: the tests will run sequentially. If you use an execution context backed by a thread-pool, such as <code>global</code>,
* however, even though tests will be started sequentially by one thread, they will be allowed to run concurrently using threads from the
* execution context's thread pool.
* </p>
*
* <p>
* The latter behavior is essentially what you'll see on Scala.js when you execute a suite that mixes in <code>ParallelTestExecution</code>.
* Because only one thread exists when running under JavaScript, you can't "enable parallel execution of suites." However, it may
* still be useful to run tests in parallel on Scala.js, because tests can invoke API calls that are truly asynchronous by calling into
* external APIs that take advantage of non-JavaScript threads. Thus on Scala.js, <code>ParallelTestExecution</code> allows asynchronous
* tests to run in parallel, even though they must be started sequentially. This may give you better performance when you are using API
* calls in your Scala.js tests that are truly asynchronous.
* </p>
*
* <a name="futuresAndExpectedExceptions"></a><h2>Futures and expected exceptions</h2>
*
* <p>
* If you need to test for expected exceptions in the context of futures, you can use the
* <code>recoverToSucceededIf</code> and <code>recoverToExceptionIf</code> methods of trait
* <a href="../RecoverMethods.html"><code>RecoverMethods</code></a>. Because this trait is mixed into
* supertrait <code>AsyncTestSuite</code>, both of these methods are
* available by default in an <code>AsyncFunSpec</code>.
* </p>
*
* <p>
* If you just want to ensure that a future fails with a particular exception type, and do
* not need to inspect the exception further, use <code>recoverToSucceededIf</code>:
* </p>
*
* <pre class="stHighlighted">
* recoverToSucceededIf[<span class="stType">IllegalStateException</span>] { <span class="stLineComment">// Result type: Future[Assertion]</span>
* emptyStackActor ? <span class="stType">Peek</span>
* }
* </pre>
*
* <p>
* The <code>recoverToSucceededIf</code> method performs a job similar to
* <a href="../Assertions.html#assertThrowsMethod"><code>assertThrows</code></a>, except
* in the context of a future. It transforms a <code>Future</code> of any type into a
* <code>Future[Assertion]</code> that succeeds only if the original future fails with the specified
* exception. Here's an example in the REPL:
* </p>
*
* <pre class="stREPL">
* scala> import org.scalatest.RecoverMethods._
* import org.scalatest.RecoverMethods._
*
* scala> import scala.concurrent.Future
* import scala.concurrent.Future
*
* scala> import scala.concurrent.ExecutionContext.Implicits.global
* import scala.concurrent.ExecutionContext.Implicits.global
*
* scala> recoverToSucceededIf[IllegalStateException] {
* | Future { throw new IllegalStateException }
* | }
* res0: scala.concurrent.Future[org.scalatest.Assertion] = ...
*
* scala> res0.value
* res1: Option[scala.util.Try[org.scalatest.Assertion]] = Some(Success(Succeeded))
* </pre>
*
* <p>
* Otherwise it fails with an error message similar to those given by <code>assertThrows</code>:
* </p>
*
* <pre class="stREPL">
* scala> recoverToSucceededIf[IllegalStateException] {
* | Future { throw new RuntimeException }
* | }
* res2: scala.concurrent.Future[org.scalatest.Assertion] = ...
*
* scala> res2.value
* res3: Option[scala.util.Try[org.scalatest.Assertion]] =
* Some(Failure(org.scalatest.exceptions.TestFailedException: Expected exception
* java.lang.IllegalStateException to be thrown, but java.lang.RuntimeException
* was thrown))
*
* scala> recoverToSucceededIf[IllegalStateException] {
* | Future { 42 }
* | }
* res4: scala.concurrent.Future[org.scalatest.Assertion] = ...
*
* scala> res4.value
* res5: Option[scala.util.Try[org.scalatest.Assertion]] =
* Some(Failure(org.scalatest.exceptions.TestFailedException: Expected exception
* java.lang.IllegalStateException to be thrown, but no exception was thrown))
* </pre>
*
* <p>
* The <code>recoverToExceptionIf</code> method differs from the <code>recoverToSucceededIf</code> in
* its behavior when the assertion succeeds: <code>recoverToSucceededIf</code> yields a <code>Future[Assertion]</code>,
* whereas <code>recoverToExceptionIf</code> yields a <code>Future[T]</code>, where <code>T</code> is the
* expected exception type.
* </p>
*
* <pre class="stHighlighted">
* recoverToExceptionIf[<span class="stType">IllegalStateException</span>] { <span class="stLineComment">// Result type: Future[IllegalStateException]</span>
* emptyStackActor ? <span class="stType">Peek</span>
* }
* </pre>
*
* <p>
* In other words, <code>recoverToExpectionIf</code> is to
* <a href="../Assertions.html#interceptMethod"><code>intercept</code></a> as
* <code>recovertToSucceededIf</code> is to <a href="../Assertions.html#assertThrowsMethod"><code>assertThrows</code></a>. The first one allows you to
* perform further assertions on the expected exception. The second one gives you a result type that will satisfy the type checker
* at the end of the test body. Here's an example showing <code>recoverToExceptionIf</code> in the REPL:
* </p>
*
* <pre class="stREPL">
* scala> val futureEx =
* | recoverToExceptionIf[IllegalStateException] {
* | Future { throw new IllegalStateException("hello") }
* | }
* futureEx: scala.concurrent.Future[IllegalStateException] = ...
*
* scala> futureEx.value
* res6: Option[scala.util.Try[IllegalStateException]] =
* Some(Success(java.lang.IllegalStateException: hello))
*
* scala> futureEx map { ex => assert(ex.getMessage == "world") }
* res7: scala.concurrent.Future[org.scalatest.Assertion] = ...
*
* scala> res7.value
* res8: Option[scala.util.Try[org.scalatest.Assertion]] =
* Some(Failure(org.scalatest.exceptions.TestFailedException: "[hello]" did not equal "[world]"))
* </pre>
*
* <a name="ignoredTests"></a><h2>Ignored tests</h2>
*
* <p>
* To support the common use case of temporarily disabling a test, with the
* good intention of resurrecting the test at a later time, <code>AsyncFunSpec</code> provides registration
* methods that start with <code>ignore</code> instead of <code>it</code> or <code>they</code>. For example, to temporarily
* disable the test with the text <code>"will eventually compute a sum of passed Ints"</code>, just
* change “<code>it</code>” into “<code>ignore</code>,” like this:
* </p>
*
* <pre class="stHighlighted">
* <span class="stReserved">package</span> org.scalatest.examples.asyncfunspec.ignore
* <br/><span class="stReserved">import</span> org.scalatest.funspec.AsyncFunSpec
* <span class="stReserved">import</span> scala.concurrent.Future
* <br/><span class="stReserved">class</span> <span class="stType">AddSpec</span> <span class="stReserved">extends</span> <span class="stType">AsyncFunSpec</span> {
* <br/> <span class="stReserved">def</span> addSoon(addends: <span class="stType">Int</span>*): <span class="stType">Future[Int]</span> = <span class="stType">Future</span> { addends.sum }
* <br/> describe(<span class="stQuotedString">"addSoon"</span>) {
* ignore(<span class="stQuotedString">"will eventually compute a sum of passed Ints"</span>) {
* <span class="stReserved">val</span> futureSum: <span class="stType">Future[Int]</span> = addSoon(<span class="stLiteral">1</span>, <span class="stLiteral">2</span>)
* <span class="stLineComment">// You can map assertions onto a Future, then return</span>
* <span class="stLineComment">// the resulting Future[Assertion] to ScalaTest:</span>
* futureSum map { sum => assert(sum == <span class="stLiteral">3</span>) }
* }
* }
* <br/> <span class="stReserved">def</span> addNow(addends: <span class="stType">Int</span>*): <span class="stType">Int</span> = addends.sum
* <br/> describe(<span class="stQuotedString">"addNow"</span>) {
* it(<span class="stQuotedString">"will immediately compute a sum of passed Ints"</span>) {
* <span class="stReserved">val</span> sum: <span class="stType">Int</span> = addNow(<span class="stLiteral">1</span>, <span class="stLiteral">2</span>)
* <span class="stLineComment">// You can also write synchronous tests. The body</span>
* <span class="stLineComment">// must have result type Assertion:</span>
* assert(sum == <span class="stLiteral">3</span>)
* }
* }
* }
* </pre>
*
* <p>
* If you run this version of <code>AddSpec</code> with:
* </p>
*
* <pre class="stREPL">
* scala> org.scalatest.run(new AddSpec)
* </pre>
*
* <p>
* It will run only the second test and report that the first test was ignored:
* </p>
*
* <pre class="stREPL">
* <span class="stGreen">AddSpec:</span>
* <span class="stGreen">addSoon</span>
* <span class="stYellow">- will eventually compute a sum of passed Ints !!! IGNORED !!!</span>
* <span class="stGreen">addNow</span>
* <span class="stGreen">- will immediately compute a sum of passed Ints</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.asyncfunspec.ignoreall
* <br/><span class="stReserved">import</span> org.scalatest.funspec.AsyncFunSpec
* <span class="stReserved">import</span> scala.concurrent.Future
* <span class="stReserved">import</span> org.scalatest.Ignore
* <br/>@<span class="stType">Ignore</span>
* <span class="stReserved">class</span> <span class="stType">AddSpec</span> <span class="stReserved">extends</span> <span class="stType">AsyncFunSpec</span> {
* <br/> <span class="stReserved">def</span> addSoon(addends: <span class="stType">Int</span>*): <span class="stType">Future[Int]</span> = <span class="stType">Future</span> { addends.sum }
* <br/> describe(<span class="stQuotedString">"addSoon"</span>) {
* it(<span class="stQuotedString">"will eventually compute a sum of passed Ints"</span>) {
* <span class="stReserved">val</span> futureSum: <span class="stType">Future[Int]</span> = addSoon(<span class="stLiteral">1</span>, <span class="stLiteral">2</span>)
* <span class="stLineComment">// You can map assertions onto a Future, then return</span>
* <span class="stLineComment">// the resulting Future[Assertion] to ScalaTest:</span>
* futureSum map { sum => assert(sum == <span class="stLiteral">3</span>) }
* }
* }
* <br/> <span class="stReserved">def</span> addNow(addends: <span class="stType">Int</span>*): <span class="stType">Int</span> = addends.sum
* <br/> describe(<span class="stQuotedString">"addNow"</span>) {
* it(<span class="stQuotedString">"will immediately compute a sum of passed Ints"</span>) {
* <span class="stReserved">val</span> sum: <span class="stType">Int</span> = addNow(<span class="stLiteral">1</span>, <span class="stLiteral">2</span>)
* <span class="stLineComment">// You can also write synchronous tests. The body</span>
* <span class="stLineComment">// must have result type Assertion:</span>
* assert(sum == <span class="stLiteral">3</span>)
* }
* }
* }
* </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>AddSpec</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>AddSpec</code> in the Scala interpreter, you'll see:
* </p>
*
* <pre class="stREPL">
* <span class="stGreen">AddSpec:</span>
* <span class="stGreen">addSoon</span>
* <span class="stYellow">- will eventually compute a sum of passed Ints !!! IGNORED !!!</span>
* <span class="stGreen">addNow</span>
* <span class="stYellow">- will immediately compute a sum of passed Ints !!! 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>
*
* <p>
* If you want to ignore all tests of a suite on Scala.js, where annotations can't be inspected at runtime, you'll need
* to change <code>it</code> to <code>ignore</code> at each test site. To make a suite non-discoverable on Scala.js, ensure it
* does not declare a public no-arg constructor. You can either declare a public constructor that takes one or more
* arguments, or make the no-arg constructor non-public. Because this technique will also make the suite non-discoverable
* on the JVM, it is a good approach for suites you want to run (but not be discoverable) on both Scala.js and the JVM.
* </p>
*
* <a name="informers"></a><h2>Informers</h2>
*
* <p>
* One of the parameters to <code>AsyncFunSpec</code>'s <code>run</code> method is a <code>Reporter</code>, 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>AsyncFunSpec</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 <code>Informer</code> 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 one of its <code>apply</code> methods.
* The <code>Informer</code> will then pass the information to the <code>Reporter</code> via an <code>InfoProvided</code> event.
* Here's an example in which the <code>Informer</code> returned by <code>info</code> is used implicitly by the
* <code>Given</code>, <code>When</code>, and <code>Then</code> methods of trait <code>GivenWhenThen</code>:
* </p>
*
* <pre class="stHighlighted">
* <span class="stReserved">package</span> org.scalatest.examples.asyncfunspec.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">funspec.AsyncFunSpec</span> <span class="stReserved">with</span> <span class="stType">GivenWhenThen</span> {
* <br/> describe(<span class="stQuotedString">"A mutable Set"</span>) {
* it(<span class="stQuotedString">"should allow an element to be added"</span>) {
* <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>)
* succeed
* }
* }
* }
* </pre>
*
* If you run this <code>AsyncFunSpec</code> from the interpreter, you will see the following output:
*
* <pre class="stREPL">
* scala> org.scalatest.run(new SetSpec)
* <span class="stGreen">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>AsyncFunSpec</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>AsyncFunSpec</code> that uses <code>markup</code>:
* </p>
*
* <pre class="stHighlighted">
* <span class="stReserved">package</span> org.scalatest.examples.asyncfunspec.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">funspec.AsyncFunSpec</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/> describe(<span class="stQuotedString">"A mutable Set"</span>) {
* it(<span class="stQuotedString">"should allow an element to be added"</span>) {
* <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>)
* succeed
* }
* }
* }
* </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/funSpec.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.asyncfunspec.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">funspec.AsyncFunSpec</span> {
* <br/> describe(<span class="stQuotedString">"A mutable Set"</span>) {
* it(<span class="stQuotedString">"should allow an element to be added"</span>) {
* <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. At the end of the test,
* it can call method <code>pending</code>, which will cause it to complete abruptly with <code>TestPendingException</code>.
* </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, has not yet been implemented. Here's an example:
* </p>
*
* <pre class="stHighlighted">
* <span class="stReserved">package</span> org.scalatest.examples.asyncfunspec.pending
* <br/><span class="stReserved">import</span> org.scalatest.funspec.AsyncFunSpec
* <span class="stReserved">import</span> scala.concurrent.Future
* <br/><span class="stReserved">class</span> <span class="stType">AddSpec</span> <span class="stReserved">extends</span> <span class="stType">AsyncFunSpec</span> {
* <br/> <span class="stReserved">def</span> addSoon(addends: <span class="stType">Int</span>*): <span class="stType">Future[Int]</span> = <span class="stType">Future</span> { addends.sum }
* <br/> describe(<span class="stQuotedString">"addSoon"</span>) {
* it(<span class="stQuotedString">"will eventually compute a sum of passed Ints"</span>)(pending)
* }
* <br/> <span class="stReserved">def</span> addNow(addends: <span class="stType">Int</span>*): <span class="stType">Int</span> = addends.sum
* <br/> describe(<span class="stQuotedString">"addNow"</span>) {
* it(<span class="stQuotedString">"will immediately compute a sum of passed Ints"</span>) {
* <span class="stReserved">val</span> sum: <span class="stType">Int</span> = addNow(<span class="stLiteral">1</span>, <span class="stLiteral">2</span>)
* <span class="stLineComment">// You can also write synchronous tests. The body</span>
* <span class="stLineComment">// must have result type Assertion:</span>
* assert(sum == <span class="stLiteral">3</span>)
* }
* }
* }
* </pre>
*
* <p>
* (Note: "<code>(pending)</code>" is the body of the test. Thus the test contains just one statement, an invocation
* of the <code>pending</code> method, which throws <code>TestPendingException</code>.)
* If you run this version of <code>AddSpec</code> with:
* </p>
*
* <pre class="stREPL">
* scala> org.scalatest.run(new AddSpec)
* </pre>
*
* <p>
* It will run both tests, but report that first test is pending. You'll see:
* </p>
*
* <pre class="stREPL">
* <span class="stGreen">AddSpec:</span>
* <span class="stGreen">addSoon</span>
* <span class="stYellow">- will eventually compute a sum of passed Ints (pending)</span>
* <span class="stGreen">addNow</span>
* <span class="stGreen">- will immediately compute a sum of passed Ints</span>
* </pre>
*
* <p>
* One difference between an ignored test and a pending one is that an ignored test is intended to be used during
* 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>.
* </p>
*
* <a name="taggingTests"></a><h2>Tagging tests</h2>
*
* <p>
* An <code>AsyncFunSpec</code>'s tests may be classified into groups by <em>tagging</em> them with string names.
* As with any suite, when executing an <code>AsyncFunSpec</code>, groups of tests can
* optionally be included and/or excluded. To tag an <code>AsyncFunSpec</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>AsyncFunSpec</code>s like this:
* </p>
*
* <pre class="stHighlighted">
* <span class="stReserved">package</span> org.scalatest.examples.asyncfunspec.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>AsyncFunSpec</code> tests into groups with tags like this:
* </p>
*
* <pre class="stHighlighted">
* <span class="stReserved">import</span> org.scalatest.funspec.AsyncFunSpec
* <span class="stReserved">import</span> org.scalatest.tagobjects.Slow
* <span class="stReserved">import</span> scala.concurrent.Future
* <br/><span class="stReserved">class</span> <span class="stType">AddSpec</span> <span class="stReserved">extends</span> <span class="stType">AsyncFunSpec</span> {
* <br/> <span class="stReserved">def</span> addSoon(addends: <span class="stType">Int</span>*): <span class="stType">Future[Int]</span> = <span class="stType">Future</span> { addends.sum }
* <br/> describe(<span class="stQuotedString">"addSoon"</span>) {
* it(<span class="stQuotedString">"will eventually compute a sum of passed Ints"</span>, <span class="stType">Slow</span>) {
* <span class="stReserved">val</span> futureSum: <span class="stType">Future[Int]</span> = addSoon(<span class="stLiteral">1</span>, <span class="stLiteral">2</span>)
* <span class="stLineComment">// You can map assertions onto a Future, then return</span>
* <span class="stLineComment">// the resulting Future[Assertion] to ScalaTest:</span>
* futureSum map { sum => assert(sum == <span class="stLiteral">3</span>) }
* }
* }
* <br/> <span class="stReserved">def</span> addNow(addends: <span class="stType">Int</span>*): <span class="stType">Int</span> = addends.sum
* <br/> describe(<span class="stQuotedString">"addNow"</span>) {
* it(<span class="stQuotedString">"will immediately compute a sum of passed Ints"</span>,
* <span class="stType">Slow</span>, <span class="stType">DbTest</span>) {
* <br/> <span class="stReserved">val</span> sum: <span class="stType">Int</span> = addNow(<span class="stLiteral">1</span>, <span class="stLiteral">2</span>)
* <span class="stLineComment">// You can also write synchronous tests. The body</span>
* <span class="stLineComment">// must have result type Assertion:</span>
* assert(sum == <span class="stLiteral">3</span>)
* }
* }
* }
* </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 <code>Filter</code>, 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 an <code>AsyncFunSpec</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 in async styles:
* </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 eliminate the need to
* synchronize access to shared mutable state on the JVM.
* </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="#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>