diff options
| author | Schuyler Eldridge | 2018-12-04 01:35:48 -0500 |
|---|---|---|
| committer | Schuyler Eldridge | 2019-04-25 16:24:15 -0400 |
| commit | 254e7909f6c9d155f514664584f142566f0a6799 (patch) | |
| tree | fb2cdc32f8a37f8fc74cbf92ef79ee33240cc96c /src/test/scala/firrtlTests/options/phases/WriteOutputAnnotationsSpec.scala | |
| parent | b2dd0eb845081609d0aec4a873587ab3f22fe3f7 (diff) | |
Add tests for Annotations/Options refactor
- Add tests for DriverCompatibility.AddImplicitEmitter
- Add tests for DriverCompatibility.AddImplicitOutputFile
- Use a different top name in DriverSpec emit circuit tests for better
coverage
- Add tests for DriverCompatibility.WriteEmitted
- Add catchWrites firrtlTests utility to intercept file writes
- Add tests for WriteOutputAnnotations
- Add tests for --custom-transforms reversing
Signed-off-by: Schuyler Eldridge <schuyler.eldridge@ibm.com>
Diffstat (limited to 'src/test/scala/firrtlTests/options/phases/WriteOutputAnnotationsSpec.scala')
| -rw-r--r-- | src/test/scala/firrtlTests/options/phases/WriteOutputAnnotationsSpec.scala | 108 |
1 files changed, 108 insertions, 0 deletions
diff --git a/src/test/scala/firrtlTests/options/phases/WriteOutputAnnotationsSpec.scala b/src/test/scala/firrtlTests/options/phases/WriteOutputAnnotationsSpec.scala new file mode 100644 index 00000000..07aad53f --- /dev/null +++ b/src/test/scala/firrtlTests/options/phases/WriteOutputAnnotationsSpec.scala @@ -0,0 +1,108 @@ +// See LICENSE for license details. + +package firrtlTests.options.phases + +import org.scalatest.{FlatSpec, Matchers} + +import java.io.File + +import firrtl.{AnnotationSeq, EmittedFirrtlCircuitAnnotation, EmittedFirrtlCircuit} +import firrtl.annotations.{DeletedAnnotation, NoTargetAnnotation} +import firrtl.options.{InputAnnotationFileAnnotation, OutputAnnotationFileAnnotation, Phase, WriteDeletedAnnotation} +import firrtl.options.phases.{GetIncludes, WriteOutputAnnotations} +import firrtl.stage.FirrtlFileAnnotation + +class WriteOutputAnnotationsSpec extends FlatSpec with Matchers with firrtlTests.Utils { + + val dir = "test_run_dir/WriteOutputAnnotationSpec" + + /** Check if the annotations contained by a [[File]] and the same, and in the same order, as a reference + * [[AnnotationSeq]]. This uses [[GetIncludes]] as that already knows how to read annotation files. + * @param f a file to read + * @param a the expected annotations + */ + private def fileContainsAnnotations(f: File, a: AnnotationSeq): Unit = { + info(s"output file '$f' exists") + f should (exist) + + info(s"reading '$f' works") + val read = (new GetIncludes) + .transform(Seq(InputAnnotationFileAnnotation(f.toString))) + .filterNot{ + case a @ DeletedAnnotation(_, _: InputAnnotationFileAnnotation) => true + case _ => false } + + info(s"annotations in file are expected size") + read.size should be (a.size) + + read + .zip(a) + .foreach{ case (read, expected) => + info(s"$read matches") + read should be (expected) } + + f.delete() + } + + class Fixture { val phase: Phase = new WriteOutputAnnotations } + + behavior of classOf[WriteOutputAnnotations].toString + + it should "write annotations to a file (excluding DeletedAnnotations)" in new Fixture { + val file = new File(dir + "/should-write-annotations-to-a-file.anno.json") + val annotations = Seq( OutputAnnotationFileAnnotation(file.toString), + WriteOutputAnnotationsSpec.FooAnnotation, + WriteOutputAnnotationsSpec.BarAnnotation(0), + WriteOutputAnnotationsSpec.BarAnnotation(1), + DeletedAnnotation("foo", WriteOutputAnnotationsSpec.FooAnnotation) ) + val expected = annotations.filter { + case a: DeletedAnnotation => false + case a => true + } + val out = phase.transform(annotations) + + info("annotations are unmodified") + out.toSeq should be (annotations) + + fileContainsAnnotations(file, expected) + } + + it should "include DeletedAnnotations if a WriteDeletedAnnotation is present" in new Fixture { + val file = new File(dir + "should-include-deleted.anno.json") + val annotations = Seq( OutputAnnotationFileAnnotation(file.toString), + WriteOutputAnnotationsSpec.FooAnnotation, + WriteOutputAnnotationsSpec.BarAnnotation(0), + WriteOutputAnnotationsSpec.BarAnnotation(1), + DeletedAnnotation("foo", WriteOutputAnnotationsSpec.FooAnnotation), + WriteDeletedAnnotation ) + val out = phase.transform(annotations) + + info("annotations are unmodified") + out.toSeq should be (annotations) + + fileContainsAnnotations(file, annotations) + } + + it should "do nothing if no output annotation file is specified" in new Fixture { + val annotations = Seq( WriteOutputAnnotationsSpec.FooAnnotation, + WriteOutputAnnotationsSpec.BarAnnotation(0), + WriteOutputAnnotationsSpec.BarAnnotation(1) ) + + val out = catchWrites { phase.transform(annotations) } match { + case Right(a) => + info("no file writes occurred") + a + case Left(a) => + fail(s"No file writes expected, but a write to '$a' ocurred!") + } + + info("annotations are unmodified") + out.toSeq should be (annotations) + } + +} + +private object WriteOutputAnnotationsSpec { + case object FooAnnotation extends NoTargetAnnotation + case class BarAnnotation(x: Int) extends NoTargetAnnotation +} |
