/
Prettifier.scala
456 lines (433 loc) · 19.7 KB
/
Prettifier.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
/*
* 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.scalactic
import scala.collection._
import mutable.WrappedArray
import scala.util.Success
/**
* A function that given any object will produce a “pretty” string representation of that object,
* where “pretty” is in the eye of the implementer.
*
* <p>
* Scala's `Any` type declares a `toString` that will convert any object to a `String`
* representation. This `String` representation is primarily intended for programmers, and is usually sufficient.
* However, sometimes it can be helpful to provide an alternative implementation of `toString` for certain types.
* For example, the `toString` implementation on `String` prints out the value of the `String`:
* </p>
*
* <pre class="stREPL">
* scala> "1".toString
* res0: String = 1
* </pre>
*
* <p>
* If the error message that resulted from comparing `Int` 1 with `String` `"1"`
* in a ScalaTest assertion used `toString`, therefore, the error message would be:
* </p>
*
* <pre>
* 1 did not equal 1
* </pre>
*
* <p>
* To make it quicker to figure out why the assertion failed, ScalaTest ''prettifies'' the objects involved in
* the error message. The default `Prettifier` will place double quotes on either side of a `String`s
* `toString` result:
* </p>
*
* <pre class="stREPL">
* scala> import org.scalactic._
* import org.scalactic._
*
* scala> Prettifier.default("1")
* res1: String = "1"
* </pre>
*
* <p>
* Thus the error message resulting from comparing `Int` 1 with `String` `"1"`,
* in a ScalaTest assertion is:
* </p>
*
* <pre>
* 1 did not equal "1"
* </pre>
*
* <p>
* If you wish to prettify an object in production code, for example, to issue a profoundly clear debug message, you can use
* `PrettyMethods` and invoke `pretty`. Here's an example:
* </p>
*
* <pre class="stREPL">
* scala> import PrettyMethods._
* import PrettyMethods._
*
* scala> 1.pretty
* res2: String = 1
*
* scala> "1".pretty
* res3: String = "1"
* </pre>
*
* <p>
* For example, the default `Prettifier`, <a href="Prettifier$.html">`Prettifier.default`</a>, transforms:
* </p>
*
* <ul>
* <li>`Null` to: `null`</li>
* <li>`Unit` to: `<() the Unit value>`</li>
* <li>`String` to: `"string"` (the `toString` result surrounded by double quotes)</li>
* <li>`Char` to: `'c'` (the `toString` result surrounded by single quotes)</li>
* <li>`Array` to: `Array("1", "2", "3")`</li>
* <li>`scala.Some` to: `Some("3")`</li>
* <li>`scala.util.Left` to: `Left("3")`</li>
* <li>`scala.util.Right` to: `Right("3")`</li>
* <li>`scala.util.Success` to: `Success("3")`</li>
* <li>`org.scalactic.Good` to: `Good("3")`</li>
* <li>`org.scalactic.Bad` to: `Bad("3")`</li>
* <li>`org.scalactic.One` to: `One("3")`</li>
* <li>`org.scalactic.Many` to: `Many("1", "2", "3")`</li>
* <li>`scala.collection.GenTraversable` to: `List("1", "2", "3")`</li>
* <li>`java.util.Collection` to: `["1", "2", "3"]`</li>
* <li>`java.util.Map` to: `{1="one", 2="two", 3="three"}`</li>
* </ul>
*
* <p>
* For anything else, the default `Prettifier` returns the result of invoking `toString`.
* </p>
*
* <p>
* Note: `Prettifier` is not parameterized (''i.e.'', `Prettifier[T]`, where `T` is the type
* to prettify) because assertions (including matcher expressions) in ScalaTest would then need to look up `Prettifier`s implicitly by type. This would slow
* compilation even though most (let's guess 99.9%) of the time in practice assertions do not fail, and thus 99.9% of the time no error messages need to be generated.
* If no error messages are needed 99.9% of the time, no prettification is needed 99.9% of the time, so the slow down in compile time for the implicit
* look ups is unlikely to be worth the benefit. Only a few types in practice usually need prettification for testing error message purposes, and those will be covered
* by the default `Prettifier`. A future version of ScalaTest will provide a simple mechanism to replace the default `Prettifier` with a
* custom one when a test actually fails.
* </p>
*/
trait Prettifier extends Serializable { // I removed the extends (Any => String), now that we are making this implicit.
/**
* Prettifies the passed object.
*/
def apply(o: Any): String
def apply(left: Any, right: Any): PrettyPair = {
AnyDiffer.difference(left, right, this)
}
}
private[scalactic] class DefaultPrettifier extends Prettifier {
protected def prettifyCollection(o: Any, processed: Set[Any]): String =
o match {
case many: Many[_] => "Many(" + many.toIterator.map(prettify(_, processed + many)).mkString(", ") + ")"
case anArray: Array[_] => "Array(" + anArray.map(prettify(_, processed + anArray)).mkString(", ") + ")"
case aWrappedArray: WrappedArray[_] => "Array(" + aWrappedArray.map(prettify(_, processed + aWrappedArray)).mkString(", ") + ")"
case a if ArrayHelper.isArrayOps(a) =>
val anArrayOps = ArrayHelper.asArrayOps(a).iterator
"Array(" + anArrayOps.map(prettify(_, processed + anArrayOps)).mkString(", ") + ")"
case i: Iterable[_] =>
val className = i.getClass.getName
if (className.startsWith("scala.xml.NodeSeq$") || className == "scala.xml.NodeBuffer" || className == "scala.xml.Elem")
i.mkString
else
ColCompatHelper.className(i) + "(" + i.toIterator.map {
case (key, value) if className.contains("Map") => prettify(key, processed + i) + " -> " + prettify(value, processed + i)
case other => prettify(other, processed + i)
}.mkString(", ") + ")" // toIterator is needed for consistent ordering
// SKIP-SCALATESTJS-START
case javaCol: java.util.Collection[_] =>
// By default java collection follows http://download.java.net/jdk7/archive/b123/docs/api/java/util/AbstractCollection.html#toString()
// let's do our best to prettify its element when it is not overriden
import scala.collection.JavaConverters._
val theToString = javaCol.toString
if (theToString.startsWith("[") && theToString.endsWith("]")) {
val itr = javaCol.iterator().asScala
"[" + itr.map(prettify(_, processed + javaCol)).mkString(", ") + "]"
}
else
theToString
case javaMap: java.util.Map[_, _] =>
// By default java map follows http://download.java.net/jdk7/archive/b123/docs/api/java/util/AbstractMap.html#toString()
// let's do our best to prettify its element when it is not overriden
import scala.collection.JavaConverters._
val theToString = javaMap.toString
if (theToString.startsWith("{") && theToString.endsWith("}")) {
val itr = javaMap.entrySet.iterator.asScala
"{" + itr.map { entry =>
prettify(entry.getKey, processed + javaMap) + "=" + prettify(entry.getValue, processed + javaMap)
}.mkString(", ") + "}"
}
else
theToString
// SKIP-SCALATESTJS,NATIVE-END
case caseClazz: Product if caseClazz.productArity != 0 =>
// If the case class's toString starts with .productPrefix its likely the .toString hasn't been
// overridden so lets use our custom prettifying otherwise we just use .toString.
if (caseClazz.toString.startsWith(s"${caseClazz.productPrefix}("))
s"${caseClazz.productPrefix}(" + caseClazz.productIterator.map(prettify(_, processed + caseClazz)).mkString(", ") + ")"
else
caseClazz.toString
case anythingElse => anythingElse.toString
}
protected def prettify(o: Any, processed: Set[Any]): String =
if (processed.contains(o))
throw new StackOverflowError("Cyclic relationship detected, let's fail early!")
else
o match {
case null => "null"
case aUnit: Unit => "<(), the Unit value>"
case aString: String => "\"" + aString + "\""
case aStringWrapper: org.scalactic.ColCompatHelper.StringOps => "\"" + aStringWrapper.mkString + "\""
case aChar: Char => "\'" + aChar + "\'"
case Some(e) => "Some(" + prettify(e, processed) + ")"
case Success(e) => "Success(" + prettify(e, processed) + ")"
case Left(e) => "Left(" + prettify(e, processed) + ")"
case Right(e) => "Right(" + prettify(e, processed) + ")"
case s: Symbol => "'" + s.name
case Good(e) => "Good(" + prettify(e, processed) + ")"
case Bad(e) => "Bad(" + prettify(e, processed) + ")"
case One(e) => "One(" + prettify(e, processed) + ")"
case other => prettifyCollection(other, processed)
}
def apply(o: Any): String = {
try {
prettify(o, Set.empty)
}
catch {
// This is in case of crazy designs like the one for scala.xml.Node. We handle Node
// specially above, but in case someone else creates a collection whose iterator
// returns itself, which will cause infinite recursion, at least we'll pop out and
// give them a string back.
case _: StackOverflowError => o.toString
}
}
}
private[scalactic] class TruncatingPrettifier(sizeLimit: SizeLimit) extends DefaultPrettifier {
private def dotDotDotIfTruncated(value: Boolean): String =
if (value) ", ..." else ""
override protected def prettifyCollection(o: Any, processed: Set[Any]): String = {
o match {
case many: Many[_] =>
val (taken, truncated) = if (many.size > sizeLimit.value) (many.toIterator.take(sizeLimit.value), true) else (many.toIterator, false)
"Many(" + taken.map(prettify(_, processed + many)).mkString(", ") + dotDotDotIfTruncated(truncated) + ")"
case anArray: Array[_] =>
val (taken, truncated) = if (anArray.size > sizeLimit.value) (anArray.take(sizeLimit.value), true) else (anArray, false)
"Array(" + taken.map(prettify(_, processed + anArray)).mkString(", ") + dotDotDotIfTruncated(truncated) + ")"
case aWrappedArray: WrappedArray[_] =>
val (taken, truncated) = if (aWrappedArray.size > sizeLimit.value) (aWrappedArray.take(sizeLimit.value), true) else (aWrappedArray, false)
"Array(" + taken.map(prettify(_, processed + aWrappedArray)).mkString(", ") + dotDotDotIfTruncated(truncated) + ")"
case a if ArrayHelper.isArrayOps(a) =>
val anArrayOps = ArrayHelper.asArrayOps(a)//
val (taken, truncated) = if (anArrayOps.size > sizeLimit.value) (anArrayOps.iterator.take(sizeLimit.value), true) else (anArrayOps.iterator, false)
"Array(" + taken.map(prettify(_, processed + anArrayOps)).mkString(", ") + dotDotDotIfTruncated(truncated) + ")"
case aGenMap: scala.collection.GenMap[_, _] =>
val (taken, truncated) = if (aGenMap.size > sizeLimit.value) (aGenMap.toIterator.take(sizeLimit.value), true) else (aGenMap.toIterator, false)
ColCompatHelper.className(aGenMap) + "(" +
(taken.map { case (key, value) => // toIterator is needed for consistent ordering
prettify(key, processed + aGenMap) + " -> " + prettify(value, processed + aGenMap)
}).mkString(", ") + dotDotDotIfTruncated(truncated) + ")"
case aGenTraversable: GenTraversable[_] =>
val (taken, truncated) = if (aGenTraversable.size > sizeLimit.value) (aGenTraversable.take(sizeLimit.value), true) else (aGenTraversable, false)
val className = aGenTraversable.getClass.getName
if (className.startsWith("scala.xml.NodeSeq$") || className == "scala.xml.NodeBuffer" || className == "scala.xml.Elem")
aGenTraversable.mkString
else
ColCompatHelper.className(aGenTraversable) + "(" + taken.toIterator.map(prettify(_, processed + aGenTraversable)).mkString(", ") + dotDotDotIfTruncated(truncated) + ")" // toIterator is needed for consistent ordering
// SKIP-SCALATESTJS-START
case javaCol: java.util.Collection[_] =>
// By default java collection follows http://download.java.net/jdk7/archive/b123/docs/api/java/util/AbstractCollection.html#toString()
// let's do our best to prettify its element when it is not overriden
import scala.collection.JavaConverters._
val theToString = javaCol.toString
if (theToString.startsWith("[") && theToString.endsWith("]")) {
val itr = javaCol.iterator().asScala
val (taken, truncated) = if (javaCol.size > sizeLimit.value) (itr.take(sizeLimit.value), true) else (itr, false)
"[" + taken.map(prettify(_, processed + javaCol)).mkString(", ") + dotDotDotIfTruncated(truncated) + "]"
}
else
theToString
case javaMap: java.util.Map[_, _] =>
// By default java map follows http://download.java.net/jdk7/archive/b123/docs/api/java/util/AbstractMap.html#toString()
// let's do our best to prettify its element when it is not overriden
import scala.collection.JavaConverters._
val theToString = javaMap.toString
if (theToString.startsWith("{") && theToString.endsWith("}")) {
val itr = javaMap.entrySet.iterator.asScala
val (taken, truncated) = if (javaMap.size > sizeLimit.value) (itr.take(sizeLimit.value), true) else (itr, false)
"{" + taken.map { entry =>
prettify(entry.getKey, processed + javaMap) + "=" + prettify(entry.getValue, processed + javaMap)
}.mkString(", ") + dotDotDotIfTruncated(truncated) + "}"
}
else
theToString
// SKIP-SCALATESTJS,NATIVE-END
case caseClazz: Product if caseClazz.productArity != 0 =>
// Unlike in DefaultPrettifier where we check if a custom `.toString` has been overridden, with
// TruncatingPrettifier the priority is truncating the enclosed data at all costs hence why we always
// truncate the inner fields.
s"${caseClazz.productPrefix}(" + caseClazz.productIterator.map(prettify(_, processed + caseClazz)).mkString(", ") + ")"
case anythingElse => anythingElse.toString
}
}
}
/**
* Companion object for `Prettifier` that provides a default `Prettifier` implementation.
*/
object Prettifier {
/**
* Constract a new `Prettifier` from a given partial function.
*
* @param fun a partial function with which to implement the apply method of the returned `Prettifier`.
*/
def apply(fun: PartialFunction[Any, String]): Prettifier =
new Prettifier {
def apply(o: Any): String = fun(o)
}
/**
* A default `Prettifier`.
*
* <p>
* This default `Prettifier` is used in ScalaTest to clarify error messages.
* </p>
*
* <p>
* It transforms:
* </p>
*
* <ul>
* <li>`Null` to: `null`</li>
* <li>`Unit` to: `<() the Unit value>`</li>
* <li>`String` to: `"string"` (the `toString` result surrounded by double quotes)</li>
* <li>`Char` to: `'c'` (the `toString` result surrounded by single quotes)</li>
* <li>`Array` to: `Array("1", "2", "3")`</li>
* <li>`scala.Some` to: `Some("3")`</li>
* <li>`scala.util.Left` to: `Left("3")`</li>
* <li>`scala.util.Right` to: `Right("3")`</li>
* <li>`scala.util.Success` to: `Success("3")`</li>
* <li>`org.scalactic.Good` to: `Good("3")`</li>
* <li>`org.scalactic.Bad` to: `Bad("3")`</li>
* <li>`org.scalactic.One` to: `One("3")`</li>
* <li>`org.scalactic.Many` to: `Many("1", "2", "3")`</li>
* <li>`scala.collection.GenTraversable` to: `List("1", "2", "3")`</li>
* <li>`java.util.Collection` to: `["1", "2", "3"]`</li>
* <li>`java.util.Map` to: `{1="one", 2="two", 3="three"}`</li>
* </ul>
*
*
* <p>
* For anything else, it returns the result of invoking `toString`.
* </p>
*/
implicit val default: Prettifier = new DefaultPrettifier()
/**
* Create a default prettifier instance with collection size limit.
*/
def truncateAt(limit: SizeLimit): Prettifier = new TruncatingPrettifier(limit)
/**
* A basic `Prettifier`.
*
* <p>
* This was the default `Prettifier` used in ScalaTest 2.0 release.
* </p>
*
* <p>
* It transforms:
* </p>
*
* <ul>
* <li>`Null` to: `null`</li>
* <li>`Unit` to: `<() the Unit value>`</li>
* <li>`String` to: `"string"` (the `toString` result surrounded by double quotes)</li>
* <li>`Char` to: `'c'` (the `toString` result surrounded by single quotes)</li>
* <li>`Array` to: `Array("1", "2", "3")`</li>
* <li>`scala.util.Some` to: `Some("3")`</li>
* </ul>
*
* <p>
* For anything else, it returns the result of invoking `toString`.
* </p>
*/
val basic = new BasicPrettifier
private[org] def diffStrings(s: String, t: String): Tuple2[String, String] = {
def findCommonPrefixLength(s: String, t: String): Int = {
val max = s.length.min(t.length) // the maximum potential size of the prefix
var i = 0
var found = false
while (i < max & !found) {
found = (s.charAt(i) != t.charAt(i))
if (!found)
i = i + 1
}
i
}
def findCommonSuffixLength(s: String, t: String): Int = {
val max = s.length.min(t.length) // the maximum potential size of the suffix
var i = 0
var found = false
while (i < max & !found) {
found = (s.charAt(s.length - 1 - i) != t.charAt(t.length - 1 - i))
if (!found)
i = i + 1
}
i
}
if (s != t) {
val commonPrefixLength = findCommonPrefixLength(s, t)
val commonSuffixLength = findCommonSuffixLength(s.substring(commonPrefixLength), t.substring(commonPrefixLength))
val prefix = s.substring(0, commonPrefixLength)
val suffix = if (s.length - commonSuffixLength < 0) "" else s.substring(s.length - commonSuffixLength)
val sMiddleEnd = s.length - commonSuffixLength
val tMiddleEnd = t.length - commonSuffixLength
val sMiddle = s.substring(commonPrefixLength, sMiddleEnd)
val tMiddle = t.substring(commonPrefixLength, tMiddleEnd)
val MaxContext = 20
val shortPrefix = if (commonPrefixLength > MaxContext) "..." + prefix.substring(prefix.length - MaxContext) else prefix
val shortSuffix = if (commonSuffixLength > MaxContext) suffix.substring(0, MaxContext) + "..." else suffix
(shortPrefix + "[" + sMiddle + "]" + shortSuffix, shortPrefix + "[" + tMiddle + "]" + shortSuffix)
}
else
(s, t)
}
private[org] def getObjectsForFailureMessage(a: Any, b: Any) =
a match {
case aStr: String => {
b match {
case bStr: String => {
diffStrings(aStr, bStr)
}
case _ => (a, b)
}
}
case _ => (a, b)
}
private[org] val lineSeparator: String = scala.compat.Platform.EOL
}
private[scalactic] class BasicPrettifier extends Prettifier {
def apply(o: Any): String =
o match {
case null => "null"
case aUnit: Unit => "<(), the Unit value>"
case aString: String => "\"" + aString + "\""
case aChar: Char => "\'" + aChar + "\'"
case anArray: Array[_] => prettifyArrays(anArray)
case aWrappedArray: WrappedArray[_] => prettifyArrays(aWrappedArray)
case anythingElse => anythingElse.toString
}
private def prettifyArrays(o: Any): String =
o match {
case arr: Array[_] => "Array(" + (arr map (a => prettifyArrays(a))).mkString(", ") + ")"
case wrappedArr: WrappedArray[_] => "Array(" + (wrappedArr map (a => prettifyArrays(a))).mkString(", ") + ")"
case _ => if (o != null) o.toString else "null"
}
}