Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify Prettifier.truncateAt to work on case classes #2155

Merged
merged 2 commits into from Aug 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 Expand Up @@ -402,6 +406,14 @@ class PrettifierSpec extends funspec.AnyFunSpec with matchers.should.Matchers {
val prettifier = Prettifier.truncateAt(SizeLimit(2))
prettifier(col) shouldBe "List(1, 2, ...)"
}

case class CaseClazz(data: List[Int])

it("should truncate collection inside of a case class when used with Prettifier.truncateAt") {
val caseClass = CaseClazz(List(1, 2, 3))
val prettifier = Prettifier.truncateAt(SizeLimit(2))
prettifier(caseClass) shouldBe "CaseClazz(List(1, 2, ...))"
}
}
}

12 changes: 12 additions & 0 deletions 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,6 +284,11 @@ private[scalactic] class TruncatingPrettifier(sizeLimit: SizeLimit) extends Defa
else
theToString
// SKIP-SCALATESTJS,NATIVE-END
case caseClazz: Product if caseClazz.productArity != 0 =>
// Unlike in DefaultPrettifier where we check if a custom `.toString` has been overridden, with
// TruncatingPrettifier the priority is truncating the enclosed data at all costs hence why we always
// truncate the inner fields.
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