Skip to content

Commit bf1195d

Browse files
committed
Add tests for DateTimePeriod normalization
1 parent 54930c6 commit bf1195d

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

core/common/test/DateTimePeriodTest.kt

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,39 @@ import kotlin.time.*
1212

1313
class DateTimePeriodTest {
1414

15+
@Test
16+
fun normalization() {
17+
assertPeriodComponents(DateTimePeriod(years = 1) as DatePeriod, years = 1)
18+
assertPeriodComponents(DateTimePeriod(years = 1, months = 1) as DatePeriod, years = 1, months = 1)
19+
assertPeriodComponents(DateTimePeriod(years = 1, months = -1) as DatePeriod, months = 11)
20+
assertPeriodComponents(DateTimePeriod(years = -1, months = 1) as DatePeriod, months = -11)
21+
assertPeriodComponents(DateTimePeriod(years = -1, months = -1) as DatePeriod, years = -1, months = -1)
22+
assertPeriodComponents(DateTimePeriod(months = 11) as DatePeriod, months = 11)
23+
assertPeriodComponents(DateTimePeriod(months = 14) as DatePeriod, years = 1, months = 2)
24+
assertPeriodComponents(DateTimePeriod(months = -14) as DatePeriod, years = -1, months = -2)
25+
assertPeriodComponents(DateTimePeriod(months = 10, days = 5) as DatePeriod, months = 10, days = 5)
26+
assertPeriodComponents(DateTimePeriod(years = 1, days = 40) as DatePeriod, years = 1, days = 40)
27+
assertPeriodComponents(DateTimePeriod(years = 1, days = -40) as DatePeriod, years = 1, days = -40)
28+
assertPeriodComponents(DateTimePeriod(days = 5) as DatePeriod, days = 5)
29+
30+
assertPeriodComponents(DateTimePeriod(hours = 3), hours = 3)
31+
assertPeriodComponents(DateTimePeriod(hours = 1, minutes = 120), hours = 3)
32+
assertPeriodComponents(DateTimePeriod(hours = 1, minutes = 119, seconds = 60), hours = 3)
33+
assertPeriodComponents(DateTimePeriod(hours = 1, minutes = 119, seconds = 59, nanoseconds = 1_000_000_000), hours = 3)
34+
assertPeriodComponents(DateTimePeriod(hours = 1, minutes = 121, seconds = -59, nanoseconds = -1_000_000_000), hours = 3)
35+
assertPeriodComponents(DateTimePeriod())
36+
assertPeriodComponents(DatePeriod())
37+
38+
assertPeriodComponents(DateTimePeriod(days = 1, hours = -1), days = 1, hours = -1)
39+
assertPeriodComponents(DateTimePeriod(days = -1, hours = -1), days = -1, hours = -1)
40+
41+
assertPeriodComponents(DateTimePeriod(years = -1, months = -2, days = -3, hours = -4, minutes = -5, seconds = 0, nanoseconds = 500_000_000),
42+
years = -1, months = -2, days = -3, hours = -4, minutes = -4, seconds = -59, nanoseconds = -500_000_000)
43+
44+
assertPeriodComponents(DateTimePeriod(nanoseconds = 999_999_999_999_999L), hours = 277, minutes = 46, seconds = 39, nanoseconds = 999_999_999)
45+
assertPeriodComponents(DateTimePeriod(nanoseconds = -999_999_999_999_999L), hours = -277, minutes = -46, seconds = -39, nanoseconds = -999_999_999)
46+
}
47+
1548
@Test
1649
fun toStringConversion() {
1750
assertEquals("P1Y", DateTimePeriod(years = 1).toString())
@@ -104,4 +137,16 @@ class DateTimePeriodTest {
104137
assertEquals(period, duration.toDateTimePeriod())
105138
}
106139
}
140+
141+
private fun assertPeriodComponents(period: DateTimePeriod,
142+
years: Int = 0, months: Int = 0, days: Int = 0,
143+
hours: Int = 0, minutes: Int = 0, seconds: Int = 0, nanoseconds: Int = 0) {
144+
assertEquals(years, period.years)
145+
assertEquals(months, period.months)
146+
assertEquals(days, period.days)
147+
assertEquals(hours, period.hours)
148+
assertEquals(minutes, period.minutes)
149+
assertEquals(seconds, period.seconds)
150+
assertEquals(nanoseconds, period.nanoseconds)
151+
}
107152
}

0 commit comments

Comments
 (0)