Skip to content

Commit

Permalink
Pass the size limit to default prettifier through parameter.
Browse files Browse the repository at this point in the history
  • Loading branch information
cheeseng committed Jun 12, 2022
1 parent 112ecfd commit 5b3f1b7
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 83 deletions.
168 changes: 85 additions & 83 deletions jvm/scalactic/src/main/scala/org/scalactic/Prettifier.scala
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,90 @@ trait Prettifier extends Serializable { // I removed the extends (Any => String)
}
}

private[scalactic] class DefaultPrettifier(sizeLimit: SizeLimit) extends Prettifier {

val colSizeLimit: Int = sizeLimit.value

private 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 many: Many[_] => "Many(" + (if (colSizeLimit > 0) many.toIterator.take(colSizeLimit) else many.toIterator).map(prettify(_, processed + many)).mkString(", ") + ")"
case anArray: Array[_] => "Array(" + (if (colSizeLimit > 0) anArray.take(colSizeLimit) else anArray).map(prettify(_, processed + anArray)).mkString(", ") + ")"
case aWrappedArray: WrappedArray[_] => "Array(" + (if (colSizeLimit > 0) aWrappedArray.take(colSizeLimit) else aWrappedArray).map(prettify(_, processed + aWrappedArray)).mkString(", ") + ")"
case a if ArrayHelper.isArrayOps(a) =>
val anArrayOps = ArrayHelper.asArrayOps(a).iterator
"Array(" + (if (colSizeLimit > 0) anArrayOps.take(colSizeLimit) else anArrayOps).map(prettify(_, processed + anArrayOps)).mkString(", ") + ")"
case aGenMap: scala.collection.GenMap[_, _] =>
ColCompatHelper.className(aGenMap) + "(" +
((if (colSizeLimit > 0) aGenMap.take(colSizeLimit) else aGenMap).toIterator.map { case (key, value) => // toIterator is needed for consistent ordering
prettify(key, processed + aGenMap) + " -> " + prettify(value, processed + aGenMap)
}).mkString(", ") + ")"
case aGenTraversable: GenTraversable[_] =>
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) + "(" + (if (colSizeLimit > 0) aGenTraversable.take(colSizeLimit) else aGenTraversable).toIterator.map(prettify(_, processed + aGenTraversable)).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
"[" + (if (colSizeLimit > 0) itr.take(colSizeLimit) else 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
"{" + (if (colSizeLimit > 0) itr.take(colSizeLimit) else itr).map { entry =>
prettify(entry.getKey, processed + javaMap) + "=" + prettify(entry.getValue, processed + javaMap)
}.mkString(", ") + "}"
}
else
theToString
// SKIP-SCALATESTJS,NATIVE-END
case anythingElse => anythingElse.toString
}

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
}
}

}

/**
* Companion object for `Prettifier` that provides a default `Prettifier` implementation.
*/
Expand Down Expand Up @@ -181,89 +265,7 @@ object Prettifier {
* For anything else, it returns the result of invoking `toString`.
* </p>
*/
implicit val default: Prettifier =
new Prettifier {

val colSizeLimit: Int = Option(System.getProperty("scalactic.prettifier.collection.size.limit")).map(_.toInt).getOrElse(0)

private 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 many: Many[_] => "Many(" + (if (colSizeLimit > 0) many.toIterator.take(colSizeLimit) else many.toIterator).map(prettify(_, processed + many)).mkString(", ") + ")"
case anArray: Array[_] => "Array(" + (if (colSizeLimit > 0) anArray.take(colSizeLimit) else anArray).map(prettify(_, processed + anArray)).mkString(", ") + ")"
case aWrappedArray: WrappedArray[_] => "Array(" + (if (colSizeLimit > 0) aWrappedArray.take(colSizeLimit) else aWrappedArray).map(prettify(_, processed + aWrappedArray)).mkString(", ") + ")"
case a if ArrayHelper.isArrayOps(a) =>
val anArrayOps = ArrayHelper.asArrayOps(a).iterator
"Array(" + (if (colSizeLimit > 0) anArrayOps.take(colSizeLimit) else anArrayOps).map(prettify(_, processed + anArrayOps)).mkString(", ") + ")"
case aGenMap: scala.collection.GenMap[_, _] =>
ColCompatHelper.className(aGenMap) + "(" +
((if (colSizeLimit > 0) aGenMap.take(colSizeLimit) else aGenMap).toIterator.map { case (key, value) => // toIterator is needed for consistent ordering
prettify(key, processed + aGenMap) + " -> " + prettify(value, processed + aGenMap)
}).mkString(", ") + ")"
case aGenTraversable: GenTraversable[_] =>
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) + "(" + (if (colSizeLimit > 0) aGenTraversable.take(colSizeLimit) else aGenTraversable).toIterator.map(prettify(_, processed + aGenTraversable)).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
"[" + (if (colSizeLimit > 0) itr.take(colSizeLimit) else 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
"{" + (if (colSizeLimit > 0) itr.take(colSizeLimit) else itr).map { entry =>
prettify(entry.getKey, processed + javaMap) + "=" + prettify(entry.getValue, processed + javaMap)
}.mkString(", ") + "}"
}
else
theToString
// SKIP-SCALATESTJS,NATIVE-END
case anythingElse => anythingElse.toString
}

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
}
}
}
implicit val default: Prettifier = new DefaultPrettifier(SizeLimit(0))

/**
* A basic `Prettifier`.
Expand Down
21 changes: 21 additions & 0 deletions jvm/scalactic/src/main/scala/org/scalactic/SizeLimit.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright 2001-2022 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

/**
* Size limit value class.
*/
case class SizeLimit(value: Int)

0 comments on commit 5b3f1b7

Please sign in to comment.