Skip to content

Commit 3214ee3

Browse files
committed
Add tests for non-parseable strings for DateTimePeriod
1 parent 70d4db8 commit 3214ee3

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

core/common/test/DateTimePeriodTest.kt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,29 @@ class DateTimePeriodTest {
9999
assertEquals(DateTimePeriod(days = 1, nanoseconds = -1L), DateTimePeriod.parse("P1DT-0.000000001S"))
100100
assertEquals(DateTimePeriod(seconds = -1, nanoseconds = 1L), DateTimePeriod.parse("-PT0.999999999S"))
101101
assertEquals(DateTimePeriod(days = 1, seconds = -1, nanoseconds = 1L), DateTimePeriod.parse("P1DT-0.999999999S"))
102+
103+
// overflow of `Int.MAX_VALUE` months
104+
assertFailsWith<IllegalArgumentException> { DateTimePeriod.parse("P2000000000Y") }
105+
assertFailsWith<IllegalArgumentException> { DateTimePeriod.parse("P1Y2147483640M") }
106+
107+
// too large a number in a field
108+
assertFailsWith<DateTimeFormatException> { DateTimePeriod.parse("P3000000000Y") }
109+
assertFailsWith<DateTimeFormatException> { DateTimePeriod.parse("P3000000000M") }
110+
assertFailsWith<DateTimeFormatException> { DateTimePeriod.parse("P3000000000D") }
111+
assertFailsWith<DateTimeFormatException> { DateTimePeriod.parse("P3000000000H") }
112+
assertFailsWith<DateTimeFormatException> { DateTimePeriod.parse("P3000000000M") }
113+
assertFailsWith<DateTimeFormatException> { DateTimePeriod.parse("P3000000000S") }
114+
115+
// wrong order of signifiers
116+
assertFailsWith<DateTimeFormatException> { DateTimePeriod.parse("P1Y2D3M") }
117+
assertFailsWith<DateTimeFormatException> { DateTimePeriod.parse("P0DT1M2H") }
118+
119+
// loss of precision in fractional seconds
120+
assertFailsWith<DateTimeFormatException> { DateTimePeriod.parse("P0.000000000001S") }
121+
122+
// non-zero time components when parsing DatePeriod
123+
assertFailsWith<IllegalArgumentException> { DatePeriod.parse("P1DT1H") }
124+
DatePeriod.parse("P1DT0H")
102125
}
103126

104127
@Test

0 commit comments

Comments
 (0)