Best Kotest code snippet using io.kotest.matchers.date.localtime.after
DateMatchersTest.kt
Source:DateMatchersTest.kt
1package com.sksamuel.kotest.matchers.date2import io.kotest.assertions.shouldFail3import io.kotest.assertions.throwables.shouldThrow4import io.kotest.core.spec.style.StringSpec5import io.kotest.matchers.date.after6import io.kotest.matchers.date.atSameZone7import io.kotest.matchers.date.before8import io.kotest.matchers.date.haveSameDay9import io.kotest.matchers.date.haveSameHours10import io.kotest.matchers.date.haveSameInstantAs11import io.kotest.matchers.date.haveSameMinutes12import io.kotest.matchers.date.haveSameMonth13import io.kotest.matchers.date.haveSameNanos14import io.kotest.matchers.date.haveSameSeconds15import io.kotest.matchers.date.haveSameYear16import io.kotest.matchers.date.shouldBeAfter17import io.kotest.matchers.date.shouldBeBefore18import io.kotest.matchers.date.shouldBeBetween19import io.kotest.matchers.date.shouldBeToday20import io.kotest.matchers.date.shouldBeWithin21import io.kotest.matchers.date.shouldHaveDayOfMonth22import io.kotest.matchers.date.shouldHaveDayOfWeek23import io.kotest.matchers.date.shouldHaveDayOfYear24import io.kotest.matchers.date.shouldHaveHour25import io.kotest.matchers.date.shouldHaveMinute26import io.kotest.matchers.date.shouldHaveMonth27import io.kotest.matchers.date.shouldHaveNano28import io.kotest.matchers.date.shouldHaveSameDayAs29import io.kotest.matchers.date.shouldHaveSameHoursAs30import io.kotest.matchers.date.shouldHaveSameInstantAs31import io.kotest.matchers.date.shouldHaveSameMinutesAs32import io.kotest.matchers.date.shouldHaveSameMonthAs33import io.kotest.matchers.date.shouldHaveSameNanosAs34import io.kotest.matchers.date.shouldHaveSameSecondsAs35import io.kotest.matchers.date.shouldHaveSameYearAs36import io.kotest.matchers.date.shouldHaveSecond37import io.kotest.matchers.date.shouldNotBeAfter38import io.kotest.matchers.date.shouldNotBeBefore39import io.kotest.matchers.date.shouldNotBeBetween40import io.kotest.matchers.date.shouldNotBeToday41import io.kotest.matchers.date.shouldNotBeWithin42import io.kotest.matchers.date.shouldNotHaveSameDayAs43import io.kotest.matchers.date.shouldNotHaveSameHoursAs44import io.kotest.matchers.date.shouldNotHaveSameInstantAs45import io.kotest.matchers.date.shouldNotHaveSameMinutesAs46import 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))...
TaskScheduleSpec.kt
Source:TaskScheduleSpec.kt
...28 ),29 contextSwitch = Duration.ofMinutes(5),30 isActive = true31 )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)))141 (1.2).shouldNotBeNaN()142 }...
TaskSpec.kt
Source:TaskSpec.kt
...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())...
CaPluginTest.kt
Source:CaPluginTest.kt
1package family.haschka.wolkenschloss.gradle.ca2import family.haschka.wolkenschloss.testing.Template3import family.haschka.wolkenschloss.testing.createRunner4import io.kotest.assertions.assertSoftly5import io.kotest.core.spec.IsolationMode6import io.kotest.core.spec.style.FunSpec7import io.kotest.engine.spec.tempdir8import io.kotest.matchers.file.shouldBeReadable9import io.kotest.matchers.file.shouldContainFile10import io.kotest.matchers.file.shouldExist11import io.kotest.matchers.file.shouldNotBeWriteable12import io.kotest.matchers.ints.shouldBeGreaterThan13import io.kotest.matchers.shouldBe14import org.bouncycastle.asn1.x500.X500Name15import org.bouncycastle.asn1.x509.KeyUsage16import org.gradle.testkit.runner.TaskOutcome17import java.time.LocalDate18import java.time.LocalTime19import java.time.ZoneOffset20import java.time.ZonedDateTime21import java.util.*22class CaPluginTest : FunSpec({23 autoClose(Template("ca")).withClone {24 context("A project using com.github.wolkenschloss.ca gradle plugin") {25 val xdgDataHome = tempdir()26 val environment = mapOf("XDG_DATA_HOME" to xdgDataHome.absolutePath)27 context("executing ca task") {28 val result = createRunner()29 .withArguments(CaPlugin.CREATE_TASK_NAME)30 .withEnvironment(environment)31 .build()32 test("should be successful") {33 result.task(":${CaPlugin.CREATE_TASK_NAME}")!!.outcome shouldBe TaskOutcome.SUCCESS34 }35 test("should create self signed root certificate") {36 assertSoftly(CertificateWrapper.read(xdgDataHome.resolve("wolkenschloss/ca/ca.crt"))) {37 x509Certificate.basicConstraints shouldBeGreaterThan -138 x509Certificate.basicConstraints shouldBe Int.MAX_VALUE39 keyUsage.hasUsages(KeyUsage.keyCertSign) shouldBe true40 issuer shouldBe X500Name(CaPlugin.TRUST_ANCHOR_DEFAULT_SUBJECT)41 subject shouldBe X500Name(CaPlugin.TRUST_ANCHOR_DEFAULT_SUBJECT)42 }43 }44 test("should create read only certificate") {45 assertSoftly(xdgDataHome.resolve("wolkenschloss/ca/ca.crt")) {46 shouldBeReadable()47 shouldNotBeWriteable()48 }49 }50 test("should create readonly private key") {51 assertSoftly(xdgDataHome.resolve("wolkenschloss/ca/ca.key")) {52 shouldNotBeWriteable()53 shouldBeReadable()54 readPrivateKey().algorithm shouldBe "RSA"55 }56 }57 }58 context("executing truststore task") {59 val result = createRunner()60 .withArguments(CaPlugin.TRUSTSTORE_TASK_NAME)61 .withEnvironment(environment)62 .build()63 test("should execute successfully") {64 result.task(":${CaPlugin.TRUSTSTORE_TASK_NAME}")!!.outcome shouldBe TaskOutcome.SUCCESS65 }66 test("should create truststore file") {67 xdgDataHome.resolve("wolkenschloss/ca/ca.jks").shouldExist()68 }69 }70 test("should customize validity") {71 val start = ZonedDateTime.of(72 LocalDate.of(2022, 2, 4),73 LocalTime.MIDNIGHT,74 ZoneOffset.UTC75 )76 val end = ZonedDateTime.of(77 LocalDate.of(2027, 2, 4),78 LocalTime.MIDNIGHT,79 ZoneOffset.UTC80 )81 val result = createRunner()82 .withArguments(CaPlugin.CREATE_TASK_NAME, "-DnotBefore=$start", "-DnotAfter=$end")83 .withEnvironment(environment)84 .build()85 result.task(":${CaPlugin.CREATE_TASK_NAME}")!!.outcome shouldBe TaskOutcome.SUCCESS86 val certificate = xdgDataHome.resolve("wolkenschloss/ca/ca.crt")87 .readX509Certificate()88 assertSoftly(certificate) {89 notBefore.toUtc() shouldBe start90 notAfter.toUtc() shouldBe end91 }92 }93 test("should create output in user defined location") {94 val result = createRunner()95 .withArguments("createInUserDefinedLocation")96 .withEnvironment(environment)97 .build()98 result.task(":createInUserDefinedLocation")!!.outcome shouldBe TaskOutcome.SUCCESS99 assertSoftly(workingDirectory.resolve("build/ca")) {100 shouldContainFile("ca.crt")101 shouldContainFile("ca.key")102 }103 }104 }105 }106}) {107 override fun isolationMode(): IsolationMode = IsolationMode.InstancePerLeaf108}109private fun Date.toUtc(): ZonedDateTime {110 return ZonedDateTime.ofInstant(this.toInstant(), ZoneOffset.UTC)111}...
HotelControllerTest.kt
Source:HotelControllerTest.kt
1package hotel2import arrow.core.Either3import hotel.exception.RoomNotExistException4import hotel.model.Booking5import io.kotest.assertions.arrow.core.shouldBeRight6import io.kotest.assertions.throwables.shouldThrowExactly7import io.kotest.inspectors.forExactly8import io.kotest.matchers.date.shouldBeAfter9import io.kotest.matchers.should10import io.kotest.matchers.string.include11import kotlinx.coroutines.DelicateCoroutinesApi12import kotlinx.coroutines.GlobalScope13import kotlinx.coroutines.async14import kotlinx.coroutines.runBlocking15import org.junit.jupiter.api.Test16import java.time.Clock17import java.time.LocalDate18import java.time.ZoneId19private const val THREADS = 100020private val DATE = LocalDate.of(2021, 11, 7)21private val zoneId = ZoneId.systemDefault()22private val clockYesterday = Clock.fixed(DATE.minusDays(1).atStartOfDay().atZone(zoneId).toInstant(), zoneId)23@DelicateCoroutinesApi24internal class HotelControllerTest {25 @Test26 fun clock() {27 val clock = Clock.systemDefaultZone()28 val instant = clock.instant()29 Thread.sleep(1)30 clock.instant() shouldBeAfter instant31 }32 @Test33 fun wrongRoomSize() {34 shouldThrowExactly<IllegalArgumentException> {35 HotelController(-1)36 }37 shouldThrowExactly<IllegalArgumentException> {38 HotelController(0)39 }40 }41 @Test42 fun bookingRoomNotExist() {43 shouldThrowExactly<RoomNotExistException> {44 val c = getController(1)45 c.bookRoom(DATE, 201, "guest")46 }.message should include("201 not exist")47 }48 @Test49 fun bookingFromPast() {50 shouldThrowExactly<BookingDateNotValidException> {51 val c = getController(1)52 c.bookRoom(LocalDate.MIN, 201, "guest")53 }.message should include(LocalDate.MIN.toString())54 }55 @Test56 fun bookingNullCheck() {57 shouldThrowExactly<NullPointerException> {58 Booking(null, 1, DATE)59 }.message should include("guestName")60 shouldThrowExactly<NullPointerException> {61 Booking("null", 1, null as LocalDate?)62 }.message should include("date")63 shouldThrowExactly<NullPointerException> {64 Booking("null", 1, null as String?)65 }.message should include("date")66 }67 @Test68 fun bookRoomConcurrency() = bookRoomsConcurrency(1)69 @Test70 fun book2RoomsConcurrency() = bookRoomsConcurrency(2)71 @Test72 fun bookXRoomsConcurrency() = repeat(3) { bookRoomsConcurrency((3..50).random(), (1..7).random()) }73 private fun bookRoomsConcurrency(74 size: Int,75 days: Int = 1,76 runConcurrently: (function: (Int) -> Either<Throwable, Any>) -> List<Either<Throwable, Any>> = ::run77 ) {78 val c = getController(size)79 val eitherList = runConcurrently { i: Int ->80// println("Thread $i start at: ${LocalTime.now()}")81 val plusDays = i / size % days82 val room = i % size + 183 val guestName = "guest-$i"84// println("$plusDays, $room, $guestName")85 Either.catch { c.bookRoom(DATE.plusDays(plusDays.toLong()), room, guestName) }86// .also { println("Thread $i end at: ${LocalTime.now()}") }87// .also { println("Result: $it") }88 }89 eitherList.forExactly(size * days) { it.shouldBeRight() }90 }91 private fun getController(size: Int): HotelController = HotelController(size).also { it.clock = clockYesterday }92 private fun run(createEither: (Int) -> Either<Throwable, Any>): List<Either<Throwable, Any>> {93 val deferredList = (1..THREADS).map { i -> GlobalScope.async { createEither(i) } }94 return runBlocking { deferredList.map { it.await() } }95 }96}...
ScheduleTest.kt
Source:ScheduleTest.kt
...22class ScheduleTest : FreeSpec({23 val eightOClock = LocalTime.of(8, 0)24 val today = LocalDate.now()25 "A schedule should" - {26 "prevent that beginning is after end" - {27 assertThrows<BeginningOfScheduleCannotBeAfterEnd> {28 Schedule(29 today,30 eightOClock.plusHours(1),31 eightOClock32 )33 }34 }35 "allow to check if it overlaps with another schedule" - {36 val tomorrow = LocalDate.now().plusDays(1)37 val inAnHour = eightOClock.plusHours(1)38 val halfAnHourAgo = eightOClock.minusMinutes(30)39 val oneHourAgo = eightOClock.minusHours(1)40 val firstSchedule = Schedule(today, eightOClock, inAnHour)...
TimeRangeTest.kt
Source:TimeRangeTest.kt
...27 describe("not overlaps") {28 it("range A before range B") {29 TimeRange(time1, time2).overlaps(TimeRange(time3, time4))shouldBe false30 }31 it("range A after range B") {32 TimeRange(time3, time4).overlaps(TimeRange(time1, time2)) shouldBe false33 }34 }35 describe("overlaps") {36 it("range A equals range B") {37 TimeRange(time1, time2).overlaps(TimeRange(time1, time2)) shouldBe true38 }39 it("range A before range B") {40 TimeRange(time1, time3).overlaps(TimeRange(time2, time4)) shouldBe true41 }42 it("range A after range B") {43 TimeRange(time2, time4).overlaps(TimeRange(time1, time3)) shouldBe true44 }45 it("range A bigger range B") {46 TimeRange(time1, time4).overlaps(TimeRange(time2, time3)) shouldBe true47 }48 it("range A smaller range B") {49 TimeRange(time2, time3).overlaps(TimeRange(time1, time4)) shouldBe true50 }51 }52})
after
Using AI Code Generation
1 fun beBefore(expected: LocalDateTime) = object : Matcher<LocalDateTime> {2 override fun test(value: LocalDateTime) = MatcherResult(3 value.isBefore(expected),4 { "LocalDateTime $value should be before $expected" },5 { "LocalDateTime $value should not be before $expected" }6 }7 fun beAfter(expected: LocalDateTime) = object : Matcher<LocalDateTime> {8 override fun test(value: LocalDateTime) = MatcherResult(9 value.isAfter(expected),10 { "LocalDateTime $value should be after $expected" },11 { "LocalDateTime $value should not be after $expected" }12 }13}14 fun beBefore(expected: LocalDate) = object : Matcher<LocalDate> {15 override fun test(value: LocalDate) = MatcherResult(16 value.isBefore(expected),17 { "LocalDate $value should be before $expected" },18 { "LocalDate $value should not be before $expected" }19 }20 fun beAfter(expected: LocalDate) = object : Matcher<LocalDate> {21 override fun test(value: LocalDate) = MatcherResult(22 value.isAfter(expected),23 { "LocalDate $value should be after $expected" },24 { "LocalDate $value should not be after $expected" }25 }26}27 fun beBefore(expected: LocalTime) = object : Matcher<LocalTime> {28 override fun test(value: LocalTime) = MatcherResult(29 value.isBefore(expected),30 { "LocalTime $value should be before $expected" },31 { "LocalTime $value should not be before $expected" }32 }33 fun beAfter(expected: LocalTime) = object : Matcher<LocalTime> {34 override fun test(value: LocalTime) = MatcherResult(35 value.isAfter(expected),36 { "LocalTime $value should be after $expected" },37 { "LocalTime $value should not be after $expected" }38 }39}40 fun beBefore(expected: OffsetDateTime) = object
after
Using AI Code Generation
1 fun `test LocalDateTimeMatchers`() {2 val now = LocalDateTime.now()3 now should beBefore(LocalDateTime.now().plusMinutes(1))4 now should beAfter(LocalDateTime.now().minusMinutes(1))5 now should beBetween(LocalDateTime.now().minusMinutes(1), LocalDateTime.now().plusMinutes(1))6 now should beBetweenInclusive(LocalDateTime.now().minusMinutes(1), LocalDateTime.now().plusMinutes(1))7 }8}9 fun `test LocalDateMatchers`() {10 val now = LocalDate.now()11 now should beBefore(LocalDate.now().plusDays(1))12 now should beAfter(LocalDate.now().minusDays(1))13 now should beBetween(LocalDate.now().minusDays(1), LocalDate.now().plusDays(1))14 now should beBetweenInclusive(LocalDate.now().minusDays(1), LocalDate.now().plusDays(1))15 }16}17 fun `test LocalTimeMatchers`() {18 val now = LocalTime.now()19 now should beBefore(LocalTime.now().plusMinutes(1))20 now should beAfter(LocalTime.now().minusMinutes(1))21 now should beBetween(LocalTime.now().minusMinutes(1), LocalTime.now().plusMinutes(1))22 now should beBetweenInclusive(LocalTime.now().minusMinutes(1), LocalTime.now().plusMinutes(1))23 }24}25 fun `test OffsetDateTimeMatchers`() {26 val now = OffsetDateTime.now()27 now should beBefore(OffsetDateTime.now().plusMinutes(1))28 now should beAfter(OffsetDateTime.now().minusMinutes(1))29 now should beBetween(OffsetDateTime.now().minusMinutes(1), OffsetDateTime.now().plusMinutes(1))30 now should beBetweenInclusive(OffsetDateTime.now().minusMinutes(1), OffsetDateTime.now().plusMinutes(1))31 }32}33 fun `test OffsetTimeMatchers`() {34 val now = OffsetTime.now()
after
Using AI Code Generation
1@Deprecated("Use LocalDateTimeMatchers.beCloseTo instead. This will be removed in 4.0", ReplaceWith("LocalDateTimeMatchers.beCloseTo(expected, offset)"))2@Deprecated("Use LocalDateTimeMatchers.beCloseTo instead. This will be removed in 4.0", ReplaceWith("LocalDateTimeMatchers.beCloseTo(expected, offset, mode)"))3@Deprecated("Use LocalDateTimeMatchers.beCloseTo instead. This will be removed in 4.0", ReplaceWith("LocalDateTimeMatchers.beCloseTo(expected, offset, mode, precision)"))4@Deprecated("Use LocalDateTimeMatchers.beCloseTo instead. This will be removed in 4.0", ReplaceWith("LocalDateTimeMatchers.beCloseTo(expected, offset, mode, precision, toleranceMode)"))5@Deprecated("Use LocalDateTimeMatchers.beCloseTo instead. This will be removed in 4.0", ReplaceWith("LocalDateTimeMatchers.beCloseTo(expected, offset, mode, precision, toleranceMode, tolerance)"))6@Deprecated("Use LocalDateTimeMatchers.beCloseTo instead. This will be removed in 4.0", ReplaceWith("LocalDateTimeMatchers.beCloseTo(expected, offset, mode, precision, toleranceMode, tolerance, description)"))7@Deprecated("Use LocalDateTimeMatchers.beCloseTo instead. This will be removed in 4.0", ReplaceWith("LocalDateTimeMatchers.beCloseTo(expected, offset, mode, precision, toleranceMode, tolerance, description, ignoreDefaultOffset)"))8@Deprecated("Use LocalDateTimeMatchers.beCloseTo instead. This will be removed in 4.0", ReplaceWith("LocalDateTimeMatchers.beCloseTo(expected, offset, mode, precision, toleranceMode, tolerance, description, ignoreDefaultOffset, ignoreDefaultZone)"))9@Deprecated("Use LocalDateTimeMatchers.beCloseTo instead. This will be removed in 4.0", ReplaceWith("LocalDateTimeMatchers.beCloseTo(expected, offset, mode, precision, toleranceMode, tolerance, description, ignoreDefaultOffset, ignoreDefaultZone, ignoreDefaultZoneOffset)"))10@Deprecated("Use LocalDateTimeMatchers.beCloseTo instead. This will be removed in 4.0", ReplaceWith("LocalDateTimeMatchers.beCloseTo(expected, offset, mode, precision, toleranceMode, tolerance, description, ignoreDefaultOffset, ignoreDefaultZone, ignoreDefaultZoneOffset, ignoreDefaultZoneOffset)"))11@Deprecated("Use LocalDateTimeMatchers.beCloseTo instead. This will be removed in 4.0", ReplaceWith("12}13 fun `test OffsetDateTimeMatchers`() {14 val now = OffsetDateTime.now()15 now should beBefore(OffsetDateTime.now().plusMinutes(1))16 now should beAfter(OffsetDateTime.now().minusMinutes(1))17 now should beBetween(OffsetDateTime.now().minusMinutes(1), OffsetDateTime.now().plusMinutes(1))18 now should beBetweenInclusive(OffsetDateTime.now().minusMinutes(1), OffsetDateTime.now().plusMinutes(1))19 }20}21 fun `test OffsetTimeMatchers`() {22 val now = OffsetTime.now()
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!!