Skip to content

Commit

Permalink
add test case for custom nullable descriptor
Browse files Browse the repository at this point in the history
  • Loading branch information
Chuckame committed Apr 18, 2024
1 parent 4ba619d commit 588a5ae
Showing 1 changed file with 24 additions and 4 deletions.
Expand Up @@ -103,15 +103,35 @@ class SerialDescriptorBuilderTest {
String.serializer().descriptor,
ListSerializer(Int.serializer()).descriptor,
).forEach { originalDescriptor ->
// Unwrapping nullable when it is not nullable should return the same descriptor (no-op operation)
// Unwrapping original descriptor when it is not nullable should return the same descriptor (no-op operation)
assertSame(originalDescriptor.nonNullOriginal, originalDescriptor)

// Unwrapping nullable when it is nullable should return the original descriptor
// Unwrapping original descriptor when it is nullable should return the original descriptor
assertSame(originalDescriptor.nullable.nonNullOriginal, originalDescriptor)
}

@Serializable class Type(val field: String?)
// Unwrapping nullable of a nullable field should return the original non-null descriptor
// Unwrapping original descriptor of a custom nullable descriptor should return the same descriptor
val customNullableDescriptor = CustomNullableDescriptor()
assertSame(customNullableDescriptor.nonNullOriginal, customNullableDescriptor)

// Unwrapping original descriptor of a nullable field should return the original non-null descriptor
assertSame(Type.serializer().descriptor.getElementDescriptor(0).nonNullOriginal, String.serializer().descriptor)

}

@Serializable
class Type(val field: String?)

private class CustomNullableDescriptor : SerialDescriptor {
override val isNullable: Boolean = true

override val serialName: String = "CustomNullableDescriptor"
override val kind: SerialKind = PrimitiveKind.STRING
override val elementsCount: Int = 0
override fun getElementName(index: Int): String = error("Should not be called")
override fun getElementIndex(name: String): Int = error("Should not be called")
override fun isElementOptional(index: Int): Boolean = error("Should not be called")
override fun getElementAnnotations(index: Int): List<Annotation> = error("Should not be called")
override fun getElementDescriptor(index: Int): SerialDescriptor = error("Should not be called")
}
}

0 comments on commit 588a5ae

Please sign in to comment.