Skip to content

Commit

Permalink
Escape % in assertion messages
Browse files Browse the repository at this point in the history
  • Loading branch information
jackkoenig committed Feb 9, 2017
1 parent abb05d0 commit 40c19ed
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
7 changes: 4 additions & 3 deletions chiselFrontend/src/main/scala/chisel3/core/Assert.scala
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,11 @@ object assert { // scalastyle:ignore object.name

def apply_impl_do(cond: Bool, line: String, message: Option[String], data: Bits*)(implicit sourceInfo: SourceInfo) {
when (!(cond || Builder.forcedModule.reset)) {
message match {
case Some(str) => printf.printfWithoutReset(s"Assertion failed: $str\n at $line\n", data:_*)
case None => printf.printfWithoutReset(s"Assertion failed\n at $line\n", data:_*)
val fmt = message match {
case Some(str) => s"Assertion failed: $str\n at $line\n"
case None => s"Assertion failed\n at $line\n"
}
printf.printfWithoutReset(fmt.replaceAll("%", "%%"), data:_*)
pushCommand(Stop(sourceInfo, Node(Builder.forcedModule.clock), 1))
}
}
Expand Down
9 changes: 9 additions & 0 deletions src/test/scala/chiselTests/Assert.scala
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ class PipelinedResetTester extends BasicTester {
}
}

class ModuloAssertTester extends BasicTester {
assert((4.U % 2.U) === 0.U)
assert(1.U === 1.U, "I'm 110% sure this will succeed")
stop()
}

class AssertSpec extends ChiselFlatSpec {
"A failing assertion" should "fail the testbench" in {
assert(!runTester{ new FailingAssertTester })
Expand All @@ -54,4 +60,7 @@ class AssertSpec extends ChiselFlatSpec {
"An assertion" should "not assert until we come out of reset" in {
assertTesterPasses{ new PipelinedResetTester }
}
"Assertions" should "allow the modulo operator % in the message" in {
assertTesterPasses{ new ModuloAssertTester }
}
}

0 comments on commit 40c19ed

Please sign in to comment.