com-lihaoyi / PPrint

Pretty-printing value, types and type-signatures in Scala
https://com-lihaoyi.github.io/PPrint/
MIT License
227 stars 39 forks source link

pprint.apply fails for arbitrary Strings #35

Open mattelacchiato opened 4 years ago

mattelacchiato commented 4 years ago

We are using pprint in property based testing, where we create arbitrary strings. Sadly, pprint fails on some of those characters: Message: Unknown ansi-escape at index 3 inside string cannot be parsed into an fansi.Str

Example:

import org.scalatest.WordSpec
import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks

class PprintUnitTest extends WordSpec with ScalaCheckPropertyChecks {
  "pprint" in {
    forAll(minSuccessful(5000000)) { s: String =>
      pprint.apply(s)
    }
  }
}
lj-ditrapani commented 2 years ago

Ran into this same issue. Using "com.lihaoyi" %% "pprint" % "0.7.1". Library chokes on certain strings. Had to remove use from production.

TidyMaze commented 1 year ago

same thing here, crash in production, we are still using the lib but with a safe wrapper

object PrettyPrintHelper extends LazyLogging {

  /**
   * Safe wrapper around pprint.
   * This method exists because of pprint not supporting some non-printable characters,
   * exploding with an exception when attempting to do so.
   *
   * See issue: https://github.com/com-lihaoyi/PPrint/issues/35
   */
  def safePrettyPrint(anyValue: Any): String = {
    Try(BlackWhite.apply(anyValue).toString()).fold(err => {
      logger.error(s"Failed to pretty print $anyValue with message: ${err.getMessage}", err)
      anyValue.toString
    }, identity)
  }

}