How to use Arb.Companion.instant method of io.kotest.property.arbitrary.dates class

Best Kotest code snippet using io.kotest.property.arbitrary.dates.Arb.Companion.instant

dates.kt

Source:dates.kt Github

copy

Full Screen

1package io.kotest.property.arbitrary2import io.kotest.property.Arb3import java.time.Instant4import java.time.LocalDate5import java.time.LocalDateTime6import java.time.LocalTime7import java.time.Period8import java.time.Year.isLeap9import java.time.YearMonth10import java.time.temporal.ChronoUnit11import java.time.temporal.TemporalQueries.localDate12import java.time.temporal.TemporalQueries.localTime13import kotlin.random.Random14/**15 * Arberates a random [Period]s.16 *17 * This generator creates randomly generated Periods, with years less than or equal to [maxYear].18 *19 * If [maxYear] is 0, only random months and days will be generated.20 *21 * Months will always be in range [0..11]22 * Days will always be in range [0..31]23 */24fun Arb.Companion.period(maxYear: Int = 10): Arb<Period> = arbitrary(listOf(Period.ZERO)) {25 Period.of(26 (0..maxYear).random(it.random),27 (0..11).random(it.random),28 (0..31).random(it.random)29 )30}31fun Arb.Companion.localDate() = Arb.Companion.localDate(LocalDate.of(1970, 1, 1), LocalDate.of(2030, 12, 31))32/**33 * Arberates a stream of random LocalDates34 *35 * This generator creates randomly generated LocalDates, in the range [[minDate, maxDate]].36 *37 * If any of the years in the range contain a leap year, the date [29/02/YEAR] will always be a constant value of this38 * generator.39 *40 * @see [localDateTime]41 * @see [localTime]42 */43fun Arb.Companion.localDate(44 minDate: LocalDate = LocalDate.of(1970, 1, 1),45 maxDate: LocalDate = LocalDate.of(2030, 12, 31)46): Arb<LocalDate> {47 val leapYears = (minDate.year..maxDate.year).filter { isLeap(it.toLong()) }48 val february28s = leapYears.map { LocalDate.of(it, 2, 28) }49 val february29s = february28s.map { it.plusDays(1) }50 return arbitrary(february28s + february29s + minDate + maxDate) {51 minDate.plusDays(it.random.nextLong(ChronoUnit.DAYS.between(minDate, maxDate)))52 }.filter { it in minDate..maxDate }53}54/**55 * Arberates a stream of random LocalTimes56 *57 * This generator creates randomly generated LocalTimes.58 *59 * @see [localDateTime]60 * @see [localDate]61 */62fun Arb.Companion.localTime(): Arb<LocalTime> = arbitrary(listOf(LocalTime.of(23, 59, 59), LocalTime.of(0, 0, 0))) {63 LocalTime.of(it.random.nextInt(24), it.random.nextInt(60), it.random.nextInt(60))64}65/**66 * Arberates a stream of random LocalDateTimes67 *68 * This generator creates randomly generated LocalDates, in the range [[minYear, maxYear]].69 *70 * If any of the years in the range contain a leap year, the date [29/02/YEAR] will always be a constant value of this71 * generator.72 *73 * @see [localDateTime]74 * @see [localTime]75 */76fun Arb.Companion.localDateTime(77 minYear: Int? = null,78 maxYear: Int79): Arb<LocalDateTime> {80 return localDateTime(minYear ?: 1970, maxYear)81}82/**83 * Arberates a stream of random LocalDateTimes84 *85 * This generator creates randomly generated LocalDates, in the range [[minYear, maxYear]].86 *87 * If any of the years in the range contain a leap year, the date [29/02/YEAR] will always be a constant value of this88 * generator.89 *90 * @see [localDateTime]91 * @see [localTime]92 */93fun Arb.Companion.localDateTime(94 minYear: Int,95 maxYear: Int? = null96): Arb<LocalDateTime> {97 return localDateTime(minYear, maxYear ?: 2030)98}99/**100 * Arberates a stream of random LocalDateTimes101 *102 * This generator creates randomly generated LocalDates, in the range [[minYear, maxYear]].103 *104 * If any of the years in the range contain a leap year, the date [29/02/YEAR] will always be a constant value of this105 * generator.106 *107 * @see [localDateTime]108 * @see [localTime]109 */110fun Arb.Companion.localDateTime(111 minYear: Int,112 maxYear: Int113): Arb<LocalDateTime> {114 return localDateTime(115 minLocalDateTime = LocalDateTime.of(minYear, 1, 1, 0, 0),116 maxLocalDateTime = LocalDateTime.of(maxYear, 12, 31, 23, 59)117 )118}119/**120 * Arberates a stream of random LocalDateTimes121 *122 * This generator creates randomly generated LocalDates, in the range [[minLocalDateTime, maxLocalDateTime]].123 *124 * If any of the years in the range contain a leap year, the date [29/02/YEAR] will always be a constant value of this125 * generator.126 *127 * @see [localDateTime]128 * @see [localTime]129 */130fun Arb.Companion.localDateTime(131 minLocalDateTime: LocalDateTime = LocalDateTime.of(1970, 1, 1, 0, 0),132 maxLocalDateTime: LocalDateTime = LocalDateTime.of(2030, 12, 31, 23, 59)133): Arb<LocalDateTime> {134 return arbitrary(135 edgecaseFn = {136 generateSequence {137 val date = localDate(minLocalDateTime.toLocalDate(), maxLocalDateTime.toLocalDate()).edgecase(it)138 val time = localTime().edgecase(it)139 if (date == null || time == null) null else date.atTime(time)140 }.find { !it.isBefore(minLocalDateTime) && !it.isAfter(maxLocalDateTime) }141 },142 sampleFn = {143 generateSequence {144 val date = localDate(minLocalDateTime.toLocalDate(), maxLocalDateTime.toLocalDate()).single(it)145 val time = localTime().single(it)146 date.atTime(time)147 }.first { !it.isBefore(minLocalDateTime) && !it.isAfter(maxLocalDateTime) }148 }149 )150}151/**152 * Arberates a stream of random YearMonth153 *154 * If any of the years in the range contain a leap year, the date [02/YEAR] will always be a constant value of this155 * generator.156 *157 * This generator creates randomly generated YearMonth, in the range [[minYearMonth, maxYearMonth]].158 *159 * @see [yearMonth]160 */161fun Arb.Companion.yearMonth(162 minYearMonth: YearMonth = YearMonth.of(1970, 1),163 maxYearMonth: YearMonth = YearMonth.of(2030, 12)164): Arb<YearMonth> {165 val leapYears = (minYearMonth.year..maxYearMonth.year).filter { isLeap(it.toLong()) }166 val february = leapYears.map { YearMonth.of(it, 2) }167 return arbitrary(february + minYearMonth + maxYearMonth) {168 minYearMonth.plusMonths(it.random.nextLong(ChronoUnit.MONTHS.between(minYearMonth, maxYearMonth)))169 }.filter { it in minYearMonth..maxYearMonth }170}171typealias InstantRange = ClosedRange<Instant>172fun InstantRange.random(random: Random): Instant {173 try {174 val seconds = (start.epochSecond..endInclusive.epochSecond).random(random)175 val nanos = when {176 seconds == start.epochSecond && seconds == endInclusive.epochSecond -> start.nano..endInclusive.nano177 seconds == start.epochSecond -> start.nano..999_999_999178 seconds == endInclusive.epochSecond -> 0..endInclusive.nano179 else -> 0..999_999_999180 }.random(random)181 return Instant.ofEpochSecond(seconds, nanos.toLong())182 } catch (e: IllegalArgumentException) {183 throw NoSuchElementException(e.message)184 }185}186/**187 * Arberates a stream of random [Instant]188 */189fun Arb.Companion.instant(range: InstantRange): Arb<Instant> =190 arbitrary(listOf(range.start, range.endInclusive)) {191 range.random(it.random)192 }193/**194 * Arberates a stream of random [Instant]195 */196fun Arb.Companion.instant(197 minValue: Instant = Instant.MIN,198 maxValue: Instant = Instant.MAX199) = instant(minValue..maxValue)...

Full Screen

Full Screen

Arb.Companion.instant

Using AI Code Generation

copy

Full Screen

1val arbInstant = Arb.instant()2val arbLocalDate = Arb.localDate()3val arbLocalDateTime = Arb.localDateTime()4val arbLocalTime = Arb.localTime()5val arbMonthDay = Arb.monthDay()6val arbMonths = Arb.months()7val arbOffsetDateTime = Arb.offsetDateTime()8val arbOffsetTime = Arb.offsetTime()9val arbPeriod = Arb.period()10val arbYear = Arb.year()11val arbYearMonth = Arb.yearMonth()12val arbYears = Arb.years()13val arbZonedDateTime = Arb.zonedDateTime()14val arbZoneId = Arb.zoneId()15val arbZoneOffset = Arb.zoneOffset()16val arbZoneRegion = Arb.zoneRegion()17val arbZoneRules = Arb.zoneRules()

Full Screen

Full Screen

Arb.Companion.instant

Using AI Code Generation

copy

Full Screen

1val arb = Arb.dates().instant()2val arb = Arb.dates().localDate()3val arb = Arb.dates().localDateTime()4val arb = Arb.dates().localTime()5val arb = Arb.dates().monthDay()6val arb = Arb.dates().year()7val arb = Arb.dates().yearMonth()8val arb = Arb.dates().zoneId()9val arb = Arb.dates().zoneOffset()10val arb = Arb.dates().zonedDateTime()11val arb = Arb.dates().zonedOffsetDateTime()12val arb = Arb.dates().zonedOffsetTime()13val arb = Arb.dates().zonedTime()14val arb = Arb.dates().dates()15val arb = Arb.dates().instant()16val arb = Arb.dates().localDate()

Full Screen

Full Screen

Arb.Companion.instant

Using AI Code Generation

copy

Full Screen

1import io.kotest.property.arbitrary.dates2import java.time.Instant3import java.time.LocalDate4import java.time.LocalDateTime5import java.time.LocalTime6import java.time.Month7import java.time.ZoneId8import java.time.ZonedDateTime9import java.util.Date10import java.util.GregorianCalendar11import java.util.TimeZone12import java.util.concurrent.TimeUnit13import java.util.concurrent.atomic.AtomicLong14import kotlin.test.Test15import kotlin.test.assertEquals16import kotlin.test.assertTrue17class ArbDatesTest {18fun `instant should return a valid instant`() {19val instant = Arb.instant().next()20val date = Date.from(instant)21assertEquals(instant, date.toInstant())22}23fun `localDate should return a valid local date`() {24val date = Arb.localDate().next()25val gregorian = GregorianCalendar(TimeZone.getTimeZone("UTC"))26gregorian.set(date.year, date.monthValue - 1, date.dayOfMonth)27assertEquals(date, LocalDate.of(date.year, date.month, date.dayOfMonth))28}29fun `localTime should return a valid local time`() {30val time = Arb.localTime().next()31val gregorian = GregorianCalendar(TimeZone.getTimeZone("UTC"))32gregorian.set(1970, 0, 1, time.hour, time.minute, time.second)33val nanos = TimeUnit.MILLISECONDS.toNanos(millis)34val expected = LocalTime.of(time.hour, time.minute, time.second, time.nano)35assertEquals(expected, time)36}37fun `localDateTime should return a valid local date time`() {38val dateTime = Arb.localDateTime().next()39val gregorian = GregorianCalendar(TimeZone.getTimeZone("UTC"))40gregorian.set(dateTime.year, dateTime.monthValue - 1, dateTime.dayOfMonth, dateTime.hour, dateTime.minute, dateTime.second)41val nanos = TimeUnit.MILLISECONDS.toNanos(millis)42val expected = LocalDateTime.of(dateTime.year, dateTime.month, dateTime.dayOfMonth, dateTime.hour, dateTime.minute, dateTime.second, dateTime.nano)43assertEquals(expected, dateTime)44}45fun `zonedDateTime should return a valid zoned date time`() {46val zonedDateTime = Arb.zonedDateTime().next()47val date = Date.from(zonedDateTime.toInstant())48val expected = ZonedDateTime.ofInstant(date.toInstant(), ZoneId.of(zonedDateTime.zone.id))49assertEquals(expected, zonedDateTime)50}51}

Full Screen

Full Screen

Arb.Companion.instant

Using AI Code Generation

copy

Full Screen

1import io.kotest.property.arbitrary.dates.instant2import io.kotest.property.arbitrary.dates.localDate3import io.kotest.property.arbitrary.dates.localDateTime4import io.kotest.property.arbitrary.dates.localTime5import io.kotest.property.arbitrary.dates.monthDay6import io.kotest.property.arbitrary.dates.month7import io.kotest.property.arbitrary.dates.offsetDateTime8import io.kotest.property.arbitrary.dates.offsetTime9import io.kotest.property.arbitrary.dates.yearMonth10import io.kotest.property.arbitrary.dates.year11import io.kotest.property.arbitrary.dates.zonedDateTime12val zonedDateTimeArb = zonedDateTime()

Full Screen

Full Screen

Arb.Companion.instant

Using AI Code Generation

copy

Full Screen

1 val arbInstant = instant()2 val arbInstantBetween = instant(min = Instant.now(), max = Instant.now().plusSeconds(1000))3 val arbLocalDate = localDate()4 val arbLocalDateBetween = localDate(min = LocalDate.now(), max = LocalDate.now().plusDays(1000))5 val arbLocalDateTime = localDateTime()6 val arbLocalDateTimeBetween = localDateTime(min = LocalDateTime.now(), max = LocalDateTime.now().plusDays(1000))7 val arbLocalTime = localTime()8 val arbLocalTimeBetween = localTime(min = LocalTime.now(), max = LocalTime.now().plusHours(1000))9 val arbMonthDay = monthDay()10 val arbMonthDayBetween = monthDay(min = MonthDay.now(), max = MonthDay.now().plusDays(1000))11 val arbOffsetDateTime = offsetDateTime()12 val arbOffsetDateTimeBetween = offsetDateTime(min = OffsetDateTime.now(), max = OffsetDateTime.now().plusDays(1000))13 val arbOffsetTime = offsetTime()14 val arbOffsetTimeBetween = offsetTime(min = OffsetTime.now(), max = OffsetTime.now().plusHours(1000))15 val arbYear = year()16 val arbYearBetween = year(min = Year.now(), max = Year.now().plusYears(1000))17 val arbYearMonth = yearMonth()18 val arbYearMonthBetween = yearMonth(min = YearMonth.now(), max = YearMonth.now().plusMonths(1000))

Full Screen

Full Screen

Arb.Companion.instant

Using AI Code Generation

copy

Full Screen

1val arb = Arb.Companion.instant()2val prop = forAll(arb) { it is Instant }3prop.check(PropTestConfig(invocations = 1000))4val arb = Arb.Companion.duration()5val prop = forAll(arb) { it is Duration }6prop.check(PropTestConfig(invocations = 1000))7val arb = Arb.Companion.localDate()8val prop = forAll(arb) { it is LocalDate }9prop.check(PropTestConfig(invocations = 1000))10val arb = Arb.Companion.localTime()11val prop = forAll(arb) { it is LocalTime }12prop.check(PropTestConfig(invocations = 1000))13val arb = Arb.Companion.localDateTime()14val prop = forAll(arb) { it is LocalDateTime }15prop.check(PropTestConfig(invocations = 1000))16val arb = Arb.Companion.zonedDateTime()17val prop = forAll(arb) { it is ZonedDateTime }18prop.check(PropTestConfig(invocations = 1000))19val arb = Arb.Companion.offsetDateTime()20val prop = forAll(arb) { it is OffsetDateTime }21prop.check(PropTestConfig(invocations = 1000))22val arb = Arb.Companion.offsetTime()23val prop = forAll(arb) { it is OffsetTime }24prop.check(PropTestConfig(invocations = 1000))25val arb = Arb.Companion.zoneId()26val prop = forAll(arb) { it is ZoneId }27prop.check(PropTestConfig(invocations = 1000))

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Kotest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful