Skip to content

Commit 9e29f80

Browse files
committed
Clean up of tests
1 parent 1ebeec1 commit 9e29f80

File tree

1 file changed

+51
-87
lines changed

1 file changed

+51
-87
lines changed

jsoniter-scala-macros/shared/src/test/scala/com/github/plokhotnyuk/jsoniter_scala/macros/JsonCodecMakerSpec.scala

Lines changed: 51 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1914,6 +1914,7 @@ class JsonCodecMakerSpec extends VerifyingSpec {
19141914
"don't generate codecs for case classes with field that have duplicated @named annotation" in {
19151915
assert(intercept[TestFailedException](assertCompiles {
19161916
"""case class DuplicatedNamed(@named("x") @named("y") z: Int)
1917+
|
19171918
|JsonCodecMaker.make[DuplicatedNamed]""".stripMargin
19181919
}).getMessage.contains {
19191920
"Duplicated 'com.github.plokhotnyuk.jsoniter_scala.macros.named' defined for 'z' of 'DuplicatedNamed'."
@@ -1922,7 +1923,9 @@ class JsonCodecMakerSpec extends VerifyingSpec {
19221923
"don't generate codecs for ADT leaf case classes that have duplicated @named annotation" in {
19231924
assert(intercept[TestFailedException](assertCompiles {
19241925
"""sealed trait Z
1926+
|
19251927
|@named("x") @named("y") case class DuplicatedNamed(z: Int) extends Z
1928+
|
19261929
|JsonCodecMaker.make[Z]""".stripMargin
19271930
}).getMessage.contains {
19281931
"Duplicated 'com.github.plokhotnyuk.jsoniter_scala.macros.named' defined for 'DuplicatedNamed'."
@@ -1937,10 +1940,12 @@ class JsonCodecMakerSpec extends VerifyingSpec {
19371940
|'com.github.plokhotnyuk.jsoniter_scala.macros.CodecMakerConfig.discriminatorFieldName' option.""".stripMargin.replace('\n', ' ')
19381941
assert(intercept[TestFailedException](assertCompiles {
19391942
"""case class DuplicatedJsonName(x: Int, @named("x") z: Int)
1943+
|
19401944
|JsonCodecMaker.make[DuplicatedJsonName]""".stripMargin
19411945
}).getMessage.contains(expectedError))
19421946
assert(intercept[TestFailedException](assertCompiles {
19431947
"""case class DuplicatedJsonName(y: Int, z: Int)
1948+
|
19441949
|JsonCodecMaker.make[DuplicatedJsonName](CodecMakerConfig.withFieldNameMapper { case _ => "x" })""".stripMargin
19451950
}).getMessage.contains(expectedError))
19461951
}
@@ -1969,6 +1974,7 @@ class JsonCodecMakerSpec extends VerifyingSpec {
19691974
"don't generate codecs for recursive types by default" in {
19701975
assert(intercept[TestFailedException](assertCompiles {
19711976
"""case class Recursive(r: Recursive)
1977+
|
19721978
|JsonCodecMaker.make[Recursive]""".stripMargin
19731979
}).getMessage.contains {
19741980
"""Recursive type(s) detected: 'Recursive'. Please consider using a custom implicitly accessible codec for this
@@ -1978,9 +1984,13 @@ class JsonCodecMakerSpec extends VerifyingSpec {
19781984
})
19791985
assert(intercept[TestFailedException](assertCompiles {
19801986
"""case class NonRecursive(r1: Recursive1)
1987+
|
19811988
|case class Recursive1(r2: Recursive2)
1989+
|
19821990
|case class Recursive2(r3: Recursive3)
1991+
|
19831992
|case class Recursive3(r1: Recursive1)
1993+
|
19841994
|JsonCodecMaker.make[NonRecursive]""".stripMargin
19851995
}).getMessage.contains {
19861996
"""Recursive type(s) detected: 'Recursive1', 'Recursive2', 'Recursive3'. Please consider using a custom
@@ -1990,6 +2000,7 @@ class JsonCodecMakerSpec extends VerifyingSpec {
19902000
})
19912001
assert(intercept[TestFailedException](assertCompiles {
19922002
"""case class HigherKindedType[F[_]](f: F[Int], fs: F[HigherKindedType[F]])
2003+
|
19932004
|JsonCodecMaker.make[HigherKindedType[Option]]""".stripMargin
19942005
}).getMessage.contains(
19952006
s"""Recursive type(s) detected: ${
@@ -2252,7 +2263,9 @@ class JsonCodecMakerSpec extends VerifyingSpec {
22522263
"don't generate codecs for CodecMakerConfig.withDiscriminatorFieldName(_root_.scala.None).withAlwaysEmitDiscriminator(true) compile-time configuration" in {
22532264
assert(intercept[TestFailedException](assertCompiles {
22542265
"""sealed trait A
2266+
|
22552267
|case class B(y: Int) extends A
2268+
|
22562269
|JsonCodecMaker.make[B](CodecMakerConfig.withDiscriminatorFieldName(_root_.scala.None).withAlwaysEmitDiscriminator(true))""".stripMargin
22572270
}).getMessage.contains {
22582271
"'discriminatorFieldName' should not be 'None' when 'alwaysEmitDiscriminator' is 'true'"
@@ -2314,91 +2327,6 @@ class JsonCodecMakerSpec extends VerifyingSpec {
23142327
"""'discriminatorFieldName' should be 'None' when 'circeLikeObjectEncoding' is 'true'"""
23152328
})
23162329
}
2317-
"serialize (but don't require on deserialization) discriminators for ADT leaf codecs when alwaysEmitDiscriminator is enabled" in {
2318-
sealed trait MyAdt
2319-
case class FooLeaf(x: Int, y: String) extends MyAdt
2320-
case class BarLeaf(a: String, b: Int) extends MyAdt
2321-
@named("baz")
2322-
case class BazLeaf(x: String, z: Double) extends MyAdt
2323-
sealed abstract class Intermediate extends MyAdt {
2324-
def tag: String
2325-
}
2326-
final case class Alpha(tag: String, a: Long) extends Intermediate
2327-
final case class Beta(tag: String, b: Double) extends Intermediate
2328-
@named("aa")
2329-
final case class AlwaysEmitInCompanion(c: String, d: Double) extends MyAdt
2330-
object AlwaysEmitInCompanion {
2331-
implicit val alwaysEmitCodec: JsonValueCodec[AlwaysEmitInCompanion] = make(CodecMakerConfig.withAlwaysEmitDiscriminator(true))
2332-
}
2333-
2334-
val defaultBaseAdtCodec: JsonValueCodec[MyAdt] = make[MyAdt]
2335-
val alwaysEmitBaseAdtCodec: JsonValueCodec[MyAdt] = make(CodecMakerConfig.withAlwaysEmitDiscriminator(true))
2336-
verifySerDeser(defaultBaseAdtCodec, FooLeaf(12, "VV"), """{"type":"FooLeaf","x":12,"y":"VV"}""")
2337-
verifySerDeser(alwaysEmitBaseAdtCodec, FooLeaf(12, "VV"), """{"type":"FooLeaf","x":12,"y":"VV"}""")
2338-
verifySerDeser(alwaysEmitBaseAdtCodec, BazLeaf("BB", 1.2), """{"type":"baz","x":"BB","z":1.2}""")
2339-
verifySerDeser(defaultBaseAdtCodec, BazLeaf("BB", 1.2), """{"type":"baz","x":"BB","z":1.2}""")
2340-
verifySerDeser(defaultBaseAdtCodec, Alpha("ttt", 1000L), """{"type":"Alpha","tag":"ttt","a":1000}""")
2341-
verifySerDeser(alwaysEmitBaseAdtCodec, Alpha("ttt", 1000L), """{"type":"Alpha","tag":"ttt","a":1000}""")
2342-
verifySerDeser(defaultBaseAdtCodec, AlwaysEmitInCompanion("ccc", 3.4), """{"type":"aa","c":"ccc","d":3.4}""")
2343-
verifySerDeser(alwaysEmitBaseAdtCodec, AlwaysEmitInCompanion("ccc", 3.4), """{"type":"aa","c":"ccc","d":3.4}""")
2344-
verifyDeserError(defaultBaseAdtCodec, """{"x":12,"y":"VV"}""", """expected key: "type"""")
2345-
verifyDeserError(alwaysEmitBaseAdtCodec, """{"x":12,"y":"VV"}""", """expected key: "type"""")
2346-
2347-
val defaultBarLeafCodec: JsonValueCodec[BarLeaf] = make
2348-
val alwaysEmitBarLeafCodec: JsonValueCodec[BarLeaf] = make(CodecMakerConfig.withAlwaysEmitDiscriminator(true))
2349-
verifySerDeser(defaultBarLeafCodec, BarLeaf("AA",13), """{"a":"AA","b":13}""")
2350-
verifySerDeser(alwaysEmitBarLeafCodec, BarLeaf("AA", 13), """{"type":"BarLeaf","a":"AA","b":13}""")
2351-
verifyDeser(alwaysEmitBarLeafCodec, BarLeaf("AA", 13), """{"a":"AA","b":13}""")
2352-
verifyDeser(defaultBarLeafCodec, BarLeaf("AA", 13), """{"a":"AA","b":13}""")
2353-
2354-
val defaultBazLeafCodec: JsonValueCodec[BazLeaf] = make
2355-
val alwaysEmitBazLeafCodec: JsonValueCodec[BazLeaf] = make(CodecMakerConfig.withAlwaysEmitDiscriminator(true))
2356-
verifySerDeser(defaultBazLeafCodec, BazLeaf("BB", 1.2), """{"x":"BB","z":1.2}""")
2357-
verifySerDeser(alwaysEmitBazLeafCodec, BazLeaf("BB", 1.2), """{"type":"baz","x":"BB","z":1.2}""")
2358-
verifyDeser(alwaysEmitBazLeafCodec, BazLeaf("BB", 1.2), """{"x":"BB","z":1.2}""")
2359-
2360-
val defaultListBazLeafCodec: JsonValueCodec[List[BazLeaf]] = make
2361-
val alwaysEmitListBazLeafCodec: JsonValueCodec[List[BazLeaf]] = make(CodecMakerConfig.withAlwaysEmitDiscriminator(true))
2362-
verifySerDeser(defaultListBazLeafCodec, List(BazLeaf("BB", 1.2)), """[{"x":"BB","z":1.2}]""")
2363-
verifySerDeser(alwaysEmitListBazLeafCodec, List(BazLeaf("BB", 1.2)), """[{"type":"baz","x":"BB","z":1.2}]""")
2364-
verifyDeser(alwaysEmitListBazLeafCodec, List(BazLeaf("BB", 1.2)), """[{"x":"BB","z":1.2}]""")
2365-
2366-
val defaultIntermediateCodec: JsonValueCodec[Intermediate] = make
2367-
val alwaysEmitIntermediateCodec: JsonValueCodec[Intermediate] = make(CodecMakerConfig.withAlwaysEmitDiscriminator(true))
2368-
verifySerDeser(defaultIntermediateCodec, Alpha("ttt", 1000L), """{"type":"Alpha","tag":"ttt","a":1000}""")
2369-
verifySerDeser(alwaysEmitIntermediateCodec, Alpha("ttt", 1000L), """{"type":"Alpha","tag":"ttt","a":1000}""")
2370-
verifySerDeser(defaultIntermediateCodec, Beta("ttt", 2.3), """{"type":"Beta","tag":"ttt","b":2.3}""")
2371-
verifySerDeser(alwaysEmitIntermediateCodec, Beta("ttt", 2.3), """{"type":"Beta","tag":"ttt","b":2.3}""")
2372-
verifyDeserError(defaultIntermediateCodec, """{"tag":"ttt","b":2.3}""", """expected key: "type"""")
2373-
verifyDeserError(alwaysEmitIntermediateCodec, """{"tag":"ttt","b":2.3}""", """expected key: "type"""")
2374-
2375-
// Don't change behavior for standalone classes that are not a member of an ADT
2376-
case class Standalone(f: String, g: Int)
2377-
val defaultStandaloneCodec: JsonValueCodec[Standalone] = make
2378-
val alwaysEmitStandaloneCodec: JsonValueCodec[Standalone] = make(CodecMakerConfig.withAlwaysEmitDiscriminator(true))
2379-
verifySerDeser(defaultStandaloneCodec, Standalone("FF", 99), """{"f":"FF","g":99}""")
2380-
verifySerDeser(alwaysEmitStandaloneCodec, Standalone("FF", 99), """{"f":"FF","g":99}""")
2381-
2382-
// Make sure we compose well with alwaysEmit codecs defined in companion class
2383-
final case class Composed(refined: AlwaysEmitInCompanion, otherRefined: FooLeaf, unrefined: MyAdt)
2384-
val defaultComposedCodec: JsonValueCodec[Composed] = make
2385-
val alwaysEmitComposedCodec: JsonValueCodec[Composed] = make(CodecMakerConfig.withAlwaysEmitDiscriminator(true))
2386-
val instance = Composed(AlwaysEmitInCompanion("aeic", 4.5), FooLeaf(19, "foo"), BazLeaf("bb", 3.7))
2387-
// Even though we are using the default non-always-emit config, we should pick up the implicit codec for AlwaysEmitInCompanion
2388-
// from the companion object
2389-
verifySerDeser(defaultComposedCodec, instance,
2390-
"""{"refined":{"type":"aa","c":"aeic","d":4.5},"otherRefined":{"x":19,"y":"foo"},"unrefined":{"type":"baz","x":"bb","z":3.7}}""")
2391-
// With always emit all of the fields should have their type disciminator emitted
2392-
verifySerDeser(alwaysEmitComposedCodec, instance,
2393-
"""{"refined":{"type":"aa","c":"aeic","d":4.5},"otherRefined":{"type":"FooLeaf","x":19,"y":"foo"},"unrefined":{"type":"baz","x":"bb","z":3.7}}""")
2394-
// With always emit we still shouldn't require discriminators where they are not necessary
2395-
verifyDeser(alwaysEmitComposedCodec, instance,
2396-
"""{"refined":{"c":"aeic","d":4.5},"otherRefined":{"x":19,"y":"foo"},"unrefined":{"type":"baz","x":"bb","z":3.7}}""")
2397-
// But we should still require type discriminators where they ARE necessary, in the `unrefined` field
2398-
verifyDeserError(alwaysEmitComposedCodec,
2399-
"""{"refined":{"c":"aeic","d":4.5},"otherRefined":{"x":19,"y":"foo"},"unrefined":{"x":"bb","z":3.7}}""", """expected key: "type"""")
2400-
2401-
}
24022330
"deserialize and throw non-implemented error for serialization with decodingOnly" in {
24032331
val decodingOnlyCodec = make[Int](CodecMakerConfig.withDecodingOnly(true))
24042332
verifyDeser(decodingOnlyCodec, 1, "1")
@@ -2651,17 +2579,23 @@ class JsonCodecMakerSpec extends VerifyingSpec {
26512579
"don't generate codecs for non sealed traits or abstract classes as an ADT base" in {
26522580
assert(intercept[TestFailedException](assertCompiles {
26532581
"""trait X1799
2582+
|
26542583
|case class A1799(i: Int) extends X1799
2584+
|
26552585
|case object B1799 extends X1799
2586+
|
26562587
|JsonCodecMaker.make[X1799]""".stripMargin
26572588
}).getMessage.contains {
26582589
"""Only sealed traits or abstract classes are supported as an ADT base. Please consider sealing the 'X1799' or
26592590
|provide a custom implicitly accessible codec for it.""".stripMargin.replace('\n', ' ')
26602591
})
26612592
assert(intercept[TestFailedException](assertCompiles {
26622593
"""abstract class X
2594+
|
26632595
|case class A(i: Int) extends X
2596+
|
26642597
|case object B extends X
2598+
|
26652599
|JsonCodecMaker.make[X]""".stripMargin
26662600
}).getMessage.contains {
26672601
"""Only sealed traits or abstract classes are supported as an ADT base. Please consider sealing the 'X' or
@@ -2671,21 +2605,31 @@ class JsonCodecMakerSpec extends VerifyingSpec {
26712605
"don't generate codecs for ADTs that have intermediate non-sealed traits or abstract classes" in {
26722606
assert(intercept[TestFailedException](assertCompiles {
26732607
"""sealed trait X
2608+
|
26742609
|sealed abstract class AX extends X
2610+
|
26752611
|abstract class BX extends X
2612+
|
26762613
|case class A(i: Int) extends AX
2614+
|
26772615
|case object B extends BX
2616+
|
26782617
|JsonCodecMaker.make[X]""".stripMargin
26792618
}).getMessage.contains {
26802619
"""Only sealed intermediate traits or abstract classes are supported. Please consider using of them for ADT
26812620
|with base 'X' or provide a custom implicitly accessible codec for the ADT base.""".stripMargin.replace('\n', ' ')
26822621
})
26832622
assert(intercept[TestFailedException](assertCompiles {
26842623
"""sealed trait X
2624+
|
26852625
|sealed trait AX extends X
2626+
|
26862627
|trait BX extends X
2628+
|
26872629
|case class A(i: Int) extends AX
2630+
|
26882631
|case object B extends BX
2632+
|
26892633
|JsonCodecMaker.make[X]""".stripMargin
26902634
}).getMessage.contains {
26912635
"""Only sealed intermediate traits or abstract classes are supported. Please consider using of them for ADT
@@ -2695,13 +2639,15 @@ class JsonCodecMakerSpec extends VerifyingSpec {
26952639
"don't generate codecs for ADT bases without leaf classes" in {
26962640
assert(intercept[TestFailedException](assertCompiles {
26972641
"""sealed trait X1859 extends Product with Serializable
2642+
|
26982643
|JsonCodecMaker.make[X1859]""".stripMargin
26992644
}).getMessage.contains {
27002645
"""Cannot find leaf classes for ADT base 'X1859'. Please add them or provide a custom implicitly
27012646
|accessible codec for the ADT base.""".stripMargin.replace('\n', ' ')
27022647
})
27032648
assert(intercept[TestFailedException](assertCompiles {
27042649
"""sealed abstract class X extends Product with Serializable
2650+
|
27052651
|JsonCodecMaker.make[X]""".stripMargin
27062652
}).getMessage.contains {
27072653
"""Cannot find leaf classes for ADT base 'X'. Please add them or provide a custom implicitly
@@ -2711,8 +2657,11 @@ class JsonCodecMakerSpec extends VerifyingSpec {
27112657
"don't generate codecs for case objects which are mapped to the same discriminator value" in {
27122658
assert(intercept[TestFailedException](assertCompiles {
27132659
"""sealed trait X extends Product with Serializable
2660+
|
27142661
|case object A extends X
2662+
|
27152663
|case object B extends X
2664+
|
27162665
|JsonCodecMaker.make[X](CodecMakerConfig.withAdtLeafClassNameMapper(_ => "Z"))""".stripMargin
27172666
}).getMessage.contains {
27182667
"""Duplicated discriminator defined for ADT base 'X': 'Z'. Values for leaf classes of ADT that are returned by
@@ -2721,8 +2670,11 @@ class JsonCodecMakerSpec extends VerifyingSpec {
27212670
})
27222671
assert(intercept[TestFailedException](assertCompiles {
27232672
"""sealed trait Data extends Product with Serializable
2673+
|
27242674
|case class Data1(i: Int, s: String) extends Data
2675+
|
27252676
|case object Data1 extends Data
2677+
|
27262678
|val c = make[Data]""".stripMargin
27272679
}).getMessage.contains {
27282680
"""Duplicated discriminator defined for ADT base 'Data': 'Data1'. Values for leaf classes of ADT that are
@@ -2733,7 +2685,9 @@ class JsonCodecMakerSpec extends VerifyingSpec {
27332685
"don't generate codecs for case classes with fields that the same name as discriminator name" in {
27342686
assert(intercept[TestFailedException](assertCompiles {
27352687
"""sealed trait DuplicatedJsonName extends Product with Serializable
2688+
|
27362689
|case class A(x: Int) extends DuplicatedJsonName
2690+
|
27372691
|JsonCodecMaker.make[DuplicatedJsonName](CodecMakerConfig.withDiscriminatorFieldName(_root_.scala.Some("x")))""".stripMargin
27382692
}).getMessage.contains {
27392693
"""Duplicated JSON key(s) defined for 'A': 'x'. Keys are derived from field names of the class that are mapped
@@ -2910,6 +2864,7 @@ class JsonCodecMakerSpec extends VerifyingSpec {
29102864
"don't generate codecs for first-order types that are specified using 'Any' type parameter" in {
29112865
assert(intercept[TestFailedException](assertCompiles {
29122866
"""case class FirstOrder[A](a: A)
2867+
|
29132868
|JsonCodecMaker.make[FirstOrder[_]]""".stripMargin
29142869
}).getMessage.contains {
29152870
if (ScalaVersionCheck.isScala2) {
@@ -2997,6 +2952,7 @@ class JsonCodecMakerSpec extends VerifyingSpec {
29972952
"don't generate codecs for case classes with non public parameters of the primary constructor" in {
29982953
assert(intercept[TestFailedException](assertCompiles {
29992954
"""case class MultiListOfArgsWithNonPublicParam(i: Int)(l: Long)
2955+
|
30002956
|JsonCodecMaker.make[MultiListOfArgsWithNonPublicParam]""".stripMargin
30012957
}).getMessage.contains {
30022958
if (ScalaVersionCheck.isScala2) "'l' parameter of 'MultiListOfArgsWithNonPublicParam' should be defined as 'val' or 'var' in the primary constructor."
@@ -3006,6 +2962,7 @@ class JsonCodecMakerSpec extends VerifyingSpec {
30062962
"don't generate codecs for classes with parameters in a primary constructor that have no accessor for read" in {
30072963
assert(intercept[TestFailedException](assertCompiles {
30082964
"""class ParamHasNoAccessor(val i: Int, a: String)
2965+
|
30092966
|JsonCodecMaker.make[ParamHasNoAccessor]""".stripMargin
30102967
}).getMessage.contains {
30112968
if (ScalaVersionCheck.isScala2) "'a' parameter of 'ParamHasNoAccessor' should be defined as 'val' or 'var' in the primary constructor."
@@ -3015,12 +2972,17 @@ class JsonCodecMakerSpec extends VerifyingSpec {
30152972
"don't generate codecs when all generic type parameters cannot be resolved" in {
30162973
assert(intercept[TestFailedException](assertCompiles {
30172974
"""sealed trait Foo[F[_]] extends Product with Serializable
2975+
|
30182976
|case class FooImpl[F[_], A](fa: F[A], as: Vector[A]) extends Foo[F]
2977+
|
30192978
|sealed trait Bar[A] extends Product with Serializable
2979+
|
30202980
|case object Baz extends Bar[Int]
2981+
|
30212982
|case object Qux extends Bar[String]
2983+
|
30222984
|val v = FooImpl[Bar, String](Qux, Vector.empty[String])
3023-
|val c = make[Foo[Bar]]""".stripMargin
2985+
|val c = JsonCodecMaker.make[Foo[Bar]]""".stripMargin
30242986
}).getMessage.contains {
30252987
if (ScalaVersionCheck.isScala2) "Cannot resolve generic type(s) for `FooImpl[F,A]`. Please provide a custom implicitly accessible codec for it."
30262988
else "Type parameter A of class FooImpl can't be deduced from type arguments of Foo[[A >: scala.Nothing <: scala.Any] => Bar[A]]. Please provide a custom implicitly accessible codec for it."
@@ -3029,8 +2991,10 @@ class JsonCodecMakerSpec extends VerifyingSpec {
30292991
"don't generate codecs when 'AnyVal' or one value classes with 'CodecMakerConfig.withInlineOneValueClasses(true)' are leaf types of the ADT base" in {
30302992
assert(intercept[TestFailedException](assertCompiles {
30312993
"""sealed trait X extends Any
2994+
|
30322995
|case class D(value: Double) extends X
3033-
|make[X](CodecMakerConfig.withInlineOneValueClasses(true))""".stripMargin
2996+
|
2997+
|JsonCodecMaker.make[X](CodecMakerConfig.withInlineOneValueClasses(true))""".stripMargin
30342998
}).getMessage.contains {
30352999
"'AnyVal' and one value classes with 'CodecMakerConfig.withInlineOneValueClasses(true)' are not supported as leaf classes for ADT with base 'X'."
30363000
})

0 commit comments

Comments
 (0)