trait
Prettifier extends (Any) ⇒ String
Abstract Value Members
-
abstract
def
apply(v1: Any): String
Concrete Value Members
-
final
def
!=(arg0: Any): Boolean
-
final
def
##(): Int
-
final
def
==(arg0: Any): Boolean
-
def
andThen[A](g: (String) ⇒ A): (Any) ⇒ A
-
final
def
asInstanceOf[T0]: T0
-
def
clone(): AnyRef
-
def
compose[A](g: (A) ⇒ Any): (A) ⇒ String
-
final
def
eq(arg0: AnyRef): Boolean
-
def
equals(arg0: Any): Boolean
-
def
finalize(): Unit
-
final
def
getClass(): Class[_]
-
def
hashCode(): Int
-
final
def
isInstanceOf[T0]: Boolean
-
final
def
ne(arg0: AnyRef): Boolean
-
final
def
notify(): Unit
-
final
def
notifyAll(): Unit
-
final
def
synchronized[T0](arg0: ⇒ T0): T0
-
def
toString(): String
-
final
def
wait(): Unit
-
final
def
wait(arg0: Long, arg1: Int): Unit
-
final
def
wait(arg0: Long): Unit
Inherited from (Any) ⇒ String
Value Members
-
abstract
def
apply(v1: Any): String
-
def
andThen[A](g: (String) ⇒ A): (Any) ⇒ A
-
def
compose[A](g: (A) ⇒ Any): (A) ⇒ String
-
def
toString(): String
Inherited from AnyRef
Value Members
-
final
def
!=(arg0: Any): Boolean
-
final
def
##(): Int
-
final
def
==(arg0: Any): Boolean
-
def
clone(): AnyRef
-
final
def
eq(arg0: AnyRef): Boolean
-
def
equals(arg0: Any): Boolean
-
def
finalize(): Unit
-
final
def
getClass(): Class[_]
-
def
hashCode(): Int
-
final
def
ne(arg0: AnyRef): Boolean
-
final
def
notify(): Unit
-
final
def
notifyAll(): Unit
-
final
def
synchronized[T0](arg0: ⇒ T0): T0
-
final
def
wait(): Unit
-
final
def
wait(arg0: Long, arg1: Int): Unit
-
final
def
wait(arg0: Long): Unit
Inherited from Any
Value Members
-
final
def
asInstanceOf[T0]: T0
-
final
def
isInstanceOf[T0]: Boolean
Ungrouped
-
abstract
def
apply(v1: Any): String
-
final
def
!=(arg0: Any): Boolean
-
final
def
##(): Int
-
final
def
==(arg0: Any): Boolean
-
def
andThen[A](g: (String) ⇒ A): (Any) ⇒ A
-
final
def
asInstanceOf[T0]: T0
-
def
clone(): AnyRef
-
def
compose[A](g: (A) ⇒ Any): (A) ⇒ String
-
final
def
eq(arg0: AnyRef): Boolean
-
def
equals(arg0: Any): Boolean
-
def
finalize(): Unit
-
final
def
getClass(): Class[_]
-
def
hashCode(): Int
-
final
def
isInstanceOf[T0]: Boolean
-
final
def
ne(arg0: AnyRef): Boolean
-
final
def
notify(): Unit
-
final
def
notifyAll(): Unit
-
final
def
synchronized[T0](arg0: ⇒ T0): T0
-
def
toString(): String
-
final
def
wait(): Unit
-
final
def
wait(arg0: Long, arg1: Int): Unit
-
final
def
wait(arg0: Long): Unit
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 atoString
that will convert any object to aString
representation. ThisString
representation is primarily intended for programmers, and is usually sufficient. However, sometimes it can be helpful to provide an alternative implementation oftoString
for certain types. For example, thetoString
implementation onString
prints out the value of theString
:If the error message that resulted from comparing
Int
1 withString
"1"
in a ScalaTest assertion usedtoString
, therefore, the error message would be: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 aString
stoString
result:Thus the error message resulting from comparing
Int
1 withString
"1"
, in a ScalaTest assertion is:If you wish to prettify an object in production code, for example, to issue a profoundly clear debug message, you can use
PrettyMethods
and invokepretty
. Here's an example:For example, the default
Prettifier
,Prettifier.default
, transforms:Null
to:null
Unit
to:<() the Unit value>
String
to:"string"
(thetoString
result surrounded by double quotes)Char
to:'c'
(thetoString
result surrounded by single quotes)Array
to:Array("1", "2", "3")
scala.Some
to:Some("3")
scala.util.Left
to:Left("3")
scala.util.Right
to:Right("3")
scala.util.Success
to:Success("3")
org.scalactic.Good
to:Good("3")
org.scalactic.Bad
to:Bad("3")
org.scalactic.One
to:One("3")
org.scalactic.Many
to:Many("1", "2", "3")
scala.collection.GenTraversable
to:List("1", "2", "3")
java.util.Collection
to:["1", "2", "3"]
java.util.Map
to:{1="one", 2="two", 3="three"}
For anything else, the default
Prettifier
returns the result of invokingtoString
.Note:
Prettifier
is not parameterized (i.e.,Prettifier[T]
, whereT
is the type to prettify) because assertions (including matcher expressions) in ScalaTest would then need to look upPrettifier
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 defaultPrettifier
. A future version of ScalaTest will provide a simple mechanism to replace the defaultPrettifier
with a custom one when a test actually fails.