Skip to content

Commit

Permalink
Add and improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Morten Haraldsen committed Jan 26, 2024
1 parent 89a7b8d commit 81c2aa5
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 17 deletions.
14 changes: 13 additions & 1 deletion src/test/java/com/ethlo/time/ExternalParameterizedTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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<TestParam> fromFile() throws IOException
{
final List<TestParam> result = new ObjectMapper()
Expand Down
12 changes: 10 additions & 2 deletions src/test/java/com/ethlo/time/FieldTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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));
}
}
34 changes: 30 additions & 4 deletions src/test/java/com/ethlo/time/FormatterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down
12 changes: 10 additions & 2 deletions src/test/java/com/ethlo/time/TestParam.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -66,6 +68,7 @@ public String toString()
return "TestParam{" +
"input='" + input + '\'' +
", lenient=" + lenient +
", expected=" + expected + '\'' +
", error='" + error + '\'' +
", errorOffset=" + errorIndex + '\'' +
", note=" + note +
Expand All @@ -76,4 +79,9 @@ public String getNote()
{
return note;
}

public String getExpected()
{
return expected;
}
}
20 changes: 12 additions & 8 deletions src/test/resources/test-data.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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
}
]

0 comments on commit 81c2aa5

Please sign in to comment.