Skip to content

Commit c69b495

Browse files
matchers: handle java.instant on both sides (#1605)
1 parent e8b2c74 commit c69b495

File tree

4 files changed

+40
-10
lines changed

4 files changed

+40
-10
lines changed

webtau-core/src/main/java/org/testingisdocumenting/webtau/expectation/equality/handlers/DatesCompareToHandler.java

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -94,16 +94,18 @@ private class Comparator {
9494
}
9595

9696
void compare() {
97-
if (actual instanceof LocalDateTime && expected instanceof LocalDate) {
98-
compareLocalDateTimeAndLocalDate((LocalDateTime) actual, (LocalDate) expected);
99-
} else if (actual instanceof LocalDate && expected instanceof LocalDate) {
100-
compareLocalDates((LocalDate) actual, (LocalDate) expected);
101-
} else if (actual instanceof ZonedDateTime && expected instanceof Instant) {
102-
compareZonedDateTimeAndInstant((ZonedDateTime) actual, (Instant) expected);
103-
} else if (actual instanceof ZonedDateTime && expected instanceof LocalDate) {
104-
compareZonedDateTimeAndLocalDate((ZonedDateTime) actual, (LocalDate) expected);
105-
} else if (actual instanceof ZonedDateTime && expected instanceof ZonedDateTime) {
106-
compareZonedDateTimes((ZonedDateTime) actual, (ZonedDateTime) expected);
97+
if (actual instanceof LocalDateTime a && expected instanceof LocalDate b) {
98+
compareLocalDateTimeAndLocalDate(a, b);
99+
} else if (actual instanceof LocalDate a && expected instanceof LocalDate b) {
100+
compareLocalDates(a, b);
101+
} else if (actual instanceof ZonedDateTime a && expected instanceof Instant b) {
102+
compareZonedDateTimeAndInstant(a, b);
103+
} else if (actual instanceof ZonedDateTime a && expected instanceof LocalDate b) {
104+
compareZonedDateTimeAndLocalDate(a, b);
105+
} else if (actual instanceof ZonedDateTime a && expected instanceof ZonedDateTime b) {
106+
compareZonedDateTimes(a, b);
107+
} else if (actual instanceof Instant a && expected instanceof Instant b) {
108+
compareInstants(a, b);
107109
} else {
108110
throw new UnsupportedOperationException("combination is not supported:\n" +
109111
renderActualExpected(actual, expected));
@@ -136,6 +138,10 @@ private void compareZonedDateTimeAndInstant(ZonedDateTime actual, Instant expect
136138
actualInstant, expected));
137139
}
138140

141+
private void compareInstants(Instant actual, Instant expected) {
142+
report(actual.compareTo(expected), () -> renderActualExpected(actual, expected));
143+
}
144+
139145
private void report(int compareTo, Supplier<TokenizedMessage> message) {
140146
if (isEqualOnly) {
141147
compareToComparator.reportEqualOrNotEqual(DatesCompareToHandler.this,

webtau-core/src/test/groovy/org/testingisdocumenting/webtau/expectation/equality/handlers/DatesCompareToHandlerTest.groovy

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package org.testingisdocumenting.webtau.expectation.equality.handlers
2020
import org.junit.Test
2121
import org.testingisdocumenting.webtau.testutils.TestConsoleOutput
2222

23+
import java.time.Instant
2324
import java.time.LocalDate
2425
import java.time.LocalDateTime
2526
import java.time.ZoneId
@@ -37,6 +38,12 @@ class DatesCompareToHandlerTest {
3738
1, 1, 1, 1, ZoneId.of("UTC")))
3839
}
3940

41+
@Test
42+
void "handles two java time instants"() {
43+
def handler = new DatesCompareToHandler()
44+
assert handler.handleEquality(Instant.now(), Instant.now())
45+
}
46+
4047
@Test
4148
void "actual local date gstring greater than expected local date instance"() {
4249
def month = '06'
@@ -124,6 +131,18 @@ class DatesCompareToHandlerTest {
124131
}
125132
}
126133

134+
@Test
135+
void "actual instant and expected instant"() {
136+
def a = Instant.ofEpochMilli(100000)
137+
def b = Instant.ofEpochMilli(101000)
138+
139+
runExpectExceptionAndValidateOutput(AssertionError, "X failed expecting [value] to equal 1970-01-01T00:01:41Z:\n" +
140+
" actual: 1970-01-01T00:01:40Z <java.time.Instant>\n" +
141+
" expected: 1970-01-01T00:01:41Z <java.time.Instant> (Xms)") {
142+
actual(a).should(equal(b))
143+
}
144+
}
145+
127146
@Test
128147
void "reports that given text cannot be parsed and lists currently supported formats"() {
129148
code {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* Add: Handle `time.Instant` on both sides when using [Universal Compare](matchers/universal-compare#dates)

webtau-docs/znai/release-notes/2024.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
title: 2024 Releases
33
---
44

5+
# 2.5
6+
7+
:include-markdowns: 2.5
8+
59
# 2.4
610

711
:include-markdowns: 2.4

0 commit comments

Comments
 (0)