Refactor local test dsl.
Add several cases and arabic digits conversion test.
This commit is contained in:
@@ -14,53 +14,59 @@ class ParsePrimitivesJVMTest {
|
||||
}
|
||||
|
||||
@Test fun toByte() {
|
||||
CompareBehaviorContext({it.toByte()}, {it.toByteOrNull()}).apply {
|
||||
assertProduce("+77", 77.toByte())
|
||||
assertProduce("-128", Byte.MIN_VALUE)
|
||||
compareConversion({it.toByte()}, {it.toByteOrNull()}) {
|
||||
assertProduces("127", Byte.MAX_VALUE)
|
||||
assertProduces("+77", 77.toByte())
|
||||
assertProduces("-128", Byte.MIN_VALUE)
|
||||
assertFailsOrNull("128")
|
||||
}
|
||||
|
||||
CompareBehaviorWithRadixContext(String::toByte, String::toByteOrNull).apply {
|
||||
assertProduce(16, "7F", 127.toByte())
|
||||
compareConversionWithRadix(String::toByte, String::toByteOrNull) {
|
||||
assertProduces(16, "7a", 0x7a.toByte())
|
||||
assertProduces(16, "+7F", 127.toByte())
|
||||
assertProduces(16, "-80", (-128).toByte())
|
||||
assertFailsOrNull(2, "10000000")
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun toShort() {
|
||||
CompareBehaviorContext({it.toShort()}, {it.toShortOrNull()}).apply {
|
||||
assertProduce("77", 77.toShort())
|
||||
assertProduce("-32768", Short.MIN_VALUE)
|
||||
compareConversion({it.toShort()}, {it.toShortOrNull()}) {
|
||||
assertProduces("+77", 77.toShort())
|
||||
assertProduces("32767", Short.MAX_VALUE)
|
||||
assertProduces("-32768", Short.MIN_VALUE)
|
||||
assertFailsOrNull("+32768")
|
||||
}
|
||||
|
||||
CompareBehaviorWithRadixContext(String::toShort, String::toShortOrNull).apply {
|
||||
assertProduce(16, "7F", 127.toShort())
|
||||
compareConversionWithRadix(String::toShort, String::toShortOrNull) {
|
||||
assertProduces(16, "7FFF", 0x7FFF.toShort())
|
||||
assertProduces(16, "-8000", (-0x8000).toShort())
|
||||
assertFailsOrNull(5, "10000000")
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun toInt() {
|
||||
CompareBehaviorContext({it.toInt()}, {it.toIntOrNull()}).apply {
|
||||
assertProduce("77", 77)
|
||||
assertProduce("+2147483647", Int.MAX_VALUE)
|
||||
assertProduce("-2147483648", Int.MIN_VALUE)
|
||||
compareConversion({it.toInt()}, {it.toIntOrNull()}) {
|
||||
assertProduces("77", 77)
|
||||
assertProduces("+2147483647", Int.MAX_VALUE)
|
||||
assertProduces("-2147483648", Int.MIN_VALUE)
|
||||
|
||||
assertFailsOrNull("2147483648")
|
||||
assertFailsOrNull("-2147483649")
|
||||
assertFailsOrNull("239239kotlin")
|
||||
}
|
||||
|
||||
CompareBehaviorWithRadixContext(String::toInt, String::toIntOrNull).apply {
|
||||
assertProduce(10, "0", 0)
|
||||
assertProduce(10, "473", 473)
|
||||
assertProduce(10, "+42", 42)
|
||||
assertProduce(10, "-0", 0)
|
||||
assertProduce(10, "2147483647", 2147483647)
|
||||
assertProduce(10, "-2147483648", -2147483648)
|
||||
compareConversionWithRadix(String::toInt, String::toIntOrNull) {
|
||||
assertProduces(10, "0", 0)
|
||||
assertProduces(10, "473", 473)
|
||||
assertProduces(10, "+42", 42)
|
||||
assertProduces(10, "-0", 0)
|
||||
assertProduces(10, "2147483647", 2147483647)
|
||||
assertProduces(10, "-2147483648", -2147483648)
|
||||
|
||||
assertProduce(16, "-FF", -255)
|
||||
assertProduce(2, "1100110", 102)
|
||||
assertProduce(27, "Kona", 411787)
|
||||
assertProduces(16, "-FF", -255)
|
||||
assertProduces(16, "-ff", -255)
|
||||
assertProduces(2, "1100110", 102)
|
||||
assertProduces(27, "Kona", 411787)
|
||||
|
||||
assertFailsOrNull(10, "2147483648")
|
||||
assertFailsOrNull(8, "99")
|
||||
@@ -71,11 +77,18 @@ class ParsePrimitivesJVMTest {
|
||||
assertFailsWith<NumberFormatException>("Expected to fail with radix 37") { "37".toIntOrNull(radix = 37) }
|
||||
}
|
||||
|
||||
@JvmVersion
|
||||
@Test fun toIntArabicDigits() {
|
||||
compareConversion({ it.toInt() }, { it.toIntOrNull() }) {
|
||||
assertProduces("٢٣١٩٦٠", 231960)
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun toLong() {
|
||||
CompareBehaviorContext({it.toLong()}, {it.toLongOrNull()}).apply {
|
||||
assertProduce("77", 77.toLong())
|
||||
assertProduce("+9223372036854775807", Long.MAX_VALUE)
|
||||
assertProduce("-9223372036854775808", Long.MIN_VALUE)
|
||||
compareConversion({it.toLong()}, {it.toLongOrNull()}) {
|
||||
assertProduces("77", 77.toLong())
|
||||
assertProduces("+9223372036854775807", Long.MAX_VALUE)
|
||||
assertProduces("-9223372036854775808", Long.MIN_VALUE)
|
||||
|
||||
assertFailsOrNull("9223372036854775808")
|
||||
assertFailsOrNull("-9223372036854775809")
|
||||
@@ -85,15 +98,17 @@ class ParsePrimitivesJVMTest {
|
||||
assertFailsOrNull("-922337KOTLIN775809")
|
||||
}
|
||||
|
||||
CompareBehaviorWithRadixContext(String::toLong, String::toLongOrNull).apply {
|
||||
assertProduce(10, "0", 0L)
|
||||
assertProduce(10, "473", 473L)
|
||||
assertProduce(10, "+42", 42L)
|
||||
assertProduce(10, "-0", 0L)
|
||||
compareConversionWithRadix(String::toLong, String::toLongOrNull) {
|
||||
assertProduces(10, "0", 0L)
|
||||
assertProduces(10, "473", 473L)
|
||||
assertProduces(10, "+42", 42L)
|
||||
assertProduces(10, "-0", 0L)
|
||||
|
||||
assertProduce(16, "-FF", -255L)
|
||||
assertProduce(2, "1100110", 102L)
|
||||
assertProduce(36, "Hazelnut", 1356099454469L)
|
||||
assertProduces(16, "7F11223344556677", 0x7F11223344556677)
|
||||
assertProduces(16, "+7faabbccddeeff00", 0x7faabbccddeeff00)
|
||||
assertProduces(16, "-8000000000000000", Long.MIN_VALUE)
|
||||
assertProduces(2, "1100110", 102L)
|
||||
assertProduces(36, "Hazelnut", 1356099454469L)
|
||||
|
||||
assertFailsOrNull(8, "99")
|
||||
assertFailsOrNull(10, "Hazelnut")
|
||||
@@ -103,30 +118,37 @@ class ParsePrimitivesJVMTest {
|
||||
assertFailsWith<NumberFormatException>("Expected to fail with radix 1") { "1".toLongOrNull(radix = 1) }
|
||||
}
|
||||
|
||||
@JvmVersion
|
||||
@Test fun toLongArabicDigits() {
|
||||
compareConversion({ it.toLong() }, { it.toLongOrNull() }) {
|
||||
assertProduces("٢٣١٩٦٠٧٧٨٤٥٩", 231960778459)
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun toFloat() {
|
||||
CompareBehaviorContext({it.toFloat()}, {it.toFloatOrNull()}).apply {
|
||||
assertProduce("77.0", 77.0f)
|
||||
assertProduce("-1e39", Float.NEGATIVE_INFINITY)
|
||||
assertProduce("1000000000000000000000000000000000000000", Float.POSITIVE_INFINITY)
|
||||
compareConversion(String::toFloat, String::toFloatOrNull) {
|
||||
assertProduces("77.0", 77.0f)
|
||||
assertProduces("-1e39", Float.NEGATIVE_INFINITY)
|
||||
assertProduces("1000000000000000000000000000000000000000", Float.POSITIVE_INFINITY)
|
||||
assertFailsOrNull("dark side")
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun toDouble() {
|
||||
CompareBehaviorContext({it.toDouble()}, {it.toDoubleOrNull()}).apply {
|
||||
assertProduce("-77", -77.0)
|
||||
assertProduce("77.", 77.0)
|
||||
assertProduce("77.0", 77.0)
|
||||
assertProduce("-1.77", -1.77)
|
||||
assertProduce("+.77", 0.77)
|
||||
assertProduce("\t-77 \n", -77.0)
|
||||
assertProduce("7.7e1", 77.0)
|
||||
assertProduce("+770e-1", 77.0)
|
||||
compareConversion(String::toDouble, String::toDoubleOrNull) {
|
||||
assertProduces("-77", -77.0)
|
||||
assertProduces("77.", 77.0)
|
||||
assertProduces("77.0", 77.0)
|
||||
assertProduces("-1.77", -1.77)
|
||||
assertProduces("+.77", 0.77)
|
||||
assertProduces("\t-77 \n", -77.0)
|
||||
assertProduces("7.7e1", 77.0)
|
||||
assertProduces("+770e-1", 77.0)
|
||||
|
||||
assertProduce("-NaN", -Double.NaN)
|
||||
assertProduce("+Infinity", Double.POSITIVE_INFINITY)
|
||||
assertProduce("0x77p1", (0x77 shl 1).toDouble())
|
||||
assertProduce("0x.77P8", 0x77.toDouble())
|
||||
assertProduces("-NaN", -Double.NaN)
|
||||
assertProduces("+Infinity", Double.POSITIVE_INFINITY)
|
||||
assertProduces("0x77p1", (0x77 shl 1).toDouble())
|
||||
assertProduces("0x.77P8", 0x77.toDouble())
|
||||
|
||||
assertFailsOrNull("7..7")
|
||||
assertFailsOrNull("0x77e1")
|
||||
@@ -136,9 +158,23 @@ class ParsePrimitivesJVMTest {
|
||||
}
|
||||
|
||||
|
||||
private class CompareBehaviorContext<T: Any>(val convertOrFail: (String) -> T,
|
||||
val convertOrNull: (String) -> T?) {
|
||||
fun assertProduce(input: String, output: T) {
|
||||
private fun <T : Any> compareConversion(convertOrFail: (String) -> T,
|
||||
convertOrNull: (String) -> T?,
|
||||
assertions: ConversionContext<T>.() -> Unit) {
|
||||
ConversionContext(convertOrFail, convertOrNull).assertions()
|
||||
}
|
||||
|
||||
|
||||
private fun <T : Any> compareConversionWithRadix(convertOrFail: String.(Int) -> T,
|
||||
convertOrNull: String.(Int) -> T?,
|
||||
assertions: ConversionWithRadixContext<T>.() -> Unit) {
|
||||
ConversionWithRadixContext(convertOrFail, convertOrNull).assertions()
|
||||
}
|
||||
|
||||
|
||||
private class ConversionContext<T: Any>(val convertOrFail: (String) -> T,
|
||||
val convertOrNull: (String) -> T?) {
|
||||
fun assertProduces(input: String, output: T) {
|
||||
assertEquals(output, convertOrFail(input.removeLeadingPlusOnJava6()))
|
||||
assertEquals(output, convertOrNull(input))
|
||||
}
|
||||
@@ -149,9 +185,9 @@ private class CompareBehaviorContext<T: Any>(val convertOrFail: (String) -> T,
|
||||
}
|
||||
}
|
||||
|
||||
private class CompareBehaviorWithRadixContext<T: Any>(val convertOrFail: String.(Int) -> T,
|
||||
val convertOrNull: String.(Int) -> T?) {
|
||||
fun assertProduce(radix: Int, input: String, output: T) {
|
||||
private class ConversionWithRadixContext<T: Any>(val convertOrFail: String.(Int) -> T,
|
||||
val convertOrNull: String.(Int) -> T?) {
|
||||
fun assertProduces(radix: Int, input: String, output: T) {
|
||||
assertEquals(output, input.removeLeadingPlusOnJava6().convertOrFail(radix))
|
||||
assertEquals(output, input.convertOrNull(radix))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user