/
Configuration.scala
464 lines (428 loc) · 19.6 KB
/
Configuration.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
/*
* Copyright 2001-2013 Artima, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.scalatest.prop
import org.scalacheck.Test.Parameters
import org.scalactic.anyvals.{PosZInt, PosZDouble, PosInt}
import org.scalacheck.Test.TestCallback
/**
* Trait providing methods and classes used to configure property checks provided by the
* the <code>forAll</code> methods of trait <code>GeneratorDrivenPropertyChecks</code> (for ScalaTest-style
* property checks) and the <code>check</code> methods of trait <code>Checkers</code> (for ScalaCheck-style property checks).
*
* @author Bill Venners
*/
trait Configuration {
@deprecated("Use PropertyCheckConfiguration directly instead.")
trait PropertyCheckConfigurable {
private [prop] def asPropertyCheckConfiguration: PropertyCheckConfiguration
}
object PropertyCheckConfiguration {
private[scalatest] def calculateMaxDiscardedFactor(minSuccessful: Int, maxDiscarded: Int): Double =
((maxDiscarded + 1): Double) / (minSuccessful: Double)
private[scalatest] def calculateMaxDiscarded(maxDiscardedRatio: Double, minSuccessful: Int): Double =
(maxDiscardedRatio * minSuccessful) - 1
}
case class PropertyCheckConfiguration(minSuccessful: PosInt = PosInt(10),
maxDiscardedFactor: PosZDouble = PosZDouble(5.0),
minSize: PosZInt = PosZInt(0),
sizeRange: PosZInt = PosZInt(100),
workers: PosInt = PosInt(1)) extends PropertyCheckConfigurable {
@deprecated("Transitional value to ensure upgrade compatibility when mixing PropertyCheckConfig and minSuccessful parameters. Remove with PropertyCheckConfig class")
private [scalatest] val legacyMaxDiscarded: Option[Int] = None
@deprecated("Transitional value to ensure upgrade compatibility when mixing PropertyCheckConfig and minSize parameters. Remove with PropertyCheckConfig class")
private [scalatest] val legacyMaxSize: Option[Int] = None
private [prop] def asPropertyCheckConfiguration = this
}
/**
* Configuration object for property checks.
*
* <p>
* The default values for the parameters are:
* </p>
*
* <table style="border-collapse: collapse; border: 1px solid black">
* <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>
* </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>
* </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>
* </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>
* </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>
* </tr>
* </table>
*
* @param minSuccessful the minimum number of successful property evaluations required for the property to pass.
* @param maxDiscarded the maximum number of discarded property evaluations allowed during a property check
* @param minSize the minimum size parameter to provide to ScalaCheck, which it will use when generating objects for which size matters (such as strings or lists).
* @param maxSize the maximum size parameter to provide to ScalaCheck, which it will use when generating objects for which size matters (such as strings or lists).
* @param workers specifies the number of worker threads to use during property evaluation
* @throws IllegalArgumentException if the specified <code>minSuccessful</code> value is less than or equal to zero,
* the specified <code>maxDiscarded</code> value is less than zero,
* the specified <code>minSize</code> value is less than zero,
* the specified <code>maxSize</code> value is less than zero,
* the specified <code>minSize</code> is greater than the specified or default value of <code>maxSize</code>, or
* the specified <code>workers</code> value is less than or equal to zero.
*
* @author Bill Venners
*/
@deprecated("Use PropertyCheckConfiguration instead")
case class PropertyCheckConfig(
minSuccessful: Int = 10,
maxDiscarded: Int = 500,
minSize: Int = 0,
maxSize: Int = 100,
workers: Int = 1
) extends PropertyCheckConfigurable {
require(minSuccessful > 0, "minSuccessful had value " + minSuccessful + ", but must be greater than zero")
require(maxDiscarded >= 0, "maxDiscarded had value " + maxDiscarded + ", but must be greater than or equal to zero")
require(minSize >= 0, "minSize had value " + minSize + ", but must be greater than or equal to zero")
require(maxSize >= 0, "maxSize had value " + maxSize + ", but must be greater than or equal to zero")
require(minSize <= maxSize, "minSize had value " + minSize + ", which must be less than or equal to maxSize, which had value " + maxSize)
require(workers > 0, "workers had value " + workers + ", but must be greater than zero")
private [prop] def asPropertyCheckConfiguration = this
}
import scala.language.implicitConversions
/**
* Implicitly converts <code>PropertyCheckConfig</code>s to <code>PropertyCheckConfiguration</code>,
* which enables a smoother upgrade path.
*/
implicit def PropertyCheckConfig2PropertyCheckConfiguration(p: PropertyCheckConfig): PropertyCheckConfiguration = {
val maxDiscardedFactor = PropertyCheckConfiguration.calculateMaxDiscardedFactor(p.minSuccessful, p.maxDiscarded)
new PropertyCheckConfiguration(
minSuccessful = PosInt.from(p.minSuccessful).get,
maxDiscardedFactor = PosZDouble.from(maxDiscardedFactor).get,
minSize = PosZInt.from(p.minSize).get,
sizeRange = PosZInt.from(p.maxSize - p.minSize).get,
workers = PosInt.from(p.workers).get) {
override private [scalatest] val legacyMaxDiscarded = Some(p.maxDiscarded)
override private [scalatest] val legacyMaxSize = Some(p.maxSize)
}
}
/**
* Abstract class defining a family of configuration parameters for property checks.
*
* <p>
* The subclasses of this abstract class are used to pass configuration information to
* the <code>forAll</code> methods of traits <code>PropertyChecks</code> (for ScalaTest-style
* property checks) and <code>Checkers</code>(for ScalaCheck-style property checks).
* </p>
*
* @author Bill Venners
*/
sealed abstract class PropertyCheckConfigParam extends Product with Serializable
/**
* A <code>PropertyCheckConfigParam</code> that specifies the minimum number of successful
* property evaluations required for the property to pass.
*
* @author Bill Venners
*/
case class MinSuccessful(value: PosInt) extends PropertyCheckConfigParam
/**
* A <code>PropertyCheckConfigParam</code> that specifies the maximum number of discarded
* property evaluations allowed during property evaluation.
*
* <p>
* In <code>GeneratorDrivenPropertyChecks</code>, a property evaluation is discarded if it throws
* <code>DiscardedEvaluationException</code>, which is produce by <code>whenever</code> clause that
* evaluates to false. For example, consider this ScalaTest property check:
* </p>
*
* <pre class="stHighlight">
* // forAll defined in <code>GeneratorDrivenPropertyChecks</code>
* forAll { (n: Int) =>
* whenever (n > 0) {
* doubleIt(n) should equal (n * 2)
* }
* }
*
* </pre>
*
* <p>
* In the above code, whenever a non-positive <code>n</code> is passed, the property function will complete abruptly
* with <code>DiscardedEvaluationException</code>.
* </p>
*
* <p>
* Similarly, in <code>Checkers</code>, a property evaluation is discarded if the expression to the left
* of ScalaCheck's <code>==></code> operator is false. Here's an example:
* </p>
*
* <pre class="stHighlight">
* // forAll defined in <code>Checkers</code>
* forAll { (n: Int) =>
* (n > 0) ==> doubleIt(n) == (n * 2)
* }
*
* </pre>
*
* <p>
* For either kind of property check, <code>MaxDiscarded</code> indicates the maximum number of discarded
* evaluations that will be allowed. As soon as one past this number of evaluations indicates it needs to be discarded,
* the property check will fail.
* </p>
*
* @throws IllegalArgumentException if specified <code>value</code> is less than zero.
*
* @author Bill Venners
*/
@deprecated case class MaxDiscarded(value: Int) extends PropertyCheckConfigParam {
require(value >= 0)
}
case class MaxDiscardedFactor(value: PosZDouble) extends PropertyCheckConfigParam
/**
* A <code>PropertyCheckConfigParam</code> that specifies the minimum size parameter to
* provide to ScalaCheck, which it will use when generating objects for which size matters (such as
* strings or lists).
*
* @throws IllegalArgumentException if specified <code>value</code> is less than zero.
*
* @author Bill Venners
*/
case class MinSize(value: PosZInt) extends PropertyCheckConfigParam
/**
* A <code>PropertyCheckConfigParam</code> that specifies the maximum size parameter to
* provide to ScalaCheck, which it will use when generating objects for which size matters (such as
* strings or lists).
*
* <p>
* Note that the maximum size should be greater than or equal to the minimum size. This requirement is
* enforced by the <code>PropertyCheckConfig</code> constructor and the <code>forAll</code> methods of
* traits <code>PropertyChecks</code> and <code>Checkers</code>. In other words, it is enforced at the point
* both a maximum and minimum size are provided together.
* </p>
*
* @throws IllegalArgumentException if specified <code>value</code> is less than zero.
*
* @author Bill Venners
*/
@deprecated("use SizeRange instead")
case class MaxSize(value: Int) extends PropertyCheckConfigParam {
require(value >= 0)
}
/**
* A <code>PropertyCheckConfigParam</code> that (with minSize) specifies the maximum size parameter to
* provide to ScalaCheck, which it will use when generating objects for which size matters (such as
* strings or lists).
*
* <p>
* Note that the size range is added to minSize in order to calculate the maximum size passed to ScalaCheck.
* Using a range allows compile-time checking of a non-negative number being specified.
* </p>
*
* @author Bill Venners
*/
case class SizeRange(value: PosZInt) extends PropertyCheckConfigParam
/**
* A <code>PropertyCheckConfigParam</code> that specifies the number of worker threads
* to use when evaluating a property.
*
* @throws IllegalArgumentException if specified <code>value</code> is less than or equal to zero.
*
* @author Bill Venners
*/
case class Workers(value: PosInt) extends PropertyCheckConfigParam
/**
* Returns a <code>MinSuccessful</code> property check configuration parameter containing the passed value, which specifies the minimum number of successful
* property evaluations required for the property to pass.
*
*/
def minSuccessful(value: PosInt): MinSuccessful = new MinSuccessful(value)
/**
* Returns a <code>MaxDiscarded</code> property check configuration parameter containing the passed value, which specifies the maximum number of discarded
* property evaluations allowed during property evaluation.
*
* @throws IllegalArgumentException if specified <code>value</code> is less than zero.
*/
@deprecated("use maxDiscardedFactor instead")
def maxDiscarded(value: Int): MaxDiscarded = new MaxDiscarded(value)
/**
* Returns a <code>MaxDiscardedFactor</code> property check configuration parameter containing the passed value, which specifies the factor of discarded
* property evaluations allowed during property evaluation.
*
*/
def maxDiscardedFactor(value: PosZDouble): MaxDiscardedFactor = MaxDiscardedFactor(value)
/**
* Returns a <code>MinSize</code> property check configuration parameter containing the passed value, which specifies the minimum size parameter to
* provide to ScalaCheck, which it will use when generating objects for which size matters (such as
* strings or lists).
*
*/
def minSize(value: PosZInt): MinSize = new MinSize(value)
/**
* Returns a <code>MaxSize</code> property check configuration parameter containing the passed value, which specifies the maximum size parameter to
* provide to ScalaCheck, which it will use when generating objects for which size matters (such as
* strings or lists).
*
* <p>
* Note that the maximum size should be greater than or equal to the minimum size. This requirement is
* enforced by the <code>PropertyCheckConfig</code> constructor and the <code>forAll</code> methods of
* traits <code>PropertyChecks</code> and <code>Checkers</code>. In other words, it is enforced at the point
* both a maximum and minimum size are provided together.
* </p>
*
* @throws IllegalArgumentException if specified <code>value</code> is less than zero.
*/
@deprecated("use SizeRange instead") def maxSize(value: Int): MaxSize = new MaxSize(value)
/**
* Returns a <code>SizeRange</code> property check configuration parameter containing the passed value, that (with minSize) specifies the maximum size parameter to
* provide to ScalaCheck, which it will use when generating objects for which size matters (such as
* strings or lists).
*
* <p>
* Note that the size range is added to minSize in order to calculate the maximum size passed to ScalaCheck.
* Using a range allows compile-time checking of a non-negative number being specified.
* </p>
*
* @author Bill Venners
*/
def sizeRange(value: PosZInt): SizeRange = SizeRange(value)
/**
* Returns a <code>Workers</code> property check configuration parameter containing the passed value, which specifies the number of worker threads
* to use when evaluating a property.
*
*/
def workers(value: PosInt): Workers = new Workers(value)
private[scalatest] def getScalaCheckParams(
configParams: Seq[Configuration#PropertyCheckConfigParam],
c: PropertyCheckConfigurable
): Parameters = {
val config: PropertyCheckConfiguration = c.asPropertyCheckConfiguration
var minSuccessful: Option[Int] = None
var maxDiscarded: Option[Int] = None
var maxDiscardedFactor: Option[Double] = None
var pminSize: Option[Int] = None
var psizeRange: Option[Int] = None
var pmaxSize: Option[Int] = None
var pworkers: Option[Int] = None
var minSuccessfulTotalFound = 0
var maxDiscardedTotalFound = 0
var maxDiscardedFactorTotalFound = 0
var minSizeTotalFound = 0
var sizeRangeTotalFound = 0
var maxSizeTotalFound = 0
var workersTotalFound = 0
for (configParam <- configParams) {
configParam match {
case param: MinSuccessful =>
minSuccessful = Some(param.value)
minSuccessfulTotalFound += 1
case param: MaxDiscarded =>
maxDiscarded = Some(param.value)
maxDiscardedTotalFound += 1
case param: MaxDiscardedFactor =>
maxDiscardedFactor = Some(param.value)
maxDiscardedFactorTotalFound += 1
case param: MinSize =>
pminSize = Some(param.value)
minSizeTotalFound += 1
case param: SizeRange =>
psizeRange = Some(param.value)
sizeRangeTotalFound += 1
case param: MaxSize =>
pmaxSize = Some(param.value)
maxSizeTotalFound += 1
case param: Workers =>
pworkers = Some(param.value)
workersTotalFound += 1
}
}
if (minSuccessfulTotalFound > 1)
throw new IllegalArgumentException("can pass at most one MinSuccessful config parameters, but " + minSuccessfulTotalFound + " were passed")
val maxDiscardedAndFactorTotalFound = maxDiscardedTotalFound + maxDiscardedFactorTotalFound
if (maxDiscardedAndFactorTotalFound > 1)
throw new IllegalArgumentException("can pass at most one MaxDiscarded or MaxDiscardedFactor config parameters, but " + maxDiscardedAndFactorTotalFound + " were passed")
if (minSizeTotalFound > 1)
throw new IllegalArgumentException("can pass at most one MinSize config parameters, but " + minSizeTotalFound + " were passed")
val maxSizeAndSizeRangeTotalFound = maxSizeTotalFound + sizeRangeTotalFound
if (maxSizeAndSizeRangeTotalFound > 1)
throw new IllegalArgumentException("can pass at most one SizeRange or MaxSize config parameters, but " + maxSizeAndSizeRangeTotalFound + " were passed")
if (workersTotalFound > 1)
throw new IllegalArgumentException("can pass at most one Workers config parameters, but " + workersTotalFound + " were passed")
val minSuccessfulTests: Int = minSuccessful.getOrElse(config.minSuccessful)
val minSize: Int = pminSize.getOrElse(config.minSize)
val maxSize = {
(psizeRange, pmaxSize, config.legacyMaxSize) match {
case (None, None, Some(legacyMaxSize)) =>
legacyMaxSize
case (None, Some(maxSize), _) =>
maxSize
case _ =>
psizeRange.getOrElse(config.sizeRange.value) + minSize
}
}
val maxDiscardRatio: Float = {
(maxDiscardedFactor, maxDiscarded, config.legacyMaxDiscarded, minSuccessful) match {
case (None, None, Some(legacyMaxDiscarded), Some(specifiedMinSuccessful)) =>
PropertyCheckConfiguration.calculateMaxDiscardedFactor(specifiedMinSuccessful, legacyMaxDiscarded).toFloat
case (None, Some(md), _, _) =>
if (md < 0) Parameters.default.maxDiscardRatio
else PropertyCheckConfiguration.calculateMaxDiscardedFactor(minSuccessfulTests, md).toFloat
case _ =>
maxDiscardedFactor.getOrElse(config.maxDiscardedFactor.value).toFloat
}
}
Parameters.default
.withMinSuccessfulTests(minSuccessfulTests)
.withMinSize(minSize)
.withMaxSize(maxSize)
.withWorkers(pworkers.getOrElse(config.workers))
.withTestCallback(new TestCallback {})
.withMaxDiscardRatio(maxDiscardRatio)
.withCustomClassLoader(None)
}
/**
* Implicit <code>PropertyCheckConfig</code> value providing default configuration values.
*/
implicit val generatorDrivenConfig: PropertyCheckConfiguration = PropertyCheckConfiguration()
}
/**
* Companion object that facilitates the importing of <code>Configuration</code> members as
* an alternative to mixing it in. One use case is to import <code>Configuration</code> members so you can use
* them in the Scala interpreter.
*/
object Configuration extends Configuration