1 package kotlinx.serialization.protobuf 2 3 import kotlinx.serialization.Serializable 4 import kotlinx.serialization.SerializationException 5 import kotlinx.serialization.decodeFromByteArray 6 import kotlinx.serialization.encodeToByteArray 7 import kotlin.test.Test 8 import kotlin.test.assertEquals 9 import kotlin.test.assertFailsWith 10 11 class ProtobufAbsenceTest { 12 @Serializable 13 data class SimpleValue(val i: Int) 14 @Serializable 15 data class DefaultValue(val i: Int = 42) 16 @Serializable 17 data class NullableValue(val i: Int?) 18 @Serializable 19 data class DefaultAndNullValue(val i: Int? = 42) 20 21 @Serializable 22 data class SimpleList(val l: List<Int>) 23 @Serializable 24 data class DefaultList(val l: List<Int> = listOf(42)) 25 @Serializable 26 data class NullableList(val l: List<Int>?) 27 @Serializable 28 data class DefaultNullableList(val l: List<Int>? = listOf(42)) 29 30 @Serializable 31 data class SimpleMap(val m: Map<Int, Int>) 32 @Serializable 33 data class DefaultMap(val m: Map<Int, Int> = mapOf(42 to 43)) 34 @Serializable 35 data class NullableMap(val m: Map<Int, Int>?) 36 @Serializable 37 data class DefaultNullableMap(val m: Map<Int, Int>? = mapOf(42 to 43)) 38 39 @Test testSimpleValuenull40 fun testSimpleValue() { 41 assertFailsWith(SerializationException::class) { ProtoBuf.decodeFromByteArray<SimpleValue>(ByteArray(0)) } 42 } 43 44 @Test testDefaultValuenull45 fun testDefaultValue() { 46 val bytes = ProtoBuf.encodeToByteArray(DefaultValue(42)) 47 assertEquals(0, bytes.size) 48 49 val decoded = ProtoBuf.decodeFromByteArray<DefaultValue>(bytes) 50 assertEquals(42, decoded.i) 51 } 52 53 @Test testNullableValuenull54 fun testNullableValue() { 55 val bytes = ProtoBuf.encodeToByteArray(NullableValue(null)) 56 assertEquals(0, bytes.size) 57 58 val decoded = ProtoBuf.decodeFromByteArray<NullableValue>(bytes) 59 assertEquals(null, decoded.i) 60 } 61 62 @Test testDefaultAndNullValuenull63 fun testDefaultAndNullValue() { 64 assertFailsWith(SerializationException::class) { ProtoBuf.encodeToByteArray(DefaultAndNullValue(null)) } 65 66 val bytes = ProtoBuf.encodeToByteArray(DefaultAndNullValue(42)) 67 assertEquals(0, bytes.size) 68 69 val decoded = ProtoBuf.decodeFromByteArray<DefaultAndNullValue>(bytes) 70 assertEquals(42, decoded.i) 71 } 72 73 74 @Test testSimpleListnull75 fun testSimpleList() { 76 val bytes = ProtoBuf.encodeToByteArray(SimpleList(emptyList())) 77 assertEquals(0, bytes.size) 78 79 val decoded = ProtoBuf.decodeFromByteArray<SimpleList>(bytes) 80 assertEquals(emptyList(), decoded.l) 81 } 82 83 @Test testDefaultListnull84 fun testDefaultList() { 85 val bytes = ProtoBuf.encodeToByteArray(DefaultList(listOf(42))) 86 assertEquals(0, bytes.size) 87 88 val decoded = ProtoBuf.decodeFromByteArray<DefaultList>(bytes) 89 assertEquals(listOf(42), decoded.l) 90 } 91 92 @Test testNullableListnull93 fun testNullableList() { 94 assertFailsWith(SerializationException::class) { ProtoBuf.encodeToByteArray(NullableList(null)) } 95 96 val bytes = ProtoBuf.encodeToByteArray(NullableList(emptyList())) 97 assertEquals(0, bytes.size) 98 99 val decoded = ProtoBuf.decodeFromByteArray<NullableList>(bytes) 100 assertEquals(emptyList(), decoded.l) 101 } 102 103 104 @Test testDefaultNullableListnull105 fun testDefaultNullableList() { 106 assertFailsWith(SerializationException::class) { ProtoBuf.encodeToByteArray(DefaultNullableList(null)) } 107 108 val bytes = ProtoBuf.encodeToByteArray(DefaultNullableList(listOf(42))) 109 assertEquals(0, bytes.size) 110 111 val decoded = ProtoBuf.decodeFromByteArray<DefaultNullableList>(bytes) 112 assertEquals(listOf(42), decoded.l) 113 } 114 115 @Test testSimpleMapnull116 fun testSimpleMap() { 117 val bytes = ProtoBuf.encodeToByteArray(SimpleMap(emptyMap())) 118 assertEquals(0, bytes.size) 119 val decoded = ProtoBuf.decodeFromByteArray<SimpleMap>(bytes) 120 assertEquals(emptyMap(), decoded.m) 121 } 122 123 @Test testDefaultMapnull124 fun testDefaultMap() { 125 val bytes = ProtoBuf.encodeToByteArray(DefaultMap(mapOf(42 to 43))) 126 assertEquals(0, bytes.size) 127 val decoded = ProtoBuf.decodeFromByteArray<DefaultMap>(bytes) 128 assertEquals(mapOf(42 to 43), decoded.m) 129 } 130 131 @Test testNullableMapnull132 fun testNullableMap() { 133 assertFailsWith(SerializationException::class) { ProtoBuf.encodeToByteArray(NullableMap(null)) } 134 135 val bytes = ProtoBuf.encodeToByteArray(NullableMap(emptyMap())) 136 assertEquals(0, bytes.size) 137 138 val decoded = ProtoBuf.decodeFromByteArray<NullableMap>(bytes) 139 assertEquals(emptyMap(), decoded.m) 140 } 141 142 @Test testDefaultNullableMapnull143 fun testDefaultNullableMap() { 144 assertFailsWith(SerializationException::class) { ProtoBuf.encodeToByteArray(DefaultNullableMap(null)) } 145 146 val bytes = ProtoBuf.encodeToByteArray(DefaultNullableMap(mapOf(42 to 43))) 147 assertEquals(0, bytes.size) 148 149 val decoded = ProtoBuf.decodeFromByteArray<DefaultNullableMap>(bytes) 150 assertEquals(mapOf(42 to 43), decoded.m) 151 } 152 } 153