eXparity / hamcrest-date

A Java library which provides a suite hamcrest matchers for matching dates, times, and moments in time
BSD 3-Clause "New" or "Revised" License
70 stars 11 forks source link

Additional matchers enhancement #4

Closed Proximator closed 11 years ago

Proximator commented 11 years ago

So far, there is only matchers for 2 arguments (2 dates, actual and expected) But we can make assertions for the actual only, for example assertThat(date1, isFirstDayOfMonth()) assertThat(date1, isFirstDayOfWeek()) ... assertThat(date1, isLastDayOfMonth()) assertThat(date1, isLastDayOfWeek()) assertThat(date1, isLeapYear()) ....

Regards

stewbis commented 11 years ago

Good suggestion.

You'd have to set some bounds on it though to avoid isFirstDayOfMonth(), isSecondDayOfMonth(), isThirdDayOfMonth(), etc. This would lead to a more general form of isDayOfMonth(dayOfMonth) but with some non-general for isFirstDayOfMonth(), isLastDayOfMonth().

There is some limited support on this line i.e. DateMatchers.sameWeekday(WeekDays.MONDAY) but this again could be transformed to isMonday(), isTuesday().

I'll also add isLeapYear().

I'll add matchers to support this pattern.

Regards

stewbis

Proximator commented 11 years ago

The problem with sameWeekday(WeekDays.MONDAY) is that it takes an arguments. Hamcrest should have matchers that looks like english sentence to be more readable assertThat (date1, isMonday()); this format is far better

Regards

Proximator commented 11 years ago

Perhaps, we can group all the related day matchers in DayMatchers (isMonday, isFraday,.., ) MonthMatchers (isJanuary, isFebrary,...,isFirstOfMonth, isLastOfMonth...) YearMatchers ( isLeapYear, ...) Then all these matchers in DateMatchers Like CoreMatchers/Matchers in Hamcrest, it regroups all the other matchers

stewbis commented 11 years ago

I wasn't clear in my comment. I'll add on isMonday(), isTuesday(), etc.

It makes sense to have DayMatchers, MonthMatchers, YearMatchers as the implementation classes and DateMatchers delegates to it i.e. the same pattern followed in the other matchers.

Proximator commented 11 years ago

Ok, that nice :+1:

Proximator commented 11 years ago

Hi stewbis,

Is it worth to have HourMatchers, MinuteMatchers ? (to have something like: beforeHour, beforeMiday, afterMiday, ...) ?

stewbis commented 11 years ago

MinuteMatchers would probably be going too far - I can't think of many test cases for isFirstMinuteOfHour() and if a developer really want to do that DateMatcher.sameMinute(1) would meet the test case but not as clean to read. HourMatchers perhaps, isPM(), isAM(),

stewbis commented 11 years ago

I've also commit the DayMatchers. The mismatch description needs some sorting - but the DayMatcher support is added.

stewbis commented 11 years ago

Changes have been released in version 0.9.3