org.scalautils

Prettifier

trait Prettifier extends (Any) ⇒ String

A function that given any object will produce a “pretty” string representation of that object, where “pretty” is in the eye of the implementer.

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:

scala> "1".toString
res0: String = 1

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:

1 did not equal 1

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 Strings toString result:

scala> import org.scalautils._
import org.scalautils._

scala> Prettifier.default("1") res1: String = "1"

Thus the error message resulting from comparing Int 1 with String "1", in a ScalaTest assertion is:

1 did not equal "1"

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:

scala> import PrettyMethods._
import PrettyMethods._

scala> 1.pretty res2: String = 1

scala> "1".pretty res3: String = "1"

For example, the default Prettifier, Prettifier.default, transforms:

For anything else, the default Prettifier returns the result of invoking toString.

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 Prettifiers 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.

Source
Prettifier.scala
Linear Supertypes
(Any) ⇒ String, AnyRef, Any
Ordering
  1. Alphabetic
  2. By inheritance
Inherited
  1. Prettifier
  2. Function1
  3. AnyRef
  4. Any
  1. Hide All
  2. Show all
Learn more about member selection
Visibility
  1. Public
  2. All

Abstract Value Members

  1. abstract def apply(v1: Any): String

    Definition Classes
    Function1

Concrete Value Members

  1. final def !=(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  2. final def !=(arg0: Any): Boolean

    Definition Classes
    Any
  3. final def ##(): Int

    Definition Classes
    AnyRef → Any
  4. final def ==(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  5. final def ==(arg0: Any): Boolean

    Definition Classes
    Any
  6. def andThen[A](g: (String) ⇒ A): (Any) ⇒ A

    Definition Classes
    Function1
    Annotations
    @unspecialized()
  7. final def asInstanceOf[T0]: T0

    Definition Classes
    Any
  8. def clone(): AnyRef

    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws()
  9. def compose[A](g: (A) ⇒ Any): (A) ⇒ String

    Definition Classes
    Function1
    Annotations
    @unspecialized()
  10. final def eq(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  11. def equals(arg0: Any): Boolean

    Definition Classes
    AnyRef → Any
  12. def finalize(): Unit

    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws()
  13. final def getClass(): Class[_]

    Definition Classes
    AnyRef → Any
  14. def hashCode(): Int

    Definition Classes
    AnyRef → Any
  15. final def isInstanceOf[T0]: Boolean

    Definition Classes
    Any
  16. final def ne(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  17. final def notify(): Unit

    Definition Classes
    AnyRef
  18. final def notifyAll(): Unit

    Definition Classes
    AnyRef
  19. final def synchronized[T0](arg0: ⇒ T0): T0

    Definition Classes
    AnyRef
  20. def toString(): String

    Definition Classes
    Function1 → AnyRef → Any
  21. final def wait(): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws()
  22. final def wait(arg0: Long, arg1: Int): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws()
  23. final def wait(arg0: Long): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws()

Inherited from (Any) ⇒ String

Inherited from AnyRef

Inherited from Any

Ungrouped