|
| 1 | +package kotlinx.serialization.protobuf.schema |
| 2 | + |
| 3 | +import kotlinx.serialization.* |
| 4 | +import kotlinx.serialization.protobuf.ProtoNumber |
| 5 | +import kotlin.test.Test |
| 6 | +import kotlin.test.assertFailsWith |
| 7 | + |
| 8 | +class SchemaValidationsTest { |
| 9 | + @Serializable |
| 10 | + data class ValidClass(val i: Int) |
| 11 | + |
| 12 | + @Serializable |
| 13 | + @SerialName("ValidClass") |
| 14 | + data class DuplicateClass(val l: Long) |
| 15 | + |
| 16 | + @Serializable |
| 17 | + @SerialName("invalid serial name") |
| 18 | + data class InvalidClassName(val i: Int) |
| 19 | + |
| 20 | + @Serializable |
| 21 | + data class InvalidClassFieldName(@SerialName("invalid serial name") val i: Int) |
| 22 | + |
| 23 | + @Serializable |
| 24 | + data class FieldNumberDuplicates(@ProtoNumber(42) val i: Int, @ProtoNumber(42) val j: Int) |
| 25 | + |
| 26 | + @Serializable |
| 27 | + data class FieldNumberImplicitlyDuplicates(@ProtoNumber(2) val i: Int, val j: Int) |
| 28 | + |
| 29 | + @Serializable |
| 30 | + @SerialName("invalid serial name") |
| 31 | + enum class InvalidEnumName { SINGLETON } |
| 32 | + |
| 33 | + @Serializable |
| 34 | + enum class InvalidEnumElementName { |
| 35 | + FIRST, |
| 36 | + |
| 37 | + @SerialName("invalid serial name") |
| 38 | + SECOND |
| 39 | + } |
| 40 | + |
| 41 | + |
| 42 | + @Test |
| 43 | + fun testInvalidEnumElementSerialName() { |
| 44 | + val descriptors = listOf(InvalidEnumElementName.serializer().descriptor) |
| 45 | + assertFailsWith(IllegalArgumentException::class) { ProtoBufSchemaGenerator.generateSchemaText(descriptors) } |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + fun testInvalidClassSerialName() { |
| 50 | + val descriptors = listOf(InvalidClassName.serializer().descriptor) |
| 51 | + assertFailsWith(IllegalArgumentException::class) { ProtoBufSchemaGenerator.generateSchemaText(descriptors) } |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + fun testInvalidClassFieldSerialName() { |
| 56 | + val descriptors = listOf(InvalidClassFieldName.serializer().descriptor) |
| 57 | + assertFailsWith(IllegalArgumentException::class) { ProtoBufSchemaGenerator.generateSchemaText(descriptors) } |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + fun testDuplicateSerialNames() { |
| 62 | + val descriptors = listOf(InvalidClassFieldName.serializer().descriptor) |
| 63 | + assertFailsWith(IllegalArgumentException::class) { ProtoBufSchemaGenerator.generateSchemaText(descriptors) } |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + fun testInvalidEnumSerialName() { |
| 68 | + val descriptors = listOf(InvalidEnumName.serializer().descriptor) |
| 69 | + assertFailsWith(IllegalArgumentException::class) { ProtoBufSchemaGenerator.generateSchemaText(descriptors) } |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + fun testDuplicationSerialName() { |
| 74 | + val descriptors = listOf(ValidClass.serializer().descriptor, DuplicateClass.serializer().descriptor) |
| 75 | + assertFailsWith(IllegalArgumentException::class) { ProtoBufSchemaGenerator.generateSchemaText(descriptors) } |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + fun testInvalidOptionName() { |
| 80 | + val descriptors = listOf(ValidClass.serializer().descriptor) |
| 81 | + assertFailsWith(IllegalArgumentException::class) { |
| 82 | + ProtoBufSchemaGenerator.generateSchemaText( |
| 83 | + descriptors, |
| 84 | + options = mapOf("broken name" to "value") |
| 85 | + ) |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + fun testIllegalPackageNames() { |
| 91 | + val descriptors = listOf(ValidClass.serializer().descriptor) |
| 92 | + assertFailsWith(IllegalArgumentException::class) { ProtoBufSchemaGenerator.generateSchemaText(descriptors, "") } |
| 93 | + assertFailsWith(IllegalArgumentException::class) { ProtoBufSchemaGenerator.generateSchemaText(descriptors, ".") } |
| 94 | + assertFailsWith(IllegalArgumentException::class) { ProtoBufSchemaGenerator.generateSchemaText(descriptors, ".first.dot") } |
| 95 | + assertFailsWith(IllegalArgumentException::class) { ProtoBufSchemaGenerator.generateSchemaText(descriptors, "ended.with.dot.") } |
| 96 | + assertFailsWith(IllegalArgumentException::class) { ProtoBufSchemaGenerator.generateSchemaText(descriptors, "first._underscore") } |
| 97 | + assertFailsWith(IllegalArgumentException::class) { ProtoBufSchemaGenerator.generateSchemaText(descriptors, "first.1digit") } |
| 98 | + assertFailsWith(IllegalArgumentException::class) { ProtoBufSchemaGenerator.generateSchemaText(descriptors, "illegal.sym+bol") } |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + fun testValidPackageNames() { |
| 103 | + val descriptors = listOf(ValidClass.serializer().descriptor) |
| 104 | + ProtoBufSchemaGenerator.generateSchemaText(descriptors, "singleIdent") |
| 105 | + ProtoBufSchemaGenerator.generateSchemaText(descriptors, "double.ident") |
| 106 | + ProtoBufSchemaGenerator.generateSchemaText(descriptors, "with.digits0123") |
| 107 | + ProtoBufSchemaGenerator.generateSchemaText(descriptors, "with.underscore_") |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + fun testFieldNumberDuplicates() { |
| 112 | + assertFailsWith(IllegalArgumentException::class) { ProtoBufSchemaGenerator.generateSchemaText(listOf(FieldNumberDuplicates.serializer().descriptor)) } |
| 113 | + assertFailsWith(IllegalArgumentException::class) { ProtoBufSchemaGenerator.generateSchemaText(listOf(FieldNumberImplicitlyDuplicates.serializer().descriptor)) } |
| 114 | + } |
| 115 | +} |
0 commit comments