summaryrefslogtreecommitdiff
path: root/src/test/scala/chiselTests/stage/ChiselAnnotationsSpec.scala
blob: 9dac820c7c5cde6bdec2b6fdbf26a39a9af0c279 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// SPDX-License-Identifier: Apache-2.0

package chiselTests.stage

import chisel3._
import chisel3.stage.{ChiselCircuitAnnotation, ChiselGeneratorAnnotation, DesignAnnotation}
import firrtl.options.OptionsException
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

class ChiselAnnotationsSpecFoo extends RawModule {
  val in = IO(Input(Bool()))
  val out = IO(Output(Bool()))
  out := ~in
}

class ChiselAnnotationsSpecBaz(name: String) extends ChiselAnnotationsSpecFoo {
  override val desiredName = name
}

class ChiselAnnotationsSpecQux extends ChiselAnnotationsSpecFoo {
  /* This printf requires an implicit clock and reset, but RawModule has none. This will thereby fail elaboration. */
  printf("hello")
}

class ChiselAnnotation

class ChiselAnnotationsSpec extends AnyFlatSpec with Matchers {

  behavior.of("ChiselGeneratorAnnotation elaboration")

  it should "elaborate to a ChiselCircuitAnnotation" in {
    val annotation = ChiselGeneratorAnnotation(() => new ChiselAnnotationsSpecFoo)
    val res = annotation.elaborate
    res(0) shouldBe a[ChiselCircuitAnnotation]
    res(1) shouldBe a[DesignAnnotation[ChiselAnnotationsSpecFoo]]
  }

  it should "throw an exception if elaboration fails" in {
    val annotation = ChiselGeneratorAnnotation(() => new ChiselAnnotationsSpecQux)
    intercept[ChiselException] { annotation.elaborate }
  }

  behavior.of("ChiselGeneratorAnnotation when stringly constructing from Module names")

  it should "elaborate from a String" in {
    val annotation = ChiselGeneratorAnnotation("chiselTests.stage.ChiselAnnotationsSpecFoo")
    val res = annotation.elaborate
    res(0) shouldBe a[ChiselCircuitAnnotation]
    res(1) shouldBe a[DesignAnnotation[ChiselAnnotationsSpecFoo]]
  }

  it should "throw an exception if elaboration from a String refers to nonexistant class" in {
    val bar = "chiselTests.stage.ChiselAnnotationsSpecBar"
    val annotation = ChiselGeneratorAnnotation(bar)
    intercept[OptionsException] { annotation.elaborate }.getMessage should startWith(s"Unable to locate module '$bar'")
  }

  it should "throw an exception if elaboration from a String refers to an anonymous class" in {
    val baz = "chiselTests.stage.ChiselAnnotationsSpecBaz"
    val annotation = ChiselGeneratorAnnotation(baz)
    intercept[OptionsException] { annotation.elaborate }.getMessage should startWith(
      s"Unable to create instance of module '$baz'"
    )
  }

}