Skip to content

Commit

Permalink
some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pjfanning committed Dec 8, 2023
1 parent 06c0b9e commit ce338f7
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.fasterxml.jackson.module.scala.`enum`

case class Car(make: String, color: ColorEnum)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.fasterxml.jackson.module.scala.`enum`

enum ColorEnum { case Red, Green, Blue }

case class Colors(set: Set[ColorEnum])
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.fasterxml.jackson.module.scala.`enum`

object Ctx {
enum ColorEnum { case Red, Green, Blue }
}

case class CtxCar(make: String, color: Ctx.ColorEnum)
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.fasterxml.jackson.module.scala.`enum`

import com.fasterxml.jackson.core.`type`.TypeReference
import com.fasterxml.jackson.databind.exc.InvalidDefinitionException
import com.fasterxml.jackson.databind.json.JsonMapper
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpec

class EnumDeserializerSpec extends AnyWordSpec with Matchers {
val mapper = JsonMapper.builder().addModule(DefaultScalaModule).build()

"EnumModule" should {
"deserialize ColorEnum" in {
val red = s""""${ColorEnum.Red}""""
mapper.readValue(red, classOf[ColorEnum]) shouldEqual ColorEnum.Red
}
"fail deserialization of invalid ColorEnum" in {
val json = s""""xyz""""
intercept[IllegalArgumentException] {
mapper.readValue(json, classOf[ColorEnum])
}
}
"deserialize Colors" in {
val colors = Colors(Set(ColorEnum.Red, ColorEnum.Green))
val json = mapper.writeValueAsString(colors)
mapper.readValue(json, classOf[Colors]) shouldEqual colors
}
"deserialize ColorEnum with non-singleton EnumModule" in {
val red = s""""${ColorEnum.Red}""""
mapper.readValue(red, classOf[ColorEnum]) shouldEqual ColorEnum.Red
}
"deserialize JavaCompatibleColorEnum" in {
mapper.writeValueAsString(JavaCompatibleColorEnum.Red) shouldEqual s""""${JavaCompatibleColorEnum.Red}""""
}
"deserialize Car with ColorEnum" in {
val red = s"""{"make":"Perodua","color":"${ColorEnum.Green}"}"""
mapper.readValue(red, classOf[Car]) shouldEqual Car("Perodua", ColorEnum.Green)
}
"deserialize CtxCar with Ctx.ColorEnum" in {
val red = s"""{"make":"Perodua","color":"${Ctx.ColorEnum.Green}"}"""
mapper.readValue(red, classOf[CtxCar]) shouldEqual CtxCar("Perodua", Ctx.ColorEnum.Green)
}
"deserialize Enum as Map Key" in {
val json = s"""{"Green":"green","Red":"red"}"""
val map = mapper.readValue(json, new TypeReference[Map[ColorEnum, String]] {})
map should have size 2
map(ColorEnum.Green) shouldEqual "green"
map(ColorEnum.Red) shouldEqual "red"
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.fasterxml.jackson.module.scala.`enum`

import com.fasterxml.jackson.databind.json.JsonMapper
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpec

class EnumSerializerSpec extends AnyWordSpec with Matchers {
val mapper = JsonMapper.builder().addModule(DefaultScalaModule).build()

"EnumModule" should {
"not serialize None" in {
mapper.writeValueAsString(None) should not equal s""""$None""""
}
"serialize ColorEnum" in {
mapper.writeValueAsString(ColorEnum.Red) shouldEqual s""""${ColorEnum.Red}""""
}
"serialize Colors" in {
val json = mapper.writeValueAsString(Colors(Set(ColorEnum.Red, ColorEnum.Green)))
json should startWith("""{"set":[""")
json should include(""""Red"""")
json should include(""""Green"""")
}
"serialize ColorEnum with non-singleton EnumModule" in {
mapper.writeValueAsString(ColorEnum.Red) shouldEqual s""""${ColorEnum.Red}""""
}
"serialize JavaCompatibleColorEnum" in {
mapper.writeValueAsString(ColorEnum.Red) shouldEqual s""""${ColorEnum.Red}""""
}
"serialize Car with ColorEnum" in {
mapper.writeValueAsString(Car("Perodua", ColorEnum.Green)) shouldEqual s"""{"make":"Perodua","color":"${ColorEnum.Green}"}"""
}
"serialize CtxCar with Ctx.ColorEnum" in {
mapper.writeValueAsString(CtxCar("Perodua", Ctx.ColorEnum.Green)) shouldEqual s"""{"make":"Perodua","color":"${Ctx.ColorEnum.Green}"}"""
}
"serialize Enum as Map Key" in {
mapper.writeValueAsString(Map(ColorEnum.Green -> "green")) shouldEqual s"""{"Green":"green"}"""
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.fasterxml.jackson.module.scala.`enum`

enum JavaCompatibleColorEnum extends java.lang.Enum[JavaCompatibleColorEnum] { case Red, Green, Blue }

0 comments on commit ce338f7

Please sign in to comment.