@@ -217,18 +217,65 @@ class ProtobufOneOfTest {
217
217
}
218
218
219
219
@Serializable
220
- data class FailType (@ProtoNumber(8 ) val i : Int , @ProtoNumber(9 ) val j : Int ) : IType
220
+ data class FailOuter (@ProtoOneOf val i : IFailSuper , @ProtoNumber(3 ) val name : String )
221
+
222
+ @Serializable
223
+ data class FailOuterHelper (
224
+ @ProtoNumber(5 ) val i : Int ,
225
+ @ProtoNumber(6 ) val j : Int ,
226
+ @ProtoNumber(3 ) val name : String
227
+ )
228
+
229
+ @Serializable
230
+ sealed interface IFailSuper
231
+
232
+ @Serializable
233
+ data class FailType (@ProtoNumber(5 ) val i : Int , @ProtoNumber(6 ) val j : Int ) : IFailSuper
221
234
222
235
@Test
223
236
fun testOneOfElementCheck () {
224
- val data = OneOfData (FailType (1 , 2 ), " foo" )
237
+ val data = FailOuter (FailType (1 , 2 ), " foo" )
225
238
assertFailsWithMessage<IllegalArgumentException >(
226
239
message = " Implementation of oneOf type" +
227
240
" kotlinx.serialization.protobuf.ProtobufOneOfTest.FailType" +
228
241
" should contain only 1 element, but get 2"
229
242
) {
230
- ProtoBuf .encodeToHexString(OneOfData .serializer(), data)
243
+ ProtoBuf .encodeToHexString(data)
231
244
}
245
+
246
+ /* *
247
+ * 5:VARINT 42
248
+ * 3:LEN {"foo"}
249
+ */
250
+ assertFailsWithMessage<IllegalArgumentException >(
251
+ message = " Implementation of oneOf type" +
252
+ " kotlinx.serialization.protobuf.ProtobufOneOfTest.FailType" +
253
+ " should contain only 1 element, but get 2"
254
+ ) {
255
+ ProtoBuf .decodeFromHexString<FailOuter >(" 282a1a03666f6f" )
256
+ }
257
+
258
+ /* *
259
+ * 5:VARINT 1
260
+ * 6:VARINT 2
261
+ * 3:LEN {"foo"}
262
+ */
263
+ assertFailsWithMessage<IllegalArgumentException >(
264
+ message = " Implementation of oneOf type" +
265
+ " kotlinx.serialization.protobuf.ProtobufOneOfTest.FailType" +
266
+ " should contain only 1 element, but get 2"
267
+ ) {
268
+ ProtoBuf .decodeFromHexString<FailOuter >(
269
+ ProtoBuf .encodeToHexString(
270
+ FailOuterHelper (
271
+ i = 1 ,
272
+ j = 2 ,
273
+ name = " foo"
274
+ )
275
+ )
276
+ )
277
+ }
278
+
232
279
}
233
280
234
281
@Serializable
0 commit comments