Skip to content

Commit 8044d95

Browse files
committed
adding comma grouping double parser test
1 parent db7c57d commit 8044d95

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed

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

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.jetbrains.kotlinx.dataframe.io
22

3+
import io.kotest.assertions.throwables.shouldThrow
34
import io.kotest.matchers.shouldBe
45
import kotlinx.datetime.LocalDateTime
56
import kotlinx.datetime.LocalTime
@@ -157,7 +158,9 @@ class ParserTests {
157158
// *
158159
// (3 locales as converting parameter + original converting + original converting to nullable)
159160
val parsingLocaleNotDefined: Locale? = null // takes parserOptions.locale ?: Locale.getDefault()
161+
// uses dot as decimal separator, comma as grouping separator
160162
val parsingLocaleUsesDot: Locale = Locale.forLanguageTag("en-US")
163+
// uses comma as decimal separator, NBSP as grouping separator
161164
val parsingLocaleUsesComma: Locale = Locale.forLanguageTag("ru-RU")
162165
// *
163166
// 3 system locales
@@ -246,6 +249,99 @@ class ParserTests {
246249
}
247250
}
248251

252+
@Test
253+
fun `converting String to Double in different locales with comma grouping`() {
254+
val systemLocale = Locale.getDefault()
255+
try {
256+
// Test 45 behaviour combinations:
257+
258+
// 3 source columns
259+
val columnDot = columnOf("123,456.789", "0,987,654.321")
260+
val columnComma = columnOf("123.456,789", "0.987.654,321")
261+
val columnMixed = columnOf("123,456.789", "0.987.654,321")
262+
// *
263+
// (3 locales as converting parameter + original converting + original converting to nullable)
264+
val parsingLocaleNotDefined: Locale? = null // takes parserOptions.locale ?: Locale.getDefault()
265+
val parsingLocaleUsesDot: Locale = Locale.forLanguageTag("en-US")
266+
val parsingLocaleUsesComma: Locale = Locale.forLanguageTag("nl-NL")
267+
// *
268+
// 3 system locales
269+
// --------------------------------------------------------------------------------
270+
271+
Locale.setDefault(Locale.forLanguageTag("C.UTF-8"))
272+
273+
columnDot.convertTo<Double>() shouldBe columnOf(123_456.789, 987_654.321)
274+
shouldThrow<TypeConversionException> { columnComma.convertTo<Double>() }
275+
shouldThrow<TypeConversionException> { columnMixed.convertTo<Double>() }
276+
277+
columnDot.convertTo<Double?>() shouldBe columnOf(123_456.789, 987_654.321)
278+
shouldThrow<TypeConversionException> { columnComma.convertTo<Double?>() }
279+
shouldThrow<TypeConversionException> { columnMixed.convertTo<Double?>() }
280+
281+
columnDot.convertToDouble(parsingLocaleNotDefined) shouldBe columnOf(123_456.789, 987_654.321)
282+
shouldThrow<TypeConversionException> { columnComma.convertToDouble(parsingLocaleNotDefined) }
283+
shouldThrow<TypeConversionException> { columnMixed.convertToDouble(parsingLocaleNotDefined) }
284+
285+
columnDot.convertToDouble(parsingLocaleUsesDot) shouldBe columnOf(123_456.789, 987_654.321)
286+
shouldThrow<TypeConversionException> { columnComma.convertToDouble(parsingLocaleUsesDot) }
287+
shouldThrow<TypeConversionException> { columnMixed.convertToDouble(parsingLocaleUsesDot) }
288+
289+
shouldThrow<TypeConversionException> { columnDot.convertToDouble(parsingLocaleUsesComma) }
290+
columnComma.convertToDouble(parsingLocaleUsesComma) shouldBe columnOf(123_456.789, 987_654.321)
291+
shouldThrow<TypeConversionException> { columnMixed.convertToDouble(parsingLocaleUsesComma) }
292+
293+
// --------------------------------------------------------------------------------
294+
295+
Locale.setDefault(Locale.forLanguageTag("en-US"))
296+
297+
columnDot.convertTo<Double>() shouldBe columnOf(123_456.789, 987_654.321)
298+
shouldThrow<TypeConversionException> { columnComma.convertTo<Double>() }
299+
shouldThrow<TypeConversionException> { columnMixed.convertTo<Double>() }
300+
301+
columnDot.convertTo<Double?>() shouldBe columnOf(123_456.789, 987_654.321)
302+
shouldThrow<TypeConversionException> { columnComma.convertTo<Double?>() }
303+
shouldThrow<TypeConversionException> { columnMixed.convertTo<Double?>() }
304+
305+
columnDot.convertToDouble(parsingLocaleNotDefined) shouldBe columnOf(123_456.789, 987_654.321)
306+
shouldThrow<TypeConversionException> { columnComma.convertToDouble(parsingLocaleNotDefined) }
307+
shouldThrow<TypeConversionException> { columnMixed.convertToDouble(parsingLocaleNotDefined) }
308+
309+
columnDot.convertToDouble(parsingLocaleUsesDot) shouldBe columnOf(123_456.789, 987_654.321)
310+
shouldThrow<TypeConversionException> { columnComma.convertToDouble(parsingLocaleUsesDot) }
311+
shouldThrow<TypeConversionException> { columnMixed.convertToDouble(parsingLocaleUsesDot) }
312+
313+
shouldThrow<TypeConversionException> { columnDot.convertToDouble(parsingLocaleUsesComma) }
314+
columnComma.convertToDouble(parsingLocaleUsesComma) shouldBe columnOf(123_456.789, 987_654.321)
315+
shouldThrow<TypeConversionException> { columnMixed.convertToDouble(parsingLocaleUsesComma) }
316+
317+
// --------------------------------------------------------------------------------
318+
319+
Locale.setDefault(Locale.forLanguageTag("nl-NL"))
320+
321+
columnDot.convertTo<Double>() shouldBe columnOf(123_456.789, 987_654.321)
322+
columnComma.convertTo<Double>() shouldBe columnOf(123_456.789, 987_654.321)
323+
shouldThrow<TypeConversionException> { columnMixed.convertTo<Double>() }
324+
325+
columnDot.convertTo<Double?>() shouldBe columnOf(123_456.789, 987_654.321)
326+
columnComma.convertTo<Double?>() shouldBe columnOf(123_456.789, 987_654.321)
327+
shouldThrow<TypeConversionException> { columnMixed.convertTo<Double?>() }
328+
329+
columnDot.convertToDouble(parsingLocaleNotDefined) shouldBe columnOf(123_456.789, 987_654.321)
330+
columnComma.convertToDouble(parsingLocaleNotDefined) shouldBe columnOf(123_456.789, 987_654.321)
331+
shouldThrow<TypeConversionException> { columnMixed.convertToDouble(parsingLocaleNotDefined) }
332+
333+
columnDot.convertToDouble(parsingLocaleUsesDot) shouldBe columnOf(123_456.789, 987_654.321)
334+
shouldThrow<TypeConversionException> { columnComma.convertToDouble(parsingLocaleUsesDot) }
335+
shouldThrow<TypeConversionException> { columnMixed.convertToDouble(parsingLocaleUsesDot) }
336+
337+
shouldThrow<TypeConversionException> { columnDot.convertToDouble(parsingLocaleUsesComma) }
338+
columnComma.convertToDouble(parsingLocaleUsesComma) shouldBe columnOf(123_456.789, 987_654.321)
339+
shouldThrow<TypeConversionException> { columnMixed.convertToDouble(parsingLocaleUsesComma) }
340+
} finally {
341+
Locale.setDefault(systemLocale)
342+
}
343+
}
344+
249345
/** Checks fix for [Issue #593](https://github.com/Kotlin/dataframe/issues/593) */
250346
@Test
251347
fun `Mixing null and json`() {

0 commit comments

Comments
 (0)