Skip to content

Commit 34ab701

Browse files
committed
Add tests for no-timezone-database JS implementation
1 parent 74cbbe3 commit 34ab701

File tree

3 files changed

+162
-1
lines changed

3 files changed

+162
-1
lines changed

js-without-timezones/build.gradle.kts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
@file:OptIn(ExperimentalWasmDsl::class)
2+
3+
import org.jetbrains.kotlin.gradle.targets.js.dsl.ExperimentalWasmDsl
4+
import java.util.Locale
5+
6+
plugins {
7+
id("kotlin-multiplatform")
8+
id("org.jetbrains.kotlinx.kover")
9+
}
10+
11+
val mainJavaToolchainVersion: String by project
12+
13+
java {
14+
toolchain { languageVersion.set(JavaLanguageVersion.of(mainJavaToolchainVersion)) }
15+
}
16+
17+
kotlin {
18+
19+
js {
20+
nodejs {
21+
}
22+
compilations.all {
23+
kotlinOptions {
24+
sourceMap = true
25+
moduleKind = "umd"
26+
metaInfo = true
27+
}
28+
}
29+
}
30+
31+
32+
wasmJs {
33+
nodejs {
34+
}
35+
}
36+
37+
sourceSets.all {
38+
val suffixIndex = name.indexOfLast { it.isUpperCase() }
39+
val targetName = name.substring(0, suffixIndex)
40+
val suffix = name.substring(suffixIndex).toLowerCase(Locale.ROOT).takeIf { it != "main" }
41+
kotlin.srcDir("$targetName/${suffix ?: "src"}")
42+
resources.srcDir("$targetName/${suffix?.let { it + "Resources" } ?: "resources"}")
43+
}
44+
45+
targets.withType<org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget> {
46+
compilations["test"].kotlinOptions {
47+
freeCompilerArgs += listOf("-trw")
48+
}
49+
}
50+
51+
sourceSets {
52+
commonMain {
53+
dependencies {
54+
api(project(":kotlinx-datetime"))
55+
}
56+
}
57+
58+
commonTest {
59+
dependencies {
60+
api("org.jetbrains.kotlin:kotlin-test")
61+
}
62+
}
63+
}
64+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Copyright 2019-2024 JetBrains s.r.o. and contributors.
3+
* Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file.
4+
*/
5+
6+
package kotlinx.datetime.test
7+
8+
import kotlinx.datetime.*
9+
import kotlin.test.*
10+
11+
class TimezonesWithoutDatabaseTest {
12+
@Test
13+
fun system() {
14+
val tz = TimeZone.currentSystemDefault()
15+
assertEquals("SYSTEM", tz.id)
16+
assertEquals("SYSTEM", tz.toString())
17+
val now = Clock.System.now()
18+
assertEquals(now, now.toLocalDateTime(tz).toInstant(tz))
19+
val offset = now.offsetIn(tz)
20+
println("$now = ${now.toLocalDateTime(tz)}$offset")
21+
assertEquals(now, now.toLocalDateTime(tz).toInstant(offset))
22+
assertEquals(Instant.DISTANT_PAST, Instant.DISTANT_PAST.toLocalDateTime(tz).toInstant(tz))
23+
assertEquals(Instant.DISTANT_FUTURE, Instant.DISTANT_FUTURE.toLocalDateTime(tz).toInstant(tz))
24+
val today = now.toLocalDateTime(tz).date
25+
assertEquals(today.atTime(0, 0).toInstant(tz), today.atStartOfDayIn(tz))
26+
}
27+
28+
@Test
29+
fun utc() {
30+
val utc: FixedOffsetTimeZone = TimeZone.UTC
31+
println(utc)
32+
assertEquals("Z", utc.id)
33+
assertEquals(UtcOffset.ZERO, utc.offset)
34+
assertEquals(0, utc.offset.totalSeconds)
35+
assertEquals(utc.offset, utc.offsetAt(Clock.System.now()))
36+
}
37+
38+
@Test
39+
fun available() {
40+
assertEquals(setOf("UTC"), TimeZone.availableZoneIds)
41+
}
42+
43+
@Test
44+
fun of() {
45+
assertFailsWith<IllegalTimeZoneException> { TimeZone.of("Europe/Moscow") }
46+
assertSame(TimeZone.currentSystemDefault(), TimeZone.of("SYSTEM"))
47+
}
48+
49+
// from 310bp
50+
@Test
51+
fun timeZoneEquals() {
52+
val test1 = TimeZone.of("SYSTEM")
53+
val test2 = TimeZone.of("UTC")
54+
val test2b = TimeZone.of("UTC+00:00")
55+
assertEquals(false, test1 == test2)
56+
assertEquals(false, test2 == test1)
57+
58+
assertEquals(true, test1 == test1)
59+
assertEquals(true, test2 == test2)
60+
assertEquals(true, test2 == test2b)
61+
62+
assertEquals(test1.hashCode(), test1.hashCode())
63+
assertEquals(test2.hashCode(), test2.hashCode())
64+
assertEquals(test2.hashCode(), test2b.hashCode())
65+
}
66+
67+
// from 310bp
68+
@Test
69+
fun timeZoneToString() {
70+
val idToString = arrayOf(
71+
Pair("Z", "Z"),
72+
Pair("UTC", "UTC"),
73+
Pair("UTC+01:00", "UTC+01:00"),
74+
Pair("GMT+01:00", "GMT+01:00"),
75+
Pair("UT+01:00", "UT+01:00"))
76+
for ((id, str) in idToString) {
77+
assertEquals(str, TimeZone.of(id).toString())
78+
}
79+
}
80+
81+
@Test
82+
fun utcOffsetNormalization() {
83+
val sameOffsetTZs = listOf("+04", "+04:00", "UTC+4", "UT+04", "GMT+04:00:00").map { TimeZone.of(it) }
84+
for (tz in sameOffsetTZs) {
85+
assertIs<FixedOffsetTimeZone>(tz)
86+
}
87+
val offsets = sameOffsetTZs.map { (it as FixedOffsetTimeZone).offset }
88+
val zoneIds = sameOffsetTZs.map { it.id }
89+
90+
assertTrue(offsets.distinct().size == 1, "Expected all offsets to be equal: $offsets")
91+
assertTrue(offsets.map { it.toString() }.distinct().size == 1, "Expected all offsets to have the same string representation: $offsets")
92+
93+
assertTrue(zoneIds.distinct().size > 1, "Expected some fixed offset zones to have different ids: $zoneIds")
94+
}
95+
96+
}

settings.gradle.kts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@ include(":timezones/full")
2020
project(":timezones/full").name = "kotlinx-datetime-zoneinfo"
2121
include(":serialization")
2222
project(":serialization").name = "kotlinx-datetime-serialization"
23+
include(":js-without-timezones")
24+
project(":js-without-timezones").name = "kotlinx-datetime-js-without-timezones"
2325
include(":benchmarks")
24-

0 commit comments

Comments
 (0)