Skip to content

Commit 74a08cb

Browse files
committed
Move standartizing test to common tests.
1 parent b6ea691 commit 74a08cb

File tree

5 files changed

+123
-69
lines changed

5 files changed

+123
-69
lines changed

plot-api/build.gradle.kts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ plugins {
1212
val letsPlotVersion = extra["letsPlot.version"] as String
1313
val kotlinxDatetimeVersion = extra["kotlinx.datetime.version"] as String
1414
val kotlinLoggingVersion = extra["kotlinLogging.version"] as String
15+
val kotlinxCoroutinesVersion = extra["kotlinx.coroutines.version"] as String
1516
val assertjVersion = extra["assertj.version"] as String
1617

1718
kotlin {
@@ -27,12 +28,14 @@ kotlin {
2728
api("org.jetbrains.lets-plot:plot-builder:$letsPlotVersion")
2829
api("org.jetbrains.lets-plot:plot-stem:$letsPlotVersion")
2930

30-
compileOnly("org.jetbrains.kotlinx:kotlinx-datetime:${kotlinxDatetimeVersion}") }
31+
compileOnly("org.jetbrains.kotlinx:kotlinx-datetime:${kotlinxDatetimeVersion}")
32+
}
3133
}
3234
commonTest {
3335
dependencies {
3436
implementation(kotlin("test"))
35-
}
37+
implementation("org.jetbrains.kotlinx:kotlinx-datetime:${kotlinxDatetimeVersion}")
38+
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:${kotlinxCoroutinesVersion}") }
3639
}
3740

3841
named("jvmMain") {
@@ -61,7 +64,6 @@ kotlin {
6164

6265
jvmTest {
6366
dependencies {
64-
// assertj
6567
implementation("org.assertj:assertj-core:$assertjVersion")
6668
}
6769
}

plot-api/src/jvmTest/kotlin/org/jetbrains/letsPlot/intern/standardizing/StandardizingTest.kt renamed to plot-api/src/commonTest/kotlin/org/jetbrains/letsPlot/intern/standardizing/StandardizingTest.kt

Lines changed: 45 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55

66
package org.jetbrains.letsPlot.intern.standardizing
77

8+
import kotlinx.datetime.TimeZone
9+
import kotlinx.datetime.toInstant
810
import org.jetbrains.letsPlot.commons.values.Color
9-
import java.time.*
10-
import java.util.*
1111
import kotlin.test.*
1212
import kotlinx.datetime.Instant as KInstant
1313
import kotlinx.datetime.LocalDate as KLocalDate
@@ -17,20 +17,20 @@ import org.jetbrains.letsPlot.commons.intern.datetime.Date as LPDate
1717
import org.jetbrains.letsPlot.commons.intern.datetime.DateTime as LPDateTime
1818
import org.jetbrains.letsPlot.commons.intern.datetime.Month as LPMonth
1919
import org.jetbrains.letsPlot.commons.intern.datetime.Time as LPTime
20-
import org.jetbrains.letsPlot.commons.intern.datetime.TimeZone as LPTZ
20+
import org.jetbrains.letsPlot.commons.intern.datetime.TimeZone as LPTimeZone
2121

2222

2323
class StandardizingTest {
2424

2525
@Test
26-
fun `Numeric values standardized to a Double`() {
26+
fun numeric_values_standardized_to_a_Double() {
2727
val numericValues = listOf(
2828
1.toByte(), // Byte
2929
2.toShort(), // Short
3030
2, // Int
3131
4L, // Long
32-
5.1f, // Float
33-
6.2 // Double
32+
5.1f, // Float
33+
6.2 // Double
3434
)
3535
val expected = numericValues.map { it.toDouble() }
3636

@@ -39,74 +39,62 @@ class StandardizingTest {
3939
}
4040

4141
@Test
42-
fun `Temporal values standardized to a Double`() {
42+
fun temporal_values_standardized_to_a_Double() {
4343
// Expected timestamp for 2023-01-01T12:30:45Z
4444
val expectedTimestamp = 1672576245000L
4545

46-
val expectedLocalDateTimestamp = ZonedDateTime.of(2023, 1, 1, 0, 0, 0, 0, ZoneId.of("UTC"))
47-
.toInstant().toEpochMilli()
46+
val expectedLocalDateTimestamp = KLocalDateTime(2023, 1, 1, 0, 0, 0, 0)
47+
.toInstant(TimeZone.UTC)
48+
.toEpochMilliseconds()
4849

49-
val expectedLocalTimeTimestamp = ZonedDateTime.of(1970, 1, 1, 12, 30, 45, 0, ZoneId.of("UTC"))
50-
.toInstant().toEpochMilli()
51-
52-
// Java time API
53-
val zonedDateTime = ZonedDateTime.of(2023, 1, 1, 12, 30, 45, 0, ZoneId.of("UTC"))
54-
val instant = zonedDateTime.toInstant()
55-
val localDate = zonedDateTime.toLocalDate()
56-
val localTime = zonedDateTime.toLocalTime()
57-
val localDateTime = zonedDateTime.toLocalDateTime()
58-
val date = Date.from(instant)
50+
val expectedLocalTimeTimestamp = KLocalDateTime(1970, 1, 1, 12, 30, 45, 0)
51+
.toInstant(TimeZone.UTC)
52+
.toEpochMilliseconds()
5953

6054
// Kotlinx.datetime API
6155
val kInstant = KInstant.fromEpochMilliseconds(expectedTimestamp)
6256
val kLocalDate = KLocalDate(2023, 1, 1)
6357
val kLocalTime = KLocalTime(12, 30, 45)
6458
val kLocalDateTime = KLocalDateTime(2023, 1, 1, 12, 30, 45)
6559

66-
val timestampValues = listOf(
67-
zonedDateTime,
68-
instant,
69-
localDate,
70-
localTime,
71-
localDateTime,
72-
date,
73-
60+
val values = listOf(
7461
kInstant,
7562
kLocalDate,
7663
kLocalTime,
7764
kLocalDateTime,
7865
)
7966

80-
val timestampValuesStandardized = SeriesStandardizing.toList(timestampValues)
81-
timestampValues.zip(timestampValuesStandardized).forEach { (input, result) ->
82-
assertTrue(
83-
result is Double,
84-
"Input: $input, ${input.javaClass.name}, result expected Double but was: ${result?.javaClass?.name}"
85-
)
86-
87-
val expected = when (input) {
88-
is LocalDate,
89-
is KLocalDate -> expectedLocalDateTimestamp.toDouble()
90-
91-
is LocalTime,
92-
is KLocalTime -> expectedLocalTimeTimestamp.toDouble()
93-
94-
is LocalDateTime,
67+
val expectedValues = values.map {
68+
when (it) {
69+
is KLocalDate -> expectedLocalDateTimestamp.toDouble() // Same as ZonedDateTime because here we use UTC
70+
is KLocalTime -> expectedLocalTimeTimestamp.toDouble() // Same as ZonedDateTime because here we use UTC
9571
is KLocalDateTime -> expectedTimestamp.toDouble() // Same as ZonedDateTime because here we use UTC
96-
9772
else -> expectedTimestamp.toDouble()
9873
}
74+
} + StandardizingTestJvmValues.getExpectedValues()
75+
9976

100-
assertEquals(expected, result, "Input: $input, ${input.javaClass.name}")
77+
val standardizedValues = SeriesStandardizing.toList(values + StandardizingTestJvmValues.getTestValues())
78+
values.zip(expectedValues).zip(standardizedValues) { (a, b), c ->
79+
Triple(a, b, c)
10180
}
81+
.forEach { (input, expected, result) ->
82+
// Expect all results to be Double
83+
assertTrue(
84+
result is Double,
85+
"Input: $input, ${input::class}, result expected Double but was: ${result!!::class}"
86+
)
87+
88+
assertEquals(expected, result, "Input: $input, ${input::class}")
89+
}
10290
}
10391

10492
@Test
105-
fun `LP internal datetime values are not supported`() {
93+
fun lp_internal_datetime_values_are_not_supported() {
10694
val date = LPDate(1, LPMonth.JANUARY, 2023)
10795
val time = LPTime(12, 30, 45)
10896
val dateTime = LPDateTime(date, time)
109-
val instant = dateTime.toInstant(LPTZ("UTC"))
97+
val instant = dateTime.toInstant(LPTimeZone("UTC"))
11098

11199
assertFailsWith<IllegalArgumentException> {
112100
Standardizing.standardizeValue(instant)
@@ -124,17 +112,12 @@ class StandardizingTest {
124112

125113

126114
@Test
127-
fun `Collection of mixed values`() {
128-
val dateTime = ZonedDateTime.of(
129-
2020,
130-
6,
131-
10,
132-
13,
133-
58,
134-
0,
135-
0,
136-
ZoneId.of("EST", ZoneId.SHORT_IDS)
137-
)
115+
fun a_collection_of_mixed_values() {
116+
// LocalDateTime in EST (UTC-5)
117+
val localDateTime = KLocalDateTime(2020, 6, 10, 13, 58)
118+
// val timeZone = TimeZone.of("UTC-05:00")
119+
val timeZone = TimeZone.of("UTC")
120+
val instant = localDateTime.toInstant(timeZone)
138121

139122

140123
val values = listOf(
@@ -145,10 +128,8 @@ class StandardizingTest {
145128
Double.NaN, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY,
146129
State.Idle,
147130
Color.WHITE,
148-
java.awt.Color.WHITE,
149-
dateTime,
150-
dateTime.toInstant(),
151-
Date(dateTime.toInstant().toEpochMilli()),
131+
instant,
132+
localDateTime,
152133
)
153134

154135
val expected = listOf(
@@ -159,14 +140,12 @@ class StandardizingTest {
159140
null, null, null,
160141
"Idle",
161142
"#ffffff",
162-
"#ffffff",
163-
1.59181548E12,
164-
1.59181548E12,
165-
1.59181548E12
143+
1591797480000L.toDouble(),
144+
1591797480000L.toDouble(),
166145
)
167146

168147
val result = SeriesStandardizing.toList(values)
169-
assertEquals(expected, result)
148+
assertContentEquals(expected, result)
170149
}
171150
}
172151

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*
2+
* Copyright (c) 2025. JetBrains s.r.o.
3+
* Use of this source code is governed by the MIT license that can be found in the LICENSE file.
4+
*/
5+
6+
package org.jetbrains.letsPlot.intern.standardizing
7+
8+
expect object StandardizingTestJvmValues {
9+
fun getTestValues(): List<Any?>
10+
fun getExpectedValues(): List<Any?>
11+
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*
2+
* Copyright (c) 2025. JetBrains s.r.o.
3+
* Use of this source code is governed by the MIT license that can be found in the LICENSE file.
4+
*/
5+
6+
package org.jetbrains.letsPlot.intern.standardizing
7+
8+
actual object StandardizingTestJvmValues {
9+
actual fun getTestValues(): List<Any?> = emptyList()
10+
actual fun getExpectedValues(): List<Any?> = emptyList()
11+
12+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright (c) 2025. JetBrains s.r.o.
3+
* Use of this source code is governed by the MIT license that can be found in the LICENSE file.
4+
*/
5+
6+
package org.jetbrains.letsPlot.intern.standardizing
7+
8+
import java.time.*
9+
import java.util.*
10+
11+
actual object StandardizingTestJvmValues {
12+
actual fun getTestValues(): List<Any?> {
13+
val zonedDateTime = ZonedDateTime.of(2023, 1, 1, 12, 30, 45, 0, ZoneId.of("UTC"))
14+
val instant = zonedDateTime.toInstant()
15+
val localDate = zonedDateTime.toLocalDate()
16+
val localTime = zonedDateTime.toLocalTime()
17+
val localDateTime = zonedDateTime.toLocalDateTime()
18+
val date = Date.from(instant)
19+
20+
return listOf(
21+
zonedDateTime,
22+
instant,
23+
localDate,
24+
localTime,
25+
localDateTime,
26+
date,
27+
)
28+
}
29+
30+
actual fun getExpectedValues(): List<Any?> {
31+
// Expected timestamp for 2023-01-01T12:30:45Z
32+
val expectedTimestamp = 1672576245000L
33+
34+
val expectedLocalDateTimestamp = ZonedDateTime.of(2023, 1, 1, 0, 0, 0, 0, ZoneId.of("UTC"))
35+
.toInstant().toEpochMilli()
36+
37+
val expectedLocalTimeTimestamp = ZonedDateTime.of(1970, 1, 1, 12, 30, 45, 0, ZoneId.of("UTC"))
38+
.toInstant().toEpochMilli()
39+
40+
return getTestValues().map {
41+
when (it) {
42+
is LocalDate -> expectedLocalDateTimestamp
43+
is LocalTime -> expectedLocalTimeTimestamp
44+
is LocalDateTime -> expectedTimestamp // Same as ZonedDateTime because here we use UTC
45+
else -> expectedTimestamp
46+
}
47+
}
48+
}
49+
}

0 commit comments

Comments
 (0)