Skip to content

Commit

Permalink
Added test for optional list
Browse files Browse the repository at this point in the history
  • Loading branch information
martinw-ct committed Sep 11, 2023
1 parent 1b0daf8 commit 965f752
Showing 1 changed file with 24 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.sphere.json

import io.sphere.json.generic._
import org.json4s.{JLong, JNothing, JObject, JString}
import org.json4s.{JArray, JLong, JNothing, JObject, JString}
import org.scalatest.OptionValues
import org.scalatest.matchers.must.Matchers
import org.scalatest.wordspec.AnyWordSpec
Expand All @@ -24,6 +24,11 @@ object OptionReaderSpec {
object MapClass {
implicit val json: JSON[MapClass] = jsonProduct(apply _)
}

case class ListClass(id: Long, list: Option[List[String]])
object ListClass {
implicit val json: JSON[ListClass] = jsonProduct(apply _)
}
}

class OptionReaderSpec extends AnyWordSpec with Matchers with OptionValues {
Expand Down Expand Up @@ -84,6 +89,24 @@ class OptionReaderSpec extends AnyWordSpec with Matchers with OptionValues {
JObject("id" -> JLong(1L), "map" -> JObject("a" -> JString("b")))
}

"handle optional list" in {
getFromJValue[ListClass](
JObject("id" -> JLong(1L), "list" -> JArray(List(JString("hi"))))) mustEqual
ListClass(1L, Some(List("hi")))
getFromJValue[ListClass](JObject("id" -> JLong(1L), "list" -> JArray(List.empty))) mustEqual
ListClass(1L, Some(List()))
getFromJValue[ListClass](JObject("id" -> JLong(1L))) mustEqual
ListClass(1L, None)

toJValue(ListClass(1L, Some(List("hi")))) mustEqual JObject(
"id" -> JLong(1L),
"list" -> JArray(List(JString("hi"))))
toJValue(ListClass(1L, Some(List.empty))) mustEqual JObject(
"id" -> JLong(1L),
"list" -> JArray(List.empty))
toJValue(ListClass(1L, None)) mustEqual JObject("id" -> JLong(1L), "list" -> JNothing)
}

"handle absence of all fields mixed with ignored fields" in {
val json = """{ "value3": "a" }"""
val result = getFromJSON[Option[SimpleClass]](json)
Expand Down

0 comments on commit 965f752

Please sign in to comment.