How to use Arb.map method of io.kotest.property.arbitrary.map class

Best Kotest code snippet using io.kotest.property.arbitrary.map.Arb.map

GenHelper.kt

Source:GenHelper.kt Github

copy

Full Screen

1/*2 * Copyright (c) 2020 sparetimedevs and respective authors and developers.3 *4 * Licensed under the Apache License, Version 2.0 (the "License");5 * you may not use this file except in compliance with the License.6 * You may obtain a copy of the License at7 *8 * http://www.apache.org/licenses/LICENSE-2.09 *10 * Unless required by applicable law or agreed to in writing, software11 * distributed under the License is distributed on an "AS IS" BASIS,12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13 * See the License for the specific language governing permissions and14 * limitations under the License.15 */16package com.sparetimedevs.pofpaf.test.generator17import arrow.core.Either18import arrow.core.left19import arrow.core.right20import com.microsoft.azure.functions.HttpMethod21import com.sparetimedevs.pofpaf.test.implementation.general.log.Level22import io.kotest.property.Arb23import io.kotest.property.Exhaustive24import io.kotest.property.arbitrary.bool25import io.kotest.property.arbitrary.byte26import io.kotest.property.arbitrary.choice27import io.kotest.property.arbitrary.create28import io.kotest.property.arbitrary.double29import io.kotest.property.arbitrary.element30import io.kotest.property.arbitrary.file31import io.kotest.property.arbitrary.float32import io.kotest.property.arbitrary.int33import io.kotest.property.arbitrary.localDate34import io.kotest.property.arbitrary.localDateTime35import io.kotest.property.arbitrary.localTime36import io.kotest.property.arbitrary.long37import io.kotest.property.arbitrary.map38import io.kotest.property.arbitrary.next39import io.kotest.property.arbitrary.period40import io.kotest.property.arbitrary.short41import io.kotest.property.arbitrary.string42import io.kotest.property.arbitrary.uuid43import io.kotest.property.exhaustive.exhaustive44import java.net.URI45fun Arb.Companion.suspendFunThatReturnsEitherAnyOrAnyOrThrows(): Arb<suspend () -> Either<Any, Any>> =46 choice(47 suspendFunThatReturnsAnyRight(),48 suspendFunThatReturnsAnyLeft(),49 suspendFunThatThrows()50 )51fun Arb.Companion.suspendFunThatReturnsEitherAnyOrUnitOrThrows(): Arb<suspend () -> Either<Any, Unit>> =52 choice(53 suspendFunThatReturnsUnitRight() as Arb<suspend () -> Either<Any, Unit>>,54 suspendFunThatReturnsAnyLeft() as Arb<suspend () -> Either<Any, Unit>>,55 suspendFunThatThrows() as Arb<suspend () -> Either<Any, Unit>>56 )57fun Arb.Companion.suspendFunThatReturnsUnitRight(): Arb<suspend () -> Either<Any, Unit>> =58 unit().map { suspend { it.right() } }59fun Arb.Companion.suspendFunThatReturnsAnyRight(): Arb<suspend () -> Either<Any, Any>> =60 any().map { suspend { it.right() } }61fun Arb.Companion.suspendFunThatReturnsAnyLeft(): Arb<suspend () -> Either<Any, Any>> =62 any().map { suspend { it.left() } }63fun Arb.Companion.suspendFunThatThrows(): Arb<suspend () -> Either<Any, Any>> =64 throwable().map { suspend { throw it } } as Arb<suspend () -> Either<Any, Any>>65fun Arb.Companion.suspendFunThatThrowsFatalThrowable(): Arb<suspend () -> Either<Any, Any>> =66 fatalThrowable().map { suspend { throw it } } as Arb<suspend () -> Either<Any, Any>>67fun Arb.Companion.throwable(): Arb<Throwable> =68 element(69 Exception(),70 RuntimeException(),71 IllegalArgumentException(),72 IllegalStateException(),73 IndexOutOfBoundsException(),74 UnsupportedOperationException(),75 ArithmeticException(),76 NumberFormatException(),77 NullPointerException(),78 ClassCastException(),79 AssertionError(),80 NoSuchElementException(),81 ConcurrentModificationException()82 )83fun Arb.Companion.fatalThrowable(): Arb<Throwable> =84 element(85 MyVirtualMachineError(),86 ThreadDeath(),87 InterruptedException(),88 LinkageError()89 )90class MyVirtualMachineError : VirtualMachineError()91fun Arb.Companion.any(): Arb<Any> =92 choice(93 string() as Arb<Any>,94 int() as Arb<Any>,95 short() as Arb<Any>,96 long() as Arb<Any>,97 float() as Arb<Any>,98 double() as Arb<Any>,99 bool() as Arb<Any>,100 byte() as Arb<Any>,101 uuid() as Arb<Any>,102 file() as Arb<Any>,103 localDate() as Arb<Any>,104 localTime() as Arb<Any>,105 localDateTime() as Arb<Any>,106 period() as Arb<Any>,107 throwable() as Arb<Any>,108 fatalThrowable() as Arb<Any>,109 mapOfStringAndStringGenerator() as Arb<Any>,110 uri() as Arb<Any>,111 httpMethod() as Arb<Any>,112 unit() as Arb<Any>113 )114fun Arb.Companion.unit(): Arb<Unit> =115 create { Unit }116fun Arb.Companion.mapOfStringAndStringGenerator(): Arb<Map<String, String>> =117 element(118 listOf(119 emptyMap(),120 mapOf(121 string().next() to string().next()122 ),123 mapOf(124 string().next() to string().next(),125 string().next() to string().next()126 ),127 mapOf(128 string().next() to string().next(),129 string().next() to string().next(),130 string().next() to string().next()131 ),132 mapOf(133 string().next() to string().next(),134 string().next() to string().next(),135 string().next() to string().next(),136 string().next() to string().next(),137 string().next() to string().next(),138 string().next() to string().next()139 ),140 mapOf(141 string().next() to string().next(),142 string().next() to string().next(),143 string().next() to string().next(),144 string().next() to string().next(),145 string().next() to string().next(),146 string().next() to string().next(),147 string().next() to string().next(),148 string().next() to string().next(),149 string().next() to string().next(),150 string().next() to string().next(),151 string().next() to string().next(),152 string().next() to string().next()153 ),154 mapOf(155 string().next() to string().next(),156 string().next() to string().next(),157 string().next() to string().next(),158 string().next() to string().next(),159 string().next() to string().next(),160 string().next() to string().next(),161 string().next() to string().next(),162 string().next() to string().next(),163 string().next() to string().next(),164 string().next() to string().next(),165 string().next() to string().next(),166 string().next() to string().next(),167 string().next() to string().next(),168 string().next() to string().next(),169 string().next() to string().next(),170 string().next() to string().next(),171 string().next() to string().next(),172 string().next() to string().next(),173 string().next() to string().next(),174 string().next() to string().next(),175 string().next() to string().next(),176 string().next() to string().next(),177 string().next() to string().next(),178 string().next() to string().next()179 )180 )181 )182fun Arb.Companion.uri(): Arb<URI> =183 element(184 listOf(185 URI.create("https://sparetimedevs.com"),186 URI.create("https://www.sparetimedevs.com"),187 URI.create("https://something.sparetimedevs.com"),188 URI.create("https://something.sparetimedevs.com/another/thing"),189 URI.create("https://something.sparetimedevs.com/another/thing?query=param")190 )191 )192fun Arb.Companion.httpMethod(): Arb<HttpMethod> =193 element(194 listOf(195 HttpMethod.GET,196 HttpMethod.HEAD,197 HttpMethod.POST,198 HttpMethod.PUT,199 HttpMethod.DELETE,200 HttpMethod.CONNECT,201 HttpMethod.OPTIONS,202 HttpMethod.TRACE203 )204 )205fun Exhaustive.Companion.logLevel(): Exhaustive<Level> =206 listOf(207 Level.INFO,208 Level.DEBUG,209 Level.WARN,210 Level.ERROR211 ).exhaustive()212fun Arb.Companion.stringOrNull(): Arb<String?> =213 choice(214 string() as Arb<String?>,215 create { null } as Arb<String?>216 )...

Full Screen

Full Screen

KotestHelpers.kt

Source:KotestHelpers.kt Github

copy

Full Screen

1package io.provenance.scope.loan.test2import com.google.protobuf.InvalidProtocolBufferException3import com.google.protobuf.Timestamp4import com.google.protobuf.util.Timestamps5import io.kotest.matchers.Matcher6import io.kotest.matchers.MatcherResult7import io.kotest.matchers.should8import io.kotest.matchers.shouldBe9import io.kotest.matchers.types.beInstanceOf10import io.kotest.property.Arb11import io.kotest.property.arbitrary.Codepoint12import io.kotest.property.arbitrary.UUIDVersion13import io.kotest.property.arbitrary.alphanumeric14import io.kotest.property.arbitrary.bind15import io.kotest.property.arbitrary.boolean16import io.kotest.property.arbitrary.filter17import io.kotest.property.arbitrary.filterNot18import io.kotest.property.arbitrary.int19import io.kotest.property.arbitrary.list20import io.kotest.property.arbitrary.long21import io.kotest.property.arbitrary.map22import io.kotest.property.arbitrary.pair23import io.kotest.property.arbitrary.set24import io.kotest.property.arbitrary.string25import io.kotest.property.arbitrary.uInt26import io.kotest.property.arbitrary.uuid27import io.provenance.scope.loan.utility.ContractEnforcement28import io.provenance.scope.loan.utility.ContractViolation29import io.provenance.scope.loan.utility.ContractViolationException30import io.provenance.scope.loan.utility.ContractViolationMap31import io.provenance.scope.loan.utility.UnexpectedContractStateException32import tech.figure.servicing.v1beta1.LoanStateOuterClass.LoanStateMetadata33import java.time.Instant34import tech.figure.util.v1beta1.Checksum as FigureTechChecksum35import tech.figure.util.v1beta1.UUID as FigureTechUUID36/**37 * Generators of [Arb]itrary instances.38 */39internal object LoanPackageArbs {40 /* Primitives */41 val anyNonEmptyString: Arb<String> = Arb.string().filter { it.isNotBlank() }42 val anyNonUuidString: Arb<String> = Arb.string().filterNot { it.length == 36 }43 val anyUli: Arb<String> = Arb.string(minSize = 23, maxSize = 45, codepoints = Codepoint.alphanumeric()) // TODO: Is this correct?44 val anyNonUliString: Arb<String> = Arb.string().filterNot { it.length in 23..45 } // TODO: Should be complement of anyUli45 /* Contract requirements */46 val anyContractViolation: Arb<ContractViolation> = Arb.string()47 val anyContractEnforcement: Arb<ContractEnforcement> = Arb.bind(48 Arb.boolean(),49 Arb.string(),50 ) { requirement, violationReport ->51 ContractEnforcement(requirement, violationReport)52 }53 val anyContractViolationMap: Arb<ContractViolationMap> = Arb.bind(54 Arb.list(anyContractViolation),55 Arb.list(Arb.uInt()),56 ) { violationList, countList ->57 violationList.zip(countList).toMap().toMutableMap()58 }59 /* Protobufs */60 val anyValidChecksum: Arb<FigureTechChecksum> = Arb.bind(61 anyNonEmptyString,62 Arb.string(),63 ) { checksumValue, algorithmType ->64 FigureTechChecksum.newBuilder().apply {65 checksum = checksumValue66 algorithm = algorithmType67 }.build()68 }69 val anyUuid: Arb<FigureTechUUID> = Arb.uuid(UUIDVersion.V4).map { arbUuidV4 ->70 FigureTechUUID.newBuilder().apply {71 value = arbUuidV4.toString()72 }.build()73 }74 val anyValidTimestamp: Arb<Timestamp> = anyTimestampComponents.map { (seconds, nanoSeconds) ->75 Timestamp.newBuilder().also { timestampBuilder ->76 timestampBuilder.seconds = seconds77 timestampBuilder.nanos = nanoSeconds78 }.build()79 }80 val anyValidLoanState: Arb<LoanStateMetadata> = Arb.bind(81 anyUuid,82 anyValidChecksum,83 anyValidTimestamp,84 anyNonEmptyString,85 ) { uuid, checksum, effectiveTime, uri ->86 LoanStateMetadata.newBuilder().also { loanStateBuilder ->87 loanStateBuilder.id = uuid88 loanStateBuilder.checksum = checksum89 loanStateBuilder.effectiveTime = effectiveTime90 loanStateBuilder.uri = uri91 }.build()92 }93 fun loanStateSet(size: Int, slippage: Int = 10): Arb<List<LoanStateMetadata>> =94 /** Since we need each *property* to be unique, we must fix the set size & construct the arbs from scratch with primitives */95 Arb.bind(96 Arb.set(gen = Arb.uuid(UUIDVersion.V4), size = size, slippage = slippage).map { it.toList() },97 Arb.set(gen = anyNonEmptyString, size = size, slippage = slippage).map { it.toList() },98 Arb.set(gen = anyNonEmptyString, size = size, slippage = slippage).map { it.toList() },99 Arb.set(gen = anyPastNonEpochTimestampComponents, size = size, slippage = slippage).map { it.toList() },100 ) { randomIds, randomChecksums, randomUris, randomTimestamps ->101 randomIds.indices.map { i ->102 LoanStateMetadata.newBuilder().also { loanStateBuilder ->103 loanStateBuilder.id = FigureTechUUID.newBuilder().also { uuidBuilder ->104 uuidBuilder.value = randomIds[i].toString()105 }.build()106 loanStateBuilder.checksum = FigureTechChecksum.newBuilder().also { checksumBuilder ->107 checksumBuilder.checksum = randomChecksums[i]108 }.build()109 loanStateBuilder.uri = randomUris[i]110 loanStateBuilder.effectiveTime = Timestamp.newBuilder().also { timestampBuilder ->111 timestampBuilder.seconds = randomTimestamps[i].first112 timestampBuilder.nanos = randomTimestamps[i].second113 }.build()114 }.build()115 }116 }117}118private val anyTimestampComponents: Arb<Pair<Long, Int>> = Arb.pair(119 Arb.long(min = Timestamps.MIN_VALUE.seconds, max = Timestamps.MAX_VALUE.seconds),120 Arb.int(min = Timestamps.MIN_VALUE.nanos, max = Timestamps.MAX_VALUE.nanos),121)122private val anyPastNonEpochTimestampComponents: Arb<Pair<Long, Int>> = Instant.now().let { now ->123 Arb.pair(124 Arb.long(min = Timestamps.MIN_VALUE.seconds, max = now.epochSecond),125 Arb.int(min = Timestamps.MIN_VALUE.nanos + 1, max = now.nano),126 )127}128/**129 * Defines a custom [Matcher] to check the violation count value in a [ContractViolationException].130 */131internal fun throwViolationCount(violationCount: UInt) = Matcher<ContractViolationException> { exception ->132 { count: UInt ->133 if (count == 1U) {134 "$count violation"135 } else {136 "$count violations"137 }138 }.let { violationPrinter: (UInt) -> String ->139 return@Matcher MatcherResult(140 exception.overallViolationCount == violationCount,141 {142 "Exception had ${violationPrinter(exception.overallViolationCount)} " +143 "but we expected ${violationPrinter(violationCount)}"144 },145 { "Exception should not have ${violationPrinter(violationCount)}" },146 )147 }148}149/**150 * Wraps the custom matcher [throwViolationCount] following the style outlined in the151 * [Kotest documentation](https://kotest.io/docs/assertions/custom-matchers.html#extension-variants).152 */153internal infix fun ContractViolationException.shouldHaveViolationCount(violationCount: UInt) = apply {154 this should throwViolationCount(violationCount)155}156internal infix fun UnexpectedContractStateException.shouldBeParseFailureFor(classifier: String) = apply {157 this.cause should beInstanceOf<InvalidProtocolBufferException>()158 this.message shouldBe "Could not unpack as class $classifier"159}...

Full Screen

Full Screen

Generators.kt

Source:Generators.kt Github

copy

Full Screen

1package arrow.core.test.generators2import arrow.core.Const3import arrow.core.Either4import arrow.core.Endo5import arrow.core.Eval6import arrow.core.Ior7import arrow.core.Option8import arrow.core.Tuple109import arrow.core.Tuple410import arrow.core.Tuple511import arrow.core.Tuple612import arrow.core.Tuple713import arrow.core.Tuple814import arrow.core.Tuple915import arrow.core.Validated16import arrow.core.left17import arrow.core.right18import arrow.core.toOption19import io.kotest.property.Arb20import io.kotest.property.arbitrary.bind21import io.kotest.property.arbitrary.boolean22import io.kotest.property.arbitrary.byte23import io.kotest.property.arbitrary.choice24import io.kotest.property.arbitrary.constant25import io.kotest.property.arbitrary.filter26import io.kotest.property.arbitrary.flatMap27import io.kotest.property.arbitrary.int28import io.kotest.property.arbitrary.long29import io.kotest.property.arbitrary.map30import io.kotest.property.arbitrary.numericDoubles31import io.kotest.property.arbitrary.numericFloats32import io.kotest.property.arbitrary.of33import io.kotest.property.arbitrary.orNull34import io.kotest.property.arbitrary.short35import io.kotest.property.arbitrary.string36import kotlin.Result.Companion.failure37import kotlin.Result.Companion.success38import kotlin.math.abs39public fun <A, B> Arb.Companion.functionAToB(arb: Arb<B>): Arb<(A) -> B> =40 arb.map { b: B -> { _: A -> b } }41public fun <A> Arb.Companion.functionAAToA(arb: Arb<A>): Arb<(A, A) -> A> =42 arb.map { a: A -> { _: A, _: A -> a } }43public fun <A, B> Arb.Companion.functionBAToB(arb: Arb<B>): Arb<(B, A) -> B> =44 arb.map { b: B -> { _: B, _: A -> b } }45public fun <A, B> Arb.Companion.functionABToB(arb: Arb<B>): Arb<(A, B) -> B> =46 arb.map { b: B -> { _: A, _: B -> b } }47public fun <A> Arb.Companion.functionToA(arb: Arb<A>): Arb<() -> A> =48 arb.map { a: A -> { a } }49public fun Arb.Companion.throwable(): Arb<Throwable> =50 Arb.of(listOf(RuntimeException(), NoSuchElementException(), IllegalArgumentException()))51public fun <A> Arb.Companion.result(arbA: Arb<A>): Arb<Result<A>> =52 Arb.choice(arbA.map(::success), throwable().map(::failure))53public fun Arb.Companion.doubleSmall(): Arb<Double> =54 Arb.numericDoubles(from = 0.0, to = 100.0)55public fun Arb.Companion.floatSmall(): Arb<Float> =56 Arb.numericFloats(from = 0F, to = 100F)57public fun Arb.Companion.intSmall(factor: Int = 10000): Arb<Int> =58 Arb.int((Int.MIN_VALUE / factor)..(Int.MAX_VALUE / factor))59public fun Arb.Companion.byteSmall(): Arb<Byte> =60 Arb.byte(min = (Byte.MIN_VALUE / 10).toByte(), max = (Byte.MAX_VALUE / 10).toByte())61public fun Arb.Companion.shortSmall(): Arb<Short> {62 val range = (Short.MIN_VALUE / 1000)..(Short.MAX_VALUE / 1000)63 return Arb.short().filter { it in range }64}65public fun Arb.Companion.longSmall(): Arb<Long> =66 Arb.long((Long.MIN_VALUE / 100000L)..(Long.MAX_VALUE / 100000L))67public fun <A, B, C, D> Arb.Companion.tuple4(arbA: Arb<A>, arbB: Arb<B>, arbC: Arb<C>, arbD: Arb<D>): Arb<Tuple4<A, B, C, D>> =68 Arb.bind(arbA, arbB, arbC, arbD, ::Tuple4)69public fun <A, B, C, D, E> Arb.Companion.tuple5(70 arbA: Arb<A>,71 arbB: Arb<B>,72 arbC: Arb<C>,73 arbD: Arb<D>,74 arbE: Arb<E>75): Arb<Tuple5<A, B, C, D, E>> =76 Arb.bind(arbA, arbB, arbC, arbD, arbE, ::Tuple5)77public fun <A, B, C, D, E, F> Arb.Companion.tuple6(78 arbA: Arb<A>,79 arbB: Arb<B>,80 arbC: Arb<C>,81 arbD: Arb<D>,82 arbE: Arb<E>,83 arbF: Arb<F>84): Arb<Tuple6<A, B, C, D, E, F>> =85 Arb.bind(arbA, arbB, arbC, arbD, arbE, arbF, ::Tuple6)86public fun <A, B, C, D, E, F, G> Arb.Companion.tuple7(87 arbA: Arb<A>,88 arbB: Arb<B>,89 arbC: Arb<C>,90 arbD: Arb<D>,91 arbE: Arb<E>,92 arbF: Arb<F>,93 arbG: Arb<G>94): Arb<Tuple7<A, B, C, D, E, F, G>> =95 Arb.bind(arbA, arbB, arbC, arbD, arbE, arbF, arbG, ::Tuple7)96public fun <A, B, C, D, E, F, G, H> Arb.Companion.tuple8(97 arbA: Arb<A>,98 arbB: Arb<B>,99 arbC: Arb<C>,100 arbD: Arb<D>,101 arbE: Arb<E>,102 arbF: Arb<F>,103 arbG: Arb<G>,104 arbH: Arb<H>105): Arb<Tuple8<A, B, C, D, E, F, G, H>> =106 Arb.bind(107 Arb.tuple7(arbA, arbB, arbC, arbD, arbE, arbF, arbG),108 arbH109 ) { (a, b, c, d, e, f, g), h ->110 Tuple8(a, b, c, d, e, f, g, h)111 }112public fun <A, B, C, D, E, F, G, H, I> Arb.Companion.tuple9(113 arbA: Arb<A>,114 arbB: Arb<B>,115 arbC: Arb<C>,116 arbD: Arb<D>,117 arbE: Arb<E>,118 arbF: Arb<F>,119 arbG: Arb<G>,120 arbH: Arb<H>,121 arbI: Arb<I>122): Arb<Tuple9<A, B, C, D, E, F, G, H, I>> =123 Arb.bind(124 Arb.tuple8(arbA, arbB, arbC, arbD, arbE, arbF, arbG, arbH),125 arbI126 ) { (a, b, c, d, e, f, g, h), i ->127 Tuple9(a, b, c, d, e, f, g, h, i)128 }129public fun <A, B, C, D, E, F, G, H, I, J> Arb.Companion.tuple10(130 arbA: Arb<A>,131 arbB: Arb<B>,132 arbC: Arb<C>,133 arbD: Arb<D>,134 arbE: Arb<E>,135 arbF: Arb<F>,136 arbG: Arb<G>,137 arbH: Arb<H>,138 arbI: Arb<I>,139 arbJ: Arb<J>140): Arb<Tuple10<A, B, C, D, E, F, G, H, I, J>> =141 Arb.bind(142 Arb.tuple9(arbA, arbB, arbC, arbD, arbE, arbF, arbG, arbH, arbI),143 arbJ144 ) { (a, b, c, d, e, f, g, h, i), j ->145 Tuple10(a, b, c, d, e, f, g, h, i, j)146 }147public fun Arb.Companion.nonZeroInt(): Arb<Int> = Arb.int().filter { it != 0 }148public fun Arb.Companion.intPredicate(): Arb<(Int) -> Boolean> =149 Arb.nonZeroInt().flatMap { num ->150 val absNum = abs(num)151 Arb.of(152 listOf<(Int) -> Boolean>(153 { it > num },154 { it <= num },155 { it % absNum == 0 },156 { it % absNum == absNum - 1 }157 )158 )159 }160public fun <A> Arb.Companion.endo(arb: Arb<A>): Arb<Endo<A>> = arb.map { a: A -> Endo<A> { a } }161public fun <B> Arb.Companion.option(arb: Arb<B>): Arb<Option<B>> =162 arb.orNull().map { it.toOption() }163public fun <E, A> Arb.Companion.either(arbE: Arb<E>, arbA: Arb<A>): Arb<Either<E, A>> {164 val arbLeft = arbE.map { Either.Left(it) }165 val arbRight = arbA.map { Either.Right(it) }166 return Arb.choice(arbLeft, arbRight)167}168public fun <E, A> Arb<E>.or(arbA: Arb<A>): Arb<Either<E, A>> = Arb.either(this, arbA)169public fun <E, A> Arb.Companion.validated(arbE: Arb<E>, arbA: Arb<A>): Arb<Validated<E, A>> =170 Arb.either(arbE, arbA).map { Validated.fromEither(it) }171public fun Arb.Companion.unit(): Arb<Unit> =172 Arb.constant(Unit)173public fun <A, B> Arb.Companion.ior(arbA: Arb<A>, arbB: Arb<B>): Arb<Ior<A, B>> =174 arbA.alignWith(arbB) { it }175public fun <A, B> Arb.Companion.arbConst(arb: Arb<A>): Arb<Const<A, B>> =176 arb.map { Const<A, B>(it) }177public fun <A> Arb<A>.eval(): Arb<Eval<A>> =178 map { Eval.now(it) }179private fun <A, B, R> Arb<A>.alignWith(arbB: Arb<B>, transform: (Ior<A, B>) -> R): Arb<R> =180 Arb.bind(this, arbB) { a, b -> transform(Ior.Both(a, b)) }181public fun Arb.Companion.suspendFunThatReturnsEitherAnyOrAnyOrThrows(): Arb<suspend () -> Either<Any, Any>> =182 choice(183 suspendFunThatReturnsAnyRight(),184 suspendFunThatReturnsAnyLeft(),185 suspendFunThatThrows()186 )187public fun Arb.Companion.suspendFunThatReturnsAnyRight(): Arb<suspend () -> Either<Any, Any>> =188 any().map { suspend { it.right() } }189public fun Arb.Companion.suspendFunThatReturnsAnyLeft(): Arb<suspend () -> Either<Any, Any>> =190 any().map { suspend { it.left() } }191public fun Arb.Companion.suspendFunThatThrows(): Arb<suspend () -> Either<Any, Any>> =192 throwable().map { suspend { throw it } } as Arb<suspend () -> Either<Any, Any>>193public fun Arb.Companion.any(): Arb<Any> =194 choice(195 Arb.string() as Arb<Any>,196 Arb.int() as Arb<Any>,197 Arb.long() as Arb<Any>,198 Arb.boolean() as Arb<Any>,199 Arb.throwable() as Arb<Any>,200 Arb.unit() as Arb<Any>201 )...

Full Screen

Full Screen

Arb.kt

Source:Arb.kt Github

copy

Full Screen

1package io.github.nomisrev2import io.kotest.property.Arb3import io.kotest.property.arbitrary.boolean4import io.kotest.property.arbitrary.choice5import io.kotest.property.arbitrary.constant6import io.kotest.property.arbitrary.double7import io.kotest.property.arbitrary.filterNot8import io.kotest.property.arbitrary.float9import io.kotest.property.arbitrary.int10import io.kotest.property.arbitrary.list11import io.kotest.property.arbitrary.long12import io.kotest.property.arbitrary.map13import io.kotest.property.arbitrary.string14import kotlinx.serialization.SerializationStrategy15import kotlinx.serialization.json.Json16import kotlinx.serialization.json.JsonArray17import kotlinx.serialization.json.JsonElement18import kotlinx.serialization.json.JsonNull19import kotlinx.serialization.json.JsonObject20import kotlinx.serialization.json.JsonPrimitive21import kotlinx.serialization.serializer22fun Arb.Companion.street(): Arb<Street> =23 Arb.string().map(::Street)24fun Arb.Companion.city(): Arb<City> =25 Arb.list(street()).map(::City)26fun Arb.Companion.jsInt(): Arb<JsonPrimitive> =27 int().map(::JsonPrimitive)28fun Arb.Companion.jsLong(): Arb<JsonPrimitive> =29 long().map(::JsonPrimitive)30fun Arb.Companion.jsFloat(): Arb<JsonPrimitive> =31 float().filterNot(Float::isNaN).map(::JsonPrimitive)32fun Arb.Companion.jsDouble(): Arb<JsonPrimitive> =33 double().filterNot(Double::isNaN).map(::JsonPrimitive)34fun Arb.Companion.jsString(): Arb<JsonPrimitive> =35 Arb.string().map(::JsonPrimitive)36fun Arb.Companion.jsBoolean(): Arb<JsonPrimitive> =37 boolean().map(::JsonPrimitive)38fun Arb.Companion.jsNull(): Arb<JsonNull> =39 constant(JsonNull)40private fun genJson(): Arb<JsonElement> =41 Arb.choice(Arb.jsInt(), Arb.jsLong(), Arb.jsDouble(), Arb.jsString(), Arb.jsNull())42fun Arb.Companion.jsArray(): Arb<JsonArray> =43 list(genJson()).map(::JsonArray)44fun <T> Arb.Companion.jsArray(valid: Arb<T>, EN: SerializationStrategy<T>): Arb<JsonArray> =45 list(valid).map { list -> JsonArray(list.map { elem -> Json.encodeToJsonElement(EN, elem) }) }46inline fun <reified T> Arb.Companion.jsArray(valid: Arb<T>): Arb<JsonArray> =47 jsArray(valid, serializer())48fun Arb.Companion.jsObject(): Arb<JsonObject> =49 map(Arb.string(), genJson()).map(::JsonObject)50fun <T> Arb.Companion.json(valid: Arb<T>, EN: SerializationStrategy<T>): Arb<JsonElement> =51 valid.map { Json.encodeToJsonElement(EN, it) }52inline fun <reified T> Arb.Companion.json(valid: Arb<T>): Arb<JsonElement> =53 json(valid, serializer())54fun Arb.Companion.json(): Arb<JsonElement> = choice(55 Arb.jsInt(),56 Arb.jsLong(),57 Arb.jsDouble(),58 Arb.jsString(),59 Arb.jsNull(),60 Arb.jsArray(),61 Arb.jsObject(),62)...

Full Screen

Full Screen

TobogganTrajectoryTest.kt

Source:TobogganTrajectoryTest.kt Github

copy

Full Screen

1import io.kotest.core.spec.style.DescribeSpec2import io.kotest.matchers.shouldBe3import io.kotest.property.Arb4import io.kotest.property.arbitrary.list5import io.kotest.property.arbitrary.map6import io.kotest.property.arbitrary.positiveInts7import io.kotest.property.arbitrary.stringPattern8import io.kotest.property.checkAll9import java.io.File10class TobogganTrajectoryTest : DescribeSpec({11 val t = TobogganTrajectory()12 describe("Toboggan Trajectory") {13 context("Sample input") {14 val input: List<String> = File("input.txt").readLines()15 val actual = t.tobogganTrajectory(input)16 actual shouldBe 20717 }18 context("Grid of no trees") {19 it("Returns 0") {20 checkAll(Arb.positiveInts(100).map { (".".repeat(it) + "\n").repeat(it) }) { g ->21 t.tobogganTrajectory(g.split("\n").dropLast(1)) shouldBe 022 }23 }24 }25 context("Grid of only trees") {26 it("Returns height of grid") {27 checkAll(Arb.positiveInts(100).map { ("#".repeat(it) + "\n").repeat(it) }) { g ->28 val grid = g.split("\n").dropLast(1)29 t.tobogganTrajectory(grid) shouldBe grid.size30 }31 }32 }33 context("Vertical grid") {34 it("Returns number of trees in the entire grid") {35 checkAll(Arb.list(Arb.stringPattern("[.#]"))) { g ->36 t.tobogganTrajectory(g) shouldBe g.count { l -> l == "#" }37 }38 }39 }40 }41 describe("Toboggan Trajectory 2") {42 context("Sample test") {43 val input: List<String> = File("input.txt").readLines()44 val actual = t.tobogganTrajectory2(input)45 actual shouldBe 265589280046 }47 context("Grid of no trees") {48 it("Returns 0") {49 checkAll(Arb.positiveInts(100).map { (".".repeat(it) + "\n").repeat(it) }) { g ->50 t.tobogganTrajectory2(g.split("\n").dropLast(1)) shouldBe 051 }52 }53 }54 context("Grid of only trees") {55 it("Returns height of grid") {56 checkAll(Arb.positiveInts(100).map { ("#".repeat(it) + "\n").repeat(it) }) { g ->57 val grid = g.split("\n").dropLast(1)58 t.tobogganTrajectory2(grid) shouldBe (Math.pow(grid.size.toDouble(), 4.0) * Math.ceil(grid.size / 2.0)).toLong()59 }60 }61 }62 }63})...

Full Screen

Full Screen

TestUtil.kt

Source:TestUtil.kt Github

copy

Full Screen

1package com.mattmik.rapira.objects2import com.mattmik.rapira.util.Result3import io.kotest.matchers.Matcher4import io.kotest.matchers.MatcherResult5import io.kotest.matchers.should6import io.kotest.matchers.types.beOfType7import io.kotest.property.Arb8import io.kotest.property.arbitrary.arbitrary9import io.kotest.property.arbitrary.bool10import io.kotest.property.arbitrary.choice11import io.kotest.property.arbitrary.constant12import io.kotest.property.arbitrary.double13import io.kotest.property.arbitrary.int14import io.kotest.property.arbitrary.map15import io.kotest.property.arbitrary.string16fun convertToString(expected: String) = object : Matcher<RObject> {17 override fun test(value: RObject) =18 MatcherResult(19 value.toString() == expected,20 "Object $value should convert to string $expected",21 "Object $value should not convert to string $expected"22 )23}24infix fun RObject.shouldConvertToString(expected: String) = this should convertToString(expected)25fun <T> succeedWith(expected: T) = object : Matcher<Result<T>> {26 override fun test(value: Result<T>) =27 MatcherResult(28 value is Result.Success && value.obj == expected,29 "Result $value should be successful with value $expected",30 "Result $value should not be successful with value $expected"31 )32}33infix fun <T> Result<T>.shouldSucceedWith(expected: T) = this should succeedWith(expected)34fun <T> Result<T>.shouldError() = this should beOfType<Result.Error>()35fun <T> errorWith(expected: String) = object : Matcher<Result<T>> {36 override fun test(value: Result<T>) =37 MatcherResult(38 value is Result.Error && value.reason == expected,39 "Result $value should be error with reason $expected",40 "Result $value should not be error with reason $expected"41 )42}43infix fun <T> Result<T>.shouldErrorWith(reason: String) = this should errorWith(reason)44val rapiraEmptyArb = Arb.constant(Empty)45val rapiraFunctionArb = arbitrary { Function() }46val rapiraIntegerArb = Arb.int().map { num -> num.toRInteger() }47val rapiraLogicalArb = Arb.bool().map { bool -> Logical(bool) }48val rapiraProcedureArb = arbitrary { Procedure() }49val rapiraRealArb = Arb.double().map { double -> Real(double) }50val rapiraTextArb = Arb.string().map { str -> str.toText() }51val rapiraObjectArb = Arb.choice(52 rapiraEmptyArb,53 rapiraFunctionArb,54 rapiraIntegerArb,55 rapiraLogicalArb,56 rapiraProcedureArb,57 rapiraRealArb,58 rapiraTextArb59)...

Full Screen

Full Screen

ExampleSpec.kt

Source:ExampleSpec.kt Github

copy

Full Screen

1package org.example2import arrow.core.Either3import arrow.optics.Traversal4import arrow.typeclasses.Monoid5import io.kotest.assertions.arrow.core.shouldBeRight6import io.kotest.core.spec.style.StringSpec7import io.kotest.matchers.shouldBe8import io.kotest.matchers.types.shouldBeTypeOf9import io.kotest.property.Arb10import io.kotest.property.arbitrary.int11import io.kotest.property.arbitrary.list12import io.kotest.property.arbitrary.map13import io.kotest.property.arbitrary.positiveInt14import io.kotest.property.arbitrary.string15import io.kotest.property.arrow.core.MonoidLaws16import io.kotest.property.arrow.core.either17import io.kotest.property.arrow.core.functionAToB18import io.kotest.property.arrow.laws.testLaws19import io.kotest.property.arrow.optics.TraversalLaws20import kotlin.jvm.JvmInline21class ExampleSpec : StringSpec({22 "true shouldBe true" {23 true shouldBe true24 }25 "exception should fail" {26 // throw RuntimeException("Boom2!")27 }28 "kotest arrow extension use-cases" {29 // smart-cast abilities for arrow types30 Either.Right("HI").shouldBeRight().shouldBeTypeOf<String>()31 }32 // utilise builtin or costume Laws with Generators to verify behavior33 testLaws(34 MonoidLaws.laws(Monoid.list(), Arb.list(Arb.string())),35 MonoidLaws.laws(Monoid.numbers(), Arb.numbers())36 )37 // optics Laws from arrow38 testLaws(39 TraversalLaws.laws(40 traversal = Traversal.either(),41 aGen = Arb.either(Arb.string(), Arb.int()),42 bGen = Arb.int(),43 funcGen = Arb.functionAToB(Arb.int()),44 )45 )46})47fun Arb.Companion.numbers(): Arb<Numbers> =48 Arb.positiveInt().map { it.toNumber() }49fun Int.toNumber(): Numbers =50 if (this <= 0) Zero else Suc(minus(1).toNumber())51// natural numbers form a monoid52fun Monoid.Companion.numbers(): Monoid<Numbers> =53 object : Monoid<Numbers> {54 override fun empty(): Numbers =55 Zero56 override fun Numbers.combine(b: Numbers): Numbers =57 Suc(b)58 }59// natural numbers60sealed interface Numbers61object Zero : Numbers62@JvmInline63value class Suc(val value: Numbers) : Numbers...

Full Screen

Full Screen

ApplicativeTest.kt

Source:ApplicativeTest.kt Github

copy

Full Screen

1package com.example.demo2import arrow.Kind3import io.kotest.core.spec.style.StringSpec4import io.kotest.property.Arb5import io.kotest.property.PropertyContext6import io.kotest.property.arbitrary.int7import io.kotest.property.arbitrary.list8import io.kotest.property.forAll9class ApplicativeTest : StringSpec({10 "left identity law" {11 forAll(nelArb) { seq ->12 leftIdLaw(ListApplicative, ListK(seq))13 }14 }15 "right identity law" {16 forAll(nelArb) { seq ->17 rightIdLaw(ListApplicative, ListK(seq))18 }19 }20 "associative law" {21 forAll(nelArb, nelArb, nelArb) { a, b, c ->22 assocLaw(ListApplicative, ListK(a), ListK(b), ListK(c))23 }24 }25 "naturality law" {26 forAll(nelArb, nelArb) { a, b ->27 val f: (Int) -> String = { it.toString() }28 val g: (Int) -> Int = { it * it }29 naturalityLaw(ListApplicative, ListK(a), ListK(b), f, g)30 }31 }32})33val nelArb = Arb.list(Arb.int(), 1..1000)34fun <F, A> leftIdLaw(ap: Applicative<F>, fa: Kind<F, A>): Boolean =35 ap.run {36 map2(unit { }, fa) { _, a -> a } == fa37 }38fun <F, A> rightIdLaw(ap: Applicative<F>, fa: Kind<F, A>): Boolean =39 ap.run {40 map2(fa, unit { }) { a, _ -> a } == fa41 }42fun <A, B, C> assoc(p: Pair<A, Pair<B, C>>): Pair<Pair<A, B>, C> = (p.first to p.second.first) to p.second.second43fun <F, A, B, C> assocLaw(ap: Applicative<F>, fa: Kind<F, A>, fb: Kind<F, B>, fc: Kind<F, C>): Boolean =44 ap.run {45 product(product(fa, fb), fc) == product(fa, product(fb, fc)).map { assoc(it) }46 }47fun <I, O, I2, O2> productF(f: (I) -> O, g: (I2) -> O2): (I, I2) -> Pair<O, O2> = { i, i2 -> f(i) to g(i2) }48fun <F, A, B, C, D> naturalityLaw(ap: Applicative<F>, fa: Kind<F, A>, fb: Kind<F, B>, f: (A) -> C, g: (B) -> D): Boolean =49 ap.run {50 map2(fa, fb) { a, b -> productF(f, g)(a, b) } == product(fa.map(f), fb.map(g))51 }...

Full Screen

Full Screen

Arb.map

Using AI Code Generation

copy

Full Screen

1 fun <A, B> map(arb: Arb<A>, f: (A) -> B): Arb<B> = object : Arb<B> {2 override fun edgecases() = arb.edgecases().map(f)3 override fun sample(rs: RandomSource): Sample<B> = arb.sample(rs).map(f)4 }5}6 fun <A, B> map(arb: Arb<A>, f: (A) -> B): Arb<B> = object : Arb<B> {7 override fun edgecases() = arb.edgecases().map(f)8 override fun sample(rs: RandomSource): Sample<B> = arb.sample(rs).map(f)9 }10}11 fun <A, B> map(arb: Arb<A>, f: (A) -> B): Arb<B> = object : Arb<B> {12 override fun edgecases() = arb.edgecases().map(f)13 override fun sample(rs: RandomSource): Sample<B> = arb.sample(rs).map(f)14 }15}16 fun <A, B> map(arb: Arb<A>, f: (A) -> B): Arb<B> = object : Arb<B> {17 override fun edgecases() = arb.edgecases().map(f)18 override fun sample(rs: RandomSource): Sample<B> = arb.sample(rs).map(f)19 }20}21 fun <A, B> map(arb: Arb<A>, f: (A) -> B): Arb<B> = object : Arb<B> {22 override fun edgecases() = arb.edgecases().map(f)23 override fun sample(rs: RandomSource): Sample<B> = arb.sample(rs).map(f)24 }25}26 fun <A, B> map(arb: Arb<A>, f: (A) -> B): Arb<B> = object : Arb<B> {27 override fun edgecases() = arb.edgecases().map(f)28 override fun sample(rs: RandomSource): Sample<B

Full Screen

Full Screen

Arb.map

Using AI Code Generation

copy

Full Screen

1 val intList = Arb.list(Arb.int())2 val stringList = Arb.list(Arb.string())3 val intListToString = Arb.map(intList, stringList) { (ints, strings) ->4 ints.zip(strings).toMap()5 }6}7 val intList = Arb.list(Arb.int())8 val stringList = Arb.list(Arb.string())9 val intListToString = Arb.map(intList, stringList) { (ints, strings) ->10 ints.zip(strings).toMap()11 }12}13 val intList = Arb.list(Arb.int())14 val stringList = Arb.list(Arb.string())15 val intListToString = Arb.map(intList, stringList) { (ints, strings) ->16 ints.zip(strings).toMap()17 }18}19 val intList = Arb.list(Arb.int())20 val stringList = Arb.list(Arb.string())21 val intListToString = Arb.map(intList, stringList) { (ints, strings) ->22 ints.zip(strings).toMap()23 }24}25 val intList = Arb.list(Arb.int())26 val stringList = Arb.list(Arb.string())27 val intListToString = Arb.map(intList, stringList) { (ints, strings) ->28 ints.zip(strings).toMap()29 }30}31 val intList = Arb.list(Arb.int())32 val stringList = Arb.list(Arb.string())33 val intListToString = Arb.map(intList, stringList) { (ints, strings) ->34 ints.zip(strings).toMap()35 }36}37 val intList = Arb.list(Arb.int())38 val stringList = Arb.list(Arb.string())39 val intListToString = Arb.map(intList, stringList) { (ints, strings) ->40 ints.zip(strings).toMap()41 }42}

Full Screen

Full Screen

Arb.map

Using AI Code Generation

copy

Full Screen

1class ArbTest : FunSpec({2test("test map method of io.kotest.property.arbitrary.map class Arb") {3val arb = Arb.string().map { it.length }4val sample = arb.take(5).toList()5sample shouldBe listOf(0, 1, 2, 3, 4)6}7})8class ArbTest : FunSpec({9test("test map method of io.kotest.property.arbitrary.map class Arb") {10val arb = Arb.string().map { it.length }11val sample = arb.take(5).toList()12sample shouldBe listOf(0, 1, 2, 3, 4)13}14})15class ArbTest : FunSpec({16test("test map method of io.kotest.property.arbitrary.map class Arb") {17val arb = Arb.string().map { it.length }18val sample = arb.take(5).toList()19sample shouldBe listOf(0, 1, 2, 3, 4)20}21})22class ArbTest : FunSpec({23test("test map method of io.kotest.property.arbitrary.map class Arb") {24val arb = Arb.string().map { it.length }25val sample = arb.take(5).toList()26sample shouldBe listOf(0, 1, 2, 3, 4)27}28})29class ArbTest : FunSpec({30test("test map method of io.kotest.property.arbitrary.map class Arb") {31val arb = Arb.string().map { it.length }32val sample = arb.take(5).toList()33sample shouldBe listOf(0, 1, 2, 3, 4)34}35})36class ArbTest : FunSpec({37test("test map method of io.kotest.property.arbitrary.map class Arb") {38val arb = Arb.string().map { it.length }39val sample = arb.take(5).toList()40sample shouldBe listOf(0, 1, 2, 3, 4)41}42})

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