diff options
| author | Schuyler Eldridge | 2018-11-07 13:48:06 -0500 |
|---|---|---|
| committer | GitHub | 2018-11-07 13:48:06 -0500 |
| commit | 17b4e9835bd95dcf91c5ea5a4d7c52280031ea93 (patch) | |
| tree | e54301b8019d17cce4448ce9d09589815a7315d5 /src/test/scala/firrtlTests/options/OptionsViewSpec.scala | |
| parent | d04af59c233cec994087df3d0d3fff14e20ac04c (diff) | |
| parent | 27c1b366ce58e93434e77e964365474f5e7aa8d7 (diff) | |
Merge pull request #879 from seldridge/issue-764-refactor-pr-pointer-optionsPackage
- Adds firrtl.options package and tests
- Does not use firrtl.options in any way
Diffstat (limited to 'src/test/scala/firrtlTests/options/OptionsViewSpec.scala')
| -rw-r--r-- | src/test/scala/firrtlTests/options/OptionsViewSpec.scala | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/src/test/scala/firrtlTests/options/OptionsViewSpec.scala b/src/test/scala/firrtlTests/options/OptionsViewSpec.scala new file mode 100644 index 00000000..dec6a99f --- /dev/null +++ b/src/test/scala/firrtlTests/options/OptionsViewSpec.scala @@ -0,0 +1,75 @@ +// See LICENSE for license details + +package firrtlTests.options + +import org.scalatest.{FlatSpec, Matchers} + +import firrtl.options.OptionsView +import firrtl.AnnotationSeq +import firrtl.annotations.{Annotation,NoTargetAnnotation} + +class OptionsViewSpec extends FlatSpec with Matchers { + + /* Annotations */ + case class NameAnnotation(name: String) extends NoTargetAnnotation + case class ValueAnnotation(value: Int) extends NoTargetAnnotation + + /* The type we want to view the annotations as */ + case class Foo(name: Option[String] = None, value: Option[Int] = None) + case class Bar(name: String = "bar") + + /* An OptionsView that converts an AnnotationSeq to Option[Foo] */ + implicit object FooView extends OptionsView[Foo] { + private def append(foo: Foo, anno: Annotation): Foo = anno match { + case NameAnnotation(n) => foo.copy(name = Some(n)) + case ValueAnnotation(v) => foo.copy(value = Some(v)) + case _ => foo + } + + def view(options: AnnotationSeq): Option[Foo] = { + val annoSeq = options.foldLeft(Foo())(append) + Some(annoSeq) + } + } + + /* An OptionsView that converts an AnnotationSeq to Option[Bar] */ + implicit object BarView extends OptionsView[Bar] { + private def append(bar: Bar, anno: Annotation): Bar = anno match { + case NameAnnotation(n) => bar.copy(name = n) + case _ => bar + } + + def view(options: AnnotationSeq): Option[Bar] = { + val annoSeq = options.foldLeft(Bar())(append) + Some(annoSeq) + } + } + + behavior of "OptionsView" + + it should "convert annotations to one of two types" in { + /* Some default annotations */ + val annos = Seq(NameAnnotation("foo"), ValueAnnotation(42)) + + info("Foo conversion okay") + FooView.view(annos) should be (Some(Foo(Some("foo"), Some(42)))) + + info("Bar conversion okay") + BarView.view(annos) should be (Some(Bar("foo"))) + } + + behavior of "Viewer" + + it should "implicitly view annotations as the specified type" in { + import firrtl.options.Viewer._ + + /* Some empty annotations */ + val annos = Seq[Annotation]() + + info("Foo view okay") + view[Foo](annos) should be (Some(Foo(None, None))) + + info("Bar view okay") + view[Bar](annos) should be (Some(Bar())) + } +} |
