-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEncodingOptions.scala
More file actions
47 lines (39 loc) · 1.5 KB
/
EncodingOptions.scala
File metadata and controls
47 lines (39 loc) · 1.5 KB
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
package codes.mostly.xml.examples.encoding
import codes.mostly.xml.XmlMatchers
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpecLike
class EncodingOptions extends AnyWordSpecLike with XmlMatchers with Matchers {
import codes.mostly.xml.XmlSugar._
"Encoding case classes with other encodeable case classes as optional fields" should {
case class Dog(name: String, playful: Boolean)
case class Person(name: String, pet: Option[Dog])
implicit val dogEncoder: XmlEncoder[Dog] = d => <dog name={d.name} isPlayful={d.playful.toString}/>
implicit val personEncoder: XmlEncoder[Person] = p => <person name={p.name}><pets>{p.pet.asXml}</pets></person>
"encode" when {
"optional value is empty" in {
val personWithoutPet = Person("Tobias", None)
personWithoutPet.asTopLevelXml match {
case Left(err) => fail(err)
case Right(personXml) => personXml should beLike(
<person name="Tobias">
<pets></pets>
</person>
)
}
}
"optional value is present" in {
val personWithPet = Person("Ezra", Some(Dog("Whitman", playful = true)))
personWithPet.asTopLevelXml match {
case Left(err) => fail(err)
case Right(personXml) => personXml should beLike(
<person name="Ezra">
<pets>
<dog name="Whitman" isPlayful="true"/>
</pets>
</person>
)
}
}
}
}
}