Skip to content

Commit d6a65ac

Browse files
authored
Update AnyValDeserializerTest.scala (#683)
* Update AnyValDeserializerTest.scala * update tests * refactor
1 parent 01f285a commit d6a65ac

File tree

4 files changed

+85
-2
lines changed

4 files changed

+85
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.fasterxml.jackson.module.scala.deser
2+
3+
import com.fasterxml.jackson.module.scala.{DefaultScalaModule, JacksonModule}
4+
5+
import scala.util.Properties.versionNumberString
6+
7+
object AnyValDeserializerTest {
8+
case class DoubleAnyVal(underlying: Double) extends AnyVal
9+
case class DoubleAnyValHolder(value: DoubleAnyVal)
10+
11+
case class BigIntAnyVal(underlying: BigInt) extends AnyVal
12+
case class BigIntAnyValHolder(value: BigIntAnyVal)
13+
case class BigIntOptionAnyValHolder(value: Option[BigIntAnyVal])
14+
}
15+
16+
class AnyValDeserializerTest extends DeserializerTest {
17+
import AnyValDeserializerTest._
18+
19+
lazy val module: JacksonModule = DefaultScalaModule
20+
21+
behavior of "AnyVal"
22+
23+
it should "deserialize an Double AnyVal" in {
24+
val mapper = newMapper
25+
val expected = DoubleAnyVal(42)
26+
mapper.readValue("""{"underlying":42.0}""", classOf[DoubleAnyVal]) shouldEqual expected
27+
mapper.readValue("""{"value":42.0}""", classOf[DoubleAnyValHolder]) shouldEqual DoubleAnyValHolder(expected)
28+
}
29+
30+
it should "deserialize an BigInt AnyVal" in {
31+
val mapper = newMapper
32+
val expected = BigIntAnyVal(42)
33+
mapper.readValue("""{"underlying":42}""", classOf[BigIntAnyVal]) shouldEqual expected
34+
mapper.readValue("""{"value":42}""", classOf[BigIntAnyValHolder]) shouldEqual BigIntAnyValHolder(expected)
35+
if (!versionNumberString.startsWith("2.11")) {
36+
// see https://github.com/FasterXML/jackson-module-scala/pull/675
37+
mapper.readValue("""{"value":{"underlying":42}}""", classOf[BigIntOptionAnyValHolder]) shouldEqual
38+
BigIntOptionAnyValHolder(Some(expected))
39+
}
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.fasterxml.jackson.module.scala.ser
2+
3+
import com.fasterxml.jackson.module.scala.BaseFixture
4+
5+
import scala.util.Properties.versionNumberString
6+
7+
object AnyValSerializerTest {
8+
case class DoubleAnyVal(underlying: Double) extends AnyVal
9+
case class DoubleAnyValHolder(value: DoubleAnyVal)
10+
11+
case class BigIntAnyVal(underlying: BigInt) extends AnyVal
12+
case class BigIntAnyValHolder(value: BigIntAnyVal)
13+
case class BigIntOptionAnyValHolder(value: Option[BigIntAnyVal])
14+
}
15+
16+
//see AnyVal2SerializerTest for cases that only work with Scala2 and Scala3.3 but not earlier versions of Scala3
17+
class AnyValSerializerTest extends BaseFixture {
18+
import AnyValSerializerTest._
19+
20+
behavior of "AnyVal"
21+
22+
it should "serialize an Double AnyVal" in { mapper =>
23+
val value = DoubleAnyVal(42)
24+
mapper.writeValueAsString(value) shouldBe """{"underlying":42.0}"""
25+
mapper.writeValueAsString(DoubleAnyValHolder(value)) shouldBe """{"value":42.0}"""
26+
}
27+
28+
it should "serialize an BigInt AnyVal" in { mapper =>
29+
val value = BigIntAnyVal(42)
30+
mapper.writeValueAsString(value) shouldBe """{"underlying":42}"""
31+
mapper.writeValueAsString(BigIntAnyValHolder(value)) shouldBe """{"value":42}"""
32+
if (!versionNumberString.startsWith("2.11")) {
33+
// see https://github.com/FasterXML/jackson-module-scala/pull/675
34+
mapper.writeValueAsString(BigIntOptionAnyValHolder(Some(value))) shouldBe """{"value":{"underlying":42}}"""
35+
}
36+
}
37+
38+
}

src/test/scala/com/fasterxml/jackson/module/scala/deser/AnyValDeserializerTest.scala renamed to src/test/scala-3/com/fasterxml/jackson/module/scala/deser/AnyValDeserializerTest.scala

+3-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ class AnyValDeserializerTest extends DeserializerTest {
3030
val expected = BigIntAnyVal(42)
3131
mapper.readValue("""{"underlying":42}""", classOf[BigIntAnyVal]) shouldEqual expected
3232
mapper.readValue("""{"value":42}""", classOf[BigIntAnyValHolder]) shouldEqual BigIntAnyValHolder(expected)
33-
//mapper.readValue("""{"value":{"underlying":42}}""", classOf[BigIntOptionAnyValHolder]) shouldEqual
34-
//BigIntOptionAnyValHolder(Some(expected))
33+
// see https://github.com/FasterXML/jackson-module-scala/pull/675
34+
// mapper.readValue("""{"value":{"underlying":42}}""", classOf[BigIntOptionAnyValHolder]) shouldEqual
35+
// BigIntOptionAnyValHolder(Some(expected))
3536
}
3637
}

src/test/scala/com/fasterxml/jackson/module/scala/ser/AnyValSerializerTest.scala renamed to src/test/scala-3/com/fasterxml/jackson/module/scala/ser/AnyValSerializerTest.scala

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ object AnyValSerializerTest {
88

99
case class BigIntAnyVal(underlying: BigInt) extends AnyVal
1010
case class BigIntAnyValHolder(value: BigIntAnyVal)
11+
case class BigIntOptionAnyValHolder(value: Option[BigIntAnyVal])
1112
}
1213

1314
//see AnyVal2SerializerTest for cases that only work with Scala2 and Scala3.3 but not earlier versions of Scala3
@@ -26,6 +27,8 @@ class AnyValSerializerTest extends BaseFixture {
2627
val value = BigIntAnyVal(42)
2728
mapper.writeValueAsString(value) shouldBe """{"underlying":42}"""
2829
mapper.writeValueAsString(BigIntAnyValHolder(value)) shouldBe """{"value":42}"""
30+
// see https://github.com/FasterXML/jackson-module-scala/pull/675
31+
// mapper.writeValueAsString(BigIntOptionAnyValHolder(Some(value))) shouldBe """{"value":{"underlying":42}}"""
2932
}
3033

3134
}

0 commit comments

Comments
 (0)