Skip to content

Commit

Permalink
matchers: handle java.instant on both sides (#1605)
Browse files Browse the repository at this point in the history
  • Loading branch information
MykolaGolubyev committed Jul 25, 2024
1 parent e8b2c74 commit c69b495
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,16 +94,18 @@ private class Comparator {
}

void compare() {
if (actual instanceof LocalDateTime && expected instanceof LocalDate) {
compareLocalDateTimeAndLocalDate((LocalDateTime) actual, (LocalDate) expected);
} else if (actual instanceof LocalDate && expected instanceof LocalDate) {
compareLocalDates((LocalDate) actual, (LocalDate) expected);
} else if (actual instanceof ZonedDateTime && expected instanceof Instant) {
compareZonedDateTimeAndInstant((ZonedDateTime) actual, (Instant) expected);
} else if (actual instanceof ZonedDateTime && expected instanceof LocalDate) {
compareZonedDateTimeAndLocalDate((ZonedDateTime) actual, (LocalDate) expected);
} else if (actual instanceof ZonedDateTime && expected instanceof ZonedDateTime) {
compareZonedDateTimes((ZonedDateTime) actual, (ZonedDateTime) expected);
if (actual instanceof LocalDateTime a && expected instanceof LocalDate b) {
compareLocalDateTimeAndLocalDate(a, b);
} else if (actual instanceof LocalDate a && expected instanceof LocalDate b) {
compareLocalDates(a, b);
} else if (actual instanceof ZonedDateTime a && expected instanceof Instant b) {
compareZonedDateTimeAndInstant(a, b);
} else if (actual instanceof ZonedDateTime a && expected instanceof LocalDate b) {
compareZonedDateTimeAndLocalDate(a, b);
} else if (actual instanceof ZonedDateTime a && expected instanceof ZonedDateTime b) {
compareZonedDateTimes(a, b);
} else if (actual instanceof Instant a && expected instanceof Instant b) {
compareInstants(a, b);
} else {
throw new UnsupportedOperationException("combination is not supported:\n" +
renderActualExpected(actual, expected));
Expand Down Expand Up @@ -136,6 +138,10 @@ private void compareZonedDateTimeAndInstant(ZonedDateTime actual, Instant expect
actualInstant, expected));
}

private void compareInstants(Instant actual, Instant expected) {
report(actual.compareTo(expected), () -> renderActualExpected(actual, expected));
}

private void report(int compareTo, Supplier<TokenizedMessage> message) {
if (isEqualOnly) {
compareToComparator.reportEqualOrNotEqual(DatesCompareToHandler.this,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package org.testingisdocumenting.webtau.expectation.equality.handlers
import org.junit.Test
import org.testingisdocumenting.webtau.testutils.TestConsoleOutput

import java.time.Instant
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.ZoneId
Expand All @@ -37,6 +38,12 @@ class DatesCompareToHandlerTest {
1, 1, 1, 1, ZoneId.of("UTC")))
}

@Test
void "handles two java time instants"() {
def handler = new DatesCompareToHandler()
assert handler.handleEquality(Instant.now(), Instant.now())
}

@Test
void "actual local date gstring greater than expected local date instance"() {
def month = '06'
Expand Down Expand Up @@ -124,6 +131,18 @@ class DatesCompareToHandlerTest {
}
}

@Test
void "actual instant and expected instant"() {
def a = Instant.ofEpochMilli(100000)
def b = Instant.ofEpochMilli(101000)

runExpectExceptionAndValidateOutput(AssertionError, "X failed expecting [value] to equal 1970-01-01T00:01:41Z:\n" +
" actual: 1970-01-01T00:01:40Z <java.time.Instant>\n" +
" expected: 1970-01-01T00:01:41Z <java.time.Instant> (Xms)") {
actual(a).should(equal(b))
}
}

@Test
void "reports that given text cannot be parsed and lists currently supported formats"() {
code {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* Add: Handle `time.Instant` on both sides when using [Universal Compare](matchers/universal-compare#dates)
4 changes: 4 additions & 0 deletions webtau-docs/znai/release-notes/2024.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
title: 2024 Releases
---

# 2.5

:include-markdowns: 2.5

# 2.4

:include-markdowns: 2.4

0 comments on commit c69b495

Please sign in to comment.