Skip to content

Commit 64cd9e8

Browse files
committed
Unit test "converting string to double in different locales"
1 parent 7a5fdfb commit 64cd9e8

File tree

2 files changed

+26
-4
lines changed
  • core/src
    • main/kotlin/org/jetbrains/kotlinx/dataframe/impl/api
    • test/kotlin/org/jetbrains/kotlinx/dataframe/io

2 files changed

+26
-4
lines changed

core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/impl/api/convert.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import org.jetbrains.kotlinx.dataframe.type
3636
import java.math.BigDecimal
3737
import java.net.URL
3838
import java.time.LocalTime
39+
import java.util.*
3940
import kotlin.math.roundToInt
4041
import kotlin.math.roundToLong
4142
import kotlin.reflect.KType
@@ -81,15 +82,15 @@ internal fun AnyCol.convertToTypeImpl(to: KType): AnyCol {
8182
return when {
8283
from == to -> this
8384
from.isSubtypeOf(to) -> (this as DataColumnInternal<*>).changeType(to.withNullability(hasNulls()))
84-
else -> when (val converter = getConverter(from, to)) {
85+
else -> when (val converter = getConverter(from, to, ParserOptions(locale = Locale.getDefault()))) {
8586
null -> when (from.classifier) {
8687
Any::class, Number::class, java.io.Serializable::class -> {
8788
// find converter for every value
8889
val values = values.map {
8990
it?.let {
9091
val clazz = it.javaClass.kotlin
9192
val type = clazz.createStarProjectedType(false)
92-
val converter = getConverter(type, to) ?: throw TypeConverterNotFoundException(from, to)
93+
val converter = getConverter(type, to, ParserOptions(locale = Locale.getDefault())) ?: throw TypeConverterNotFoundException(from, to)
9394
converter(it)
9495
}.checkNulls()
9596
}
@@ -107,9 +108,9 @@ internal fun AnyCol.convertToTypeImpl(to: KType): AnyCol {
107108
}
108109
}
109110

110-
internal val convertersCache = mutableMapOf<Pair<KType, KType>, TypeConverter?>()
111+
internal val convertersCache = mutableMapOf<Triple<KType, KType, ParserOptions?>, TypeConverter?>()
111112

112-
internal fun getConverter(from: KType, to: KType): TypeConverter? = convertersCache.getOrPut(from to to) { createConverter(from, to) }
113+
internal fun getConverter(from: KType, to: KType, options: ParserOptions? = null): TypeConverter? = convertersCache.getOrPut(Triple(from, to, options)) { createConverter(from, to, options) }
113114

114115
internal typealias TypeConverter = (Any) -> Any?
115116

core/src/test/kotlin/org/jetbrains/kotlinx/dataframe/io/ParserTests.kt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ package org.jetbrains.kotlinx.dataframe.io
22

33
import io.kotest.matchers.shouldBe
44
import kotlinx.datetime.LocalDateTime
5+
import org.jetbrains.kotlinx.dataframe.DataColumn
56
import org.jetbrains.kotlinx.dataframe.DataFrame
67
import org.jetbrains.kotlinx.dataframe.api.*
78
import org.jetbrains.kotlinx.dataframe.api.columnOf
89
import org.jetbrains.kotlinx.dataframe.exceptions.TypeConversionException
910
import org.junit.Test
1011
import java.math.BigDecimal
12+
import java.util.*
1113
import kotlin.reflect.typeOf
1214

1315
class ParserTests {
@@ -58,4 +60,23 @@ class ParserTests {
5860
converted[0] shouldBe 1.0f
5961
converted[1] shouldBe 0.321f
6062
}
63+
64+
@Test
65+
fun `converting string to double in different locales`() {
66+
val currentLocale = Locale.getDefault()
67+
try {
68+
val stringValues = listOf("1", "2.3", "4,5")
69+
val stringColumn = DataColumn.createValueColumn("nums", stringValues, typeOf<String>())
70+
Locale.setDefault(Locale.forLanguageTag("ru-RU"))
71+
stringColumn.convertToDouble().shouldBe(
72+
DataColumn.createValueColumn("nums", listOf(1.0, 2.3, 4.5), typeOf<Double>())
73+
)
74+
Locale.setDefault(Locale.forLanguageTag("en-US"))
75+
stringColumn.convertToDouble().shouldBe(
76+
DataColumn.createValueColumn("nums", listOf(1.0, 2.3, 45.0), typeOf<Double>())
77+
)
78+
} finally {
79+
Locale.setDefault(currentLocale)
80+
}
81+
}
6182
}

0 commit comments

Comments
 (0)