Skip to content

Commit

Permalink
feat: add assertions for JDK YearMonth type (#3141)
Browse files Browse the repository at this point in the history
(cherry picked from commit 5f93909)
  • Loading branch information
tmvlpl authored and joel-costigliola committed Mar 10, 2024
1 parent c867f23 commit 1b6aa54
Show file tree
Hide file tree
Showing 37 changed files with 2,088 additions and 2 deletions.

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions assertj-core/src/main/java/org/assertj/core/api/Assertions.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.time.Period;
import java.time.YearMonth;
import java.time.ZonedDateTime;
import java.time.temporal.TemporalUnit;
import java.util.Collection;
Expand Down Expand Up @@ -981,6 +982,17 @@ public static AbstractLocalDateAssert<?> assertThat(LocalDate actual) {
return AssertionsForClassTypes.assertThat(actual);
}

/**
* Creates a new instance of <code>{@link YearMonthAssert}</code>.
*
* @param actual the actual value.
* @return the created assertion object.
* @since 3.26.0
*/
public static AbstractYearMonthAssert<?> assertThat(YearMonth actual) {
return AssertionsForClassTypes.assertThat(actual);
}

/**
* Creates a new instance of <code>{@link InstantAssert}</code>.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.time.Period;
import java.time.YearMonth;
import java.time.ZonedDateTime;
import java.util.Date;
import java.util.List;
Expand Down Expand Up @@ -669,6 +670,17 @@ public static AbstractLocalDateAssert<?> assertThat(LocalDate localDate) {
return new LocalDateAssert(localDate);
}

/**
* Creates a new instance of <code>{@link YearMonthAssert}</code>.
*
* @param yearMonth the actual value.
* @return the created assertion object.
* @since 3.26.0
*/
public static AbstractYearMonthAssert<?> assertThat(YearMonth yearMonth) {
return new YearMonthAssert(yearMonth);
}

/**
* Creates a new instance of <code>{@link InstantAssert}</code>.
*
Expand Down
12 changes: 12 additions & 0 deletions assertj-core/src/main/java/org/assertj/core/api/Assumptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.time.Period;
import java.time.YearMonth;
import java.time.ZonedDateTime;
import java.util.Collection;
import java.util.Date;
Expand Down Expand Up @@ -1396,6 +1397,17 @@ public static AbstractLocalDateAssert<?> assumeThat(LocalDate actual) {
return asAssumption(LocalDateAssert.class, LocalDate.class, actual);
}

/**
* Creates a new instance of {@link YearMonthAssert} assumption.
*
* @param actual the YearMonth to test
* @return the created assumption for assertion object.
* @since 3.26.0
*/
public static AbstractYearMonthAssert<?> assumeThat(YearMonth actual) {
return asAssumption(YearMonthAssert.class, YearMonth.class, actual);
}

/**
* Creates a new instance of {@link InstantAssert} assumption.
*
Expand Down
12 changes: 12 additions & 0 deletions assertj-core/src/main/java/org/assertj/core/api/BDDAssertions.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.time.Period;
import java.time.YearMonth;
import java.time.ZonedDateTime;
import java.time.temporal.TemporalUnit;
import java.util.Collection;
Expand Down Expand Up @@ -1470,6 +1471,17 @@ public static AbstractLocalDateAssert<?> then(LocalDate actual) {
return assertThat(actual);
}

/**
* Creates a new instance of <code>{@link org.assertj.core.api.YearMonthAssert}</code>.
*
* @param actual the actual value.
* @return the created assertion object.
* @since 3.26.0
*/
public static AbstractYearMonthAssert<?> then(YearMonth actual) {
return assertThat(actual);
}

/**
* Creates a new instance of <code>{@link org.assertj.core.api.LocalDateTimeAssert}</code>.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.time.Period;
import java.time.YearMonth;
import java.time.ZonedDateTime;
import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -138,6 +139,17 @@ default LocalDateAssert then(LocalDate actual) {
return proxy(LocalDateAssert.class, LocalDate.class, actual);
}

/**
* Creates a new instance of <code>{@link YearMonthAssert}</code>.
*
* @param actual the actual value.
* @return the created assertion object.
* @since 3.26.0
*/
default YearMonthAssert then(YearMonth actual) {
return proxy(YearMonthAssert.class, YearMonth.class, actual);
}

/**
* Creates a new instance of <code>{@link LocalDateTimeAssert}</code>.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.time.Period;
import java.time.YearMonth;
import java.time.ZonedDateTime;
import java.util.Collection;
import java.util.Date;
Expand Down Expand Up @@ -537,6 +538,14 @@ static <ELEMENT> InstanceOfAssertFactory<ELEMENT[][], Object2DArrayAssert<ELEMEN
InstanceOfAssertFactory<LocalDate, AbstractLocalDateAssert<?>> LOCAL_DATE = new InstanceOfAssertFactory<>(LocalDate.class,
Assertions::assertThat);

/**
* {@link InstanceOfAssertFactory} for a {@link LocalDate}.
*
* @since 3.26.0
*/
InstanceOfAssertFactory<YearMonth, AbstractYearMonthAssert<?>> YEAR_MONTH = new InstanceOfAssertFactory<>(YearMonth.class,
Assertions::assertThat);

/**
* {@link InstanceOfAssertFactory} for an {@link Instant}.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.time.Period;
import java.time.YearMonth;
import java.time.ZonedDateTime;
import java.util.List;
import java.util.Optional;
Expand All @@ -40,7 +41,6 @@
import java.util.stream.IntStream;
import java.util.stream.LongStream;
import java.util.stream.Stream;

import org.assertj.core.util.CheckReturnValue;

@CheckReturnValue
Expand Down Expand Up @@ -137,6 +137,17 @@ default LocalDateAssert assertThat(LocalDate actual) {
return proxy(LocalDateAssert.class, LocalDate.class, actual);
}

/**
* Creates a new instance of <code>{@link YearMonthAssert}</code>.
*
* @param actual the actual value.
* @return the created assertion object.
* @since 3.26.0
*/
default YearMonthAssert assertThat(YearMonth actual) {
return proxy(YearMonthAssert.class, YearMonth.class, actual);
}

/**
* Creates a new instance of <code>{@link LocalDateTimeAssert}</code>.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.time.Period;
import java.time.YearMonth;
import java.time.ZonedDateTime;
import java.time.temporal.TemporalUnit;
import java.util.Collection;
Expand Down Expand Up @@ -2599,6 +2600,17 @@ default AbstractLocalDateAssert<?> assertThat(final LocalDate localDate) {
return Assertions.assertThat(localDate);
}

/**
* Creates a new instance of <code>{@link YearMonthAssert}</code>.
*
* @param yearMonth the actual value.
* @return the created assertion object.
* @since 3.26.0
*/
default AbstractYearMonthAssert<?> assertThat(final YearMonth yearMonth) {
return Assertions.assertThat(yearMonth);
}

/**
* Creates a new instance of <code>{@link LocalTimeAssert}</code>.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.time.Period;
import java.time.YearMonth;
import java.time.ZonedDateTime;
import java.util.Date;
import java.util.Iterator;
Expand Down Expand Up @@ -986,6 +987,17 @@ default AbstractLocalDateAssert<?> assumeThat(final LocalDate localDate) {
return Assumptions.assumeThat(localDate);
}

/**
* Creates a new instance of <code>{@link YearMonthAssert}</code> assumption.
*
* @param yearMonth the actual value.
* @return the created assumption for assertion object.
* @since 3.26.0
*/
default AbstractYearMonthAssert<?> assumeThat(final YearMonth yearMonth) {
return Assumptions.assumeThat(yearMonth);
}

/**
* Creates a new instance of <code>{@link LocalTimeAssert}</code> assumption.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* 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. See the License for the
* specific language governing permissions and limitations under the License.
*
* Copyright 2012-2024 the original author or authors.
*/
package org.assertj.core.api;

import java.time.YearMonth;

/**
* Assertions for {@link YearMonth} type from new Date &amp; Time API introduced in Java 8.
*
* @since 3.26.0
*/
public class YearMonthAssert extends AbstractYearMonthAssert<YearMonthAssert> {

/**
* Creates a new <code>{@link YearMonthAssert}</code>.
*
* @param actual the actual value to verify
*/
protected YearMonthAssert(YearMonth actual) {
super(actual, YearMonthAssert.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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. See the License for the
* specific language governing permissions and limitations under the License.
*
* Copyright 2012-2024 the original author or authors.
*/
package org.assertj.core.error;

import java.time.YearMonth;

/**
* Creates an error message indicating that an assertion that verifies that a {@link java.time.YearMonth}
* is the current one (matching year and month).
*
* @since 3.26.0
*/
public class ShouldBeCurrentYearMonth extends BasicErrorMessageFactory {

/**
* Creates a new <code>{@link ShouldBeCurrentYearMonth}</code>.
* @param actual the actual value in the failed assertion.
* @return the created {@code ErrorMessageFactory}.
*/
public static ErrorMessageFactory shouldBeCurrentYearMonth(YearMonth actual) {
return new ShouldBeCurrentYearMonth(actual);
}

private ShouldBeCurrentYearMonth(YearMonth actual) {
super("%nExpecting actual:%n %s%nto be the current YearMonth but was not.", actual);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.YearMonth;
import java.time.ZonedDateTime;
import java.util.Calendar;
import java.util.Collection;
Expand Down Expand Up @@ -216,6 +217,7 @@ public String toStringOf(Object object) {
if (object instanceof Date) return toStringOf((Date) object);
if (object instanceof Duration) return toStringOf((Duration) object);
if (object instanceof LocalDate) return toStringOf((LocalDate) object);
if (object instanceof YearMonth) return toStringOf((YearMonth) object);
if (object instanceof LocalDateTime) return toStringOf((LocalDateTime) object);
if (object instanceof OffsetDateTime) return toStringOf((OffsetDateTime) object);
if (object instanceof ZonedDateTime) return toStringOf((ZonedDateTime) object);
Expand Down Expand Up @@ -431,6 +433,10 @@ protected String toStringOf(LocalDate localDate) {
return defaultToStringWithClassNameDisambiguation(localDate);
}

protected String toStringOf(YearMonth yearMonth) {
return defaultToStringWithClassNameDisambiguation(yearMonth);
}

protected String classNameDisambiguation(Object o) {
return String.format(" (%s)", o.getClass().getName());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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. See the License for the
* specific language governing permissions and limitations under the License.
*
* Copyright 2012-2024 the original author or authors.
*/
package org.assertj.core.api;

import java.time.YearMonth;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

/**
* Tests for <code>{@link Assertions#assertThat(YearMonth)}</code>.
*/
class Assertions_assertThat_with_YearMonth_Test {

@Test
void should_create_Assert() {
AbstractYearMonthAssert<?> assertions = Assertions.assertThat(YearMonth.now());
assertThat(assertions).isNotNull();
}

@Test
void should_pass_actual() {
YearMonth yearMonth = YearMonth.now();
AbstractYearMonthAssert<?> assertions = Assertions.assertThat(yearMonth);
assertThat(assertions.getActual()).isSameAs(yearMonth);
}
}
Loading

0 comments on commit 1b6aa54

Please sign in to comment.