/
GenGen.scala
2732 lines (2380 loc) · 95.7 KB
/
GenGen.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-2011 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.
*/
import java.io.File
import java.io.FileWriter
import java.io.BufferedWriter
import java.util.Calendar
import scala.collection.JavaConversions._
object GenGen {
val generatorSource = new File("GenGen.scala")
val scaladocForTableFor1VerbatimString = """
/**
* A table with 1 column.
*
* <p>
* For an overview of using tables, see the documentation for trait
* <a href="TableDrivenPropertyChecks.html">TableDrivenPropertyChecks</a>.
* </p>
*
* <p>
* This table is a sequence of objects, where each object represents one row of the (one-column) table.
* This table also carries with it a <em>heading</em> tuple that gives a string name to the
* lone column of the table.
* </p>
*
* <p>
* A handy way to create a <code>TableFor1</code> is via an <code>apply</code> factory method in the <code>Table</code>
* singleton object provided by the <code>Tables</code> trait. Here's an example:
* </p>
*
* <pre class="stHighlight">
* val examples =
* Table(
* "a",
* 0,
* 1,
* 2,
* 3,
* 4,
* 5,
* 6,
* 7,
* 8,
* 9
* )
* </pre>
*
* <p>
* Because you supplied a list of non-tuple objects, the type you'll get back will be a <code>TableFor1</code>.
* </p>
*
* <p>
* The table provides an <code>apply</code> method that takes a function with a parameter list that matches
* the type of the objects contained in this table. The <code>apply</code> method will invoke the
* function with the object in each row passed as the lone argument, in ascending order by index. (<em>I.e.</em>,
* the zeroth object is checked first, then the object with index 1, then index 2, and so on until all the rows
* have been checked (or until a failure occurs). The function represents a property of the code under test
* that should succeed for every row of the table. If the function returns normally, that indicates the property
* check succeeded for that row. If the function completes abruptly with an exception, that indicates the
* property check failed and the <code>apply</code> method will complete abruptly with a
* <code>TableDrivenPropertyCheckFailedException</code> that wraps the exception thrown by the supplied property function.
* </p>
*
* <p>
* The usual way you'd invoke the <code>apply</code> method that checks a property is via a <code>forAll</code> method
* provided by trait <code>TableDrivenPropertyChecks</code>. The <code>forAll</code> method takes a <code>TableFor1</code> as its
* first argument, then in a curried argument list takes the property check function. It invokes <code>apply</code> on
* the <code>TableFor1</code>, passing in the property check function. Here's an example:
* </p>
*
* <pre class="stHighlight">
* forAll (examples) { (a) =>
* a should equal (a * 1)
* }
* </pre>
*
* <p>
* Because <code>TableFor1</code> is a <code>Seq[(A)]</code>, you can use it as a <code>Seq</code>. For example, here's how
* you could get a sequence of <a href="../Outcome.html"><code>Outcome</code></a>s for each row of the table, indicating whether a property check succeeded or failed
* on each row of the table:
* </p>
*
* <pre class="stHighlight">
* for (row <- examples) yield {
* outcomeOf { row._1 should not equal (7) }
* }
* </pre>
*
* <p>
* Note: the <code>outcomeOf</code> method, contained in the <code>OutcomeOf</code> trait, will execute the supplied code (a by-name parameter) and
* transform it to an <code>Outcome</code>. If no exception is thrown by the code, <code>outcomeOf</code> will result in a
* <a href="../Succeeded\$.html"><code>Succeeded</code></a>, indicating the "property check"
* succeeded. If the supplied code completes abruptly in an exception that would normally cause a test to fail, <code>outcomeOf</code> will result in
* in a <a href="../Failed.html"><code>Failed</code></a> instance containing that exception. For example, the previous for expression would give you:
* </p>
*
* <pre class="stHighlight">
* Vector(Succeeded, Succeeded, Succeeded, Succeeded, Succeeded, Succeeded, Succeeded,
* Failed(org.scalatest.TestFailedException: 7 equaled 7), Succeeded, Succeeded)
* </pre>
*
* <p>
* This shows that all the property checks succeeded, except for the one at index 7.
* </p>
*
* <p>
* One other way to use a <code>TableFor1</code> is to test subsequent return values
* of a stateful function. Imagine, for example, you had an object named <code>FiboGen</code>
* whose <code>next</code> method returned the <em>next</em> fibonacci number, where next
* means the next number in the series following the number previously returned by <code>next</code>.
* So the first time <code>next</code> was called, it would return 0. The next time it was called
* it would return 1. Then 1. Then 2. Then 3, and so on. <code>FiboGen</code> would need to
* be stateful, because it has to remember where it is in the series. In such a situation,
* you could create a <code>TableFor1</code> (a table with one column, which you could alternatively
* think of as one row), in which each row represents
* the next value you expect.
* </p>
*
* <pre class="stHighlight">
* val first14FiboNums =
* Table("n", 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233)
* </pre>
*
* <p>
* Then in your <code>forAll</code> simply call the function and compare it with the
* expected return value, like this:
* </p>
*
* <pre class="stHighlight">
* forAll (first14FiboNums) { n =>
* FiboGen.next should equal (n)
* }
* </pre>
*
* @param heading a string name for the lone column of this table
* @param rows a variable length parameter list of objects containing the data of this table
*
* @author Bill Venners
*/
"""
val copyrightTemplate = """/*
* Copyright 2001-$year$ 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
package prop
"""
val propertyCheckPreamble = """
import org.scalacheck.Arbitrary
import org.scalacheck.Shrink
import org.scalacheck.Prop
import org.scalacheck.Gen
import org.scalacheck.Prop._
import org.scalatest.exceptions.DiscardedEvaluationException
import org.scalatest.enablers.CheckerAsserting
import org.scalactic._
import org.scalatest.prop.Whenever
/**
* Trait containing methods that faciliate property checks against generated data using ScalaCheck.
*
* <p>
* This trait contains <code>forAll</code> methods that provide various ways to check properties using
* generated data. Use of this trait requires that ScalaCheck be on the class path when you compile and run your tests.
* It also contains a <code>wherever</code> method that can be used to indicate a property need only hold whenever
* some condition is true.
* </p>
*
* <p>
* For an example of trait <code>GeneratorDrivenPropertyChecks</code> in action, imagine you want to test this <code>Fraction</code> class:
* </p>
*
* <pre class="stHighlight">
* class Fraction(n: Int, d: Int) {
*
* require(d != 0)
* require(d != Integer.MIN_VALUE)
* require(n != Integer.MIN_VALUE)
*
* val numer = if (d < 0) -1 * n else n
* val denom = d.abs
*
* override def toString = numer + " / " + denom
* }
* </pre>
*
* <p>
* To test the behavior of <code>Fraction</code>, you could mix in or import the members of <code>GeneratorDrivenPropertyChecks</code>
* (and <code>Matchers</code>) and check a property using a <code>forAll</code> method, like this:
* </p>
*
* <pre class="stHighlight">
* forAll { (n: Int, d: Int) =>
*
* whenever (d != 0 && d != Integer.MIN_VALUE
* && n != Integer.MIN_VALUE) {
*
* val f = new Fraction(n, d)
*
* if (n < 0 && d < 0 || n > 0 && d > 0)
* f.numer should be > 0
* else if (n != 0)
* f.numer should be < 0
* else
* f.numer should be === 0
*
* f.denom should be > 0
* }
* }
* </pre>
*
* <p>
* Trait <code>GeneratorDrivenPropertyChecks</code> provides overloaded <code>forAll</code> methods
* that allow you to check properties using the data provided by a ScalaCheck generator. The simplest form
* of <code>forAll</code> method takes two parameter lists, the second of which is implicit. The first parameter list
* is a "property" function with one to six parameters. An implicit <code>Arbitrary</code> generator and <code>Shrink</code> object needs to be supplied for
* The <code>forAll</code> method will pass each row of data to
* each parameter type. ScalaCheck provides many implicit <code>Arbitrary</code> generators for common types such as
* <code>Int</code>, <code>String</code>, <code>List[Float]</code>, <em>etc.</em>, in its <code>org.scalacheck.Arbitrary</code> companion
* object. So long as you use types for which ScalaCheck already provides implicit <code>Arbitrary</code> generators, you needn't
* worry about them. Same for <code>Shrink</code> objects, which are provided by ScalaCheck's <code>org.scalacheck.Shrink</code> companion
* object. Most often you can simply pass a property function to <code>forAll</code>, and the compiler will grab the implicit
* values provided by ScalaCheck.
* </p>
*
* <p>
* The <code>forAll</code> methods use the supplied <code>Arbitrary</code> generators to generate example
* arguments and pass them to the property function, and
* generate a <code>GeneratorDrivenPropertyCheckFailedException</code> if the function
* completes abruptly for any exception that would <a href="../Suite.html#errorHandling">normally cause</a> a test to
* fail in ScalaTest other than <code>DiscardedEvaluationException</code>. An
* <code>DiscardedEvaluationException</code>,
* which is thrown by the <code>whenever</code> method (defined in trait <code>Whenever</code>, which this trait extends) to indicate
* a condition required by the property function is not met by a row
* of passed data, will simply cause <code>forAll</code> to discard that row of data.
* </p>
*
* <a name="supplyingArgumentNames"></a><h2>Supplying argument names</h2>
*
* <p>
* You can optionally specify string names for the arguments passed to a property function, which will be used
* in any error message when describing the argument values that caused the failure. To supply the names, place them in a comma separated list
* in parentheses after <code>forAll</code> before the property function (a curried form of <code>forAll</code>). Here's
* an example:
* </p>
*
* <pre class="stHighlight">
* forAll ("a", "b") { (a: String, b: String) =>
* a.length + b.length should equal ((a + b).length + 1) // Should fail
* }
* </pre>
*
* <p>
* When this fails, you'll see an error message that includes this:
* </p>
*
* <pre>
* Occurred when passed generated values (
* a = "",
* b = ""
* )
* </pre>
*
* <p>
* When you don't supply argument names, the error message will say <code>arg0</code>, <code>arg1</code>, <em>etc.</em>.
* For example, this property check:
* </p>
*
* <pre class="stHighlight">
* forAll { (a: String, b: String) =>
* a.length + b.length should equal ((a + b).length + 1) // Should fail
* }
* </pre>
*
* <p>
* Will fail with an error message that includes:
* </p>
*
* <pre>
* Occurred when passed generated values (
* arg0 = "",
* arg1 = ""
* )
* </pre>
*
* <a name="supplyingGenerators"></a><h2>Supplying generators</h2>
*
* <p>
* ScalaCheck provides a nice library of compositors that makes it easy to create your own custom generators. If you
* want to supply custom generators to a property check, place them in parentheses after <code>forAll</code>, before
* the property check function (a curried form of <code>forAll</code>).
* </p>
*
* <p>
* For example, to create a generator of even integers between (and including) -2000 and 2000, you could write this:
* </p>
*
* <pre class="stHighlight">
* import org.scalacheck.Gen
*
* val evenInts = for (n <- Gen.choose(-1000, 1000)) yield 2 * n
* </pre>
*
* <p>
* Given this generator, you could use it on a property check like this:
* </p>
*
* <pre class="stHighlight">
* forAll (evenInts) { (n) => n % 2 should equal (0) }
* </pre>
*
* <p>
* Custom generators are necessary when you want to pass data types not supported by ScalaCheck's arbitrary generators,
* but are also useful when some of the values in the full range for the passed types are not valid. For such values you
* would use a <code>whenever</code> clause. In the <code>Fraction</code> class shown above, neither the passed numerator or
* denominator can be <code>Integer.MIN_VALUE</code>, and the passed denominator cannot be zero. This shows up in the
* <code>whenever</code> clause like this:
* </p>
*
* <pre class="stHighlight">
* whenever (d != 0 && d != Integer.MIN_VALUE
* && n != Integer.MIN_VALUE) { ...
* </pre>
*
* <p>
* You could in addition define generators for the numerator and denominator that only produce valid values, like this:
* </p>
*
* <pre class="stHighlight">
* val validNumers =
* for (n <- Gen.choose(Integer.MIN_VALUE + 1, Integer.MAX_VALUE)) yield n
* val validDenoms =
* for (d <- validNumers if d != 0) yield d
* </pre>
*
* <p>
* You could then use them in the property check like this:
* </p>
*
* <pre class="stHighlight">
* forAll (validNumers, validDenoms) { (n: Int, d: Int) =>
*
* whenever (d != 0 && d != Integer.MIN_VALUE
* && n != Integer.MIN_VALUE) {
*
* val f = new Fraction(n, d)
*
* if (n < 0 && d < 0 || n > 0 && d > 0)
* f.numer should be > 0
* else if (n != 0)
* f.numer should be < 0
* else
* f.numer should be === 0
*
* f.denom should be > 0
* }
* }
* </pre>
*
* <p>
* Note that even if you use generators that don't produce the invalid values, you still need the
* <code>whenever</code> clause. The reason is that once a property fails, ScalaCheck will try and shrink
* the values to the smallest values that still cause the property to fail. During this shrinking process ScalaCheck
* may pass invalid values. The <code>whenever</code> clause is still needed to guard against those values. (The
* <code>whenever</code> clause also clarifies to readers of the code exactly what the property is in a succinct
* way, without requiring that they find and understand the generator definitions.)
* </p>
*
* <a name="supplyingGeneratorsAndArgNames"></a><h2>Supplying both generators and argument names</h2>
*
* <p>
* If you want to supply both generators and named arguments, you can do so by providing a list of <code>(<generator>, <name>)</code> pairs
* in parentheses after <code>forAll</code>, before the property function. Here's an example:
* </p>
*
* <pre class="stHighlight">
* forAll ((validNumers, "n"), (validDenoms, "d")) { (n: Int, d: Int) =>
*
* whenever (d != 0 && d != Integer.MIN_VALUE
* && n != Integer.MIN_VALUE) {
*
* val f = new Fraction(n, d)
*
* if (n < 0 && d < 0 || n > 0 && d > 0)
* f.numer should be > 0
* else if (n != 0)
* f.numer should be < 0
* else
* f.numer should be === 0
*
* f.denom should be > 0
* }
* }
* </pre>
*
* <p>
* Were this property check to fail, it would mention the names n and d in the error message, like this:
* </p>
*
* <pre>
* Occurred when passed generated values (
* n = 17,
* d = 21
* )
* </pre>
*
* <a name="propCheckConfig"></a><h2>Property check configuration</h2>
*
* <p>
* The property checks performed by the <code>forAll</code> methods of this trait can be flexibly configured via the services
* provided by supertrait <code>Configuration</code>. The five configuration parameters for property checks along with their
* default values and meanings are described in the following table:
* </p>
*
* <table style="border-collapse: collapse; border: 1px solid black">
* <tr>
* <th style="background-color: #CCCCCC; border-width: 1px; padding: 3px; text-align: center; border: 1px solid black">
* <strong>Configuration Parameter</strong>
* </th>
* <th style="background-color: #CCCCCC; border-width: 1px; padding: 3px; text-align: center; border: 1px solid black">
* <strong>Default Value</strong>
* </th>
* <th style="background-color: #CCCCCC; border-width: 1px; padding: 3px; text-align: center; border: 1px solid black">
* <strong>Meaning</strong>
* </th>
* </tr>
* <tr>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: center">
* minSuccessful
* </td>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: center">
* 100
* </td>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: left">
* the minimum number of successful property evaluations required for the property to pass
* </td>
* </tr>
* <tr>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: center">
* maxDiscarded
* </td>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: center">
* 500
* </td>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: left">
* the maximum number of discarded property evaluations allowed during a property check
* </td>
* </tr>
* <tr>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: center">
* minSize
* </td>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: center">
* 0
* </td>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: left">
* the minimum size parameter to provide to ScalaCheck, which it will use when generating objects for which size matters (such as strings or lists)
* </td>
* </tr>
* <tr>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: center">
* maxSize
* </td>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: center">
* 100
* </td>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: left">
* the maximum size parameter to provide to ScalaCheck, which it will use when generating objects for which size matters (such as strings or lists)
* </td>
* </tr>
* <tr>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: center">
* workers
* </td>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: center">
* 1
* </td>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: left">
* specifies the number of worker threads to use during property evaluation
* </td>
* </tr>
* </table>
*
* <p>
* The <code>forAll</code> methods of trait <code>GeneratorDrivenPropertyChecks</code> each take a <code>PropertyCheckConfiguration</code>
* object as an implicit parameter. This object provides values for each of the five configuration parameters. Trait <code>Configuration</code>
* provides an implicit <code>val</code> named <code>generatorDrivenConfig</code> with each configuration parameter set to its default value.
* If you want to set one or more configuration parameters to a different value for all property checks in a suite you can override this
* val (or hide it, for example, if you are importing the members of the <code>GeneratorDrivenPropertyChecks</code> companion object rather
* than mixing in the trait.) For example, if
* you want all parameters at their defaults except for <code>minSize</code> and <code>maxSize</code>, you can override
* <code>generatorDrivenConfig</code>, like this:
*
* <pre class="stHighlight">
* implicit override val generatorDrivenConfig =
* PropertyCheckConfiguration(minSize = 10, sizeRange = 10)
* </pre>
*
* <p>
* Or, hide it by declaring a variable of the same name in whatever scope you want the changed values to be in effect:
* </p>
*
* <pre class="stHighlight">
* implicit val generatorDrivenConfig =
* PropertyCheckConfiguration(minSize = 10, sizeRange = 10)
* </pre>
*
* <p>
* In addition to taking a <code>PropertyCheckConfiguration</code> object as an implicit parameter, the <code>forAll</code> methods of trait
* <code>GeneratorDrivenPropertyChecks</code> also take a variable length argument list of <code>PropertyCheckConfigParam</code>
* objects that you can use to override the values provided by the implicit <code>PropertyCheckConfiguration</code> for a single <code>forAll</code>
* invocation. For example, if you want to set <code>minSuccessful</code> to 500 for just one particular <code>forAll</code> invocation,
* you can do so like this:
* </p>
*
* <pre class="stHighlight">
* forAll (minSuccessful(500)) { (n: Int, d: Int) => ...
* </pre>
*
* <p>
* This invocation of <code>forAll</code> will use 500 for <code>minSuccessful</code> and whatever values are specified by the
* implicitly passed <code>PropertyCheckConfiguration</code> object for the other configuration parameters.
* If you want to set multiple configuration parameters in this way, just list them separated by commas:
* </p>
*
* <pre class="stHighlight">
* forAll (minSuccessful(500), maxDiscardedFactor(0.6)) { (n: Int, d: Int) => ...
* </pre>
*
* <p>
* If you are using an overloaded form of <code>forAll</code> that already takes an initial parameter list, just
* add the configuration parameters after the list of generators, names, or generator/name pairs, as in:
* </p>
*
* <pre class="stHighlight">
* // If providing argument names
* forAll ("n", "d", minSuccessful(500), maxDiscarded(300)) {
* (n: Int, d: Int) => ...
*
* // If providing generators
* forAll (validNumers, validDenoms, minSuccessful(500), maxDiscarded(300)) {
* (n: Int, d: Int) => ...
*
* // If providing (<generators>, <name>) pairs
* forAll ((validNumers, "n"), (validDenoms, "d"), minSuccessful(500), maxDiscarded(300)) {
* (n: Int, d: Int) => ...
* </pre>
*
* <p>
* For more information, see the documentation for supertrait <a href="Configuration.html"><code>Configuration</code></a>.
* </p>
*
* @author Bill Venners
*/
@deprecated("GeneratorDrivenPropertyChecks has been moved from org.scalatest.prop to org.scalatestplus.scalacheck and renamed as ScalaCheckDrivenPropertyChecks. Please update your imports, as this deprecated type alias will be removed in a future version of ScalaTest.")
trait GeneratorDrivenPropertyChecks extends Whenever with ScalaCheckConfiguration {
/**
* Performs a property check by applying the specified property check function to arguments
* supplied by implicitly passed generators, modifying the values in the implicitly passed
* <code>PropertyGenConfig</code> object with explicitly passed parameter values.
*
* <p>
* This method creates a <code>ConfiguredPropertyCheck</code> object that has six overloaded apply methods
* that take a function. Thus it is used with functions of all six arities.
* Here are some examples:
* </p>
*
* <pre class="stHighlight">
* forAll (minSize(1), maxSize(10)) { (a: String) =>
* a.length should equal ((a).length)
* }
*
* forAll (minSize(1), maxSize(10)) { (a: String, b: String) =>
* a.length + b.length should equal ((a + b).length)
* }
*
* forAll (minSize(1), maxSize(10)) { (a: String, b: String, c: String) =>
* a.length + b.length + c.length should equal ((a + b + c).length)
* }
*
* forAll (minSize(1), maxSize(10)) { (a: String, b: String, c: String, d: String) =>
* a.length + b.length + c.length + d.length should equal ((a + b + c + d).length)
* }
*
* forAll (minSize(1), maxSize(10)) { (a: String, b: String, c: String, d: String, e: String) =>
* a.length + b.length + c.length + d.length + e.length should equal ((a + b + c + d + e).length)
* }
*
* forAll (minSize(1), maxSize(10)) { (a: String, b: String, c: String, d: String, e: String, f: String) =>
* a.length + b.length + c.length + d.length + e.length + f.length should equal ((a + b + c + d + e + f).length)
* }
* </pre>
*
* @param configParams a variable length list of <code>PropertyCheckConfigParam</code> objects that should override corresponding
* values in the <code>PropertyCheckConfiguration</code> implicitly passed to the <code>apply</code> methods of the <code>ConfiguredPropertyCheck</code>
* object returned by this method.
*/
def forAll(configParams: PropertyCheckConfigParam*): ConfiguredPropertyCheck = new ConfiguredPropertyCheck(configParams)
/**
* Performs a configured property checks by applying property check functions passed to its <code>apply</code> methods to arguments
* supplied by implicitly passed generators, modifying the values in the
* <code>PropertyGenConfig</code> object passed implicitly to its <code>apply</code> methods with parameter values passed to its constructor.
*
* <p>
* Instances of this class are returned by trait <code>GeneratorDrivenPropertyChecks</code> <code>forAll</code> method that accepts a variable length
* argument list of <code>PropertyCheckConfigParam</code> objects. Thus it is used with functions of all six arities.
* Here are some examples:
* </p>
*
* <pre class="stHighlight">
* forAll (minSize(1), maxSize(10)) { (a: String) =>
* a.length should equal ((a).length)
* }
*
* forAll (minSize(1), maxSize(10)) { (a: String, b: String) =>
* a.length + b.length should equal ((a + b).length)
* }
*
* forAll (minSize(1), maxSize(10)) { (a: String, b: String, c: String) =>
* a.length + b.length + c.length should equal ((a + b + c).length)
* }
*
* forAll (minSize(1), maxSize(10)) { (a: String, b: String, c: String, d: String) =>
* a.length + b.length + c.length + d.length should equal ((a + b + c + d).length)
* }
*
* forAll (minSize(1), maxSize(10)) { (a: String, b: String, c: String, d: String, e: String) =>
* a.length + b.length + c.length + d.length + e.length should equal ((a + b + c + d + e).length)
* }
*
* forAll (minSize(1), maxSize(10)) { (a: String, b: String, c: String, d: String, e: String, f: String) =>
* a.length + b.length + c.length + d.length + e.length + f.length should equal ((a + b + c + d + e + f).length)
* }
* </pre>
*
* <p>
* In the first example above, the <code>ConfiguredPropertyCheck</code> object is returned by:
* </p>
*
* <pre class="stHighlight">
* forAll (minSize(1), maxSize(10))
* </pre>
*
* <p>
* The code that follows is an invocation of one of the <code>ConfiguredPropertyCheck</code> <code>apply</code> methods:
* </p>
*
* <pre class="stHighlight">
* { (a: String) =>
* a.length should equal ((a).length)
* }
* </pre>
*
* @param configParams a variable length list of <code>PropertyCheckConfigParam</code> objects that should override corresponding
* values in the <code>PropertyCheckConfiguration</code> implicitly passed to the <code>apply</code> methods of instances of this class.
*
* @author Bill Venners
*/
class ConfiguredPropertyCheck(configParams: Seq[PropertyCheckConfigParam]) {
/**
* Performs a property check by applying the specified property check function to arguments
* supplied by implicitly passed generators, modifying the values in the implicitly passed
* <code>PropertyGenConfig</code> object with parameter values passed to this object's constructor.
*
* <p>
* Here's an example:
* </p>
*
* <pre class="stHighlight">
* forAll (minSize(1), maxSize(10)) { (a: String) =>
* a.length should equal ((a).length)
* }
* </pre>
*
* @param fun the property check function to apply to the generated arguments
*/
def apply[A, ASSERTION](fun: (A) => ASSERTION)
(implicit
config: PropertyCheckConfigurable,
arbA: Arbitrary[A], shrA: Shrink[A],
asserting: CheckerAsserting[ASSERTION],
prettifier: Prettifier,
pos: source.Position
): asserting.Result = {
val propF = { (a: A) =>
val (unmetCondition, exception) =
try {
fun(a)
(false, None)
}
catch {
case e: DiscardedEvaluationException => (true, None)
case e: Throwable => (false, Some(e))
}
!unmetCondition ==> (
if (exception.isEmpty) Prop.passed else Prop.exception(exception.get)
)
}
val prop = Prop.forAll(propF)
val params = getParams(configParams, config)
asserting.check(prop, params, prettifier, pos)
}
/**
* Performs a property check by applying the specified property check function to arguments
* supplied by implicitly passed generators, modifying the values in the implicitly passed
* <code>PropertyGenConfig</code> object with parameter values passed to this object's constructor.
*
* <p>
* Here's an example:
* </p>
*
* <pre class="stHighlight">
* forAll (minSize(1), maxSize(10)) { (a: String, b: String) =>
* a.length + b.length should equal ((a + b).length)
* }
* </pre>
*
* @param fun the property check function to apply to the generated arguments
*/
def apply[A, B, ASSERTION](fun: (A, B) => ASSERTION)
(implicit
config: PropertyCheckConfigurable,
arbA: Arbitrary[A], shrA: Shrink[A],
arbB: Arbitrary[B], shrB: Shrink[B],
asserting: CheckerAsserting[ASSERTION],
prettifier: Prettifier,
pos: source.Position
): asserting.Result = {
val propF = { (a: A, b: B) =>
val (unmetCondition, exception) =
try {
fun(a, b)
(false, None)
}
catch {
case e: DiscardedEvaluationException => (true, None)
case e: Throwable => (false, Some(e))
}
!unmetCondition ==> (
if (exception.isEmpty) Prop.passed else Prop.exception(exception.get)
)
}
val prop = Prop.forAll(propF)
val params = getParams(configParams, config)
asserting.check(prop, params, prettifier, pos)
}
/**
* Performs a property check by applying the specified property check function to arguments
* supplied by implicitly passed generators, modifying the values in the implicitly passed
* <code>PropertyGenConfig</code> object with parameter values passed to this object's constructor.
*
* <p>
* Here's an example:
* </p>
*
* <pre class="stHighlight">
* forAll (minSize(1), maxSize(10)) { (a: String, b: String, c: String) =>
* a.length + b.length + c.length should equal ((a + b + c).length)
* }
* </pre>
*
* @param fun the property check function to apply to the generated arguments
*/
def apply[A, B, C, ASSERTION](fun: (A, B, C) => ASSERTION)
(implicit
config: PropertyCheckConfigurable,
arbA: Arbitrary[A], shrA: Shrink[A],
arbB: Arbitrary[B], shrB: Shrink[B],
arbC: Arbitrary[C], shrC: Shrink[C],
asserting: CheckerAsserting[ASSERTION],
prettifier: Prettifier,
pos: source.Position
): asserting.Result = {
val propF = { (a: A, b: B, c: C) =>
val (unmetCondition, exception) =
try {
fun(a, b, c)
(false, None)
}
catch {
case e: DiscardedEvaluationException => (true, None)
case e: Throwable => (false, Some(e))
}
!unmetCondition ==> (
if (exception.isEmpty) Prop.passed else Prop.exception(exception.get)
)
}
val prop = Prop.forAll(propF)
val params = getParams(configParams, config)
asserting.check(prop, params, prettifier, pos)
}
/**
* Performs a property check by applying the specified property check function to arguments
* supplied by implicitly passed generators, modifying the values in the implicitly passed
* <code>PropertyGenConfig</code> object with parameter values passed to this object's constructor.
*
* <p>
* Here's an example:
* </p>
*
* <pre class="stHighlight">
* forAll (minSize(1), maxSize(10)) { (a: String, b: String, c: String, d: String) =>
* a.length + b.length + c.length + d.length should equal ((a + b + c + d).length)
* }
* </pre>
*
* @param fun the property check function to apply to the generated arguments
*/
def apply[A, B, C, D, ASSERTION](fun: (A, B, C, D) => ASSERTION)
(implicit
config: PropertyCheckConfigurable,
arbA: Arbitrary[A], shrA: Shrink[A],
arbB: Arbitrary[B], shrB: Shrink[B],
arbC: Arbitrary[C], shrC: Shrink[C],
arbD: Arbitrary[D], shrD: Shrink[D],
asserting: CheckerAsserting[ASSERTION],
prettifier: Prettifier,
pos: source.Position
): asserting.Result = {
val propF = { (a: A, b: B, c: C, d: D) =>
val (unmetCondition, exception) =
try {
fun(a, b, c, d)
(false, None)
}
catch {
case e: DiscardedEvaluationException => (true, None)
case e: Throwable => (false, Some(e))
}
!unmetCondition ==> (
if (exception.isEmpty) Prop.passed else Prop.exception(exception.get)
)
}
val prop = Prop.forAll(propF)
val params = getParams(configParams, config)
asserting.check(prop, params, prettifier, pos)
}
/**
* Performs a property check by applying the specified property check function to arguments
* supplied by implicitly passed generators, modifying the values in the implicitly passed
* <code>PropertyGenConfig</code> object with parameter values passed to this object's constructor.
*
* <p>
* Here's an example:
* </p>
*
* <pre class="stHighlight">
* forAll (minSize(1), maxSize(10)) { (a: String, b: String, c: String, d: String, e: String) =>
* a.length + b.length + c.length + d.length + e.length should equal ((a + b + c + d + e).length)
* }
* </pre>
*
* @param fun the property check function to apply to the generated arguments
*/
def apply[A, B, C, D, E, ASSERTION](fun: (A, B, C, D, E) => ASSERTION)
(implicit
config: PropertyCheckConfigurable,
arbA: Arbitrary[A], shrA: Shrink[A],
arbB: Arbitrary[B], shrB: Shrink[B],
arbC: Arbitrary[C], shrC: Shrink[C],
arbD: Arbitrary[D], shrD: Shrink[D],
arbE: Arbitrary[E], shrE: Shrink[E],
asserting: CheckerAsserting[ASSERTION],
prettifier: Prettifier,
pos: source.Position
): asserting.Result = {
val propF = { (a: A, b: B, c: C, d: D, e: E) =>
val (unmetCondition, exception) =
try {
fun(a, b, c, d, e)
(false, None)
}
catch {
case e: DiscardedEvaluationException => (true, None)
case e: Throwable => (false, Some(e))
}
!unmetCondition ==> (
if (exception.isEmpty) Prop.passed else Prop.exception(exception.get)
)
}
val prop = Prop.forAll(propF)
val params = getParams(configParams, config)
asserting.check(prop, params, prettifier, pos)
}
/**
* Performs a property check by applying the specified property check function to arguments
* supplied by implicitly passed generators, modifying the values in the implicitly passed
* <code>PropertyGenConfig</code> object with parameter values passed to this object's constructor.
*
* <p>
* Here's an example:
* </p>
*
* <pre class="stHighlight">
* forAll (minSize(1), maxSize(10)) { (a: String, b: String, c: String, d: String, e: String, f: String) =>
* a.length + b.length + c.length + d.length + e.length + f.length should equal ((a + b + c + d + e + f).length)
* }
* </pre>
*
* @param fun the property check function to apply to the generated arguments
*/
def apply[A, B, C, D, E, F, ASSERTION](fun: (A, B, C, D, E, F) => ASSERTION)
(implicit
config: PropertyCheckConfigurable,
arbA: Arbitrary[A], shrA: Shrink[A],
arbB: Arbitrary[B], shrB: Shrink[B],
arbC: Arbitrary[C], shrC: Shrink[C],
arbD: Arbitrary[D], shrD: Shrink[D],
arbE: Arbitrary[E], shrE: Shrink[E],
arbF: Arbitrary[F], shrF: Shrink[F],
asserting: CheckerAsserting[ASSERTION],
prettifier: Prettifier,
pos: source.Position
): asserting.Result = {
val propF = { (a: A, b: B, c: C, d: D, e: E, f: F) =>
val (unmetCondition, exception) =
try {
fun(a, b, c, d, e, f)
(false, None)
}
catch {
case e: DiscardedEvaluationException => (true, None)
case e: Throwable => (false, Some(e))
}
!unmetCondition ==> (
if (exception.isEmpty) Prop.passed else Prop.exception(exception.get)
)
}
val prop = Prop.forAll(propF)
val params = getParams(configParams, config)
asserting.check(prop, params, prettifier, pos)
}
}
"""
val propertyCheckForAllTemplate = """
/**
* Performs a property check by applying the specified property check function to arguments
* supplied by implicitly passed generators.
*
* <p>
* Here's an example:
* </p>
*
* <pre class="stHighlight">
* forAll { ($namesAndTypes$) =>
* $sumOfArgLengths$ should equal (($sumOfArgs$).length)
* }
* </pre>
*
* @param fun the property check function to apply to the generated arguments
*/
def forAll[$alphaUpper$, ASSERTION](fun: ($alphaUpper$) => ASSERTION)
(implicit
config: PropertyCheckConfigurable,
$arbShrinks$,
asserting: CheckerAsserting[ASSERTION],
prettifier: Prettifier,
pos: source.Position
): asserting.Result = {
val propF = { ($argType$) =>
val (unmetCondition, exception) =
try {
fun($alphaLower$)