Skip to content

Commit

Permalink
Merge pull request #818 from morgen-peschke/issue-817
Browse files Browse the repository at this point in the history
Add a TestTransform to append to a failure message
  • Loading branch information
tgodzik authored Sep 11, 2024
2 parents 9902e66 + 8cafaaa commit 7eb4a9a
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 1 deletion.
1 change: 1 addition & 0 deletions munit/shared/src/main/scala/munit/FailExceptionLike.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package munit
trait FailExceptionLike[T <: AssertionError] extends Serializable {
self: AssertionError =>
def withMessage(message: String): T
def updateMessage(f: String => String): T = withMessage(f(getMessage))
def location: Location
def isStackTracesEnabled: Boolean
}
35 changes: 35 additions & 0 deletions munit/shared/src/main/scala/munit/TestTransforms.scala
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,39 @@ trait TestTransforms { this: BaseFunSuite =>
}
)

/**
* Optionally augment a failure with additional information.
*
* Failures that are not `FailExceptionLike` subclasses will be wrapped, if needed.
*/
def munitAppendToFailureMessage(
buildSuffix: Test => Option[String]
): TestTransform =
new TestTransform(
"failureSuffix",
{ t =>
t.withBodyMap(
_.transformCompat {
case s @ Success(_) => s
case f @ Failure(exception) =>
buildSuffix(t).fold(f) { suffix =>
def append(existing: String): String =
if (existing.endsWith("\n")) s"$existing$suffix\n"
else s"$existing\n$suffix"

Failure(Exceptions.rootCause(exception) match {
case fel: FailExceptionLike[_] => fel.updateMessage(append)
case e =>
new FailException(
message = append(e.getMessage),
cause = e,
isStackTracesEnabled = false,
location = t.location
)
})
}
}(munitExecutionContext)
)
}
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,46 @@ package munit

class TestTransformFrameworkSuite extends munit.FunSuite {
override val munitTestTransforms: List[TestTransform] = List(
new TestTransform("ok", test => test.withName(test.name + "-ok"))
new TestTransform(
"ok",
test =>
if (test.name == "hello") test.withName(test.name + "-ok") else test
),
munitAppendToFailureMessage(t =>
if (t.name.startsWith("suffix")) Some("==> extra info") else None
)
)

test("hello") {}

test("suffix-success") {}
test("suffix-fail") {
fail("boom")
}
test("suffix-assertEquals") {
assertEquals(0, 1)
}
}
object TestTransformFrameworkSuite
extends FrameworkTest(
classOf[TestTransformFrameworkSuite],
"""|==> success munit.TestTransformFrameworkSuite.hello-ok
|==> success munit.TestTransformFrameworkSuite.suffix-success
|==> failure munit.TestTransformFrameworkSuite.suffix-fail - /scala/munit/TestTransformFrameworkSuite.scala:19 boom
|18: test("suffix-fail") {
|19: fail("boom")
|20: }
|==> extra info
|==> failure munit.TestTransformFrameworkSuite.suffix-assertEquals - /scala/munit/TestTransformFrameworkSuite.scala:22
|21: test("suffix-assertEquals") {
|22: assertEquals(0, 1)
|23: }
|values are not the same
|=> Obtained
|0
|=> Diff (- obtained, + expected)
|-0
|+1
|==> extra info
|""".stripMargin
)

0 comments on commit 7eb4a9a

Please sign in to comment.