aboutsummaryrefslogtreecommitdiff
path: root/src/test/scala/firrtlTests/options/phases/ChecksSpec.scala
blob: d8ad7e17579ca5e0487ece80592318145b3f2545 (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
// SPDX-License-Identifier: Apache-2.0

package firrtlTests.options.phases

import firrtl.AnnotationSeq
import firrtl.options.{OptionsException, OutputAnnotationFileAnnotation, Phase, TargetDirAnnotation}
import firrtl.options.phases.Checks
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

class ChecksSpec extends AnyFlatSpec with Matchers {

  val targetDir = TargetDirAnnotation("foo")
  val annoOut = OutputAnnotationFileAnnotation("bar")

  class Fixture { val phase: Phase = new Checks }

  /* A minimum annotation Seq that will pass [[Checks]] */
  val min = Seq(targetDir)

  def checkExceptionMessage(phase: Phase, annotations: AnnotationSeq, messageStart: String): Unit =
    intercept[OptionsException] { phase.transform(annotations) }.getMessage should startWith(messageStart)

  behavior.of(classOf[Checks].toString)

  it should "enforce exactly one TargetDirAnnotation" in new Fixture {
    info("0 target directories throws an exception")
    checkExceptionMessage(phase, Seq.empty, "Exactly one target directory must be specified")

    info("2 target directories throws an exception")
    checkExceptionMessage(phase, Seq(targetDir, targetDir), "Exactly one target directory must be specified")
  }

  it should "enforce zero or one output annotation files" in new Fixture {
    info("0 output annotation files is okay")
    phase.transform(Seq(targetDir))

    info("2 output annotation files throws an exception")
    val in = Seq(targetDir, annoOut, annoOut)
    checkExceptionMessage(phase, in, "At most one output annotation file can be specified")
  }

  it should "pass if the minimum annotations are specified" in new Fixture {
    info(s"""Minimum required: ${min.map(_.getClass.getSimpleName).mkString(", ")}""")
    phase.transform(min)
  }

}