-
Notifications
You must be signed in to change notification settings - Fork 623
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Throw SerializationException instead of IllegalStateException in Enum… (
#1325) * Throw SerializationException instead of IllegalStateException in EnumSerializer * Fix (almost) infinite loop in readVarintSlowPath on JS Because of shift+=7 addition and strict condition shift != 64, the loop never ends. This resulted in test timeout fail on JS. On JVM and Native, due to shift overflow, the test eventually came out of the loop (but that took quite a big amount of time, about million overflows). Fixes #1303 Co-authored-by: Leonid Startsev <[email protected]>
- Loading branch information
1 parent
358dc0b
commit 7a0f671
Showing
4 changed files
with
53 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
formats/protobuf/commonTest/src/kotlinx/serialization/protobuf/ProtobufEnumTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright 2017-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package kotlinx.serialization.protobuf | ||
|
||
import kotlinx.serialization.* | ||
import kotlin.test.* | ||
|
||
class ProtobufEnumTest { | ||
|
||
enum class SomeEnum { ALPHA, BETA, GAMMA } | ||
|
||
@Serializable | ||
data class EnumWithUnion(@ProtoNumber(5) val s: String, | ||
@ProtoNumber(6) val e: SomeEnum = SomeEnum.ALPHA, | ||
@ProtoNumber(7) val i: Int = 42) | ||
|
||
@Test | ||
fun testEnumWithUnion() { | ||
val data = EnumWithUnion("foo", SomeEnum.BETA) | ||
val hex = ProtoBuf.encodeToHexString(EnumWithUnion.serializer(), data) | ||
val restored = ProtoBuf.decodeFromHexString(EnumWithUnion.serializer(), hex) | ||
assertEquals(data, restored) | ||
} | ||
|
||
@Serializable | ||
class EnumHolder(val e: SomeEnum) | ||
|
||
@Test | ||
fun testUnknownValue() { | ||
val bytes = ProtoBuf.encodeToByteArray(EnumHolder(SomeEnum.ALPHA)) | ||
bytes[1] = 3 | ||
assertFailsWith<SerializationException> { ProtoBuf.decodeFromByteArray<EnumHolder>(bytes) } | ||
bytes[1] = -1 | ||
assertFailsWith<SerializationException> { ProtoBuf.decodeFromByteArray<EnumHolder>(bytes) } | ||
|
||
} | ||
} |
26 changes: 0 additions & 26 deletions
26
formats/protobuf/commonTest/src/kotlinx/serialization/protobuf/ProtobufUnionEnumTest.kt
This file was deleted.
Oops, something went wrong.