@@ -16,25 +16,23 @@ class ProtobufCollectionsTest {
16
16
@Serializable
17
17
data class MapWithNullableNestedLists (val m : Map <List <Int >? , List <Int >? >)
18
18
19
+ @Serializable
20
+ data class MapWithNullableNestedMaps (val m : Map <Map <String , Int >? , Map <String , Int >? >)
21
+
19
22
@Serializable
20
23
data class NullableListElement (val l : List <Int ?>)
21
24
22
25
@Serializable
23
26
data class NullableMapKey (val m : Map <Int ?, Int >)
24
27
25
28
@Serializable
26
- data class NullableMapValue (val m : Map <Int , Int ?>)
29
+ data class NullableMap (val m : Map <Int ? , Int ?>)
27
30
28
31
@Test
29
32
fun testEncodeNullAsListElement () {
30
33
assertFailsWith(SerializationException ::class ) { ProtoBuf .encodeToByteArray(NullableListElement (listOf (null ))) }
31
34
}
32
35
33
- @Test
34
- fun testEncodeNullAsMapKey () {
35
- assertFailsWith(SerializationException ::class ) { ProtoBuf .encodeToByteArray(NullableMapKey (mapOf (null to 42 ))) }
36
- }
37
-
38
36
@Test
39
37
fun testEmptyListIsNotListOfEmpty () {
40
38
val emptyListBytes = ProtoBuf .encodeToByteArray(ListWithNestedList (emptyList()))
@@ -54,15 +52,46 @@ class ProtobufCollectionsTest {
54
52
}
55
53
56
54
@Test
57
- fun testEncodeNullAsMapValue () {
58
- assertFailsWith(SerializationException ::class ) { ProtoBuf .encodeToByteArray(NullableMapValue (mapOf (42 to null ))) }
55
+ fun testNullMap () {
56
+ val keyNull = NullableMap (mapOf (null to 42 ))
57
+ val valueNull = NullableMap (mapOf (42 to null ))
58
+ val bothNull = NullableMap (mapOf (null to null ))
59
+
60
+ val encodedKeyNull = ProtoBuf .encodeToHexString(keyNull)
61
+ val encodedValueNull = ProtoBuf .encodeToHexString(valueNull)
62
+ val encodedBothNull = ProtoBuf .encodeToHexString(bothNull)
63
+ assertEquals(encodedKeyNull, " 0a02102a" )
64
+ assertEquals(encodedValueNull, " 0a02082a" )
65
+ assertEquals(encodedBothNull, " 0a00" )
66
+
67
+ val decodedKeyNull = ProtoBuf .decodeFromHexString<NullableMap >(encodedKeyNull)
68
+ val decodedValueNull = ProtoBuf .decodeFromHexString<NullableMap >(encodedValueNull)
69
+ val decodedBothNull = ProtoBuf .decodeFromHexString<NullableMap >(encodedBothNull)
70
+ assertEquals(decodedKeyNull, keyNull)
71
+ assertEquals(decodedValueNull, valueNull)
72
+ assertEquals(decodedBothNull, bothNull)
73
+ }
74
+
75
+ @Test
76
+ fun testRepeatNullKeyInMap () {
77
+ // there are two entries in message: (null to 42) and (null to 43), the last one is used
78
+ val decoded = ProtoBuf .decodeFromHexString<NullableMap >(" 0a04102a102b" )
79
+ assertEquals(NullableMap (mapOf (null to 43 )), decoded)
80
+ }
81
+
82
+ @Test
83
+ fun testCollectionsInNullableMap () {
84
+ assertFailsWith(SerializationException ::class ) { ProtoBuf .encodeToByteArray(MapWithNullableNestedLists (mapOf (null to listOf (42 ))) ) }
85
+ assertFailsWith(SerializationException ::class ) { ProtoBuf .encodeToByteArray(MapWithNullableNestedLists (mapOf (listOf (42 ) to null )) ) }
86
+ assertFailsWith(SerializationException ::class ) { ProtoBuf .encodeToByteArray(MapWithNullableNestedMaps (mapOf (null to mapOf (" key" to 42 ))) ) }
87
+ assertFailsWith(SerializationException ::class ) { ProtoBuf .encodeToByteArray(MapWithNullableNestedMaps (mapOf (mapOf (" key" to 42 ) to null )) ) }
59
88
}
60
89
61
90
@Test
62
91
fun testEncodeMapWithNullableValue () {
63
- val map = NullableMapValue (mapOf (42 to 43 ))
92
+ val map = NullableMap (mapOf (42 to 43 ))
64
93
val bytes = ProtoBuf .encodeToByteArray(map)
65
- val decoded = ProtoBuf .decodeFromByteArray<NullableMapValue >(bytes)
94
+ val decoded = ProtoBuf .decodeFromByteArray<NullableMap >(bytes)
66
95
assertEquals(map, decoded)
67
96
}
68
97
0 commit comments