/
CheckerAsserting.scala
345 lines (302 loc) · 15.1 KB
/
CheckerAsserting.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
/*
* 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.scalatestplus.scalacheck
import org.scalactic.{Resources => _, FailureMessages => _, UnquotedString => _, _}
import NameUtil.getSimpleNameOfAnObjectsClass
import org.scalacheck.Prop
import org.scalacheck.Prop.Arg
import org.scalacheck.Test
import org.scalacheck.util.Pretty
import org.scalatest.Assertion
//import org.scalatest.Expectation
import org.scalatest.Fact
import org.scalatest.Succeeded
import org.scalatest.exceptions.StackDepthException
import org.scalatestplus.scalacheck.FailureMessages.decorateToStringValue
import org.scalatest.exceptions.GeneratorDrivenPropertyCheckFailedException
import org.scalatest.exceptions.StackDepth
/**
* Supertrait for <code>CheckerAsserting</code> typeclasses, which are used to implement and determine the result
* type of [[org.scalatest.prop.GeneratorDrivenPropertyChecks GeneratorDrivenPropertyChecks]]'s <code>apply</code> and <code>forAll</code> method.
*
* <p>
* Currently, an [[org.scalatest.prop.GeneratorDrivenPropertyChecks GeneratorDrivenPropertyChecks]] expression will have result type <code>Assertion</code>, if the function passed has result type <code>Assertion</code>,
* else it will have result type <code>Unit</code>.
* </p>
*/
trait CheckerAsserting[T] {
/**
* The result type of the <code>check</code> method.
*/
type Result
def succeed(result: T): (Boolean, Option[Throwable])
/**
* Perform the property check using the given <code>Prop</code> and <code>Test.Parameters</code>.
*
* @param p the <code>Prop</code> to be used to check
* @param prms the <code>Test.Parameters</code> to be used to check
* @param prettifier the <code>Prettifier</code> to be used to prettify error message
* @param pos the <code>Position</code> of the caller site
* @param argNames the list of argument names
* @return the <code>Result</code> of the property check.
*/
def check(p: Prop, prms: Test.Parameters, prettifier: Prettifier, pos: source.Position, argNames: Option[List[String]] = None): Result
}
/**
* Class holding lowest priority <code>CheckerAsserting</code> implicit, which enables [[org.scalatest.prop.GeneratorDrivenPropertyChecks GeneratorDrivenPropertyChecks]] expressions that have result type <code>Unit</code>.
*/
abstract class UnitCheckerAsserting {
/**
* Abstract subclass of <code>CheckerAsserting</code> that provides the bulk of the implementations of <code>CheckerAsserting</code>
* <code>check</code> method.
*/
/* protected[scalatest]*/ abstract class CheckerAssertingImpl[T] extends CheckerAsserting[T] {
import CheckerAsserting._
/**
* Check the given <code>Prop</code> and <code>Test.Parameters</code> by calling [[http://www.scalacheck.org ScalaCheck]]'s <code>Test.check</code>.
* If the check succeeds, call <code>indicateSuccess</code>, else call <code>indicateFailure</code>.
*
*
* @param p the <code>Prop</code> to be used to check
* @param prms the <code>Test.Parameters</code> to be used to check
* @param prettifier the <code>Prettifier</code> to be used to prettify error message
* @param pos the <code>Position</code> of the caller site
* @param argNames the list of argument names
* @return the <code>Result</code> of the property check.
*/
def check(p: Prop, prms: Test.Parameters, prettifier: Prettifier, pos: source.Position, argNames: Option[List[String]] = None): Result = {
val result = Test.check(prms, p)
if (!result.passed) {
val (args, labels) = argsAndLabels(result)
(result.status: @unchecked) match {
case Test.Exhausted =>
val failureMsg =
if (result.succeeded == 1)
FailureMessages.propCheckExhaustedAfterOne(prettifier, result.discarded)
else
FailureMessages.propCheckExhausted(prettifier, result.succeeded, result.discarded)
indicateFailure(
sde => failureMsg,
failureMsg,
args,
labels,
None,
pos
)
case Test.Failed(scalaCheckArgs, scalaCheckLabels) =>
val stackDepth = 1
indicateFailure(
sde => FailureMessages.propertyException(prettifier, UnquotedString(sde.getClass.getSimpleName)) + "\n" +
( sde.failedCodeFileNameAndLineNumberString match { case Some(s) => " (" + s + ")"; case None => "" }) + "\n" +
" " + FailureMessages.propertyFailed(prettifier, result.succeeded) + "\n" +
(
sde match {
case sd: StackDepth if sd.failedCodeFileNameAndLineNumberString.isDefined =>
" " + FailureMessages.thrownExceptionsLocation(prettifier, UnquotedString(sd.failedCodeFileNameAndLineNumberString.get)) + "\n"
case _ => ""
}
) +
" " + FailureMessages.occurredOnValues() + "\n" +
prettyArgs(getArgsWithSpecifiedNames(argNames, scalaCheckArgs), prettifier) + "\n" +
" )" +
getLabelDisplay(scalaCheckLabels),
FailureMessages.propertyFailed(prettifier, result.succeeded),
scalaCheckArgs,
scalaCheckLabels.toList,
None,
pos
)
case Test.PropException(scalaCheckArgs, e, scalaCheckLabels) =>
indicateFailure(
sde => FailureMessages.propertyException(prettifier, UnquotedString(e.getClass.getSimpleName)) + "\n" +
" " + FailureMessages.thrownExceptionsMessage(prettifier, if (e.getMessage == null) "None" else UnquotedString(e.getMessage)) + "\n" +
(
e match {
case sd: StackDepth if sd.failedCodeFileNameAndLineNumberString.isDefined =>
" " + FailureMessages.thrownExceptionsLocation(prettifier, UnquotedString(sd.failedCodeFileNameAndLineNumberString.get)) + "\n"
case _ => ""
}
) +
" " + FailureMessages.occurredOnValues() + "\n" +
prettyArgs(getArgsWithSpecifiedNames(argNames, scalaCheckArgs), prettifier) + "\n" +
" )" +
getLabelDisplay(scalaCheckLabels),
FailureMessages.propertyException(prettifier, UnquotedString(e.getClass.getName)),
scalaCheckArgs,
scalaCheckLabels.toList,
Some(e),
pos
)
}
} else indicateSuccess(FailureMessages.propertyCheckSucceeded())
}
private[scalacheck] def indicateSuccess(message: => String): Result
private[scalacheck] def indicateFailure(messageFun: StackDepthException => String, undecoratedMessage: => String, scalaCheckArgs: List[Any], scalaCheckLabels: List[String], optionalCause: Option[Throwable], pos: source.Position): Result
}
/**
* Provides support of [[org.scalatest.enablers.CheckerAsserting CheckerAsserting]] for Unit. Do nothing when the check succeeds,
* but throw [[org.scalatest.exceptions.GeneratorDrivenPropertyCheckFailedException GeneratorDrivenPropertyCheckFailedException]]
* when check fails.
*/
implicit def assertingNatureOfT[T]: CheckerAsserting[T] { type Result = Unit } =
new CheckerAssertingImpl[T] {
type Result = Unit
def succeed(result: T) = (true, None)
private[scalacheck] def indicateSuccess(message: => String): Unit = ()
private[scalacheck] def indicateFailure(messageFun: StackDepthException => String, undecoratedMessage: => String, scalaCheckArgs: List[Any], scalaCheckLabels: List[String], optionalCause: Option[Throwable], pos: source.Position): Unit = {
throw new GeneratorDrivenPropertyCheckFailedException(
messageFun,
optionalCause,
pos,
None,
undecoratedMessage,
scalaCheckArgs,
None,
scalaCheckLabels.toList
)
}
}
}
/**
* Abstract class that in the future will hold an intermediate priority <code>CheckerAsserting</code> implicit, which will enable inspector expressions
* that have result type <code>Expectation</code>, a more composable form of assertion that returns a result instead of throwing an exception when it fails.
*/
abstract class ExpectationCheckerAsserting extends UnitCheckerAsserting {
/*implicit def assertingNatureOfExpectation(implicit prettifier: Prettifier): CheckerAsserting[Expectation] { type Result = Expectation } = {
new CheckerAssertingImpl[Expectation] {
type Result = Expectation
def succeed(result: Expectation) = (result.isYes, result.cause)
private[scalacheck] def indicateSuccess(message: => String): Expectation = Fact.Yes(message)(prettifier)
private[scalacheck] def indicateFailure(messageFun: StackDepthException => String, undecoratedMessage: => String, scalaCheckArgs: List[Any], scalaCheckLabels: List[String], optionalCause: Option[Throwable], pos: source.Position): Expectation = {
val gdpcfe =
new GeneratorDrivenPropertyCheckFailedException(
messageFun,
optionalCause,
pos,
None,
undecoratedMessage,
scalaCheckArgs,
None,
scalaCheckLabels.toList
)
val message: String = gdpcfe.getMessage
Fact.No(message)(prettifier)
}
}
}*/
}
/**
* Companion object to <code>CheckerAsserting</code> that provides two implicit providers, a higher priority one for passed functions that have result
* type <code>Assertion</code>, which also yields result type <code>Assertion</code>, and one for any other type, which yields result type <code>Unit</code>.
*/
object CheckerAsserting extends ExpectationCheckerAsserting {
/**
* Provides support of [[org.scalatest.enablers.CheckerAsserting CheckerAsserting]] for Assertion. Returns [[org.scalatest.Succeeded Succeeded]] when the check succeeds,
* but throw [[org.scalatest.exceptions.GeneratorDrivenPropertyCheckFailedException GeneratorDrivenPropertyCheckFailedException]]
* when check fails.
*/
implicit def assertingNatureOfAssertion: CheckerAsserting[Assertion] { type Result = Assertion } = {
new CheckerAssertingImpl[Assertion] {
type Result = Assertion
def succeed(result: Assertion) = (true, None)
private[scalacheck] def indicateSuccess(message: => String): Assertion = Succeeded
private[scalacheck] def indicateFailure(messageFun: StackDepthException => String, undecoratedMessage: => String, scalaCheckArgs: List[Any], scalaCheckLabels: List[String], optionalCause: Option[Throwable], pos: source.Position): Assertion = {
throw new GeneratorDrivenPropertyCheckFailedException(
messageFun,
optionalCause,
pos,
None,
undecoratedMessage,
scalaCheckArgs,
None,
scalaCheckLabels.toList
)
}
}
}
private[scalacheck] def getArgsWithSpecifiedNames(argNames: Option[List[String]], scalaCheckArgs: List[Arg[Any]]) = {
if (argNames.isDefined) {
// length of scalaCheckArgs should equal length of argNames
val zipped = argNames.get zip scalaCheckArgs
zipped map { case (argName, arg) => arg.copy(label = argName) }
}
else
scalaCheckArgs
}
private[scalacheck] def getLabelDisplay(labels: Set[String]): String =
if (labels.size > 0)
"\n " + (if (labels.size == 1) Resources.propCheckLabel() else Resources.propCheckLabels()) + "\n" + labels.map(" " + _).mkString("\n")
else
""
private[scalacheck] def argsAndLabels(result: Test.Result): (List[Any], List[String]) = {
val (scalaCheckArgs, scalaCheckLabels) =
result.status match {
case Test.Proved(args) => (args.toList, List())
case Test.Failed(args, labels) => (args.toList, labels.toList)
case Test.PropException(args, _, labels) => (args.toList, labels.toList)
case _ => (List(), List())
}
val args: List[Any] = for (scalaCheckArg <- scalaCheckArgs.toList) yield scalaCheckArg.arg
// scalaCheckLabels is a Set[String], I think
val labels: List[String] = for (scalaCheckLabel <- scalaCheckLabels.iterator.toList) yield scalaCheckLabel
(args, labels)
}
// TODO: Internationalize these, and make them consistent with FailureMessages stuff (only strings get quotes around them, etc.)
private[scalacheck] def prettyTestStats(result: Test.Result, prettifier: Prettifier) = result.status match {
case Test.Proved(args) =>
"OK, proved property: \n" + prettyArgs(args, prettifier)
case Test.Passed =>
"OK, passed " + result.succeeded + " tests."
case Test.Failed(args, labels) =>
"Falsified after " + result.succeeded + " passed tests:\n" + prettyLabels(labels) + prettyArgs(args, prettifier)
case Test.Exhausted =>
"Gave up after only " + result.succeeded + " passed tests. " +
result.discarded + " tests were discarded."
case Test.PropException(args, e, labels) =>
FailureMessages.propertyException(prettifier, UnquotedString(e.getClass.getSimpleName)) + "\n" + prettyLabels(labels) + prettyArgs(args, prettifier)
}
private[scalacheck] def prettyLabels(labels: Set[String]) = {
if (labels.isEmpty) ""
else if (labels.size == 1) "Label of failing property: " + labels.iterator.next + "\n"
else "Labels of failing property: " + labels.mkString("\n") + "\n"
}
//
// If scalacheck arg contains a type that
// decorateToStringValue processes, then let
// decorateToStringValue handle it. Otherwise use its
// prettyArg method to generate the display string.
//
// Passes 0 as verbosity value to prettyArg function.
//
private[scalacheck] def decorateArgToStringValue(arg: Arg[_], prettifier: Prettifier): String =
arg.arg match {
case null => decorateToStringValue(prettifier, arg.arg)
case _: Unit => decorateToStringValue(prettifier, arg.arg)
case _: String => decorateToStringValue(prettifier, arg.arg)
case _: Char => decorateToStringValue(prettifier, arg.arg)
case _: Array[_] => decorateToStringValue(prettifier, arg.arg)
case _ => arg.prettyArg(new Pretty.Params(0))
}
private[scalacheck] def prettyArgs(args: List[Arg[_]], prettifier: Prettifier) = {
val strs = for((a, i) <- args.zipWithIndex) yield (
" " +
(if (a.label == "") "arg" + i else a.label) +
" = " + decorateArgToStringValue(a, prettifier) + (if (i < args.length - 1) "," else "") +
(if (a.shrinks > 0) " // " + a.shrinks + (if (a.shrinks == 1) " shrink" else " shrinks") else "")
)
strs.mkString("\n")
}
}