Skip to content

Commit

Permalink
Added Year-Month tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
jdereg committed Feb 4, 2024
1 parent 5610555 commit 8e92f37
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ static YearMonth toYearMonth(Object from, Converter converter, ConverterOptions
catch (DateTimeParseException e) {
try {
ZonedDateTime zdt = DateUtilities.parseDate(yearMonth, options.getZoneId(), true);
return YearMonth.of(zdt.getYear(), zdt.getDayOfMonth());
return YearMonth.of(zdt.getYear(), zdt.getMonthValue());
}
catch (Exception ex) {
throw new IllegalArgumentException("Unable to extract Year-Month from string: " + yearMonth);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.math.BigDecimal;
import java.math.BigInteger;
import java.time.MonthDay;
import java.time.YearMonth;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
Expand Down Expand Up @@ -251,6 +252,29 @@ class ConverterEverythingTest
{ mapOf("month","6", "day", 30), MonthDay.of(6, 30) },
{ mapOf("month",6L, "day", "30"), MonthDay.of(6, 30)},
});

TEST_FACTORY.put(pair(Void.class, YearMonth.class), new Object[][] {
{ null, null },
});
TEST_FACTORY.put(pair(YearMonth.class, YearMonth.class), new Object[][] {
{ YearMonth.of(2023, 12), YearMonth.of(2023, 12) },
{ YearMonth.of(1970, 1), YearMonth.of(1970, 1) },
{ YearMonth.of(1999, 6), YearMonth.of(1999, 6) },
});
TEST_FACTORY.put(pair(String.class, YearMonth.class), new Object[][] {
{ "2024-01", YearMonth.of(2024, 1) },
{ "2024-1", new IllegalArgumentException("Unable to extract Year-Month from string: 2024-1") },
{ "2024-1-1", YearMonth.of(2024, 1) },
{ "2024-06-01", YearMonth.of(2024, 6) },
{ "2024-12-31", YearMonth.of(2024, 12) },
{ "05:45 2024-12-31", YearMonth.of(2024, 12) },
});
TEST_FACTORY.put(pair(Map.class, YearMonth.class), new Object[][] {
{ mapOf("_v", "2024-01"), YearMonth.of(2024, 1) },
{ mapOf("year", "2024", "month", 12), YearMonth.of(2024, 12) },
{ mapOf("year", new BigInteger("2024"), "month", "12"), YearMonth.of(2024, 12) },
{ mapOf("year", mapOf("_v", 2024), "month", "12"), YearMonth.of(2024, 12) }, // Proving we recursively call .convert()
});
}

@BeforeEach
Expand Down Expand Up @@ -296,6 +320,7 @@ void testEverything() {
System.err.println();
e.printStackTrace();
System.err.println();
System.err.flush();
failed = true;
}
}
Expand All @@ -304,6 +329,7 @@ void testEverything() {

if (neededTests > 0) {
System.err.println("Conversions needing tests: " + neededTests);
System.err.flush();
}
if (failed) {
throw new RuntimeException("One or more tests failed.");
Expand Down

0 comments on commit 8e92f37

Please sign in to comment.