/
EnumerationsSuite.scala
83 lines (63 loc) · 4.2 KB
/
EnumerationsSuite.scala
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
76
77
78
79
80
81
82
83
package pureconfig
import com.typesafe.config.{ConfigFactory, ConfigValueFactory, ConfigValueType}
import pureconfig.error.WrongType
import pureconfig.generic.error.NoValidCoproductOptionFound
import pureconfig.generic.semiauto._
import shapeless.test.illTyped
class EnumerationsSuite extends BaseSuite {
sealed trait Color
case object RainyBlue extends Color
case object SunnyYellow extends Color
behavior of "deriveEnumeration"
it should "provide methods to derive readers for enumerations encoded as sealed traits" in {
implicit val colorReader = deriveEnumerationReader[Color]
ConfigReader[Color].from(ConfigValueFactory.fromAnyRef("rainy-blue")) shouldBe Right(RainyBlue)
ConfigReader[Color].from(ConfigValueFactory.fromAnyRef("sunny-yellow")) shouldBe Right(SunnyYellow)
val unknownValue = ConfigValueFactory.fromAnyRef("blue")
ConfigReader[Color]
.from(unknownValue) should failWith(NoValidCoproductOptionFound(unknownValue, Seq.empty), "", emptyConfigOrigin)
val conf = ConfigFactory.parseString("{ type: person, name: John, surname: Doe }")
ConfigReader[Color].from(conf.root()) should failWith(
WrongType(ConfigValueType.OBJECT, Set(ConfigValueType.STRING)),
"",
stringConfigOrigin(1)
)
}
it should "provide methods to derive writers for enumerations encoded as sealed traits" in {
implicit val colorWriter = deriveEnumerationWriter[Color]
ConfigWriter[Color].to(RainyBlue) shouldEqual ConfigValueFactory.fromAnyRef("rainy-blue")
ConfigWriter[Color].to(SunnyYellow) shouldEqual ConfigValueFactory.fromAnyRef("sunny-yellow")
}
it should "provide methods to derive full converters for enumerations encoded as sealed traits" in {
implicit val colorConvert = deriveEnumerationConvert[Color]
ConfigConvert[Color].from(ConfigValueFactory.fromAnyRef("rainy-blue")) shouldBe Right(RainyBlue)
ConfigConvert[Color].from(ConfigValueFactory.fromAnyRef("sunny-yellow")) shouldBe Right(SunnyYellow)
ConfigConvert[Color].to(RainyBlue) shouldEqual ConfigValueFactory.fromAnyRef("rainy-blue")
ConfigConvert[Color].to(SunnyYellow) shouldEqual ConfigValueFactory.fromAnyRef("sunny-yellow")
}
it should "provide customizable methods to derive readers for enumerations encoded as sealed traits" in {
implicit val colorReader = deriveEnumerationReader[Color](ConfigFieldMapping(PascalCase, SnakeCase))
ConfigReader[Color].from(ConfigValueFactory.fromAnyRef("rainy_blue")) shouldBe Right(RainyBlue)
ConfigReader[Color].from(ConfigValueFactory.fromAnyRef("sunny_yellow")) shouldBe Right(SunnyYellow)
}
it should "provide customizable methods to derive writers for enumerations encoded as sealed traits" in {
implicit val colorWriter = deriveEnumerationWriter[Color](ConfigFieldMapping(PascalCase, SnakeCase))
ConfigWriter[Color].to(RainyBlue) shouldEqual ConfigValueFactory.fromAnyRef("rainy_blue")
ConfigWriter[Color].to(SunnyYellow) shouldEqual ConfigValueFactory.fromAnyRef("sunny_yellow")
}
it should "provide customizable methods to derive full converters for enumerations encoded as sealed traits" in {
implicit val colorConvert = deriveEnumerationConvert[Color](ConfigFieldMapping(PascalCase, SnakeCase))
ConfigConvert[Color].from(ConfigValueFactory.fromAnyRef("rainy_blue")) shouldBe Right(RainyBlue)
ConfigConvert[Color].from(ConfigValueFactory.fromAnyRef("sunny_yellow")) shouldBe Right(SunnyYellow)
ConfigConvert[Color].to(RainyBlue) shouldEqual ConfigValueFactory.fromAnyRef("rainy_blue")
ConfigConvert[Color].to(SunnyYellow) shouldEqual ConfigValueFactory.fromAnyRef("sunny_yellow")
}
it should "not allow deriving readers, writers and full converters for enumerations encoded as sealed traits whose subclasses are not all case objects" in {
sealed trait Entity
case class Person(name: String, surname: String) extends Entity
case class Place(name: String, lat: Double, lon: Double) extends Entity
illTyped("deriveEnumerationReader[Entity]", ".*could not find Lazy implicit value of type.*")
illTyped("deriveEnumerationWriter[Entity]", ".*could not find Lazy implicit value of type.*")
illTyped("deriveEnumerationConvert[Entity]", ".*could not find Lazy implicit value of type.*")
}
}