Skip to content

Commit

Permalink
Add label support for assertTrue (zio#5743) (zio#6275)
Browse files Browse the repository at this point in the history
  • Loading branch information
jupposessho authored Jan 25, 2022
1 parent 2b20488 commit 6639501
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ object DefaultTestReporterSpec extends ZIOBaseSpec {
test("correctly reports labeled failures") {
assertM(runLog(test7))(equalTo(test7Expected.mkString + "\n" + reportStats(0, 0, 1)))
},
test("correctly reports labeled failures for assertTrue") {
for {
log <- runLog(test9)
} yield assertTrue(log.contains("""?? "third""""), log.contains("""?? "fourth""""))
},
test("correctly reports negated failures") {
assertM(runLog(test8))(equalTo(test8Expected.mkString + "\n" + reportStats(0, 0, 1)))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,13 @@ object ReportingTestUtils {
withOffset(2)(assertSourceLocation() + "\n")
)

def test9(implicit trace: ZTraceElement): ZSpec[Any, Nothing] = test("labeled failures") {
assertTrue(1 == 1).map(_.label("first")) &&
assertTrue(1 == 1).map(_.label("second")) &&
assertTrue(1 == 0).map(_.label("third")) &&
assertTrue(1 == 0).map(_.label("fourth"))
}

def suite1(implicit trace: ZTraceElement): Spec[Any, TestFailure[Nothing], TestSuccess] =
suite("Suite1")(test1, test2)
def suite1Expected(implicit trace: ZTraceElement): Vector[String] = Vector(
Expand Down
9 changes: 6 additions & 3 deletions test/shared/src/main/scala/zio/test/AssertionResult.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ sealed trait AssertionResult { self =>
case result: FailureDetailsResult =>
result.copy(failureDetails = result.failureDetails.label(label))
case result: AssertionResult.TraceResult =>
result
result.copy(label = Some(label))
}

def setGenFailureDetails(details: GenFailureDetails): AssertionResult =
Expand All @@ -27,6 +27,9 @@ sealed trait AssertionResult { self =>
object AssertionResult {
case class FailureDetailsResult(failureDetails: FailureDetails, genFailureDetails: Option[GenFailureDetails] = None)
extends AssertionResult
case class TraceResult(trace: Trace[Boolean], genFailureDetails: Option[GenFailureDetails] = None)
extends AssertionResult
case class TraceResult(
trace: Trace[Boolean],
genFailureDetails: Option[GenFailureDetails] = None,
label: Option[String] = None
) extends AssertionResult
}
14 changes: 7 additions & 7 deletions test/shared/src/main/scala/zio/test/DefaultTestReporter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ object DefaultTestReporter {
result
.fold[Option[TestResult]] {
case result: AssertionResult.FailureDetailsResult => Some(BoolAlgebra.success(result))
case AssertionResult.TraceResult(trace, genFailureDetails) =>
case AssertionResult.TraceResult(trace, genFailureDetails, label) =>
Trace
.prune(trace, false)
.map(a => BoolAlgebra.success(AssertionResult.TraceResult(a, genFailureDetails)))
.map(a => BoolAlgebra.success(AssertionResult.TraceResult(a, genFailureDetails, label)))
}(
{
case (Some(a), Some(b)) => Some(a && b)
Expand Down Expand Up @@ -185,12 +185,12 @@ object DefaultTestReporter {

def renderAssertionResult(assertionResult: AssertionResult, offset: Int): Message =
assertionResult match {
case AssertionResult.TraceResult(trace, genFailureDetails) =>
case AssertionResult.TraceResult(trace, genFailureDetails, label) =>
val failures = FailureCase.fromTrace(trace)
failures
.map(fc =>
renderGenFailureDetails(genFailureDetails, offset) ++
Message(renderFailureCase(fc, offset))
Message(renderFailureCase(fc, offset, label))
)
.foldLeft(Message.empty)(_ ++ _)

Expand All @@ -199,7 +199,7 @@ object DefaultTestReporter {
renderFailureDetails(failureDetails, offset)
}

def renderFailureCase(failureCase: FailureCase, offset: Int): Chunk[Line] =
def renderFailureCase(failureCase: FailureCase, offset: Int, testLabel: Option[String]): Chunk[Line] =
failureCase match {
case FailureCase(errorMessage, codeString, location, path, _, nested, _) =>
val errorMessageLines =
Expand All @@ -210,8 +210,8 @@ object DefaultTestReporter {

val result =
errorMessageLines ++
Chunk(Line.fromString(codeString)) ++
nested.flatMap(renderFailureCase(_, offset)).map(_.withOffset(1)) ++
Chunk(Line.fromString(testLabel.fold(codeString)(l => s"""$codeString ?? "$l""""))) ++
nested.flatMap(renderFailureCase(_, offset, None)).map(_.withOffset(1)) ++
Chunk.fromIterable(path.flatMap { case (label, value) =>
Chunk.fromIterable(PrettyPrint(value).split("\n").map(primary(_).toLine)) match {
case head +: lines => (dim(s"${label.trim} = ") +: head) +: lines
Expand Down

0 comments on commit 6639501

Please sign in to comment.