Skip to content

Commit 876f5f6

Browse files
authored
[test] Convert LayerSpec to use FileCheck, NFC (#4547)
Convert `chiselTests.LayerSpec` to use FileCheck. This is much stricter testing as the ordering of matches is now guaranteed. Signed-off-by: Schuyler Eldridge <[email protected]>
1 parent 4b40f24 commit 876f5f6

File tree

1 file changed

+104
-95
lines changed

1 file changed

+104
-95
lines changed

src/test/scala/chiselTests/LayerSpec.scala

Lines changed: 104 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,14 @@ import chisel3._
66
import chisel3.experimental.hierarchy.core.{Definition, Instance}
77
import chisel3.experimental.hierarchy.instantiable
88
import chisel3.probe.{define, Probe, ProbeValue}
9-
import chiselTests.{ChiselFlatSpec, MatchesAndOmits, Utils}
9+
import chiselTests.{ChiselFlatSpec, FileCheck, Utils}
1010
import java.nio.file.{FileSystems, Paths}
1111
import _root_.circt.stage.ChiselStage
1212

13-
class LayerSpec extends ChiselFlatSpec with Utils with MatchesAndOmits {
13+
class LayerSpec extends ChiselFlatSpec with Utils with FileCheck {
1414

1515
val sep: String = FileSystems.getDefault().getSeparator()
1616

17-
def bindLayer(name: String, dirs: List[String]): String = {
18-
val dirsStr = if (dirs.nonEmpty) s""", "${dirs.mkString(sep)}"""" else ""
19-
s"layer $name, bind$dirsStr :"
20-
}
21-
22-
def inlineLayer(name: String): String = {
23-
s"layer $name, inline :"
24-
}
25-
2617
object A extends layer.Layer(layer.LayerConfig.Extract()) {
2718
object B extends layer.Layer(layer.LayerConfig.Extract())
2819
}
@@ -47,19 +38,19 @@ class LayerSpec extends ChiselFlatSpec with Utils with MatchesAndOmits {
4738
y := DontCare
4839
}
4940

50-
val chirrtl = ChiselStage.emitCHIRRTL(new Foo, Array("--full-stacktrace"))
51-
5241
info("CHIRRTL emission looks correct")
53-
// TODO: Switch to FileCheck for this testing. This is going to miss all sorts of ordering issues.
54-
matchesAndOmits(chirrtl)(
55-
"layer A, bind, \"A\" :",
56-
"layer B, bind, \"A" ++ sep ++ "B\" :",
57-
"layerblock A :",
58-
"wire w : UInt<1>",
59-
"layerblock B :",
60-
"wire x : UInt<1>",
61-
"wire y : UInt<1>"
62-
)()
42+
generateFirrtlAndFileCheck(new Foo) {
43+
s"""|CHECK: layer A, bind, "A" :
44+
|CHECK-NEXT: layer B, bind, "A${sep}B" :
45+
|
46+
|CHECK: module Foo :
47+
|CHECK: layerblock A :
48+
|CHECK-NEXT: wire w : UInt<1>
49+
|CHECK: layerblock B :
50+
|CHECK-NEXT: wire x : UInt<1>
51+
|CHECK: wire y : UInt<1>
52+
|""".stripMargin
53+
}
6354
}
6455

6556
they should "create parent layer blocks automatically" in {
@@ -71,11 +62,13 @@ class LayerSpec extends ChiselFlatSpec with Utils with MatchesAndOmits {
7162
}
7263
}
7364

74-
matchesAndOmits(ChiselStage.emitCHIRRTL(new Foo))(
75-
"layerblock A :",
76-
"layerblock B :",
77-
"layerblock C :"
78-
)()
65+
val check =
66+
generateFirrtlAndFileCheck(new Foo) {
67+
"""|CHECK: layerblock A :
68+
|CHECK: layerblock B :
69+
|CHECK: layerblock C :
70+
|""".stripMargin
71+
}
7972
}
8073

8174
they should "respect the 'skipIfAlreadyInBlock' parameter" in {
@@ -86,9 +79,12 @@ class LayerSpec extends ChiselFlatSpec with Utils with MatchesAndOmits {
8679
}
8780
}
8881

89-
matchesAndOmits(ChiselStage.emitCHIRRTL(new Foo))(
90-
"layerblock A"
91-
)("layerblock C")
82+
val check =
83+
generateFirrtlAndFileCheck(new Foo) {
84+
"""|CHECK: layerblock A :
85+
|CHECK-NOT: layerblock C :
86+
|""".stripMargin
87+
}
9288
}
9389

9490
they should "respect the 'skipIfLayersEnabled' parameter" in {
@@ -97,7 +93,7 @@ class LayerSpec extends ChiselFlatSpec with Utils with MatchesAndOmits {
9793
layer.block(A.B, skipIfLayersEnabled = true) {}
9894
}
9995

100-
matchesAndOmits(ChiselStage.emitCHIRRTL(new Foo))()("layerblock")
96+
generateFirrtlAndFileCheck(new Foo)("CHECK-NOT: layerblock")
10197
}
10298

10399
they should "create no layer blocks when wrapped in 'elideBlocks'" in {
@@ -106,8 +102,7 @@ class LayerSpec extends ChiselFlatSpec with Utils with MatchesAndOmits {
106102
layer.block(A.B) {}
107103
}
108104
}
109-
110-
matchesAndOmits(ChiselStage.emitCHIRRTL(new Foo))()("layerblock")
105+
generateFirrtlAndFileCheck(new Foo)("CHECK-NOT: layerblock")
111106
}
112107

113108
they should "generate valid CHIRRTL when module instantiated under layer block has layer blocks" in {
@@ -128,10 +123,16 @@ class LayerSpec extends ChiselFlatSpec with Utils with MatchesAndOmits {
128123

129124
// Check the generated CHIRRTL only.
130125
// Layer-under-module-under-layer is rejected by firtool presently.
131-
matchesAndOmits(ChiselStage.emitCHIRRTL(new Foo))(
132-
// Whitespace shows nested under another block.
133-
" layerblock B : "
134-
)()
126+
generateFirrtlAndFileCheck(new Foo) {
127+
"""|CHECK: module Bar :
128+
|CHECK: layerblock A :
129+
|CHECK-NEXT: layerblock B :
130+
|
131+
|CHECK: module Foo :
132+
|CHECK: layerblock A :
133+
|CHECK-NEXT: inst bar of Bar
134+
|""".stripMargin
135+
}
135136
}
136137

137138
they should "allow for defines to layer-colored probes" in {
@@ -151,13 +152,17 @@ class LayerSpec extends ChiselFlatSpec with Utils with MatchesAndOmits {
151152
}
152153
}
153154

154-
matchesAndOmits(ChiselStage.emitCHIRRTL(new Foo))(
155-
"define x = probe(a)",
156-
"define y = probe(b)",
157-
"define x = probe(c)",
158-
"define y = probe(d)",
159-
"define y = probe(e)"
160-
)()
155+
generateFirrtlAndFileCheck(new Foo) {
156+
"""|CHECK: module Foo :
157+
|CHECK: define x = probe(a)
158+
|CHECK-NEXT: define y = probe(b)
159+
|CHECK-NEXT: layerblock A :
160+
|CHECK-NEXT: define x = probe(c)
161+
|CHECK-NEXT: define y = probe(d)
162+
|CHECK-NEXT: layerblock B :
163+
|CHECK-NEXT: define y = probe(e)
164+
|""".stripMargin
165+
}
161166
}
162167

163168
they should "allow for defines to layer-colored probes without layer blocks" in {
@@ -196,10 +201,14 @@ class LayerSpec extends ChiselFlatSpec with Utils with MatchesAndOmits {
196201
layer.enable(layer.Layer.root)
197202
}
198203

199-
matchesAndOmits(ChiselStage.emitCHIRRTL(new Foo))(
200-
"layer A, bind",
201-
"module Foo enablelayer A.B enablelayer C :"
202-
)()
204+
generateFirrtlAndFileCheck(new Foo) {
205+
"""|CHECK: layer A, bind
206+
|CHECK-NEXT: layer B, bind
207+
|CHECK-NEXT: layer C, bind
208+
|
209+
|CHECK: module Foo enablelayer A.B enablelayer C :
210+
|""".stripMargin
211+
}
203212

204213
}
205214

@@ -215,7 +224,7 @@ class LayerSpec extends ChiselFlatSpec with Utils with MatchesAndOmits {
215224
}
216225

217226
val chirrtl = circt.stage.ChiselStage.emitCHIRRTL(new Foo)
218-
matchesAndOmits(chirrtl)("layer A")()
227+
chirrtl should include("layer A")
219228
}
220229

221230
they should "emit the output directory when present" in {
@@ -266,21 +275,22 @@ class LayerSpec extends ChiselFlatSpec with Utils with MatchesAndOmits {
266275
s"layer $name, bind$dirsStr :"
267276
}
268277

269-
val text = ChiselStage.emitCHIRRTL(new Foo)
270-
matchesAndOmits(text)(
271-
bindLayer("LayerWithDefaultOutputDir", List("LayerWithDefaultOutputDir")),
272-
bindLayer("SublayerWithDefaultOutputDir", List("LayerWithDefaultOutputDir", "SublayerWithDefaultOutputDir")),
273-
bindLayer("SublayerWithCustomOutputDir", List("myOtherOutputDir")),
274-
bindLayer("SublayerWithNoOutputDir", List()),
275-
bindLayer("LayerWithCustomOutputDir", List("myOutputDir")),
276-
bindLayer("SublayerWithDefaultOutputDir", List("myOutputDir", "SublayerWithDefaultOutputDir")),
277-
bindLayer("SublayerWithCustomOutputDir", List("myOtherOutputDir")),
278-
bindLayer("SublayerWithNoOutputDir", List()),
279-
bindLayer("LayerWithNoOutputDir", List()),
280-
bindLayer("SublayerWithDefaultOutputDir", List("SublayerWithDefaultOutputDir")),
281-
bindLayer("SublayerWithCustomOutputDir", List("myOtherOutputDir")),
282-
bindLayer("SublayerWithNoOutputDir", List())
283-
)()
278+
generateFirrtlAndFileCheck(new Foo) {
279+
s"""|CHECK: circuit Foo :
280+
|CHECK-NEXT: layer LayerWithDefaultOutputDir, bind, "LayerWithDefaultOutputDir" :
281+
|CHECK-NEXT: layer SublayerWithDefaultOutputDir, bind, "LayerWithDefaultOutputDir${sep}SublayerWithDefaultOutputDir" :
282+
|CHECK-NEXT: layer SublayerWithCustomOutputDir, bind, "myOtherOutputDir" :
283+
|CHECK-NEXT: layer SublayerWithNoOutputDir, bind :
284+
|CHECK-NEXT: layer LayerWithCustomOutputDir, bind, "myOutputDir" :
285+
|CHECK-NEXT: layer SublayerWithDefaultOutputDir, bind, "myOutputDir${sep}SublayerWithDefaultOutputDir"
286+
|CHECK-NEXT: layer SublayerWithCustomOutputDir, bind, "myOtherOutputDir" :
287+
|CHECK-NEXT: layer SublayerWithNoOutputDir, bind :
288+
|CHECK-NEXT: layer LayerWithNoOutputDir, bind :
289+
|CHECK-NEXT: layer SublayerWithDefaultOutputDir, bind, "SublayerWithDefaultOutputDir" :
290+
|CHECK-NEXT: layer SublayerWithCustomOutputDir, bind, "myOtherOutputDir" :
291+
|CHECK-NEXT: layer SublayerWithNoOutputDir, bind :
292+
|""".stripMargin
293+
}
284294
}
285295

286296
they should "allow manually overriding the parent layer" in {
@@ -300,31 +310,28 @@ class LayerSpec extends ChiselFlatSpec with Utils with MatchesAndOmits {
300310
layer.addLayer(A)
301311
}
302312

303-
matchesAndOmits(ChiselStage.emitCHIRRTL(new Foo))("layer A")("layer block")
313+
generateFirrtlAndFileCheck(new Foo) {
314+
"""|CHECK: layer A
315+
|CHECK-NOT: layerblock
316+
|""".stripMargin
317+
}
304318
}
305319

306320
"Default Layers" should "always be emitted" in {
307321
class Foo extends RawModule {}
308322
val chirrtl = ChiselStage.emitCHIRRTL(new Foo)
309323

310324
info("default layers are emitted")
311-
matchesAndOmits(chirrtl)(
312-
"layer Verification",
313-
"layer Assert",
314-
"layer Assume",
315-
"layer Cover"
316-
)()
325+
fileCheckString(chirrtl) {
326+
s"""|CHECK: layer Verification, bind, "verification" :
327+
|CHECK-NEXT: layer Assert, bind, "verification${sep}assert" :
328+
|CHECK-NEXT: layer Assume, bind, "verification${sep}assume" :
329+
|CHECK-NEXT: layer Cover, bind, "verification${sep}cover" :
330+
|""".stripMargin
331+
}
317332

318333
info("user-defined layers are not emitted if not used")
319334
(chirrtl should not).include("layer B")
320-
321-
info("default layers have lowercase directories")
322-
matchesAndOmits(chirrtl)(
323-
"""layer Verification, bind, "verification"""",
324-
"""layer Assert, bind, "verification/assert"""",
325-
"""layer Assume, bind, "verification/assume"""",
326-
"""layer Cover, bind, "verification/cover""""
327-
)()
328335
}
329336

330337
"Layers error checking" should "require that the current layer is an ancestor of the desired layer" in {
@@ -384,10 +391,11 @@ class LayerSpec extends ChiselFlatSpec with Utils with MatchesAndOmits {
384391
}
385392
}
386393

387-
matchesAndOmits(ChiselStage.emitCHIRRTL(new Foo))(
388-
"layer A, inline :",
389-
"layer B, inline :"
390-
)()
394+
generateFirrtlAndFileCheck(new Foo) {
395+
"""|CHECK: layer A, inline :
396+
|CHECK-NEXT: layer B, inline :
397+
|""".stripMargin
398+
}
391399
}
392400

393401
"Inline layers" should "be ignored when choosing default output directories" in {
@@ -430,16 +438,17 @@ class LayerSpec extends ChiselFlatSpec with Utils with MatchesAndOmits {
430438
}
431439
}
432440

433-
matchesAndOmits(ChiselStage.emitCHIRRTL(new Foo))(
434-
bindLayer("LayerWithDefaultOutputDir", List("LayerWithDefaultOutputDir")),
435-
inlineLayer("InlineSublayer"),
436-
bindLayer("SublayerWithDefaultOutputDir", List("LayerWithDefaultOutputDir", "SublayerWithDefaultOutputDir")),
437-
bindLayer("LayerWithCustomOutputDir", List("myOutputDir")),
438-
inlineLayer("InlineSublayer"),
439-
bindLayer("SublayerWithDefaultOutputDir", List("myOutputDir", "SublayerWithDefaultOutputDir")),
440-
bindLayer("LayerWithNoOutputDir", List()),
441-
inlineLayer("InlineSublayer"),
442-
bindLayer("SublayerWithDefaultOutputDir", List("SublayerWithDefaultOutputDir"))
443-
)()
441+
generateFirrtlAndFileCheck(new Foo) {
442+
s"""|CHECK: layer LayerWithDefaultOutputDir, bind, "LayerWithDefaultOutputDir" :
443+
|CHECK-NEXT: layer InlineSublayer, inline :
444+
|CHECK-NEXT: layer SublayerWithDefaultOutputDir, bind, "LayerWithDefaultOutputDir${sep}SublayerWithDefaultOutputDir" :
445+
|CHECK-NEXT: layer LayerWithCustomOutputDir, bind, "myOutputDir" :
446+
|CHECK-NEXT: layer InlineSublayer, inline :
447+
|CHECK-NEXT: layer SublayerWithDefaultOutputDir, bind, "myOutputDir${sep}SublayerWithDefaultOutputDir" :
448+
|CHECK-NEXT: layer LayerWithNoOutputDir, bind :
449+
|CHECK-NEXT: layer InlineSublayer, inline :
450+
|CHECK-NEXT: layer SublayerWithDefaultOutputDir, bind, "SublayerWithDefaultOutputDir" :
451+
|""".stripMargin
452+
}
444453
}
445454
}

0 commit comments

Comments
 (0)