Skip to content

Commit

Permalink
simulator: add factory method for giving failed expect sourceInfo/ext…
Browse files Browse the repository at this point in the history
…raContext.

Co-authored-by: Jack Koenig <jack.koenig3@gmail.com>
  • Loading branch information
kivikakk and jackkoenig committed Jun 6, 2024
1 parent 3570d52 commit 77ebff8
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions src/main/scala/chisel3/simulator/PeekPokeAPI.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,21 @@ import chisel3.internal.ExceptionHelpers
object PeekPokeAPI extends PeekPokeAPI

trait PeekPokeAPI {
case class FailedExpectationException[T](
sourceInfo: SourceInfo,
extraContext: Seq[String],
observed: T,
expected: T,
message: String)
extends Exception(
s"Failed Expectation: Observed value '$observed' != $expected. " +
s"$message ${sourceInfo.makeMessage(x => x)}" +
(if (extraContext.nonEmpty) s"\n${extraContext.mkString("\n")}" else "")
)
case class FailedExpectationException[T](observed: T, expected: T, message: String)
extends Exception(s"Failed Expectation: Observed value '$observed' != $expected. $message")
object FailedExpectationException {
def apply[T](
observed: T,
expected: T,
message: String,
sourceInfo: SourceInfo,
extraContext: Seq[String]
): FailedExpectationException[T] = {
val fullMessage = s"$message ${sourceInfo.makeMessage(x => x)}" +
(if (extraContext.nonEmpty) s"\n${extraContext.mkString("\n")}" else "")
new FailedExpectationException(observed, expected, fullMessage)
}
}

implicit class testableClock(clock: Clock) {
def step(cycles: Int = 1): Unit = {
Expand Down Expand Up @@ -157,11 +161,11 @@ trait PeekPokeAPI {
Seq()
}
throw FailedExpectationException(
sourceInfo,
extraContext,
observed,
expected,
buildMessage(observed, expected)
buildMessage(observed, expected),
sourceInfo,
extraContext
)
}
}
Expand Down

0 comments on commit 77ebff8

Please sign in to comment.