aboutsummaryrefslogtreecommitdiff
path: root/src/test/scala/firrtlTests/transforms/SortModulesSpec.scala
blob: 4aabeeeaf19bdc00833b948236d78417973b65ab (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
// SPDX-License-Identifier: Apache-2.0

package firrtlTests.transforms

import firrtl.{ir, CircuitState, Parser}
import firrtl.transforms.SortModules
import firrtl.traversals.Foreachers._

import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

import scala.collection.mutable

class SortModulesSpec extends AnyFlatSpec with Matchers {

  private def collectModules(names: mutable.ArrayBuffer[String], module: ir.DefModule): Unit = names += module.name

  behavior.of("SortModules")

  it should "enforce define before use of modules" in {

    val input =
      """|circuit Foo:
         |  module Foo:
         |    inst bar of Bar
         |  module Bar:
         |    inst baz of Baz
         |  extmodule Baz:
         |    input a: UInt<1>
         |""".stripMargin

    val state = CircuitState(Parser.parse(input), Seq.empty)
    val moduleNames = mutable.ArrayBuffer.empty[String]

    (new SortModules)
      .execute(state)
      .circuit
      .foreach(collectModules(moduleNames, _: ir.DefModule))

    (moduleNames should contain).inOrderOnly("Baz", "Bar", "Foo")
  }

}