forked from Kotlin/kotlinx.serialization
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support to decode numeric literals containing an exponent (Kotlin…
…#2227) Fixes Kotlin#2078
- Loading branch information
1 parent
de4198b
commit 124aa5d
Showing
5 changed files
with
159 additions
and
11 deletions.
There are no files selected for viewing
79 changes: 79 additions & 0 deletions
79
formats/json-tests/commonTest/src/kotlinx/serialization/json/JsonExponentTest.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,79 @@ | ||
package kotlinx.serialization.json | ||
|
||
import kotlinx.serialization.Serializable | ||
import kotlinx.serialization.test.* | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
class JsonExponentTest : JsonTestBase() { | ||
@Serializable | ||
data class SomeData(val count: Long) | ||
@Serializable | ||
data class SomeDataDouble(val count: Double) | ||
|
||
@Test | ||
fun testExponentDecodingPositive() = parametrizedTest { | ||
val decoded = Json.decodeFromString<SomeData>("""{ "count": 23e11 }""", it) | ||
assertEquals(2300000000000, decoded.count) | ||
} | ||
|
||
@Test | ||
fun testExponentDecodingNegative() = parametrizedTest { | ||
val decoded = Json.decodeFromString<SomeData>("""{ "count": -10E1 }""", it) | ||
assertEquals(-100, decoded.count) | ||
} | ||
|
||
@Test | ||
fun testExponentDecodingPositiveDouble() = parametrizedTest { | ||
val decoded = Json.decodeFromString<SomeDataDouble>("""{ "count": 1.5E1 }""", it) | ||
assertEquals(15.0, decoded.count) | ||
} | ||
|
||
@Test | ||
fun testExponentDecodingNegativeDouble() = parametrizedTest { | ||
val decoded = Json.decodeFromString<SomeDataDouble>("""{ "count": -1e-1 }""", it) | ||
assertEquals(-0.1, decoded.count) | ||
} | ||
|
||
@Test | ||
fun testExponentDecodingErrorTruncatedDecimal() = parametrizedTest { | ||
assertFailsWithSerial("JsonDecodingException") | ||
{ Json.decodeFromString<SomeData>("""{ "count": -1E-1 }""", it) } | ||
} | ||
|
||
@Test | ||
fun testExponentDecodingErrorExponent() = parametrizedTest { | ||
assertFailsWithSerial("JsonDecodingException") | ||
{ Json.decodeFromString<SomeData>("""{ "count": 1e-1e-1 }""", it) } | ||
} | ||
|
||
@Test | ||
fun testExponentDecodingErrorExponentDouble() = parametrizedTest { | ||
assertFailsWithSerial("JsonDecodingException") | ||
{ Json.decodeFromString<SomeDataDouble>("""{ "count": 1e-1e-1 }""", it) } | ||
} | ||
|
||
@Test | ||
fun testExponentOverflowDouble() = parametrizedTest { | ||
assertFailsWithSerial("JsonDecodingException") | ||
{ Json.decodeFromString<SomeDataDouble>("""{ "count": 10000e10000 }""", it) } | ||
} | ||
|
||
@Test | ||
fun testExponentUnderflowDouble() = parametrizedTest { | ||
assertFailsWithSerial("JsonDecodingException") | ||
{ Json.decodeFromString<SomeDataDouble>("""{ "count": -100e2222 }""", it) } | ||
} | ||
|
||
@Test | ||
fun testExponentOverflow() = parametrizedTest { | ||
assertFailsWithSerial("JsonDecodingException") | ||
{ Json.decodeFromString<SomeData>("""{ "count": 10000e10000 }""", it) } | ||
} | ||
|
||
@Test | ||
fun testExponentUnderflow() = parametrizedTest { | ||
assertFailsWithSerial("JsonDecodingException") | ||
{ Json.decodeFromString<SomeData>("""{ "count": -10000e10000 }""", it) } | ||
} | ||
} |
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
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