Skip to content

Commit

Permalink
Also add case class prettifying to DefaultPrettifier
Browse files Browse the repository at this point in the history
  • Loading branch information
mdedetrich committed Aug 6, 2022
1 parent 8db0199 commit 7fc9ae6
Show file tree
Hide file tree
Showing 15 changed files with 441 additions and 386 deletions.
Expand Up @@ -345,6 +345,10 @@ class PrettifierSpec extends funspec.AnyFunSpec with matchers.should.Matchers {
it("should pretty print nested string Java Map") {
Prettifier.default(javaSortedMap(Entry("akey", javaSortedMap(Entry(1, "one"), Entry(2, "two"), Entry(3, "three"))))) should be ("{\"akey\"={1=\"one\", 2=\"two\", 3=\"three\"}}")
}
case class CaseClazzWithArray(data: Array[Int])
it("should pretty print data inside a case class") {
Prettifier.default(CaseClazzWithArray(Array(1,2,3))) should be ("CaseClazzWithArray(Array(1, 2, 3))")
}
it("should pretty print xml <a></a>") {
Prettifier.default(<a></a>) should be ("<a></a>")
}
Expand Down
9 changes: 8 additions & 1 deletion jvm/scalactic/src/main/scala/org/scalactic/Prettifier.scala
Expand Up @@ -177,6 +177,13 @@ private[scalactic] class DefaultPrettifier extends Prettifier {
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
}

Expand Down Expand Up @@ -277,7 +284,7 @@ private[scalactic] class TruncatingPrettifier(sizeLimit: SizeLimit) extends Defa
else
theToString
// SKIP-SCALATESTJS,NATIVE-END
case caseClazz: Product =>
case caseClazz: Product if caseClazz.productArity != 0 =>
s"${caseClazz.productPrefix}(" + caseClazz.productIterator.map(prettify(_, processed + caseClazz)).mkString(", ") + ")"
case anythingElse => anythingElse.toString
}
Expand Down
Expand Up @@ -345,10 +345,11 @@ class AMatcherSpec extends funspec.AnyFunSpec {

it("should throw TestFailedException with correct stack depth and message when 'should be a' assertion failed") {
val tom = Person("Tom", 60)
val tomPrettified = "Person(\"Tom\", 60)"
val e = intercept[exceptions.TestFailedException] {
tom should be a youngMan
}
e.message should be (Some(s"${tom.toString()} was not a young man"))
e.message should be (Some(s"$tomPrettified was not a young man"))
e.failedCodeFileName should be (Some("AMatcherSpec.scala"))
e.failedCodeLineNumber should be (Some(thisLineNumber - 4))
}
Expand All @@ -359,10 +360,11 @@ class AMatcherSpec extends funspec.AnyFunSpec {

it("should throw TestFailedException with correct stack depth and message when 'should not be a' assertion failed") {
val tom = Person("Tom", 30)
val tomPrettified = "Person(\"Tom\", 30)"
val e = intercept[exceptions.TestFailedException] {
tom should not be a (youngMan)
}
e.message should be (Some(s"${tom.toString()} was a young man"))
e.message should be (Some(s"$tomPrettified was a young man"))
e.failedCodeFileName should be (Some("AMatcherSpec.scala"))
e.failedCodeLineNumber should be (Some(thisLineNumber - 4))
}
Expand Down
Expand Up @@ -346,10 +346,11 @@ class AnMatcherSpec extends funspec.AnyFunSpec {

it("should throw TestFailedException with correct stack depth and message when 'should be a' assertion failed") {
val tom = Person("Tom", 30)
val tomPrettified = "Person(\"Tom\", 30)"
val e = intercept[exceptions.TestFailedException] {
tom should be an oldMan
}
e.message should be (Some(s"${tom.toString()} was not an old man"))
e.message should be (Some(s"$tomPrettified was not an old man"))
e.failedCodeFileName should be (Some("AnMatcherSpec.scala"))
e.failedCodeLineNumber should be (Some(thisLineNumber - 4))
}
Expand All @@ -360,10 +361,11 @@ class AnMatcherSpec extends funspec.AnyFunSpec {

it("should throw TestFailedException with correct stack depth and message when 'should not be a' assertion failed") {
val tom = Person("Tom", 60)
val tomPrettified = "Person(\"Tom\", 60)"
val e = intercept[exceptions.TestFailedException] {
tom should not be an (oldMan)
}
e.message should be (Some(s"${tom.toString()} was an old man"))
e.message should be (Some(s"$tomPrettified was an old man"))
e.failedCodeFileName should be (Some("AnMatcherSpec.scala"))
e.failedCodeLineNumber should be (Some(thisLineNumber - 4))
}
Expand Down

0 comments on commit 7fc9ae6

Please sign in to comment.