truth
truth copied to clipboard
Subjects for java.time
I see that you added subjects from JDK 8, but I wonder if you planned on adding java.time classes such as LocalDate, LocalTime, LocalDateTime, ZonedDateTime and Instant.
I am willing to help on this. I'm not a pro with dates and such, so I might need some help with the API of these subjects.
It would be useful to compare dates, to know if it's the same date given same/different timezones, etc.
We have an internal bug about this, but we've yet to write up an API proposal. Here's some snippets from the internal bug:
We briefly discussed subjects for JodaTime, but maybe we want to re-evaluate for java.time types? I'm imaging something like:
assertThat(duration).isEqualToMinutes(1);
assertThat(duration).isEqualToSeconds(60);
assertThat(duration).isEqualToMillis(60000);
Those otherwise would have to be:
assertThat(duration).isEqualTo(Duration.ofMinutes(1));
assertThat(duration).isEqualTo(Duration.ofSeconds(60));
assertThat(duration).isEqualTo(Duration.ofMillis(60000));
Someone else also asked for something like:
assertThat(actualInstant).isWithin(Duration.ofSeconds(5)).of(expectedInstant);
@ALWAL12 Did you have a specific use or API in mind? Since most of the java.time types implement Comparable, you will automatically get things like isGreaterThan(), isLessThan(), etc.
I believe special cases happen when you work with timezones. I understand most date objects are Comparable and unaware of timezones, but ZonedDateTime is apart. Assume this test:
ZonedDateTime montreal = ZonedDateTime.of(LocalDate.of(2012, Month.JANUARY, 28), LocalTime.of(16, 0), ZoneId.of("America/Montreal"));
ZonedDateTime vancouver = ZonedDateTime.of(LocalDate.of(2012, Month.JANUARY, 28), LocalTime.of(13, 0), ZoneId.of("America/Vancouver")); // 3 hours apart
assertThat(montreal).isEqualTo(vancouver);
They aren't technically equals due to different timezones, but they represent the same point in time. We have many cases that we receive a zoned date time but we returns back UTC. We always need to convert our ZonedDateTime to Instant to make those tests pass.
They are many different ways to represent an API, such as
assertThat(montreal).hasSameInstantOf(vancouver);
or
assertThat(montreal).isEqualTo(vancouver).inSameTimezone();
or
assertThat(montreal).inTimezone(ZoneOffset.UTC).isEqualTo(vancouver);
So without the subject, you'd have to write:
assertThat(montreal.toInstant().isEqualTo(vancouver.toInstant());
right?
Exactly. That would be kind of a shortcut. In our case, we also mix these dates with Optional so the line is a bit messier than this. 😅
I can't think of other cases at the moment, but there is room for improvements or more-natural assertions about dates.