blob: dec6a99f7f3d4f6bebd8e1f38e1ff6459420871b (
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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()))
}
}
|