diff --git a/src/test/java/com/ethlo/time/ExternalParameterizedTest.java b/src/test/java/com/ethlo/time/ExternalParameterizedTest.java index 87077be..c6a11d2 100644 --- a/src/test/java/com/ethlo/time/ExternalParameterizedTest.java +++ b/src/test/java/com/ethlo/time/ExternalParameterizedTest.java @@ -59,7 +59,7 @@ void testAll(TestParam param) } // Compare to Java's parser result - final Instant expected = Instant.parse(param.getInput()); + final Instant expected = getExpected(param); if (result instanceof DateTime) { assertThat(((DateTime) result).toInstant()).isEqualTo(expected); @@ -91,6 +91,18 @@ void testAll(TestParam param) } + private Instant getExpected(TestParam input) + { + try + { + return Instant.parse(input.getExpected() != null ? input.getExpected() : input.getInput()); + } + catch (DateTimeException exc) + { + throw new IllegalArgumentException("Cannot parse using Instant: " + input); + } + } + public static List fromFile() throws IOException { final List result = new ObjectMapper() diff --git a/src/test/java/com/ethlo/time/FieldTest.java b/src/test/java/com/ethlo/time/FieldTest.java index 9e98464..5ff8cf6 100644 --- a/src/test/java/com/ethlo/time/FieldTest.java +++ b/src/test/java/com/ethlo/time/FieldTest.java @@ -26,7 +26,9 @@ import java.time.OffsetDateTime; import java.time.Year; import java.time.YearMonth; +import java.time.temporal.ChronoField; import java.time.temporal.Temporal; +import java.time.temporal.UnsupportedTemporalTypeException; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Tag; @@ -36,7 +38,7 @@ public class FieldTest { @Test - public void testGetKnownFields() + void testGetKnownFields() { assertThat(Field.valueOf(Year.class)).isEqualTo(Field.YEAR); assertThat(Field.valueOf(YearMonth.class)).isEqualTo(Field.MONTH); @@ -45,8 +47,14 @@ public void testGetKnownFields() } @Test - public void testGetUnknown() + void testGetUnknown() { Assertions.assertThrows(IllegalArgumentException.class, () -> Field.valueOf(Temporal.class)); } + + @Test + void testUnknownField() + { + Assertions.assertThrows(UnsupportedTemporalTypeException.class, () -> Field.of(ChronoField.NANO_OF_DAY)); + } } diff --git a/src/test/java/com/ethlo/time/FormatterTest.java b/src/test/java/com/ethlo/time/FormatterTest.java index 947f6e7..2ca4045 100644 --- a/src/test/java/com/ethlo/time/FormatterTest.java +++ b/src/test/java/com/ethlo/time/FormatterTest.java @@ -9,9 +9,9 @@ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -31,11 +31,37 @@ public class FormatterTest { @Test - void testFormat3() + void testFormatSpecifiedResolution() { final String s = "2017-02-21T10:00:00.000+12:00"; final OffsetDateTime date = ITU.parseDateTime(s); - assertThat(ITU.formatUtcMilli(date)).isEqualTo("2017-02-20T22:00:00.000Z"); + assertThat(ITU.format(date, 9)).isEqualTo("2017-02-21T10:00:00.000000000+12:00"); + } + + @Test + void testFormatYear() + { + assertThat(DateTime.ofYear(1234).toString()).isEqualTo("1234"); + } + + @Test + void testFormatYearMonth() + { + assertThat(DateTime.ofYearMonth(1234, 12).toString()).isEqualTo("1234-12"); + } + + @Test + void testFormatHigherGranularityThanAvailable() + { + final DateTimeException exc = assertThrows(DateTimeException.class, () -> DateTime.ofYear(1234).toString(Field.DAY)); + assertThat(exc).hasMessage("Requested granularity was DAY, but contains only granularity YEAR"); + } + + @Test + void testFormatWithFractionDigits() + { + final DateTimeException exc = assertThrows(DateTimeException.class, () -> DateTime.ofYear(1234).toString(Field.DAY)); + assertThat(exc).hasMessage("Requested granularity was DAY, but contains only granularity YEAR"); } @Test diff --git a/src/test/java/com/ethlo/time/TestParam.java b/src/test/java/com/ethlo/time/TestParam.java index f73e928..fa1c316 100644 --- a/src/test/java/com/ethlo/time/TestParam.java +++ b/src/test/java/com/ethlo/time/TestParam.java @@ -28,15 +28,17 @@ public class TestParam private final boolean lenient; private final String error; private final int errorIndex; + private final String expected; private final String note; - @ConstructorProperties(value = {"input", "lenient", "error", "error_index", "note"}) - public TestParam(String input, boolean lenient, String error, Integer errorIndex, String note) + @ConstructorProperties(value = {"input", "lenient", "error", "error_index", "expected", "note"}) + public TestParam(String input, boolean lenient, String error, Integer errorIndex, String expected, String note) { this.input = input; this.lenient = lenient; this.error = error; this.errorIndex = errorIndex != null ? errorIndex : -1; + this.expected = expected; this.note = note; } @@ -66,6 +68,7 @@ public String toString() return "TestParam{" + "input='" + input + '\'' + ", lenient=" + lenient + + ", expected=" + expected + '\'' + ", error='" + error + '\'' + ", errorOffset=" + errorIndex + '\'' + ", note=" + note + @@ -76,4 +79,9 @@ public String getNote() { return note; } + + public String getExpected() + { + return expected; + } } diff --git a/src/test/resources/test-data.json b/src/test/resources/test-data.json index 715aa08..64eac51 100644 --- a/src/test/resources/test-data.json +++ b/src/test/resources/test-data.json @@ -213,12 +213,6 @@ "input": "1994-11-05t08:15:30z", "note": "Lowercase 'z' as UTC timezone" }, - { - "input": "1994-11-05 08:15:30Z", - "error": "Text '1994-11-05 08:15:30Z' could not be parsed at index 10", - "error_index": 10, - "note": "Space as date/time separator" - }, { "input": "2017-02-21T15:27:39+0000", "error": "Invalid timezone offset: 2017-02-21T15:27:39+0000", @@ -247,8 +241,18 @@ }, { "input": "2020-12-31T22:22:", - "error": "Text '2020-12-31T22:22:' could not be parsed at index 17", - "error_index": 17, + "error": "Unexpected end of input: 2020-12-31T22:22:", + "error_index": 16, + "lenient": true + }, + { + "input": "2020-12-31T22:56", + "expected": "2020-12-31T22:56:00Z", + "lenient": true + }, + { + "input": "2020-12-31", + "expected": "2020-12-31T00:00:00Z", "lenient": true } ]