Best Kotest code snippet using io.kotest.matchers.date.matchers.within
DateMatchersTest.kt
Source:DateMatchersTest.kt
...46import io.kotest.matchers.date.shouldNotHaveSameMonthAs47import io.kotest.matchers.date.shouldNotHaveSameNanosAs48import io.kotest.matchers.date.shouldNotHaveSameSecondsAs49import io.kotest.matchers.date.shouldNotHaveSameYearAs50import io.kotest.matchers.date.within51import io.kotest.matchers.shouldBe52import io.kotest.matchers.should53import io.kotest.matchers.shouldNot54import io.kotest.matchers.shouldNotBe55import java.time.DayOfWeek.SATURDAY56import java.time.Duration57import java.time.LocalDate58import java.time.LocalDateTime59import java.time.LocalTime60import java.time.Month61import java.time.OffsetDateTime62import java.time.Period63import java.time.ZoneId64import java.time.ZoneOffset65import java.time.ZonedDateTime66class DateMatchersTest : StringSpec() {67 init {68 "LocalTime should have same nanos ignoring other fields" {69 LocalTime.of(1, 2, 3, 4) should haveSameNanos(LocalTime.of(5, 6, 7, 4))70 LocalTime.of(1, 2, 3, 4) shouldNot haveSameNanos(LocalTime.of(1, 2, 3, 8))71 LocalTime.of(1, 2, 3, 4).shouldHaveSameNanosAs(LocalTime.of(5, 6, 7, 4))72 LocalTime.of(1, 2, 3, 4).shouldNotHaveSameNanosAs(LocalTime.of(1, 2, 3, 8))73 }74 "LocalTime should have same seconds ignoring other fields" {75 LocalTime.of(1, 2, 3, 4) should haveSameSeconds(LocalTime.of(5, 6, 3, 4))76 LocalTime.of(1, 2, 3, 4) shouldNot haveSameSeconds(LocalTime.of(1, 2, 5, 4))77 LocalTime.of(1, 2, 3, 4).shouldHaveSameSecondsAs(LocalTime.of(5, 6, 3, 4))78 LocalTime.of(1, 2, 3, 4).shouldNotHaveSameSecondsAs(LocalTime.of(1, 2, 5, 4))79 }80 "LocalTime should have same minutes ignoring other fields" {81 LocalTime.of(1, 2, 3, 4) should haveSameMinutes(LocalTime.of(5, 2, 7, 8))82 LocalTime.of(1, 2, 3, 4) shouldNot haveSameMinutes(LocalTime.of(1, 5, 3, 4))83 LocalTime.of(1, 2, 3, 4).shouldHaveSameMinutesAs(LocalTime.of(5, 2, 7, 8))84 LocalTime.of(1, 2, 3, 4).shouldNotHaveSameMinutesAs(LocalTime.of(1, 5, 3, 4))85 }86 "LocalTime should have same hours ignoring other fields" {87 LocalTime.of(12, 1, 2, 7777) should haveSameHours(LocalTime.of(12, 59, 58, 9999))88 LocalTime.of(3, 59, 58, 9999) shouldNot haveSameHours(LocalTime.of(12, 59, 58, 9999))89 LocalTime.of(12, 1, 2, 7777).shouldHaveSameHoursAs(LocalTime.of(12, 59, 58, 9999))90 LocalTime.of(3, 59, 58, 9999).shouldNotHaveSameHoursAs(LocalTime.of(12, 59, 58, 9999))91 }92 "LocalDate should have same year ignoring other fields" {93 LocalDate.of(2014, 1, 2) should haveSameYear(LocalDate.of(2014, 5, 6))94 LocalDate.of(2014, 1, 2) shouldNot haveSameYear(LocalDate.of(2018, 5, 6))95 LocalDate.of(2014, 1, 2).shouldHaveSameYearAs(LocalDate.of(2014, 5, 6))96 LocalDate.of(2014, 1, 2).shouldNotHaveSameYearAs(LocalDate.of(2018, 5, 6))97 }98 "LocalDateTime should have same year ignoring other fields" {99 LocalDateTime.of(2014, 1, 2, 4, 3, 2) should haveSameYear(LocalDateTime.of(2014, 5, 6, 3, 2, 1))100 LocalDateTime.of(2014, 1, 2, 3, 2, 1) shouldNot haveSameYear(LocalDateTime.of(2018, 5, 6, 3, 2, 1))101 LocalDateTime.of(2014, 1, 2, 4, 3, 2).shouldHaveSameYearAs(LocalDateTime.of(2014, 5, 6, 3, 2, 1))102 LocalDateTime.of(2014, 1, 2, 3, 2, 1).shouldNotHaveSameYearAs(LocalDateTime.of(2018, 5, 6, 3, 2, 1))103 }104 "ZonedDateTime should have same year ignoring other fields" {105 LocalDateTime.of(2014, 1, 2, 4, 3, 2).atZone(ZoneId.of("Z")) should haveSameYear(LocalDateTime.of(2014, 5, 6, 3, 2, 1).atZone(ZoneId.of("Z")))106 LocalDateTime.of(2014, 1, 2, 3, 2, 1).atZone(ZoneId.of("Z")) shouldNot haveSameYear(LocalDateTime.of(2018, 5, 6, 3, 2, 1).atZone(ZoneId.of("Z")))107 LocalDateTime.of(2014, 1, 2, 4, 3, 2).atZone(ZoneId.of("Z")).shouldHaveSameYearAs(LocalDateTime.of(2014, 5, 6, 3, 2, 1).atZone(ZoneId.of("Z")))108 LocalDateTime.of(2014, 1, 2, 3, 2, 1).atZone(ZoneId.of("Z")).shouldNotHaveSameYearAs(LocalDateTime.of(2018, 5, 6, 3, 2, 1).atZone(ZoneId.of("Z")))109 }110 "OffsetDateTime should have same year ignoring other fields" {111 LocalDateTime.of(2014, 1, 2, 4, 3, 2).atOffset(ZoneOffset.UTC) should haveSameYear(LocalDateTime.of(2014, 5, 6, 3, 2, 1).atOffset(ZoneOffset.UTC))112 LocalDateTime.of(2014, 1, 2, 3, 2, 1).atOffset(ZoneOffset.UTC) shouldNot haveSameYear(LocalDateTime.of(2018, 5, 6, 3, 2, 1).atOffset(ZoneOffset.UTC))113 LocalDateTime.of(2014, 1, 2, 4, 3, 2).atOffset(ZoneOffset.UTC).shouldHaveSameYearAs(LocalDateTime.of(2014, 5, 6, 3, 2, 1).atOffset(ZoneOffset.UTC))114 LocalDateTime.of(2014, 1, 2, 3, 2, 1).atOffset(ZoneOffset.UTC).shouldNotHaveSameYearAs(LocalDateTime.of(2018, 5, 6, 3, 2, 1).atOffset(ZoneOffset.UTC))115 }116 "LocalDate should have same month ignoring other fields" {117 LocalDate.of(2014, 1, 2) should haveSameMonth(LocalDate.of(2016, 1, 6))118 LocalDate.of(2014, 1, 2) shouldNot haveSameMonth(LocalDate.of(2018, 4, 6))119 LocalDate.of(2014, 1, 2).shouldHaveSameMonthAs(LocalDate.of(2016, 1, 6))120 LocalDate.of(2014, 1, 2).shouldNotHaveSameMonthAs(LocalDate.of(2018, 4, 6))121 }122 "LocalDateTime should have same month ignoring other fields" {123 LocalDateTime.of(2014, 1, 2, 4, 3, 2) should haveSameMonth(LocalDateTime.of(2014, 1, 6, 3, 2, 1))124 LocalDateTime.of(2014, 1, 2, 3, 2, 1) shouldNot haveSameMonth(LocalDateTime.of(2018, 2, 6, 3, 2, 1))125 LocalDateTime.of(2014, 1, 2, 4, 3, 2).shouldHaveSameMonthAs(LocalDateTime.of(2014, 1, 6, 3, 2, 1))126 LocalDateTime.of(2014, 1, 2, 3, 2, 1).shouldNotHaveSameMonthAs(LocalDateTime.of(2018, 2, 6, 3, 2, 1))127 }128 "ZonedDateTime should have same month ignoring other fields" {129 LocalDateTime.of(2014, 1, 2, 4, 3, 2).atZone(ZoneId.of("Z")) should haveSameMonth(LocalDateTime.of(2014, 1, 6, 3, 2, 1).atZone(ZoneId.of("Z")))130 LocalDateTime.of(2014, 1, 2, 3, 2, 1).atZone(ZoneId.of("Z")) shouldNot haveSameMonth(LocalDateTime.of(2018, 2, 6, 3, 2, 1).atZone(ZoneId.of("Z")))131 LocalDateTime.of(2014, 1, 2, 4, 3, 2).atZone(ZoneId.of("Z")).shouldHaveSameMonthAs(LocalDateTime.of(2014, 1, 6, 3, 2, 1).atZone(ZoneId.of("Z")))132 LocalDateTime.of(2014, 1, 2, 3, 2, 1).atZone(ZoneId.of("Z")).shouldNotHaveSameMonthAs(LocalDateTime.of(2018, 2, 6, 3, 2, 1).atZone(ZoneId.of("Z")))133 }134 "OffsetDateTime should have same month ignoring other fields" {135 LocalDateTime.of(2014, 1, 2, 4, 3, 2).atOffset(ZoneOffset.UTC) should haveSameMonth(LocalDateTime.of(2014, 1, 6, 3, 2, 1).atOffset(ZoneOffset.UTC))136 LocalDateTime.of(2014, 1, 2, 3, 2, 1).atOffset(ZoneOffset.UTC) shouldNot haveSameMonth(LocalDateTime.of(2018, 2, 6, 3, 2, 1).atOffset(ZoneOffset.UTC))137 LocalDateTime.of(2014, 1, 2, 4, 3, 2).atOffset(ZoneOffset.UTC).shouldHaveSameMonthAs(LocalDateTime.of(2014, 1, 6, 3, 2, 1).atOffset(ZoneOffset.UTC))138 LocalDateTime.of(2014, 1, 2, 3, 2, 1).atOffset(ZoneOffset.UTC).shouldNotHaveSameMonthAs(LocalDateTime.of(2018, 2, 6, 3, 2, 1).atOffset(ZoneOffset.UTC))139 }140 "LocalDate should have same day ignoring other fields" {141 LocalDate.of(2014, 1, 2) should haveSameDay(LocalDate.of(2014, 1, 2))142 LocalDate.of(2014, 1, 2) shouldNot haveSameDay(LocalDate.of(2014, 4, 6))143 LocalDate.of(2014, 1, 2).shouldHaveSameDayAs(LocalDate.of(2014, 1, 2))144 LocalDate.of(2014, 1, 2).shouldNotHaveSameDayAs(LocalDate.of(2014, 4, 6))145 }146 "LocalDateTime should have same day ignoring other fields" {147 LocalDateTime.of(2014, 1, 2, 4, 3, 2) should haveSameDay(LocalDateTime.of(2014, 1, 2, 3, 2, 1))148 LocalDateTime.of(2014, 1, 2, 3, 2, 1) shouldNot haveSameDay(LocalDateTime.of(2014, 2, 6, 3, 2, 1))149 LocalDateTime.of(2014, 1, 2, 4, 3, 2).shouldHaveSameDayAs(LocalDateTime.of(2014, 1, 2, 3, 2, 1))150 LocalDateTime.of(2014, 1, 2, 3, 2, 1).shouldNotHaveSameDayAs(LocalDateTime.of(2014, 2, 6, 3, 2, 1))151 }152 "ZonedDateTime should have same day ignoring other fields" {153 LocalDateTime.of(2014, 1, 2, 4, 3, 2).atZone(ZoneId.of("Z")) should haveSameDay(LocalDateTime.of(2014, 1, 2, 3, 2, 1).atZone(ZoneId.of("Z")))154 LocalDateTime.of(2014, 1, 2, 3, 2, 1).atZone(ZoneId.of("Z")) shouldNot haveSameDay(LocalDateTime.of(2014, 2, 6, 3, 2, 1).atZone(ZoneId.of("Z")))155 LocalDateTime.of(2014, 1, 2, 4, 3, 2).atZone(ZoneId.of("Z")).shouldHaveSameDayAs(LocalDateTime.of(2014, 1, 2, 3, 2, 1).atZone(ZoneId.of("Z")))156 LocalDateTime.of(2014, 1, 2, 3, 2, 1).atZone(ZoneId.of("Z")).shouldNotHaveSameDayAs(LocalDateTime.of(2014, 2, 6, 3, 2, 1).atZone(ZoneId.of("Z")))157 }158 "OffsetDateTime should have same day ignoring other fields" {159 LocalDateTime.of(2014, 1, 2, 4, 3, 2).atOffset(ZoneOffset.UTC) should haveSameDay(LocalDateTime.of(2014, 1, 2, 3, 2, 1).atOffset(ZoneOffset.UTC))160 LocalDateTime.of(2014, 1, 2, 3, 2, 1).atOffset(ZoneOffset.UTC) shouldNot haveSameDay(LocalDateTime.of(2014, 2, 6, 3, 2, 1).atOffset(ZoneOffset.UTC))161 LocalDateTime.of(2014, 1, 2, 4, 3, 2).atOffset(ZoneOffset.UTC).shouldHaveSameDayAs(LocalDateTime.of(2014, 1, 2, 3, 2, 1).atOffset(ZoneOffset.UTC))162 LocalDateTime.of(2014, 1, 2, 3, 2, 1).atOffset(ZoneOffset.UTC).shouldNotHaveSameDayAs(LocalDateTime.of(2014, 2, 6, 3, 2, 1).atOffset(ZoneOffset.UTC))163 }164 "LocalTime shouldBe before" {165 LocalTime.of(1, 2, 3, 1000) shouldBe before(LocalTime.of(1, 10, 3, 1000))166 LocalTime.of(1, 2, 3, 1000) shouldNotBe before(LocalTime.of(1, 1, 3, 1000))167 LocalTime.of(1, 2, 3, 1000).shouldBeBefore(LocalTime.of(5, 2, 3, 1000))168 LocalTime.of(1, 2, 3, 1000).shouldNotBeBefore(LocalTime.of(0, 2, 3, 1000))169 }170 "LocalDate shouldBe before" {171 LocalDate.of(2014, 1, 2) shouldBe before(LocalDate.of(2014, 1, 3))172 LocalDate.of(2014, 1, 2) shouldNotBe before(LocalDate.of(2014, 1, 1))173 LocalDate.of(2014, 1, 2).shouldBeBefore(LocalDate.of(2014, 1, 3))174 LocalDate.of(2014, 1, 2).shouldNotBeBefore(LocalDate.of(2014, 1, 1))175 }176 "LocalDateTime shouldBe before" {177 LocalDateTime.of(2014, 1, 2, 4, 3, 2) shouldBe before(LocalDateTime.of(2014, 2, 2, 3, 2, 1))178 LocalDateTime.of(2014, 1, 2, 3, 2, 1) shouldNotBe before(LocalDateTime.of(2014, 1, 1, 3, 2, 1))179 LocalDateTime.of(2014, 1, 2, 4, 3, 2).shouldBeBefore(LocalDateTime.of(2014, 2, 2, 3, 2, 1))180 LocalDateTime.of(2014, 1, 2, 3, 2, 1).shouldNotBeBefore(LocalDateTime.of(2014, 1, 1, 3, 2, 1))181 }182 "ZonedDateTime shouldBe before" {183 LocalDateTime.of(2014, 1, 2, 4, 3, 2).atZone(ZoneId.of("Z")) shouldBe before(LocalDateTime.of(2014, 1, 3, 3, 2, 1).atZone(ZoneId.of("Z")))184 LocalDateTime.of(2014, 1, 2, 3, 2, 1).atZone(ZoneId.of("Z")) shouldNotBe before(LocalDateTime.of(2014, 1, 1, 3, 2, 1).atZone(ZoneId.of("Z")))185 LocalDateTime.of(2014, 1, 2, 4, 3, 2).atZone(ZoneId.of("Z")).shouldBeBefore(LocalDateTime.of(2014, 1, 3, 3, 2, 1).atZone(ZoneId.of("Z")))186 LocalDateTime.of(2014, 1, 2, 3, 2, 1).atZone(ZoneId.of("Z")).shouldNotBeBefore(LocalDateTime.of(2014, 1, 1, 3, 2, 1).atZone(ZoneId.of("Z")))187 }188 "ZonedDateTime shouldBe equal" {189 ZonedDateTime.of(2019, 12, 10, 10, 0, 0, 0, ZoneOffset.UTC) shouldBe190 ZonedDateTime.of(2019, 12, 10, 4, 0, 0, 0, ZoneId.of("America/Chicago")).atSameZone()191 shouldThrow<AssertionError> {192 ZonedDateTime.of(2019, 12, 10, 10, 0, 0, 0, ZoneOffset.UTC) shouldBe193 ZonedDateTime.of(2019, 12, 10, 4, 1, 0, 0, ZoneId.of("America/Chicago")).atSameZone()194 }.message shouldBe "expected:<2019-12-10T10:01Z> but was:<2019-12-10T10:00Z>"195 }196 "OffsetDateTime shouldBe before" {197 LocalDateTime.of(2014, 1, 2, 4, 3, 2).atOffset(ZoneOffset.UTC) shouldBe before(LocalDateTime.of(2016, 1, 2, 3, 2, 1).atOffset(ZoneOffset.UTC))198 LocalDateTime.of(2014, 1, 2, 3, 2, 1).atOffset(ZoneOffset.UTC) shouldNotBe before(LocalDateTime.of(2012, 2, 6, 3, 2, 1).atOffset(ZoneOffset.UTC))199 LocalDateTime.of(2014, 1, 2, 4, 3, 2).atOffset(ZoneOffset.UTC).shouldBeBefore(LocalDateTime.of(2016, 1, 2, 3, 2, 1).atOffset(ZoneOffset.UTC))200 LocalDateTime.of(2014, 1, 2, 3, 2, 1).atOffset(ZoneOffset.UTC).shouldNotBeBefore(LocalDateTime.of(2012, 2, 6, 3, 2, 1).atOffset(ZoneOffset.UTC))201 }202 "LocalTime shouldBe after" {203 LocalTime.of(1, 2, 3, 9999) shouldBe after(LocalTime.of(1, 2, 3, 1111))204 LocalTime.of(1, 2, 3, 1000) shouldNotBe after(LocalTime.of(1, 2, 10, 1000))205 LocalTime.of(1, 2, 3, 9999).shouldBeAfter(LocalTime.of(1, 2, 3, 1111))206 LocalTime.of(1, 2, 3, 1000).shouldNotBeAfter(LocalTime.of(1, 2, 10, 1000))207 }208 "LocalDate shouldBe after" {209 LocalDate.of(2014, 1, 2) shouldBe after(LocalDate.of(2013, 1, 3))210 LocalDate.of(2014, 1, 2) shouldNotBe after(LocalDate.of(2014, 1, 3))211 LocalDate.of(2014, 1, 2).shouldBeAfter(LocalDate.of(2013, 1, 3))212 LocalDate.of(2014, 1, 2).shouldNotBeAfter(LocalDate.of(2014, 1, 3))213 }214 "LocalDateTime shouldBe after" {215 LocalDateTime.of(2014, 1, 2, 4, 3, 2) shouldBe after(LocalDateTime.of(2014, 1, 1, 3, 2, 1))216 LocalDateTime.of(2014, 1, 2, 3, 2, 1) shouldNotBe after(LocalDateTime.of(2014, 1, 3, 3, 2, 1))217 LocalDateTime.of(2014, 1, 2, 4, 3, 2).shouldBeAfter(LocalDateTime.of(2014, 1, 1, 3, 2, 1))218 LocalDateTime.of(2014, 1, 2, 3, 2, 1).shouldNotBeAfter(LocalDateTime.of(2014, 1, 3, 3, 2, 1))219 }220 "ZonedDateTime shouldBe after" {221 LocalDateTime.of(2014, 1, 2, 4, 3, 2).atZone(ZoneId.of("Z")) shouldBe after(LocalDateTime.of(2014, 1, 1, 3, 2, 1).atZone(ZoneId.of("Z")))222 LocalDateTime.of(2014, 1, 2, 3, 2, 1).atZone(ZoneId.of("Z")) shouldNotBe after(LocalDateTime.of(2014, 1, 3, 3, 2, 1).atZone(ZoneId.of("Z")))223 LocalDateTime.of(2014, 1, 2, 4, 3, 2).atZone(ZoneId.of("Z")).shouldBeAfter(LocalDateTime.of(2014, 1, 1, 3, 2, 1).atZone(ZoneId.of("Z")))224 LocalDateTime.of(2014, 1, 2, 3, 2, 1).atZone(ZoneId.of("Z")).shouldNotBeAfter(LocalDateTime.of(2014, 1, 3, 3, 2, 1).atZone(ZoneId.of("Z")))225 }226 "OffsetDateTime shouldBe after" {227 LocalDateTime.of(2014, 1, 2, 4, 3, 2).atOffset(ZoneOffset.UTC) shouldBe after(LocalDateTime.of(2014, 1, 1, 3, 2, 1).atOffset(ZoneOffset.UTC))228 LocalDateTime.of(2014, 1, 2, 3, 2, 1).atOffset(ZoneOffset.UTC) shouldNotBe after(LocalDateTime.of(2014, 2, 1, 3, 2, 1).atOffset(ZoneOffset.UTC))229 LocalDateTime.of(2014, 1, 2, 4, 3, 2).atOffset(ZoneOffset.UTC).shouldBeAfter(LocalDateTime.of(2014, 1, 1, 3, 2, 1).atOffset(ZoneOffset.UTC))230 LocalDateTime.of(2014, 1, 2, 3, 2, 1).atOffset(ZoneOffset.UTC).shouldNotBeAfter(LocalDateTime.of(2014, 2, 1, 3, 2, 1).atOffset(ZoneOffset.UTC))231 }232 "LocalDate shouldBe within(period, date)" {233 LocalDate.of(2014, 1, 2) shouldBe within(Period.ofDays(3), LocalDate.of(2014, 1, 1))234 LocalDate.of(2014, 1, 2) shouldBe within(Period.ofDays(3), LocalDate.of(2014, 1, 5))235 LocalDate.of(2014, 1, 2) shouldNotBe within(Period.ofDays(3), LocalDate.of(2014, 1, 6))236 LocalDate.of(2014, 1, 2).shouldBeWithin(Period.ofDays(3), LocalDate.of(2014, 1, 5))237 LocalDate.of(2014, 1, 2).shouldNotBeWithin(Period.ofDays(3), LocalDate.of(2014, 1, 6))238 }239 "LocalDateTime shouldBe within(period, date)" {240 LocalDateTime.of(2014, 1, 2, 4, 3, 2) shouldBe within(Period.ofDays(1), LocalDateTime.of(2014, 1, 2, 9, 3, 2))241 LocalDateTime.of(2014, 1, 2, 3, 2, 1) shouldNotBe within(Period.ofDays(1), LocalDateTime.of(2014, 1, 3, 3, 2, 2))242 LocalDateTime.of(2014, 1, 2, 4, 3, 2).shouldBeWithin(Period.ofDays(1), LocalDateTime.of(2014, 1, 2, 9, 3, 2))243 LocalDateTime.of(2014, 1, 2, 3, 2, 1).shouldNotBeWithin(Period.ofDays(1), LocalDateTime.of(2014, 1, 3, 3, 2, 2))244 }245 "ZonedDateTime shouldBe within(period, date)" {246 LocalDateTime.of(2014, 1, 2, 4, 3, 2).atZone(ZoneId.of("Z")) shouldBe within(Period.ofDays(1), LocalDateTime.of(2014, 1, 3, 4, 3, 2).atZone(ZoneId.of("Z")))247 LocalDateTime.of(2014, 1, 2, 4, 3, 2).atZone(ZoneId.of("Z")) shouldBe within(Period.ofDays(1), LocalDateTime.of(2014, 1, 1, 4, 3, 2).atZone(ZoneId.of("Z")))248 LocalDateTime.of(2014, 1, 2, 4, 3, 2).atZone(ZoneId.of("Z")) shouldBe within(Period.ofDays(1), LocalDateTime.of(2014, 1, 2, 9, 3, 2).atZone(ZoneId.of("Z")))249 LocalDateTime.of(2014, 1, 2, 3, 2, 1).atZone(ZoneId.of("Z")) shouldNotBe within(Period.ofDays(1), LocalDateTime.of(2014, 1, 3, 3, 2, 2).atZone(ZoneId.of("Z")))250 LocalDateTime.of(2014, 1, 2, 3, 2, 1).atZone(ZoneId.of("Z")) shouldNotBe within(Period.ofDays(1), LocalDateTime.of(2014, 1, 1, 3, 2, 0).atZone(ZoneId.of("Z")))251 LocalDateTime.of(2014, 1, 2, 4, 3, 2).atZone(ZoneId.of("Z")).shouldBeWithin(Period.ofDays(1), LocalDateTime.of(2014, 1, 3, 4, 3, 2).atZone(ZoneId.of("Z")))252 LocalDateTime.of(2014, 1, 2, 4, 3, 2).atZone(ZoneId.of("Z")).shouldBeWithin(Period.ofDays(1), LocalDateTime.of(2014, 1, 1, 4, 3, 2).atZone(ZoneId.of("Z")))253 LocalDateTime.of(2014, 1, 2, 4, 3, 2).atZone(ZoneId.of("Z")).shouldBeWithin(Period.ofDays(1), LocalDateTime.of(2014, 1, 2, 9, 3, 2).atZone(ZoneId.of("Z")))254 LocalDateTime.of(2014, 1, 2, 3, 2, 1).atZone(ZoneId.of("Z")).shouldNotBeWithin(Period.ofDays(1), LocalDateTime.of(2014, 1, 3, 3, 2, 2).atZone(ZoneId.of("Z")))255 LocalDateTime.of(2014, 1, 2, 3, 2, 1).atZone(ZoneId.of("Z")).shouldNotBeWithin(Period.ofDays(1), LocalDateTime.of(2014, 1, 1, 3, 2, 0).atZone(ZoneId.of("Z")))256 }257 "ZonedDateTime shouldBe within(duration, date)" {258 LocalDateTime.of(2014, 1, 2, 4, 3, 2).atZone(ZoneId.of("Z")) shouldBe within(Duration.ofDays(1), LocalDateTime.of(2014, 1, 3, 4, 3, 2).atZone(ZoneId.of("Z")))259 LocalDateTime.of(2014, 1, 2, 4, 3, 2).atZone(ZoneId.of("Z")) shouldBe within(Duration.ofDays(1), LocalDateTime.of(2014, 1, 1, 4, 3, 2).atZone(ZoneId.of("Z")))260 LocalDateTime.of(2014, 1, 2, 4, 3, 2).atZone(ZoneId.of("Z")) shouldBe within(Duration.ofDays(1), LocalDateTime.of(2014, 1, 2, 9, 3, 2).atZone(ZoneId.of("Z")))261 LocalDateTime.of(2014, 1, 2, 3, 2, 1).atZone(ZoneId.of("Z")) shouldNotBe within(Duration.ofDays(1), LocalDateTime.of(2014, 1, 3, 3, 2, 2).atZone(ZoneId.of("Z")))262 LocalDateTime.of(2014, 1, 2, 3, 2, 1).atZone(ZoneId.of("Z")) shouldNotBe within(Duration.ofDays(1), LocalDateTime.of(2014, 1, 1, 3, 2, 0).atZone(ZoneId.of("Z")))263 LocalDateTime.of(2014, 1, 2, 4, 3, 2).atZone(ZoneId.of("Z")).shouldBeWithin(Duration.ofDays(1), LocalDateTime.of(2014, 1, 3, 4, 3, 2).atZone(ZoneId.of("Z")))264 LocalDateTime.of(2014, 1, 2, 4, 3, 2).atZone(ZoneId.of("Z")).shouldBeWithin(Duration.ofDays(1), LocalDateTime.of(2014, 1, 1, 4, 3, 2).atZone(ZoneId.of("Z")))265 LocalDateTime.of(2014, 1, 2, 4, 3, 2).atZone(ZoneId.of("Z")).shouldBeWithin(Duration.ofDays(1), LocalDateTime.of(2014, 1, 2, 9, 3, 2).atZone(ZoneId.of("Z")))266 LocalDateTime.of(2014, 1, 2, 3, 2, 1).atZone(ZoneId.of("Z")).shouldNotBeWithin(Duration.ofDays(1), LocalDateTime.of(2014, 1, 3, 3, 2, 2).atZone(ZoneId.of("Z")))267 LocalDateTime.of(2014, 1, 2, 3, 2, 1).atZone(ZoneId.of("Z")).shouldNotBeWithin(Duration.ofDays(1), LocalDateTime.of(2014, 1, 1, 3, 2, 0).atZone(ZoneId.of("Z")))268 }269 "OffsetDateTime shouldBe within(period, date)" {270 LocalDateTime.of(2014, 1, 2, 4, 3, 2).atOffset(ZoneOffset.UTC) shouldBe within(Period.ofDays(1), LocalDateTime.of(2014, 1, 3, 4, 3, 2).atOffset(ZoneOffset.UTC))271 LocalDateTime.of(2014, 1, 2, 3, 2, 1).atOffset(ZoneOffset.UTC) shouldNotBe within(Period.ofDays(1), LocalDateTime.of(2014, 2, 1, 3, 2, 1).atOffset(ZoneOffset.UTC))272 LocalDateTime.of(2014, 1, 2, 4, 3, 2).atOffset(ZoneOffset.UTC).shouldBeWithin(Period.ofDays(1), LocalDateTime.of(2014, 1, 3, 4, 3, 2).atOffset(ZoneOffset.UTC))273 LocalDateTime.of(2014, 1, 2, 3, 2, 1).atOffset(ZoneOffset.UTC).shouldNotBeWithin(Period.ofDays(1), LocalDateTime.of(2014, 2, 1, 3, 2, 1).atOffset(ZoneOffset.UTC))274 }275 "OffsetDateTime shouldBe within(duration, date)" {276 LocalDateTime.of(2014, 1, 2, 4, 3, 2).atOffset(ZoneOffset.UTC) shouldBe within(Duration.ofDays(1), LocalDateTime.of(2014, 1, 3, 4, 3, 2).atOffset(ZoneOffset.UTC))277 LocalDateTime.of(2014, 1, 2, 3, 2, 1).atOffset(ZoneOffset.UTC) shouldNotBe within(Duration.ofDays(1), LocalDateTime.of(2014, 2, 1, 3, 2, 1).atOffset(ZoneOffset.UTC))278 LocalDateTime.of(2014, 1, 2, 4, 3, 2).atOffset(ZoneOffset.UTC).shouldBeWithin(Duration.ofDays(1), LocalDateTime.of(2014, 1, 3, 4, 3, 2).atOffset(ZoneOffset.UTC))279 LocalDateTime.of(2014, 1, 2, 3, 2, 1).atOffset(ZoneOffset.UTC).shouldNotBeWithin(Duration.ofDays(1), LocalDateTime.of(2014, 2, 1, 3, 2, 1).atOffset(ZoneOffset.UTC))280 }281 "LocalTime shouldBe between" {282 LocalTime.of(14, 20, 50, 1000).shouldBeBetween(LocalTime.of(14, 20, 49), LocalTime.of(14, 20, 51))283 LocalTime.of(14, 20, 50, 1000).shouldNotBeBetween(LocalTime.of(14, 20, 51), LocalTime.of(14, 20, 52))284 }285 "LocalDate shouldBe between" {286 LocalDate.of(2019, 2, 16).shouldBeBetween(LocalDate.of(2019, 2, 15), LocalDate.of(2019, 2, 17))287 LocalDate.of(2019, 2, 16).shouldNotBeBetween(LocalDate.of(2019, 2, 17), LocalDate.of(2019, 2, 18))288 }289 "LocalDateTime shouldBe between" {290 LocalDateTime.of(2019, 2, 16, 12, 0, 0).shouldBeBetween(LocalDateTime.of(2019, 2, 15, 12, 0, 0), LocalDateTime.of(2019, 2, 17, 12, 0, 0))291 LocalDateTime.of(2019, 2, 16, 12, 0, 0).shouldBeBetween(LocalDateTime.of(2019, 2, 16, 10, 0, 0), LocalDateTime.of(2019, 2, 16, 14, 0, 0))...
TaskScheduleSpec.kt
Source:TaskScheduleSpec.kt
...31 )32 fun after(): Instant = LocalTime.of(0, 50).atDate(LocalDate.now()).toInstant(ZoneOffset.UTC)33 "support updating scheduling" {34 val after = after()35 val within = Duration.ofMinutes(5)36 val schedule = TaskSchedule(task)37 schedule.task shouldBe (task)38 schedule.instances shouldBe (emptyMap())39 val updatedSchedule = schedule.update(after = after, within = within)40 updatedSchedule.task shouldBe (task)41 updatedSchedule.instances.size shouldBe (1)42 val instance = updatedSchedule.instances.values.first()43 instance.instant shouldBe (task.schedule.next(after = after, within = within).first())44 instance.postponed shouldBe (null)45 }46 "not update scheduling if a task is not active" {47 val after = after()48 val within = Duration.ofMinutes(5)49 val inactiveTask = task.copy(isActive = false)50 val schedule = TaskSchedule(inactiveTask)51 schedule.task shouldBe (inactiveTask)52 schedule.instances shouldBe (emptyMap())53 val updatedSchedule = schedule.update(after = after, within = within)54 updatedSchedule.task shouldBe (inactiveTask)55 schedule.instances shouldBe (emptyMap())56 }57 "not update scheduling if a task's next instance already exists" {58 val after = after()59 val within = Duration.ofMinutes(5)60 val schedule = TaskSchedule(task).update(after = after, within = within)61 schedule.task shouldBe (task)62 schedule.instances.size shouldBe (1)63 val updatedSchedule = schedule.update(after = after, within = within)64 updatedSchedule.task shouldBe (task)65 updatedSchedule.instances.size shouldBe (1)66 val instance = updatedSchedule.instances.values.first()67 instance.instant shouldBe (task.schedule.next(after = after, within = within).first())68 instance.postponed shouldBe (null)69 }70 "not update scheduling if a task's next instance was already dismissed (non-repeating schedules)" {71 val after = after()72 val within = Duration.ofMinutes(5)73 val updatedTask = task.copy(74 schedule = Task.Schedule.Once(75 instant = LocalTime.of(0, 5).atDate(LocalDate.now()).toInstant(ZoneOffset.UTC)76 )77 )78 val schedule = TaskSchedule(updatedTask).update(after = after, within = within)79 schedule.task shouldBe (updatedTask)80 schedule.instances.size shouldBe (1)81 val instance = schedule.instances.values.first()82 val updatedSchedule = schedule.dismiss(instance = instance.id)83 updatedSchedule.task shouldBe (updatedTask)84 updatedSchedule.instances shouldBe (emptyMap())85 val finalSchedule = updatedSchedule.update(after = after, within = within)86 finalSchedule.task shouldBe (updatedTask)87 finalSchedule.instances shouldBe (emptyMap())88 }89 "support providing the next task instance" {90 val after = after()91 val within = Duration.ofMinutes(5)92 val schedule = TaskSchedule(task).update(after = after, within = within)93 schedule.instances.size shouldBe (1)94 val instance = schedule.instances.values.first()95 when (val next = schedule.next(after = after).firstOrNull()) {96 null -> fail("Expected next task instance but none found")97 else -> {98 next.first shouldBe (instance)99 next.second.isAfter(after) shouldBe (true)100 }101 }102 val multiInstanceSchedule = schedule103 .update(after = after.plus(Duration.ofMinutes(15)), within = within)104 .update(after = after.plus(Duration.ofMinutes(25)), within = within)105 .update(after = after.plus(Duration.ofMinutes(45)), within = within)106 multiInstanceSchedule.instances.size shouldBe (3)107 when (val next = multiInstanceSchedule.next(after = after).firstOrNull()) {108 null -> fail("Expected next task instance but none found")109 else -> {110 next.first shouldBe (instance)111 next.second.isAfter(after) shouldBe (true)112 }113 }114 val dismissedInstanceSchedule = TaskSchedule(115 task = task,116 instances = emptyMap(),117 dismissed = multiInstanceSchedule.instances.map { it.value.instant }118 ).update(after = after.plus(Duration.ofMinutes(45)), within = within)119 when (val next = dismissedInstanceSchedule.next(after = after).firstOrNull()) {120 null -> fail("Expected next task instance but none found")121 else -> {122 val every = (task.schedule as Task.Schedule.Repeating).every123 val duration = (every as Task.Schedule.Repeating.Interval.DurationInterval).value124 next.first.instant shouldBe (instance.instant.plus(duration.multipliedBy(3)))125 next.second.isAfter(after) shouldBe (true)126 }127 }128 }129 "support dismissing task instances" {130 val after = after()131 val within = Duration.ofMinutes(5)132 val schedule = TaskSchedule(task).update(after = after, within = within)133 schedule.instances.size shouldBe (1)134 val instance = schedule.instances.keys.first()135 val updatedSchedule = schedule.dismiss(instance)136 updatedSchedule.instances shouldBe (emptyMap())137 }138 "fail to dismiss missing task instances" {139 val schedule = TaskSchedule(task)140 val e = shouldThrow<IllegalArgumentException> {141 schedule.dismiss(instance = UUID.randomUUID())142 }143 e.message should startWith("Cannot dismiss instance")144 }145 "support undoing dismissal of task instances" {146 val after = after()147 val within = Duration.ofMinutes(5)148 val schedule = TaskSchedule(task).update(after = after, within = within)149 schedule.instances.size shouldBe (1)150 val instance = schedule.instances.values.first()151 val updatedSchedule = schedule.dismiss(instance.id)152 updatedSchedule.instances shouldBe (emptyMap())153 updatedSchedule.dismissed shouldBe (listOf(instance.instant))154 val latestSchedule = updatedSchedule.undoDismiss(instance.instant)155 latestSchedule.instances.size shouldBe (1)156 latestSchedule.dismissed shouldBe (emptyList())157 val newInstance = latestSchedule.instances.values.first()158 newInstance.id shouldNotBe (instance.id)159 newInstance.instant shouldBe (instance.instant)160 }161 "fail to undo dismissal of missing task instants" {162 val schedule = TaskSchedule(task)163 val e = shouldThrow<IllegalArgumentException> {164 schedule.undoDismiss(instant = Instant.now())165 }166 e.message should startWith("Cannot undo dismissal")167 }168 "support postponing task instances" {169 val after = after()170 val within = Duration.ofMinutes(5)171 val schedule = TaskSchedule(task).update(after = after, within = within)172 schedule.instances.size shouldBe (1)173 val instance = schedule.instances.values.first()174 instance.postponed shouldBe (null)175 val postponedDuration = Duration.ofMinutes(2)176 val updatedSchedule = schedule.postpone(instance = instance.id, by = postponedDuration)177 updatedSchedule.instances.size shouldBe (1)178 val updatedInstance = updatedSchedule.instances.values.first()179 updatedInstance.postponed shouldBe (postponedDuration)180 val finalSchedule = updatedSchedule.postpone(instance = instance.id, by = postponedDuration)181 finalSchedule.instances.size shouldBe (1)182 val finalInstance = finalSchedule.instances.values.first()183 finalInstance.postponed shouldBe (postponedDuration.multipliedBy(2))184 }185 "fail to postpone missing task instances" {186 val schedule = TaskSchedule(task)187 val e = shouldThrow<IllegalArgumentException> {188 schedule.postpone(instance = UUID.randomUUID(), by = Duration.ofSeconds(1))189 }190 e.message should startWith("Cannot postpone instance")191 }192 "support schedule matching" {193 val after = after()194 val within = Duration.ofMinutes(5)195 val tolerance = Duration.ofSeconds(2)196 val schedule = TaskSchedule(task)197 schedule.match(198 instant = after,199 withTolerance = tolerance200 ) shouldBe (emptyList())201 val updatedSchedule = schedule.update(after = after, within = within)202 updatedSchedule.instances.size shouldBe (1)203 val instance = updatedSchedule.instances.values.first()204 val diff = Duration.between(after, instance.execution())205 updatedSchedule.match(206 instant = after.minus(diff).minusSeconds(1),207 withTolerance = tolerance208 ) shouldBe (listOf(TaskSchedule.Matched.None))209 updatedSchedule.match(210 instant = after.plus(diff).minus(task.contextSwitch).plusSeconds(1),211 withTolerance = tolerance212 ) shouldBe (listOf(TaskSchedule.Matched.ContextSwitch(instance)))213 updatedSchedule.match(214 instant = after.plus(diff),215 withTolerance = tolerance216 ) shouldBe (listOf(TaskSchedule.Matched.Instant(instance)))217 }218 "support adjusting scheduling when a task's schedule is updated" {219 val now = Instant.now()220 val within = Duration.ofMinutes(5)221 val schedule = TaskSchedule(task)222 schedule.task shouldBe (task)223 schedule.instances shouldBe (emptyMap())224 val updatedSchedule = schedule225 .update(after = now.minus(Duration.ofMinutes(25)), within = within)226 .update(after = now.minus(Duration.ofMinutes(45)), within = within)227 .update(after = now, within = within)228 updatedSchedule.task shouldBe (task)229 updatedSchedule.instances.size shouldBe (3)230 val updatedTask = task.copy(231 schedule = Task.Schedule.Repeating(232 start = LocalTime.of(0, 5).atDate(LocalDate.now()).toInstant(ZoneOffset.UTC),233 every = Duration.ofMinutes(21).toInterval()234 )235 )236 val scheduleWithUpdatedTask = updatedSchedule.withTask(updatedTask)237 scheduleWithUpdatedTask.task shouldBe (updatedTask)238 scheduleWithUpdatedTask.instances.size shouldBe (2)239 scheduleWithUpdatedTask.instances.values.forEach { instance ->240 instance.execution().isBefore(now) shouldBe (true)241 }242 }243 "support adjusting scheduling when a task's state is updated" {244 val now = Instant.now()245 val within = Duration.ofMinutes(5)246 val schedule = TaskSchedule(task)247 schedule.task shouldBe (task)248 schedule.instances shouldBe (emptyMap())249 val updatedSchedule = schedule250 .update(after = now.minus(Duration.ofMinutes(25)), within = within)251 .update(after = now.minus(Duration.ofMinutes(45)), within = within)252 .update(after = now, within = within)253 updatedSchedule.task shouldBe (task)254 updatedSchedule.instances.size shouldBe (3)255 val updatedTask = task.copy(isActive = false)256 val scheduleWithUpdatedTask = updatedSchedule.withTask(updatedTask)257 scheduleWithUpdatedTask.task shouldBe (updatedTask)258 scheduleWithUpdatedTask.instances.size shouldBe (2)259 scheduleWithUpdatedTask.instances.values.forEach { instance ->260 instance.execution().isBefore(now) shouldBe (true)261 }262 }263 "not adjust scheduling when a task's details are updated" {264 val now = Instant.now()265 val within = Duration.ofMinutes(5)266 val schedule = TaskSchedule(task)267 schedule.task shouldBe (task)268 schedule.instances shouldBe (emptyMap())269 val updatedSchedule = schedule270 .update(after = now.minus(Duration.ofMinutes(25)), within = within)271 .update(after = now.minus(Duration.ofMinutes(45)), within = within)272 .update(after = now, within = within)273 updatedSchedule.task shouldBe (task)274 updatedSchedule.instances.size shouldBe (3)275 val updatedTask = task.copy(276 id = 1,277 name = "other-name",278 description = "other-description",279 goal = "other-goal",280 contextSwitch = Duration.ofMinutes(42)281 )282 val scheduleWithUpdatedTask = updatedSchedule.withTask(updatedTask)283 scheduleWithUpdatedTask.task shouldBe (updatedTask)284 scheduleWithUpdatedTask.instances.size shouldBe (3)285 }286 }...
matchers.kt
Source:matchers.kt
...122 shouldNotBeEmpty()123 }124 // executable, hidden, readable, smaller, writeable, containFile, extension, path, ...125 LocalDate.now().shouldBeToday()126 // before/after, within, same, between, have year/month/day/hour/...127 LocalTime.now().shouldHaveSameHoursAs(LocalTime.now())128 // before/after/between, sameMinute/Seconds/Nanos129 }130 it("numbers") {131 1 shouldBeLessThan 2132 1 shouldBeLessThanOrEqual 1 // Int-based; returns this133 1 shouldBeLessThanOrEqualTo 1 // Comparble-based; void134 1 shouldBeEqualComparingTo 1 // Comparable-based135 1.shouldBeBetween(0, 2)136 1 shouldBeInRange 0..2137 0.shouldBeZero()138 1.shouldBePositive()139 1.shouldBeOdd()140 (1.2).shouldBe(1.20001.plusOrMinus(Percentage(20.0)))...
TaskSpec.kt
Source:TaskSpec.kt
...19class TaskSpec : WordSpec({20 "A Task" should {21 "support scheduling single-execution events" {22 val now = Instant.now()23 val within = Duration.ofMinutes(15)24 val expected = now.plus(Duration.ofSeconds(42))25 val schedule = Task.Schedule.Once(instant = expected)26 schedule.next(after = now, within = within) shouldBe (listOf(expected))27 }28 "support scheduling repeating events" {29 fun verify(hour: Int, minute: Int, duration: Long) {30 val now = Instant.now().truncatedTo(ChronoUnit.MINUTES)31 val within = Duration.ofMinutes(5)32 val originalTime = LocalTime.of(hour, minute)33 val originalSchedule = now.truncatedTo(ChronoUnit.DAYS).plusSeconds(34 originalTime.toSecondOfDay().toLong()35 )36 val repetitionDuration = Duration.ofMinutes(duration)37 val events = abs(Duration.between(now, originalSchedule).toMillis() / repetitionDuration.toMillis())38 val expectedNext = originalSchedule.plusSeconds(repetitionDuration.seconds * (events + 1))39 val futureSchedule = Task.Schedule.Repeating(40 start = originalTime.atDate(LocalDate.now()).toInstant(ZoneOffset.UTC),41 every = repetitionDuration.toInterval(),42 days = Task.Schedule.Repeating.DefaultDays43 )44 if (originalSchedule.isAfter(now)) {45 val next = futureSchedule.next(after = now, within = within)46 next.first() shouldBe (originalSchedule)47 next.withIndex().forEach { (i, instant) ->48 instant shouldBe originalSchedule.plusSeconds(repetitionDuration.seconds * i)49 }50 } else {51 val next = futureSchedule.next(after = now, within = within)52 next.first() shouldBe (expectedNext)53 next.withIndex().forEach { (i, instant) ->54 instant shouldBe expectedNext.plusSeconds(repetitionDuration.seconds * i)55 }56 }57 }58 for (hour in 0..23) {59 for (minute in 0..59) {60 for (duration in 1..120L) {61 withClue("Initial schedule at [$hour:$minute] with duration of [$duration] minutes") {62 verify(hour = hour, minute = minute, duration = duration)63 }64 }65 }66 }67 }68 "support scheduling repeating events on specific days only" {69 val today = ZonedDateTime.now()70 val tomorrow = today.plusDays(1)71 val within = Duration.ofDays(7)72 val schedule = Task.Schedule.Repeating(73 start = today.toInstant(),74 every = Period.ofDays(1).toInterval(),75 days = setOf(76 today.dayOfWeek,77 tomorrow.dayOfWeek78 )79 )80 val expected = 281 val next = schedule.next(after = schedule.start, within = within)82 next.size shouldBe (expected)83 next.first() shouldBe (tomorrow.toInstant())84 next.last() shouldBe (today.plusWeeks(1).toInstant())85 }86 "support scheduling repeating events with multiple interval unit types" {87 val now = Instant.now()88 fun nextInstant(forInterval: Task.Schedule.Repeating.Interval) =89 Task.Schedule.Repeating(start = now, every = forInterval)90 .next(now, within = Duration.ofMinutes(5)).first()91 nextInstant(forInterval = Duration.ofSeconds(1).toInterval()) shouldBe (now.plus(1, ChronoUnit.SECONDS))92 nextInstant(forInterval = Duration.ofMinutes(2).toInterval()) shouldBe (now.plus(2, ChronoUnit.MINUTES))93 nextInstant(forInterval = Duration.ofHours(3).toInterval()) shouldBe (now.plus(3, ChronoUnit.HOURS))94 nextInstant(forInterval = Period.ofDays(4).toInterval()) shouldBe (now.plus(4, ChronoUnit.DAYS))95 nextInstant(forInterval = Period.ofMonths(5).toInterval()) shouldBe (now.atZone(ZoneId.systemDefault())96 .plus(5, ChronoUnit.MONTHS).toInstant())97 nextInstant(forInterval = Period.ofYears(6).toInterval()) shouldBe (now.atZone(ZoneId.systemDefault())98 .plus(6, ChronoUnit.YEARS).toInstant())99 }100 "support duration-based intervals for repeating schedules" {101 val duration = Duration.ofSeconds(42)102 val scheduleInterval = duration.toInterval()103 (scheduleInterval is Task.Schedule.Repeating.Interval.DurationInterval) shouldBe (true)104 scheduleInterval.amount() shouldBe (duration)...
CreateTaskSpec.kt
Source:CreateTaskSpec.kt
1package family.haschka.wolkenschloss.gradle.ca2import family.haschka.wolkenschloss.gradle.ca.CaPlugin3import family.haschka.wolkenschloss.gradle.ca.TrustStore4import family.haschka.wolkenschloss.gradle.testbed.Directories5import io.kotest.assertions.throwables.shouldNotThrowAny6import io.kotest.assertions.throwables.shouldThrow7import io.kotest.core.spec.style.FunSpec8import io.kotest.engine.spec.tempdir9import io.kotest.engine.spec.tempfile10import io.kotest.extensions.system.withEnvironment11import io.kotest.matchers.date.shouldBeWithin12import io.kotest.matchers.file.shouldStartWithPath13import io.kotest.matchers.shouldBe14import org.bouncycastle.asn1.x500.X500Name15import org.bouncycastle.asn1.x500.style.BCStyle16import org.gradle.api.tasks.StopExecutionException17import org.gradle.kotlin.dsl.*18import org.gradle.testfixtures.ProjectBuilder19import java.time.Duration20import java.time.ZonedDateTime21class CreateTaskSpec : FunSpec({22 context("A project with create task") {23 withEnvironment(mapOf("XDG_DATA_HOME" to tempdir().path)) {24 val projectDir = tempdir()25 val project = ProjectBuilder.builder()26 .withProjectDir(projectDir)27 .withName(PROJECT_NAME)28 .build()29 project.pluginManager.apply(CaPlugin::class.java)30 test("should have a task named ${CaPlugin.CREATE_TASK_NAME}") {31 shouldNotThrowAny {32 project.tasks.named(CaPlugin.CREATE_TASK_NAME, TrustAnchor::class)33 }34 }35 test("should have a task names ${CaPlugin.TRUSTSTORE_TASK_NAME}") {36 shouldNotThrowAny {37 project.tasks.named(CaPlugin.TRUSTSTORE_TASK_NAME, TrustStore::class)38 }39 }40 test("certificate file defaults to \$XDG_DATA_HOME/wolkenschloss/ca/ca.crt") {41 val create = project.tasks.named(CaPlugin.CREATE_TASK_NAME, TrustAnchor::class.java)42 create.get().certificate.get().asFile shouldStartWithPath Directories.certificateAuthorityHome.resolve("ca.crt")43 }44 test("private key file defaults to \$XDG_DATA_HOME/wolkenschloss/ca/ca.key") {45 val create = project.tasks.named(CaPlugin.CREATE_TASK_NAME, TrustAnchor::class.java)46 create.get().privateKey.get().asFile shouldStartWithPath Directories.certificateAuthorityHome.resolve("ca.key")47 }48 test("The default for the start of validity is the current time") {49 val ca by project.tasks.existing(TrustAnchor::class)50 ca.get().notBefore.get().shouldBeWithin(Duration.ofSeconds(5), ZonedDateTime.now())51 }52 test("The default validity period is 5 years") {53 val ca by project.tasks.existing(TrustAnchor::class)54 ca.get().notAfter.get().shouldBeWithin(Duration.ofSeconds(5), ZonedDateTime.now().plusYears(10))55 }56 test("should have default subject") {57 val ca by project.tasks.existing(TrustAnchor::class)58 ca.get().subject.get() shouldBe CaPlugin.TRUST_ANCHOR_DEFAULT_SUBJECT59 }60 test("should stop execution if certificate already exists") {61 val certificate = tempfile()62 val create = project.tasks.create("crash", TrustAnchor::class.java)63 create.certificate.set(certificate)64 val exception = shouldThrow<StopExecutionException> {65 create.execute()66 }67 exception.message shouldBe "Certificate already exists"68 }69 test("should stop execution if private key already exists") {70 val create by project.tasks.creating(TrustAnchor::class) {71 notBefore.set(ZonedDateTime.now())72 notAfter.set(ZonedDateTime.now().plusYears(5))73 privateKey.set(tempfile())74 certificate.set(projectDir.resolve("build/ca/certificate"))75 }76 val exception = shouldThrow<StopExecutionException> {77 create.execute()78 }79 exception.message shouldBe "Private key already exists"80 }81 test("should customize subject") {82 val custom by project.tasks.registering(TrustAnchor::class) {83 subject {84 addRDN(BCStyle.CN, "Wolkenschloss Root CA")85 addRDN(BCStyle.OU, "Development")86 addRDN(BCStyle.O, "Wolkenschloss")87 addRDN(BCStyle.C, "DE")88 }89 }90 X500Name(custom.get().subject.get()) shouldBe X500Name(CaPlugin.TRUST_ANCHOR_DEFAULT_SUBJECT)91 }92 test("should customize subject with dsl") {93 val customDsl by project.tasks.registering(TrustAnchor::class) {94 subject {95 this[BCStyle.CN] = "Wolkenschloss Root CA"96 }97 }98 customDsl.get().subject.get() shouldBe "CN=Wolkenschloss Root CA"99 }100 }101 }102}) {103 companion object {104 const val PROJECT_NAME = "ca"105 }106}...
KuidTest.kt
Source:KuidTest.kt
...85 86 // Test the rollover behavior. It should reset the increment and delay 1ms.87 test("Increment Rollover")88 {89 // Todo: Is the JVM too slow to reliably test this to within 1ms?90 91 val time = Clock.System.now().toEpochMilliseconds()92 93 generator = discordGenerator(increment = 4095L, timestamp = time)94 95 val rollover = generator.next()96 97 rollover[Discord.increment].shouldBe(0)98 rollover[Discord.timestamp].toEpochMilliseconds().shouldBeGreaterThan(time) // Delayed 1ms99 }100 101 test("Parameter Range Exceptions")102 {103 shouldThrow<IllegalArgumentException>...
InMemoryEvictionRepositoryTest.kt
Source:InMemoryEvictionRepositoryTest.kt
...33 val repo = InMemoryEvictionRepository()34 val channelMessages = repo.getMessages("test", ZonedDateTime.now(), ZonedDateTime.now())35 channelMessages shouldBe emptyList()36 }37 should("return messages within the specific channel only") {38 val repo = InMemoryEvictionRepository()39 val now = ZonedDateTime.now()40 repo.addChannelMessage("chnl1", ChannelMessage("test1", false, "user1", now.plusSeconds(1), 0u, 0u, 0u))41 repo.addChannelMessage("chnl1", ChannelMessage("test2", false, "user2", now.plusSeconds(2), 0u, 0u, 0u))42 repo.addChannelMessage("chnl1", ChannelMessage("test3", false, "user1", now.plusSeconds(3), 0u, 0u, 0u))43 repo.addChannelMessage("chnl2", ChannelMessage("test4", false, "user3", now.plusSeconds(4), 0u, 0u, 0u))44 repo.addChannelMessage("chnl2", ChannelMessage("test5", false, "user3", now.plusSeconds(5), 0u, 0u, 0u))45 val channelMessages = repo.getMessages("chnl2", now, now.plusMinutes(1))46 val expectedChannelMessages = listOf(47 ChannelMessage("test5", false, "user3", now.plusSeconds(5), 0u, 0u, 0u),48 ChannelMessage("test4", false, "user3", now.plusSeconds(4), 0u, 0u, 0u),49 )50 channelMessages shouldContainAll expectedChannelMessages51 }...
DbUtils.kt
Source:DbUtils.kt
1package no.nav.amt.tiltak.test.database2import io.kotest.matchers.date.shouldBeWithin3import io.kotest.matchers.shouldNotBe4import java.time.Duration5import java.time.LocalDate6import java.time.LocalDateTime7import java.time.ZonedDateTime8object DbUtils {9 /**10 * A helping function as SQL Timestamp and LocalDateTime does not have the same precision11 */12 fun LocalDateTime.isEqualTo(other: LocalDateTime?): Boolean {13 if (other == null) {14 return false15 }16 return this.year == other.year17 && this.month == other.month18 && this.dayOfMonth == other.dayOfMonth19 && this.hour == other.hour20 && this.minute == other.minute21 && this.second == other.second22 }23 /**24 * Should be used to check equality, 1 second skew is allowed to work around different precision on milliseconds25 */26 infix fun ZonedDateTime.shouldBeEqualTo(expected: ZonedDateTime?) {27 expected shouldNotBe null28 expected!!.shouldBeWithin(Duration.ofSeconds(1), this)29 }30 infix fun ZonedDateTime.shouldBeCloseTo(expected: ZonedDateTime?) {31 expected shouldNotBe null32 expected!!.shouldBeWithin(Duration.ofSeconds(10), this)33 }34 fun LocalDate.isEqualTo(other: LocalDate?): Boolean {35 if (other == null) {36 return false37 }38 return this.year == other.year39 && this.month == other.month40 && this.dayOfMonth == other.dayOfMonth41 }42}...
within
Using AI Code Generation
1val date = LocalDate.of(2018, 1, 1)2date should beAfter(LocalDate.of(2017, 12, 31))3date should beAfterOrEqual(LocalDate.of(2017, 12, 31))4date should beAfterOrEqual(LocalDate.of(2018, 1, 1))5date should beBefore(LocalDate.of(2018, 1, 2))6date should beBeforeOrEqual(LocalDate.of(2018, 1, 2))7date should beBeforeOrEqual(LocalDate.of(2018, 1, 1))8date should beBetween(LocalDate.of(2017, 12, 31), LocalDate.of(2018, 1, 2))9date should beBetween(LocalDate.of(2018, 1, 1), LocalDate.of(2018, 1, 2))10date should beBetween(LocalDate.of(2017, 12, 31), LocalDate.of(2018, 1, 1))11date should beBetween(LocalDate.of(2018, 1, 1), LocalDate.of(2018, 1, 1))12date should beEqual(LocalDate.of(2018, 1, 1))13date should beIn(LocalDate.of(2017, 12, 31) .. LocalDate.of(2018, 1, 2))14date should beIn(LocalDate.of(2018, 1, 1) .. LocalDate.of(2018, 1, 2))15date should beIn(LocalDate.of(2017, 12, 31) .. LocalDate.of(2018, 1, 1))16date should beIn(LocalDate.of(2018, 1, 1) .. LocalDate.of(2018, 1, 1))17date should beIn(LocalDate.of(2017, 12, 31), LocalDate.of(2018, 1, 2))18date should beIn(LocalDate.of(2018, 1, 1), LocalDate.of(2018, 1, 2))19date should beIn(LocalDate.of(2017, 12, 31), LocalDate.of(2018, 1, 1))20date should beIn(LocalDate.of(2018, 1, 1), LocalDate.of(2018, 1, 1))21date should beIn(LocalDate.of(2017, 12, 31), LocalDate.of(2018,
within
Using AI Code Generation
1 val date = Date(2019, 1, 1, 0, 0, 0)2 date shouldBe before(Date(2019, 1, 1, 0, 0, 1))3 val date = Date(2019, 1, 1, 0, 0, 0)4 date shouldBe before(Date(2019, 1, 1, 0, 0, 1))5 val date = Date(2019, 1, 1, 0, 0, 0)6 date shouldBe before(Date(2019, 1, 1, 0, 0, 1))7 val date = Date(2019, 1, 1, 0, 0, 0)8 date shouldBe before(Date(2019, 1, 1, 0, 0, 1))9 val date = Date(2019, 1, 1, 0, 0, 0)10 date shouldBe before(Date(2019, 1, 1, 0, 0, 1))11 val date = Date(2019, 1, 1, 0, 0, 0)12 date shouldBe before(Date(2019, 1, 1, 0, 0, 1))13 val date = Date(2019, 1, 1, 0, 0, 0)14 date shouldBe before(Date(2019, 1, 1, 0, 0, 1))15 val date = Date(2019, 1, 1, 0, 0, 0)16 date shouldBe before(Date
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!