Best Kotest code snippet using io.kotest.matchers.date.instant.between
TaskScheduleSpec.kt
Source:TaskScheduleSpec.kt  
...200            ) 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" {...TaskSpec.kt
Source:TaskSpec.kt  
...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)...TidspunktTest.kt
Source:TidspunktTest.kt  
...17    private val instant = Instant.parse("1970-01-01T01:01:01.123456789Z")18    @Test19    fun `truncate instant to same format as repo, precision in micros`() {20        val tidspunkt = instant.toTidspunkt()21        ChronoUnit.MICROS.between(instant, tidspunkt) shouldBe 022        instant.toString().length.shouldNotBeLessThan(tidspunkt.toString().length)23        tidspunkt.nano % 1000 shouldBe 024        val addedMicrosInstant = instant.plus(251, ChronoUnit.MICROS)25        val addedMicrosTidspunkt = addedMicrosInstant.toTidspunkt()26        ChronoUnit.MICROS.between(addedMicrosInstant, addedMicrosTidspunkt) shouldBe 027        addedMicrosInstant.toString().length.shouldNotBeLessThan(addedMicrosTidspunkt.toString().length)28        addedMicrosTidspunkt.nano % 1000 shouldBe 029        val addedNanosInstant = instant.plusNanos(378)30        val addedNanosTidspunkt = addedNanosInstant.toTidspunkt()31        (addedNanosInstant.nano - addedNanosTidspunkt.nano).shouldBeBetween(1, 1000)32        addedNanosInstant shouldNotBe addedNanosTidspunkt.instant33        addedNanosTidspunkt shouldBe addedNanosInstant34        ChronoUnit.MICROS.between(addedNanosInstant, addedNanosTidspunkt) shouldBe 035        addedNanosInstant.toString().length.shouldNotBeLessThan(addedNanosTidspunkt.toString().length)36        addedNanosTidspunkt.nano % 1000 shouldBe 037    }38    @Test39    fun `should equal instant truncated to same precision`() {40        val instant = instant.plusNanos(515)41        val tidspunkt = instant.toTidspunkt()42        instant shouldNotBe tidspunkt.instant43        instant.truncatedTo(Tidspunkt.unit) shouldBe tidspunkt.instant44        tidspunkt shouldBe instant45        tidspunkt shouldBe instant.truncatedTo(Tidspunkt.unit)46        val othertidspunkt = instant.toTidspunkt()47        tidspunkt shouldBe othertidspunkt48        tidspunkt shouldBe tidspunkt...ItemRepositoryTestOld.kt
Source:ItemRepositoryTestOld.kt  
...83        items shouldHaveSize 284        items shouldNotContain listOf(db[0], db[1])85    }86    @Test87    fun `should read all items between dates`() = runBlocking<Unit> {88        val db = (1L..5L).map { tokenId ->89            val i = itemRepository.coSave(createItem(tokenId = tokenId))90            delay(10)91            i92        }93        val before = db[2].updatedAt94        val after = db[0].updatedAt95        val items = itemRepository.search(96            ItemFilter.All(lastUpdatedFrom = after, lastUpdatedTo = before),97            null,98            null,99            ItemFilter.Sort.LAST_UPDATE100        ).asFlow().toList()101        items shouldHaveSize 2...timestampTest.kt
Source:timestampTest.kt  
...48      "timestamp of current instance should not be before the another timestamp of same instance" {49         val nowInstance = Instant.now()50         Timestamp.from(nowInstance) shouldNotBeBefore Timestamp.from(nowInstance)51      }52      "current timestamp should be between timestamp of past and future" {53         val nowInstant = Instant.now()54         val currentTimestamp = Timestamp.from(nowInstant)55         val pastTimestamp = Timestamp.from(nowInstant.minusMillis(5000))56         val futureTimestamp = Timestamp.from(nowInstant.plusMillis(5000))57         currentTimestamp.shouldBeBetween(pastTimestamp, futureTimestamp)58      }59      "past timestamp should not be between timestamp of current instant and future" {60         val nowInstant = Instant.now()61         val currentTimestamp = Timestamp.from(nowInstant)62         val pastTimestamp = Timestamp.from(nowInstant.minusMillis(5000))63         val futureTimestamp = Timestamp.from(nowInstant.plusMillis(5000))64         pastTimestamp.shouldNotBeBetween(currentTimestamp, futureTimestamp)65      }66      "future timestamp should not be between timestamp of current instant and future" {67         val nowInstant = Instant.now()68         val currentTimestamp = Timestamp.from(nowInstant)69         val pastTimestamp = Timestamp.from(nowInstant.minusMillis(5000))70         val futureTimestamp = Timestamp.from(nowInstant.plusMillis(5000))71         futureTimestamp.shouldNotBeBetween(pastTimestamp, currentTimestamp)72      }73   }74}...instantMatcherTest.kt
Source:instantMatcherTest.kt  
...48      }49      "instant of same time should not be after another instant of same time" {50         Instant.ofEpochMilli(30000) shouldNotBeAfter Instant.ofEpochMilli(30000)51      }52      "current instant should be between past instant and future instant" {53         val currentInstant = Instant.now()54         val pastInstant = currentInstant.minusMillis(30000)55         val futureInstant = currentInstant.plusMillis(30000)56         currentInstant.shouldBeBetween(pastInstant, futureInstant)57      }58      "past instant should not be between current instant and future instant" {59         val currentInstant = Instant.now()60         val pastInstant = currentInstant.minusMillis(30000)61         val futureInstant = currentInstant.plusMillis(30000)62         pastInstant.shouldNotBeBetween(currentInstant, futureInstant)63      }64      "future instant should not be between past instant and current instant" {65         val currentInstant = Instant.now()66         val pastInstant = currentInstant.minusMillis(30000)67         val futureInstant = currentInstant.plusMillis(30000)68         futureInstant.shouldNotBeBetween(pastInstant, currentInstant)69      }70   }71}...LatestBTest.kt
Source:LatestBTest.kt  
1package com.painkillergis.fall_color_history.snapshot2import com.painkillergis.fall_color_history.util.BFunSpec3import com.painkillergis.fall_color_history.util.toJsonObject4import io.kotest.matchers.date.shouldBeBetween5import io.kotest.matchers.shouldBe6import io.ktor.client.call.*7import io.ktor.client.request.*8import io.ktor.client.statement.*9import io.ktor.http.*10import kotlinx.serialization.json.JsonObject11import kotlinx.serialization.json.JsonPrimitive12import java.time.Instant13import java.time.format.DateTimeFormatter14class LatestBTest : BFunSpec({ httpClient ->15  afterEach {16    httpClient.delete<Unit>("/snapshots")17  }18  test("no latest") {19    httpClient.get<HttpResponse>("/snapshots/latest").apply {20      status shouldBe HttpStatusCode.OK21      receive<JsonObject>() shouldBe mapOf(22        "timestamp" to "",23        "content" to emptyMap<String, Unit>(),24      ).toJsonObject()25    }26  }27  test("replace latest") {28    val latest = mapOf("locations" to emptyList<Unit>())29    httpClient.put<HttpResponse>("/snapshots/latest") {30      contentType(ContentType.Application.Json)31      body = latest32    }.apply {33      status shouldBe HttpStatusCode.NoContent34    }35    httpClient.get<HttpResponse>("/snapshots/latest").apply {36      status shouldBe HttpStatusCode.OK37      receive<JsonObject>().apply {38        (get("timestamp") as JsonPrimitive)39          .content40          .let { Instant.from(DateTimeFormatter.ISO_OFFSET_DATE_TIME.parse(it)) }41          .shouldBeBetween(42            Instant.now().minusSeconds(5),43            Instant.now().plusSeconds(5),44          )45        get("content") shouldBe latest46      }47    }48  }49})...CreateTrialUserImplTest.kt
Source:CreateTrialUserImplTest.kt  
1package com.falcon.falcon.core.usecase.trial2import com.falcon.falcon.core.entity.User3import com.falcon.falcon.core.enumeration.UserType4import com.falcon.falcon.core.usecase.user.CreateUserUseCase5import io.kotest.matchers.date.shouldBeBetween6import io.kotest.matchers.shouldBe7import io.kotest.matchers.string.shouldBeUUID8import io.kotest.matchers.types.shouldBeTypeOf9import io.mockk.clearAllMocks10import io.mockk.every11import io.mockk.mockk12import org.junit.jupiter.api.BeforeEach13import org.junit.jupiter.api.Test14import org.junit.jupiter.api.TestInstance15import java.time.Instant16import java.time.temporal.ChronoUnit17@TestInstance(TestInstance.Lifecycle.PER_CLASS)18internal class CreateTrialUserImplTest {19    private val createUserUseCase: CreateUserUseCase = mockk()20    private val trialDuration = 1L21    private val underTest: CreateTrialUserImpl = CreateTrialUserImpl(createUserUseCase, trialDuration)22    @BeforeEach23    fun init() {24        clearAllMocks()25    }26    @Test27    fun `Should generate random user`() {28        // Given29        every { createUserUseCase.execute(any()) } returnsArgument 030        // When31        val result = underTest.execute()32        // Then33        result.shouldBeTypeOf<User>()34        result.username.shouldBeUUID()35        result.password.shouldBeUUID()36        result.type.shouldBe(UserType.TRIAL)37        result.expirationDate?.shouldBeBetween(38            Instant.now().plus(50, ChronoUnit.MINUTES),39            Instant.now().plus(70, ChronoUnit.MINUTES),40        )41    }42}...between
Using AI Code Generation
1Instant . now ( ) . shouldBeBetween ( Instant . now ( ) . minus ( 1 , ChronoUnit . DAYS ) , Instant . now ( ) )2LocalDate . now ( ) . shouldBeBetween ( LocalDate . now ( ) . minus ( 1 , ChronoUnit . DAYS ) , LocalDate . now ( ) )3LocalDateTime . now ( ) . shouldBeBetween ( LocalDateTime . now ( ) . minus ( 1 , ChronoUnit . DAYS ) , LocalDateTime . now ( ) )4LocalTime . now ( ) . shouldBeBetween ( LocalTime . now ( ) . minus ( 1 , ChronoUnit . DAYS ) , LocalTime . now ( ) )5OffsetDateTime . now ( ) . shouldBeBetween ( OffsetDateTime . now ( ) . minus ( 1 , ChronoUnit . DAYS ) , OffsetDateTime . now ( ) )6OffsetTime . now ( ) . shouldBeBetween ( OffsetTime . now ( ) . minus ( 1 , ChronoUnit . DAYS ) , OffsetTime . now ( ) )7ZonedDateTime . now ( ) . shouldBeBetween ( ZonedDateTime . now ( ) . minus ( 1 , ChronoUnit . DAYS ) , ZonedDateTime . now ( ) )8ZonedOffset . now ( ) . shouldBeBetween ( ZonedOffset . now ( ) . minus ( 1 , ChronoUnit . DAYS ) , ZonedOffset . now ( ) )9ZonedTime . now ( ) . shouldBeBetween ( ZonedTime . now ( ) . minus ( 1 , ChronoUnit . DAYS ) , ZonedTime . now ( ) )10listOf ( 1 , 2 ,between
Using AI Code Generation
1val instant = Instant.now()2instant.shouldBeBefore(Instant.now().plus(1, ChronoUnit.DAYS))3instant.shouldBeBeforeOrEqual(Instant.now().plus(1, ChronoUnit.DAYS))4instant.shouldBeAfter(Instant.now().minus(1, ChronoUnit.DAYS))5instant.shouldBeAfterOrEqual(Instant.now().minus(1, ChronoUnit.DAYS))6instant.shouldBeBetween(Instant.now().minus(1, ChronoUnit.DAYS), Instant.now().plus(1, ChronoUnit.DAYS))7instant.shouldNotBeBetween(Instant.now().minus(2, ChronoUnit.DAYS), Instant.now().minus(1, ChronoUnit.DAYS))8instant.shouldBeToday()9instant.shouldBeYesterday()10instant.shouldBeTomorrow()11instant.shouldBeThisYear()12instant.shouldBeThisMonth()13instant.shouldBeThisWeek()14instant.shouldBeThisDay()15instant.shouldBeThisHour()16instant.shouldBeThisMinute()17instant.shouldBeThisSecond()18instant.shouldBeThisMillisecond()19instant.shouldBeTodayOrBefore()20instant.shouldBeYesterdayOrBefore()21instant.shouldBeTomorrowOrAfter()22instant.shouldBeThisYearOrBefore()23instant.shouldBeThisMonthOrBefore()24instant.shouldBeThisWeekOrBefore()25instant.shouldBeThisDayOrBefore()26instant.shouldBeThisHourOrBefore()27instant.shouldBeThisMinuteOrBefore()28instant.shouldBeThisSecondOrBefore()29instant.shouldBeThisMillisecondOrBefore()30instant.shouldBeTodayOrAfter()31instant.shouldBeYesterdayOrAfter()32instant.shouldBeTomorrowOrAfter()33instant.shouldBeThisYearOrAfter()34instant.shouldBeThisMonthOrAfter()35instant.shouldBeThisWeekOrAfter()36instant.shouldBeThisDayOrAfter()37instant.shouldBeThisHourOrAfter()38instant.shouldBeThisMinuteOrAfter()39instant.shouldBeThisSecondOrAfter()40instant.shouldBeThisMillisecondOrAfter()41instant.shouldBeThisYearOrToday()42instant.shouldBeThisMonthOrToday()43instant.shouldBeThisWeekOrToday()44instant.shouldBeThisDayOrToday()45instant.shouldBeThisHourOrToday()46instant.shouldBeThisMinuteOrToday()47instant.shouldBeThisSecondOrToday()48instant.shouldBeThisMillisecondOrToday()49instant.shouldBeThisYearOrYesterday()50instant.shouldBeThisMonthOrYesterday()51instant.shouldBeThisWeekOrYesterday()52instant.shouldBeThisDayOrYesterday()53instant.shouldBeThisHourOrYesterday()54instant.shouldBeThisMinuteOrYesterday()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!!
