Skip to content

Commit

Permalink
Merge 60bf5db into 703234d
Browse files Browse the repository at this point in the history
  • Loading branch information
stewbis committed Sep 20, 2019
2 parents 703234d + 60bf5db commit d204e86
Show file tree
Hide file tree
Showing 103 changed files with 3,535 additions and 3,768 deletions.
482 changes: 270 additions & 212 deletions src/main/java/org/exparity/hamcrest/date/DateMatchers.java

Large diffs are not rendered by default.

125 changes: 56 additions & 69 deletions src/main/java/org/exparity/hamcrest/date/LocalDateMatchers.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,36 @@

import static java.time.DayOfWeek.*;
import static java.time.Month.*;
import static org.exparity.hamcrest.date.core.TemporalConverters.*;
import static org.exparity.hamcrest.date.core.TemporalFunctions.LOCALDATE;
import static org.exparity.hamcrest.date.core.TemporalProviders.*;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.Month;
import java.time.Period;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
import java.util.stream.Stream;

import org.exparity.hamcrest.date.core.*;
import org.exparity.hamcrest.date.core.format.DatePartFormatter;
import org.exparity.hamcrest.date.core.format.LocalDateFormatter;
import org.exparity.hamcrest.date.core.wrapper.FieldLocalDateWrapper;
import org.exparity.hamcrest.date.core.wrapper.LocalDateWrapper;
import org.hamcrest.Factory;
import org.exparity.hamcrest.date.core.DateMatcher;
import org.exparity.hamcrest.date.core.IsAfter;
import org.exparity.hamcrest.date.core.IsBefore;
import org.exparity.hamcrest.date.core.IsDayOfMonth;
import org.exparity.hamcrest.date.core.IsDayOfWeek;
import org.exparity.hamcrest.date.core.IsFirstDayOfMonth;
import org.exparity.hamcrest.date.core.IsLastDayOfMonth;
import org.exparity.hamcrest.date.core.IsLeapYear;
import org.exparity.hamcrest.date.core.IsMaximum;
import org.exparity.hamcrest.date.core.IsMinimum;
import org.exparity.hamcrest.date.core.IsMonth;
import org.exparity.hamcrest.date.core.IsSameDay;
import org.exparity.hamcrest.date.core.IsSameOrAfter;
import org.exparity.hamcrest.date.core.IsSameOrBefore;
import org.exparity.hamcrest.date.core.IsWithin;
import org.exparity.hamcrest.date.core.IsYear;
import org.exparity.hamcrest.date.core.TemporalConverters;
import org.exparity.hamcrest.date.core.TemporalProviders;
import org.exparity.hamcrest.date.core.types.Interval;

/**
* Static factory for creating {@link org.hamcrest.Matcher} instances for comparing {@link LocalDate} instances
Expand All @@ -36,7 +52,7 @@ public abstract class LocalDateMatchers {
* @param date the reference date against which the examined date is checked
*/
public static DateMatcher<LocalDate> after(final LocalDate date) {
return new IsAfter<>(new LocalDateWrapper(date), new LocalDateFormatter());
return new IsAfter<>(LOCALDATE_AS_LOCALDATE, localDate(date), LOCALDATE);
}

/**
Expand Down Expand Up @@ -68,7 +84,7 @@ public static DateMatcher<LocalDate> after(final int year, final Month month, fi
* @param date the reference date against which the examined date is checked
*/
public static DateMatcher<LocalDate> before(final LocalDate date) {
return new IsBefore<>(new LocalDateWrapper(date), new LocalDateFormatter());
return new IsBefore<>(LOCALDATE_AS_LOCALDATE, localDate(date), LOCALDATE);
}

/**
Expand Down Expand Up @@ -100,7 +116,7 @@ public static DateMatcher<LocalDate> before(final int year, final Month month, f
* @param date the reference date against which the examined date is checked
*/
public static DateMatcher<LocalDate> sameDay(final LocalDate date) {
return new IsSameDay<>(new LocalDateWrapper(date), new LocalDateFormatter());
return new IsSameDay<>(LOCALDATE_AS_LOCALDATE, localDate(date));
}

/**
Expand Down Expand Up @@ -132,7 +148,7 @@ public static DateMatcher<LocalDate> isDay(final int year, final Month month, fi
* @param date the reference date against which the examined date is checked
*/
public static DateMatcher<LocalDate> sameOrBefore(final LocalDate date) {
return new IsSameOrBefore<>(new LocalDateWrapper(date), new LocalDateFormatter());
return new IsSameOrBefore<>(LOCALDATE_AS_LOCALDATE, localDate(date), LOCALDATE);
}

/**
Expand All @@ -149,7 +165,6 @@ public static DateMatcher<LocalDate> sameOrBefore(final LocalDate date) {
* @param month the month against which the examined date is checked
* @param day the day of the month against which the examined date is checked
*/
@Factory
public static DateMatcher<LocalDate> sameOrBefore(final int year, final Month month, final int day) {
return sameOrBefore(LocalDate.of(year, month, day));
}
Expand All @@ -166,7 +181,7 @@ public static DateMatcher<LocalDate> sameOrBefore(final int year, final Month mo
* @param date the reference date against which the examined date is checked
*/
public static DateMatcher<LocalDate> sameOrAfter(final LocalDate date) {
return new IsSameOrAfter<>(new LocalDateWrapper(date), new LocalDateFormatter());
return new IsSameOrAfter<>(LOCALDATE_AS_LOCALDATE, localDate(date), LOCALDATE);
}

/**
Expand Down Expand Up @@ -213,10 +228,7 @@ public static DateMatcher<LocalDate> sameMonthOfYear(final LocalDate date) {
* @param date the reference date against which the examined date is checked
*/
public static DateMatcher<LocalDate> sameDayOfMonth(final LocalDate date) {
return new IsDayOfMonth<>(
new FieldLocalDateWrapper(date, ChronoField.DAY_OF_MONTH),
(d, z) -> d.atStartOfDay(z).getDayOfMonth()
);
return new IsDayOfMonth<>(LOCALDATE_AS_DAYOFMONTH, dayOfMonth(date));
}

/**
Expand All @@ -231,10 +243,7 @@ public static DateMatcher<LocalDate> sameDayOfMonth(final LocalDate date) {
* @param dayOfMonth the expected day of the month
*/
public static DateMatcher<LocalDate> isDayOfMonth(final int dayOfMonth) {
return new IsDayOfMonth<>(
new FieldLocalDateWrapper(dayOfMonth, ChronoField.DAY_OF_MONTH),
(d, z) -> d.atStartOfDay(z).getDayOfMonth()
);
return new IsDayOfMonth<>(LOCALDATE_AS_DAYOFMONTH, dayOfMonth(dayOfMonth));
}

/**
Expand All @@ -249,10 +258,7 @@ public static DateMatcher<LocalDate> isDayOfMonth(final int dayOfMonth) {
* @param date the reference date against which the examined date is checked
*/
public static DateMatcher<LocalDate> sameYear(final LocalDate date) {
return new IsYear<>(
new FieldLocalDateWrapper(date, ChronoField.YEAR),
(d, z) -> d.atStartOfDay(z).getYear()
);
return new IsYear<>(LOCALDATE_AS_YEAR, year(date));
}

/**
Expand All @@ -267,12 +273,24 @@ public static DateMatcher<LocalDate> sameYear(final LocalDate date) {
* @param year the reference year against which the examined date is checked
*/
public static DateMatcher<LocalDate> isYear(final int year) {
return new IsYear<>(
new FieldLocalDateWrapper(year, ChronoField.YEAR),
(d, z) -> d.atStartOfDay(z).getYear()
);
return new IsYear<>(TemporalConverters.LOCALDATE_AS_YEAR, TemporalProviders.year(year));
}

/**
* Creates a matcher that matches when the examined date is within a defined period the reference date
* <p/>
* For example:
*
* <pre>
* assertThat(myDate, within(10, TimeUnit.DAYS, Moments.today()))
* </pre>
*
* @param date the reference date against which the examined date is checked
*/
public static DateMatcher<LocalDate> within(final Period period , final LocalDate date) {
return new IsWithin<>(Interval.of(period), LOCALDATE_AS_LOCALDATE, localDate(date), LOCALDATE);
}

/**
* Creates a matcher that matches when the examined date is within a defined period the reference date
* <p/>
Expand All @@ -285,7 +303,7 @@ public static DateMatcher<LocalDate> isYear(final int year) {
* @param date the reference date against which the examined date is checked
*/
public static DateMatcher<LocalDate> within(final long period, final ChronoUnit unit, final LocalDate date) {
return new IsWithin<>(period, unit, new LocalDateWrapper(date), new LocalDateFormatter());
return new IsWithin<>(Interval.of(period, unit), LOCALDATE_AS_LOCALDATE, localDate(date), LOCALDATE);
}

/**
Expand Down Expand Up @@ -375,10 +393,7 @@ public static DateMatcher<LocalDate> sameDayOfWeek(final LocalDate date) {
* </pre>
*/
public static DateMatcher<LocalDate> isDayOfWeek(final DayOfWeek dayOfWeek) {
return new IsDayOfWeek<>(
new FieldLocalDateWrapper(dayOfWeek.getValue(), ChronoField.DAY_OF_WEEK),
(d, z) -> d.atStartOfDay(z).getDayOfWeek().getValue()
);
return new IsDayOfWeek<>(LOCALDATE_AS_DAYOFWEEK, daysOfWeek(dayOfWeek));
}

/**
Expand All @@ -391,10 +406,7 @@ public static DateMatcher<LocalDate> isDayOfWeek(final DayOfWeek dayOfWeek) {
* </pre>
*/
public static DateMatcher<LocalDate> isDayOfWeek(final DayOfWeek... daysOfWeek) {
return new AnyOf<>(
Stream.of(daysOfWeek).map(LocalDateMatchers::isDayOfWeek),
(d, z) -> "the date is on a " + d.atStartOfDay(z).getDayOfWeek().name().toLowerCase()
);
return new IsDayOfWeek<>(LOCALDATE_AS_DAYOFWEEK, daysOfWeek(daysOfWeek));
}

/**
Expand Down Expand Up @@ -524,13 +536,7 @@ public static DateMatcher<LocalDate> isWeekend() {
* </pre>
*/
public static DateMatcher<LocalDate> isFirstDayOfMonth() {
return new IsMinimum<>(
ChronoField.DAY_OF_MONTH,
(d, z) -> d.atStartOfDay(z).get(ChronoField.DAY_OF_MONTH),
(d, z) -> ChronoField.DAY_OF_MONTH.rangeRefinedBy(d.atStartOfDay(z)),
new DatePartFormatter(),
() -> "the date is the first day of the month"
);
return new IsFirstDayOfMonth<>(LOCALDATE_AS_LOCALDATE);
}

/**
Expand All @@ -546,12 +552,7 @@ public static DateMatcher<LocalDate> isFirstDayOfMonth() {
* @param field the temporal field to check
*/
public static DateMatcher<LocalDate> isMinimum(final ChronoField field) {
return new IsMinimum<>(
field,
(d, z) -> d.atStartOfDay(z).get(field),
(d, z) -> field.rangeRefinedBy(d.atStartOfDay(z)),
new DatePartFormatter()
);
return new IsMinimum<>(LOCALDATE_AS_LOCALDATE, field);
}

/**
Expand All @@ -564,13 +565,7 @@ public static DateMatcher<LocalDate> isMinimum(final ChronoField field) {
* </pre>
*/
public static DateMatcher<LocalDate> isLastDayOfMonth() {
return new IsMaximum<>(
ChronoField.DAY_OF_MONTH,
(d, z) -> d.atStartOfDay(z).get(ChronoField.DAY_OF_MONTH),
(d, z) -> ChronoField.DAY_OF_MONTH.rangeRefinedBy(d.atStartOfDay(z)),
new DatePartFormatter(),
() -> "the date is the last day of the month"
);
return new IsLastDayOfMonth<>(LOCALDATE_AS_LOCALDATE);
}

/**
Expand All @@ -586,12 +581,7 @@ public static DateMatcher<LocalDate> isLastDayOfMonth() {
* @param field the temporal field to check
*/
public static DateMatcher<LocalDate> isMaximum(final ChronoField field) {
return new IsMaximum<>(
field,
(d, z) -> d.atStartOfDay(z).get(field),
(d, z) -> field.rangeRefinedBy(d.atStartOfDay(z)),
new DatePartFormatter()
);
return new IsMaximum<>(LOCALDATE_AS_LOCALDATE, field);
}

/**
Expand All @@ -604,10 +594,7 @@ public static DateMatcher<LocalDate> isMaximum(final ChronoField field) {
* </pre>
*/
public static DateMatcher<LocalDate> isMonth(final Month month) {
return new IsMonth<>(
new FieldLocalDateWrapper(month.getValue(), ChronoField.MONTH_OF_YEAR),
(d, z) -> d.atStartOfDay(z).getMonthValue()
);
return new IsMonth<>(LOCALDATE_AS_MONTH, month(month));
}

/**
Expand Down Expand Up @@ -776,6 +763,6 @@ public static DateMatcher<LocalDate> isDecember() {
* </pre>
*/
public static DateMatcher<LocalDate> isLeapYear() {
return new IsLeapYear<>((d, z) -> d, new LocalDateFormatter());
return new IsLeapYear<>(LOCALDATE_AS_YEAR);
}
}

0 comments on commit d204e86

Please sign in to comment.