-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from MarkusAmshove/datetime
Assertions for dates and time
- Loading branch information
Showing
12 changed files
with
2,508 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
# java.time Assertions | ||
|
||
The following documentation is a overview around the assertions for `java.time.LocalDateTime`, `java.time.LocalDate` and `java.time.LocalTime` | ||
|
||
Not all cases are covered, but all methods work as appropriate to the tested datetype. | ||
|
||
There are extension functions for x.years(), x.months(), x.days(), x.hours(), x.minutes() and x.seconds(). | ||
|
||
```kt | ||
// Test that a LocalDateTime is after another | ||
val dateToTest = LocalDateTime.of(2017, 3, 1, 10, 0) | ||
val dateBefore = dateToTest.minusDays(1) | ||
dateToTest shouldBeAfter dateBefore | ||
|
||
// Test that a LocalDateTime is before another | ||
val dateToTest = LocalDateTime.of(2017, 3, 1, 10, 0) | ||
val dateAfter = dateToTest.plusDays(1) | ||
dateToTest shouldBeBefore dateAfter | ||
|
||
// Test that a LocalDateTime is on or after another | ||
val dateToTest = LocalDateTime.of(2017, 3, 1, 10, 0) | ||
val dateBefore = dateToTest.minusDays(1) | ||
dateToTest shouldBeOnOrAfter dateBefore | ||
|
||
// Test that a LocalDateTime is on or before another | ||
val dateToTest = LocalDateTime.of(2017, 3, 1, 10, 0) | ||
val dateAfter = dateToTest.plusDays(1) | ||
dateToTest shouldBeOnOrBefore dateAfter | ||
|
||
// Test that a LocalDateTime is on a specific day | ||
val dateToTest = LocalDateTime.of(2017, 3, 1, 10, 0) | ||
dateToTest shouldBeOn DayOfWeek.WEDNESDAY | ||
|
||
// Test that a LocalDateTime is not on a specific day | ||
val dateToTest = LocalDateTime.of(2017, 3, 1, 10, 0) | ||
dateToTest shouldNotBeOn DayOfWeek.MONDAY | ||
|
||
// Test that a LocalDateTime is in a specific month | ||
val dateToTest = LocalDateTime.of(2017, 3, 1, 10, 0) | ||
dateToTest shouldBeIn Month.MARCH | ||
|
||
// Test that a LocalDateTime is not in a specific month | ||
val dateToTest = LocalDateTime.of(2017, 3, 1, 10, 0) | ||
dateToTest shouldBeIn Month.APRIL | ||
|
||
// Test that a LocalDateTime is in a specific year | ||
val dateToTest = LocalDateTime.of(2017, 3, 1, 10, 0) | ||
dateToTest shouldBeInYear 2017 | ||
|
||
// Test that a LocalDateTime is not in a specific year | ||
val dateToTest = LocalDateTime.of(2017, 3, 1, 10, 0) | ||
dateToTest shouldBeInYear 2012 | ||
|
||
// Test that a LocalDateTime is within 5 days after another | ||
val orderDate = LocalDateTime.of(2017, 6, 5, 10, 0) | ||
val shippingDate = LocalDateTime.of(2017, 6, 10, 10, 0) | ||
shippingDate shouldBe 5.days() after orderDate | ||
|
||
// Test that a LocalDateTime is at least 5 days after another | ||
val orderDate = LocalDateTime.of(2017, 6, 5, 10, 0) | ||
val shippingDate = LocalDateTime.of(2017, 6, 11, 10, 0) | ||
shippingDate shouldBeAtLeast 5.days() after orderDate | ||
|
||
// Test that a LocalDateTime is at most 5 days after another | ||
val orderDate = LocalDateTime.of(2017, 6, 5, 10, 0) | ||
val shippingDate = LocalDateTime.of(2017, 6, 9, 10, 0) | ||
shippingDate shouldBeAtMost 5.days() after orderDate | ||
|
||
// Test that a LocalDateTime is within 5 days before another | ||
val shippingDate = LocalDateTime.of(2017, 6, 15, 10, 0) | ||
val orderDate = LocalDateTime.of(2017, 6, 10, 10, 0) | ||
orderDate shouldBe 5.days() before shippingDate | ||
|
||
// Test that a LocalDateTime is at least 5 days before another | ||
val shippingDate = LocalDateTime.of(2017, 6, 15, 10, 0) | ||
val orderDate = LocalDateTime.of(2017, 6, 9, 10, 0) | ||
orderDate shouldBeAtLeast 5.days() before shippingDate | ||
|
||
// Test that a LocalDateTime is at most 5 days before another | ||
val shippingDate = LocalDateTime.of(2017, 6, 15, 10, 0) | ||
val orderDate = LocalDateTime.of(2017, 6, 12, 10, 0) | ||
orderDate shouldBeAtMost 5.days() before shippingDate | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
package org.amshove.kluent | ||
|
||
import org.junit.Assert.assertTrue | ||
import java.time.* | ||
|
||
infix fun LocalDateTime.`should be after`(theOther: LocalDateTime) = assertTrue("Expected $this to be after $theOther", this > theOther) | ||
infix fun LocalDateTime.shouldBeAfter(theOther: LocalDateTime) = this `should be after` theOther | ||
|
||
infix fun LocalDateTime.`should be after`(theTime: LocalTime) = assertTrue("Expected $this to be after $theTime", this.toLocalTime() > theTime) | ||
infix fun LocalDateTime.shouldBeAfter(theTime: LocalTime) = this `should be after` theTime | ||
|
||
infix fun LocalDateTime.`should be before`(theOther: LocalDateTime) = assertTrue("Expected $this to be before $theOther", this < theOther) | ||
infix fun LocalDateTime.shouldBeBefore(theOther: LocalDateTime) = this `should be before` theOther | ||
|
||
infix fun LocalDateTime.`should be before`(theTime: LocalTime) = assertTrue("Expected $this to be before $theTime", this.toLocalTime() < theTime) | ||
infix fun LocalDateTime.shouldBeBefore(theTime: LocalTime) = this `should be before` theTime | ||
|
||
infix fun LocalDateTime.`should be in hour`(theHour: Int) = this.toLocalTime() `should be in hour` theHour | ||
infix fun LocalDateTime.shouldBeInHour(theHour: Int) = this `should be in hour` theHour | ||
|
||
infix fun LocalDateTime.`should not be in hour`(theHour: Int) = this.toLocalTime() `should not be in hour` theHour | ||
infix fun LocalDateTime.shouldNotBeInHour(theHour: Int) = this `should not be in hour` theHour | ||
|
||
infix fun LocalDateTime.`should be in minute`(theMinute: Int) = this.toLocalTime() `should be in minute` theMinute | ||
infix fun LocalDateTime.shouldBeInMinute(theMinute: Int) = this `should be in minute` theMinute | ||
|
||
infix fun LocalDateTime.`should not be in minute`(theMinute: Int) = this.toLocalTime() `should not be in minute` theMinute | ||
infix fun LocalDateTime.shouldNotBeInMinute(theMinute: Int) = this `should not be in minute` theMinute | ||
|
||
infix fun LocalDateTime.`should be in second`(theSecond: Int) = this.toLocalTime() `should be in second` theSecond | ||
infix fun LocalDateTime.shouldBeInSecond(theSecond: Int) = this `should be in second` theSecond | ||
|
||
infix fun LocalDateTime.`should not be in second`(theSecond: Int) = this.toLocalTime() `should not be in second` theSecond | ||
infix fun LocalDateTime.shouldNotBeInSecond(theSecond: Int) = this `should not be in second` theSecond | ||
|
||
infix fun LocalDateTime.`should be on or after`(theDate: LocalDateTime) = assertTrue("Expected $this to be on or after $theDate", this >= theDate) | ||
infix fun LocalDateTime.shouldBeOnOrAfter(theDate: LocalDateTime) = this `should be on or after` theDate | ||
|
||
infix fun LocalDateTime.`should be on or before`(theDate: LocalDateTime) = assertTrue("Expected $this to be on or before $theDate", this <= theDate) | ||
infix fun LocalDateTime.shouldBeOnOrBefore(theDate: LocalDateTime) = this `should be on or before` theDate | ||
|
||
infix fun LocalDateTime.`should be on`(theDay: DayOfWeek) = assertTrue("Expected $this to be a $theDay, but was ${this.dayOfWeek}", this.dayOfWeek == theDay) | ||
infix fun LocalDateTime.shouldBeOn(theDay: DayOfWeek) = this `should be on` theDay | ||
|
||
infix fun LocalDateTime.`should not be on`(theDay: DayOfWeek) = this.toLocalDate() `should not be on` theDay | ||
infix fun LocalDateTime.shouldNotBeOn(theDay: DayOfWeek) = this `should not be on` theDay | ||
|
||
infix fun LocalDateTime.`should be in`(theMonth: Month) = assertTrue("Expected $this to be in $theMonth, but was ${this.month}", this.month == theMonth) | ||
infix fun LocalDateTime.shouldBeIn(theMonth: Month) = this `should be in` theMonth | ||
|
||
infix fun LocalDateTime.`should not be in`(theMonth: Month) = this.toLocalDate() `should not be in` theMonth | ||
infix fun LocalDateTime.shouldNotBeIn(theMonth: Month) = this `should not be in` theMonth | ||
|
||
infix fun LocalDateTime.`should be in year`(theYear: Int) = assertTrue("Expected $this to be in $theYear, but was ${this.year}", this.year == theYear) | ||
infix fun LocalDateTime.shouldBeInYear(theYear: Int) = this `should be in year` theYear | ||
|
||
infix fun LocalDateTime.`should not be in year`(theYear: Int) = this.toLocalDate() `should not be in year` theYear | ||
infix fun LocalDateTime.shouldNotBeInYear(theYear: Int) = this `should not be in year` theYear | ||
|
||
infix fun LocalDate.`should be after`(theOther: LocalDate) = assertTrue("Expected $this to be after $theOther", this > theOther) | ||
infix fun LocalDate.shouldBeAfter(theOther: LocalDate) = this `should be after` theOther | ||
|
||
infix fun LocalDate.`should be before`(theOther: LocalDate) = assertTrue("Expected $this to be before $theOther", this < theOther) | ||
infix fun LocalDate.shouldBeBefore(theOther: LocalDate) = this `should be before` theOther | ||
|
||
infix fun LocalDate.`should be on or after`(theDate: LocalDate) = assertTrue("Expected $this to be on or after $theDate", this >= theDate) | ||
infix fun LocalDate.shouldBeOnOrAfter(theDate: LocalDate) = this `should be on or after` theDate | ||
|
||
infix fun LocalDate.`should be on or before`(theDate: LocalDate) = assertTrue("Expected $this to be on or before $theDate", this <= theDate) | ||
infix fun LocalDate.shouldBeOnOrBefore(theDate: LocalDate) = this `should be on or before` theDate | ||
|
||
infix fun LocalDate.`should be on`(theDay: DayOfWeek) = assertTrue("Expected $this to be a $theDay, but was ${this.dayOfWeek}", this.dayOfWeek == theDay) | ||
infix fun LocalDate.shouldBeOn(theDay: DayOfWeek) = this `should be on` theDay | ||
|
||
infix fun LocalDate.`should not be on`(theDay: DayOfWeek) = assertTrue("Expected $this to not be a $theDay, but was ${this.dayOfWeek}", this.dayOfWeek != theDay) | ||
infix fun LocalDate.shouldNotBeOn(theDay: DayOfWeek) = this `should not be on` theDay | ||
|
||
infix fun LocalDate.`should be in`(theMonth: Month) = assertTrue("Expected $this to be in $theMonth, but was ${this.month}", this.month == theMonth) | ||
infix fun LocalDate.shouldBeIn(theMonth: Month) = this `should be in` theMonth | ||
|
||
infix fun LocalDate.`should not be in`(theMonth: Month) = assertTrue("Expected $this to not be in $theMonth, but was ${this.month}", this.month != theMonth) | ||
infix fun LocalDate.shouldNotBeIn(theMonth: Month) = this `should not be in` theMonth | ||
|
||
infix fun LocalDate.`should be in year`(theYear: Int) = assertTrue("Expected $this to be in $theYear, but was ${this.year}", this.year == theYear) | ||
infix fun LocalDate.shouldBeInYear(theYear: Int) = this `should be in year` theYear | ||
|
||
infix fun LocalDate.`should not be in year`(theYear: Int) = assertTrue("Expected $this to not be in $theYear, but was ${this.year}", this.year != theYear) | ||
infix fun LocalDate.shouldNotBeInYear(theYear: Int) = this `should not be in year` theYear | ||
|
||
fun Int.hours() = TimeComparator(addedHours = this) | ||
fun Int.minutes() = TimeComparator(addedMinutes = this) | ||
fun Int.seconds() = TimeComparator(addedSeconds = this) | ||
fun Int.years() = DateComparator(addedYears = this) | ||
fun Int.months() = DateComparator(addedMonths = this) | ||
fun Int.days() = DateComparator(addedDays = this) | ||
|
||
infix fun LocalTime.`should be`(timeComparator: TimeComparator) = timeComparator.withStartValue(this) | ||
infix fun LocalTime.shouldBe(timeComparator: TimeComparator) = this `should be` timeComparator | ||
|
||
infix fun LocalTime.`should be at least`(timeComparator: TimeComparator) = timeComparator.withStartValue(this).withComparatorType(ComparatorType.AtLeast) | ||
infix fun LocalTime.shouldBeAtLeast(timeComparator: TimeComparator) = this `should be at least` timeComparator | ||
|
||
infix fun LocalTime.`should be at most`(timeComparator: TimeComparator) = timeComparator.withStartValue(this).withComparatorType(ComparatorType.AtMost) | ||
infix fun LocalTime.shouldBeAtMost(timeComparator: TimeComparator) = this `should be at most` timeComparator | ||
|
||
infix fun LocalTime.`should be in hour`(theHour: Int) = assertTrue("Expected $this to be in hour $theHour", this.hour == theHour) | ||
infix fun LocalTime.shouldBeInHour(theHour: Int) = this `should be in hour` theHour | ||
|
||
infix fun LocalTime.`should not be in hour`(theHour: Int) = assertTrue("Expected $this to not be in hour $theHour", this.hour != theHour) | ||
infix fun LocalTime.shouldNotBeInHour(theHour: Int) = this `should not be in hour` theHour | ||
|
||
infix fun LocalTime.`should be in minute`(theMinute: Int) = assertTrue("Expected $this to be in minute $theMinute", this.minute == theMinute) | ||
infix fun LocalTime.shouldBeInMinute(theMinute: Int) = this `should be in minute` theMinute | ||
|
||
infix fun LocalTime.`should not be in minute`(theMinute: Int) = assertTrue("Expected $this to not be in minute $theMinute", this.minute != theMinute) | ||
infix fun LocalTime.shouldNotBeInMinute(theMinute: Int) = this `should not be in minute` theMinute | ||
|
||
infix fun LocalTime.`should be in second`(theSecond: Int) = assertTrue("Expected $this to be in second $theSecond", this.second == theSecond) | ||
infix fun LocalTime.shouldBeInSecond(theSecond: Int) = this `should be in second` theSecond | ||
|
||
infix fun LocalTime.`should not be in second`(theSecond: Int) = assertTrue("Expected $this to not be in second $theSecond", this.second != theSecond) | ||
infix fun LocalTime.shouldNotBeInSecond(theSecond: Int) = this `should not be in second` theSecond | ||
|
||
infix fun LocalDate.`should be`(dateComparator: DateComparator) = dateComparator.withStartValue(this) | ||
infix fun LocalDate.shouldBe(dateComparator: DateComparator) = this `should be` dateComparator | ||
|
||
infix fun LocalDate.`should be at least`(dateComparator: DateComparator) = dateComparator.withStartValue(this).withComparatorType(ComparatorType.AtLeast) | ||
infix fun LocalDate.shouldBeAtLeast(dateComparator: DateComparator) = this `should be at least` dateComparator | ||
|
||
infix fun LocalDate.`should be at most`(dateComparator: DateComparator) = dateComparator.withStartValue(this).withComparatorType(ComparatorType.AtMost) | ||
infix fun LocalDate.shouldBeAtMost(dateComparator: DateComparator) = this `should be at most` dateComparator | ||
|
||
infix fun LocalDateTime.`should be`(dateComparator: DateComparator): DateTimeComparator = DateTimeComparator(dateComparator).withStartValue(this) as DateTimeComparator | ||
infix fun LocalDateTime.shouldBe(dateComparator: DateComparator) = this `should be` dateComparator | ||
|
||
infix fun LocalDateTime.`should be at least`(dateComparator: DateComparator) = DateTimeComparator(dateComparator).withStartValue(this).withComparatorType(ComparatorType.AtLeast) | ||
infix fun LocalDateTime.shouldBeAtLeast(dateComparator: DateComparator) = this `should be at least` dateComparator | ||
|
||
infix fun LocalDateTime.`should be at most`(dateComparator: DateComparator) = DateTimeComparator(dateComparator).withStartValue(this).withComparatorType(ComparatorType.AtMost) | ||
infix fun LocalDateTime.shouldBeAtMost(dateComparator: DateComparator) = this `should be at most` dateComparator | ||
|
||
infix fun LocalDateTime.`should be`(timeComparator: TimeComparator) = DateTimeComparator(timeComparator = timeComparator).withStartValue(this) as DateTimeComparator | ||
infix fun LocalDateTime.shouldBe(timeComparator: TimeComparator) = this `should be` timeComparator | ||
|
||
infix fun LocalDateTime.`should be at least`(timeComparator: TimeComparator) = DateTimeComparator(timeComparator = timeComparator).withStartValue(this).withComparatorType(ComparatorType.AtLeast) | ||
infix fun LocalDateTime.shouldBeAtLeast(timeComparator: TimeComparator) = this `should be at least` timeComparator | ||
|
||
infix fun LocalDateTime.`should be at most`(timeComparator: TimeComparator) = DateTimeComparator(timeComparator = timeComparator).withStartValue(this).withComparatorType(ComparatorType.AtMost) | ||
infix fun LocalDateTime.shouldBeAtMost(timeComparator: TimeComparator) = this `should be at most` timeComparator | ||
|
||
infix fun <T : Comparable<T>> AbstractJavaTimeComparator<T>.after(theOther: T) = this.assertAfter(theOther) | ||
infix fun <T : Comparable<T>> AbstractJavaTimeComparator<T>.before(theOther: T) = this.assertBefore(theOther) | ||
|
||
infix fun DateTimeComparator.after(theDate: LocalDate) = this.assertAfter(theDate) | ||
infix fun DateTimeComparator.before(theDate: LocalDate) = this.assertBefore(theDate) | ||
|
||
infix fun DateTimeComparator.after(theTime: LocalTime) = this.assertAfter(theTime) | ||
infix fun DateTimeComparator.before(theTime: LocalTime) = this.assertBefore(theTime) | ||
|
||
internal enum class ComparatorType { | ||
AtMost, | ||
AtLeast, | ||
Exactly | ||
} |
Oops, something went wrong.