Best Kotest code snippet using io.kotest.property.arbitrary.arbs
AppendLoanStatesContractUnitTest.kt
Source:AppendLoanStatesContractUnitTest.kt
1package io.provenance.scope.loan.contracts2import io.kotest.assertions.throwables.shouldNotThrow3import io.kotest.assertions.throwables.shouldThrow4import io.kotest.core.spec.style.WordSpec5import io.kotest.core.test.TestCaseOrder6import io.kotest.matchers.ints.shouldBeExactly7import io.kotest.matchers.string.shouldContain8import io.kotest.matchers.string.shouldContainIgnoringCase9import io.kotest.property.Arb10import io.kotest.property.arbitrary.arbitrary11import io.kotest.property.arbitrary.flatMap12import io.kotest.property.arbitrary.int13import io.kotest.property.arbitrary.pair14import io.kotest.property.checkAll15import io.provenance.scope.loan.test.Constructors.appendLoanStatesContractWithNoExistingStates16import io.provenance.scope.loan.test.LoanPackageArbs.anyUuid17import io.provenance.scope.loan.test.LoanPackageArbs.anyValidChecksum18import io.provenance.scope.loan.test.LoanPackageArbs.anyValidLoanState19import io.provenance.scope.loan.test.LoanPackageArbs.anyValidTimestamp20import io.provenance.scope.loan.test.LoanPackageArbs.loanStateSet21import io.provenance.scope.loan.utility.ContractViolationException22import io.provenance.scope.util.toOffsetDateTime23import tech.figure.servicing.v1beta1.LoanStateOuterClass.LoanStateMetadata24import tech.figure.servicing.v1beta1.LoanStateOuterClass.ServicingData25import kotlin.math.max26class AppendLoanStatesContractUnitTest : WordSpec({27 "appendLoanStates" When {28 "given an empty input" should {29 "throw an appropriate exception" {30 shouldThrow<ContractViolationException> {31 appendLoanStatesContractWithNoExistingStates.appendLoanStates(emptyList())32 }.let { exception ->33 exception.message shouldContainIgnoringCase "Must supply at least one loan state"34 }35 }36 }37 "given at least one loan state with invalid fields" should {38 "throw an appropriate exception" {39 checkAll(anyValidLoanState) { randomLoanState ->40 shouldThrow<ContractViolationException> {41 appendLoanStatesContractWithNoExistingStates.appendLoanStates(42 listOf(43 LoanStateMetadata.getDefaultInstance(),44 randomLoanState,45 )46 )47 }.let { exception ->48 exception.message shouldContainIgnoringCase "must have valid ID"49 exception.message shouldContainIgnoringCase "missing URI"50 exception.message shouldContainIgnoringCase "missing checksum"51 }52 }53 }54 }55 "given loan states which duplicate existing loan state checksums" should {56 "throw an appropriate exception" {57 checkAll(anyValidLoanState, anyValidLoanState, anyValidChecksum) { randomExistingLoanState, randomNewLoanState, randomChecksum ->58 shouldThrow<ContractViolationException> {59 AppendLoanStatesContract(60 existingServicingData = ServicingData.newBuilder().also { servicingDataBuilder ->61 servicingDataBuilder.clearLoanState()62 servicingDataBuilder.addLoanState(63 randomExistingLoanState.toBuilder().also { loanStateBuilder ->64 loanStateBuilder.checksum = randomChecksum65 }.build()66 )67 }.build()68 ).appendLoanStates(69 listOf(70 randomNewLoanState.toBuilder().also { loanStateBuilder ->71 loanStateBuilder.checksum = randomChecksum72 }.build()73 )74 )75 }.let { exception ->76 exception.message shouldContain "Loan state with checksum ${randomChecksum.checksum} already exists"77 }78 }79 }80 }81 "given loan states which duplicate existing loan state IDs" should {82 "throw an appropriate exception" {83 checkAll(anyValidLoanState, anyValidLoanState, anyUuid) { randomExistingLoanState, randomNewLoanState, randomUuid ->84 shouldThrow<ContractViolationException> {85 AppendLoanStatesContract(86 existingServicingData = ServicingData.newBuilder().also { servicingDataBuilder ->87 servicingDataBuilder.clearLoanState()88 servicingDataBuilder.addLoanState(89 randomExistingLoanState.toBuilder().also { loanStateBuilder ->90 loanStateBuilder.id = randomUuid91 }.build()92 )93 }.build()94 ).appendLoanStates(95 listOf(96 randomNewLoanState.toBuilder().also { loanStateBuilder ->97 loanStateBuilder.id = randomUuid98 }.build()99 )100 )101 }.let { exception ->102 exception.message shouldContain "Loan state with ID ${randomUuid.value} already exists"103 }104 }105 }106 }107 "given loan states which duplicate existing loan state times" should {108 "throw an appropriate exception" {109 checkAll(anyValidLoanState, anyValidLoanState, anyValidTimestamp) { randomExistingLoanState, randomNewLoanState, randomTimestamp ->110 shouldThrow<ContractViolationException> {111 AppendLoanStatesContract(112 existingServicingData = ServicingData.newBuilder().also { servicingDataBuilder ->113 servicingDataBuilder.clearLoanState()114 servicingDataBuilder.addLoanState(115 randomExistingLoanState.toBuilder().also { loanStateBuilder ->116 loanStateBuilder.effectiveTime = randomTimestamp117 }.build()118 )119 }.build()120 ).appendLoanStates(121 listOf(122 randomNewLoanState.toBuilder().also { loanStateBuilder ->123 loanStateBuilder.effectiveTime = randomTimestamp124 }.build()125 )126 )127 }.let { exception ->128 exception.message shouldContain "Loan state with effective time ${randomTimestamp.toOffsetDateTime()} already exists"129 }130 }131 }132 }133 "given only new & valid loan states" should {134 "not throw an exception" {135 val stateCountRange = 2..4 // Reduce the upper bound of this range (to no lower than 3) to decrease the execution time136 val arbitraryStateCountAndSplit = Arb.int(stateCountRange).flatMap { randomStateCount ->137 Arb.pair(arbitrary { randomStateCount }, Arb.int(0..max(randomStateCount - 1, 1)))138 }139 checkAll(arbitraryStateCountAndSplit) { (randomStateCount, randomSplit) ->140 checkAll(loanStateSet(size = randomStateCount)) { randomStateSet ->141 val (randomExistingStates, randomNewStates) = randomStateSet.let { orderedRandomStateSet ->142 orderedRandomStateSet.take(randomSplit) to orderedRandomStateSet.drop(randomSplit)143 }144 shouldNotThrow<ContractViolationException> {145 AppendLoanStatesContract(146 existingServicingData = ServicingData.newBuilder().also { servicingDataBuilder ->147 servicingDataBuilder.clearLoanState()148 servicingDataBuilder.addAllLoanState(randomExistingStates)149 }.build()150 ).appendLoanStates(151 randomNewStates152 ).let { outputRecord ->153 outputRecord.loanStateCount shouldBeExactly randomStateCount154 }155 }156 }157 }158 }159 }160 }161}) {162 override fun testCaseOrder() = TestCaseOrder.Random163}...
products.kt
Source:products.kt
1package io.kotest.property.arbs.products2import io.kotest.property.Arb3import io.kotest.property.arbitrary.flatMap4import io.kotest.property.arbitrary.map5import io.kotest.property.arbitrary.take6import io.kotest.property.arbs.Color7import io.kotest.property.arbs.color8import kotlin.random.Random9data class Product(10 val sku: Sku,11 val brand: Brand,12 val gtin: Gtin,13 val price: Int, // cents14 val quantity: Int,15 val material: String,16 val color: Color?,17 val size: String,18 val name: String,19 val taxonomy: GoogleTaxonomy20)21data class Sku(val value: String)...
wine.kt
Source:wine.kt
1package io.kotest.property.arbs.wine2import io.kotest.property.Arb3import io.kotest.property.arbitrary.flatMap4import io.kotest.property.arbitrary.map5import io.kotest.property.arbitrary.of6import io.kotest.property.arbitrary.take7import io.kotest.property.arbs.Name8import io.kotest.property.arbs.loadResourceAsLines9import io.kotest.property.arbs.name10import kotlin.random.Random11private val vineyards = lazy { loadResourceAsLines("/wine/vineyards.txt") }12private val regions = lazy { loadResourceAsLines("/wine/region.txt") }13private val wineries = lazy { loadResourceAsLines("/wine/winery.txt") }14private val varities = lazy { loadResourceAsLines("/wine/variety.txt") }15fun Arb.Companion.vineyards() = Arb.of(vineyards.value).map { Vineyard(it) }16fun Arb.Companion.wineRegions() = Arb.of(regions.value).map { WineRegion(it) }17fun Arb.Companion.wineries() = Arb.of(wineries.value).map { Winery(it) }18fun Arb.Companion.wineVarities() = Arb.of(varities.value).map { WineVariety(it) }19fun Arb.Companion.wines() = vineyards().flatMap { vineyard ->20 wineRegions().flatMap { region ->21 wineVarities().flatMap { variety ->22 wineries().map { winery ->23 Wine(vineyard, variety, winery, region, Random.nextInt(1920, 2020))...
addresses.kt
Source:addresses.kt
1package io.kotest.property.arbs.places2import io.kotest.property.Arb3import io.kotest.property.arbitrary.bind4import io.kotest.property.arbitrary.element5import io.kotest.property.arbitrary.int6import io.kotest.property.arbs.colours7private val endings = listOf(8 "Avenue",9 "Road",10 "Street",11 "Place",12 "Terrace",13 "Drive",14 "Court",15 "Way",16 "Grove",17 "Walk",18 "Crescent",19 "Mews",20 "Close",...
transactions.kt
Source:transactions.kt
1package io.kotest.property.arbs.payments2import io.kotest.property.Arb3import io.kotest.property.arbitrary.bind4import io.kotest.property.arbitrary.enum5import io.kotest.property.arbitrary.int6import io.kotest.property.arbitrary.long7import io.kotest.property.arbs.geo.Country8import io.kotest.property.arbs.geo.country9import io.kotest.property.kotlinx.datetime.datetime10import kotlinx.datetime.LocalDateTime11enum class CardType {12 Visa, Mastercard, Amex, Discover, Paypal, GooglePay, ApplePay13}14enum class TransactionType {15 Online, InStore, Recurring16}17data class Transaction(18 val date: LocalDateTime,19 val txType: TransactionType,20 val cardNumber: String,21 val cardType: CardType,22 val country: Country,...
harrypotter.kt
Source:harrypotter.kt
1package io.kotest.property.arbs.movies2import io.kotest.property.Arb3import io.kotest.property.arbitrary.arbitrary4import io.kotest.property.arbitrary.take5import io.kotest.property.arbs.loadResourceAsLines6private val first = loadResourceAsLines("/harrypotter_first_names.csv")7private val last = loadResourceAsLines("/harrypotter_last_names.csv")8data class Character(val firstName: String, val lastName: String)9fun Arb.Companion.harryPotterCharacter() = arbitrary {10 Character(first.random(it.random), last.random(it.random))11}12fun main() {13 Arb.harryPotterCharacter().take(100).forEach(::println)14}...
gpc.kt
Source:gpc.kt
1package io.kotest.property.arbs.products2import io.kotest.property.Arb3import io.kotest.property.arbitrary.map4import io.kotest.property.arbitrary.of5import io.kotest.property.arbs.loadResourceAsLines6fun Arb.Companion.googleTaxonomy() = Arb.of(taxonomies.value).map { GoogleTaxonomy(it) }7private val taxonomies = lazy { loadResourceAsLines("/gpc.txt") }8data class GoogleTaxonomy(val value: String)...
brands.kt
Source:brands.kt
1package io.kotest.property.arbs.products2import io.kotest.property.Arb3import io.kotest.property.arbitrary.map4import io.kotest.property.arbitrary.of5import io.kotest.property.arbs.loadResourceAsLines6private val brands = lazy { loadResourceAsLines("/brands.txt") }7data class Brand(val value: String)8fun Arb.Companion.brand() = Arb.of(brands.value).map(::Brand)...
arbs
Using AI Code Generation
1import io.kotest.property.arbitrary.arbs2val arbInt = arbs().int()3val arbString = arbs().string()4val arbIntString = arbs().tuple(arbInt, arbString)5val arbIntStringList = arbs().list(arbIntString)6import io.kotest.property.arbitrary.arbs7val genInt = generators().int()8val genString = generators().string()9val genIntString = generators().tuple(genInt, genString)10val genIntStringList = generators().list(genIntString)11import io.kotest.property.arbitrary.arbs12val arbInt = arbitraries().int()13val arbString = arbitraries().string()14val arbIntString = arbitraries().tuple(arbInt, arbString)15val arbIntStringList = arbitraries().list(arbIntString)16import io.kotest.property.arbitrary.arbs17val genInt = generators().int()18val genString = generators().string()19val genIntString = generators().tuple(genInt, genString)20val genIntStringList = generators().list(genIntString)21import io.kotest.property.arbitrary.arbs22val arbInt = arbitraries().int()23val arbString = arbitraries().string()24val arbIntString = arbitraries().tuple(arbInt, arbString)25val arbIntStringList = arbitraries().list(arbIntString)26import io.kotest.property.arbitrary.arbs27val genInt = generators().int()28val genString = generators().string()29val genIntString = generators().tuple(genInt, genString)30val genIntStringList = generators().list(genIntString)31import io.kotest.property.arbitrary.arbs32val arbInt = arbitraries().int()33val arbString = arbitraries().string()34val arbIntString = arbitraries().tuple(arbInt, arbString)35val arbIntStringList = arbitraries().list(arbIntString)
arbs
Using AI Code Generation
1val arb = Arb.int()2val arb = Arb.int().filter { it > 0 }3val arb = Arb.int().map { it.toString() }4val arb = Arb.int().flatMap { Arb.of(it.toString(), it.toString()) }5val arb = Arb.int().map { it.toString() }.flatMap { Arb.of(it, it) }6val arb = Arb.int().map { it.toString() }.flatMap { Arb.of(it, it) }.filter { it.length > 1 }7val arb = Arb.int().map { it.toString() }.flatMap { Arb.of(it, it) }.filter { it.length > 1 }.edgecases("a", "b")8val arb = Arb.int().map { it.toString() }.flatMap { Arb.of(it, it) }.filter { it.length > 1 }.edgecases("a", "b").shrinkTowards("")9val arb = Arb.int().map { it.toString() }.flatMap { Arb.of(it, it) }.filter { it.length > 1 }.edgecases("a", "b").shrinkTowards("").withEdgecases()10val arb = Arb.int().map { it.toString() }.flatMap { Arb.of(it, it) }.filter { it.length > 1 }.edgecases("a", "b").shrinkTowards("").withEdgecases().noShrink()11val arb = Arb.int().map { it.toString() }.flatMap { Arb.of(it, it) }.filter { it.length > 1 }.edgecases("a", "b").shrinkTowards("").withEdgecases().noShrink().noEdgecases()12val arb = Arb.int().map { it.toString() }.flatMap { Arb.of(it, it) }.filter { it.length > 1 }.edgecases("a", "b").shrinkTowards("").withEdgecases().noShrink().noEdgecases().withShrinker { a, b -> emptyList() }13val arb = Arb.int().map { it.toString() }.flatMap { Arb.of(it, it) }.filter { it.length > 1 }.edgecases("a", "b").shrinkTowards("").withEdgecases().noShrink().noEdgecases().withShrinker { a, b -> emptyList() }.withEdgecasesShrinker { a, b -> emptyList() }14val arb = Arb.int().map { it.toString() }.flatMap { Arb.of(it, it) }.filter { it.length >
arbs
Using AI Code Generation
1val arb = Arb.int(1..100)2val arb2 = Arb.int(1..100)3val arb3 = Arb.int(1..100)4val gen = Gen.bind(arb, arb2, arb3) { a, b, c -> Triple(a, b, c) }5val result = gen.take(1000).toList()6val result2 = Gen.bind(arb, arb2, arb3) { a, b, c -> Triple(a, b, c) }.take(1000).toList()7val result3 = Gen.bind(Arb.int(1..100), Arb.int(1..100), Arb.int(1..100)) { a, b, c -> Triple(a, b, c) }.take(1000).toList()8@Deprecated("This function is deprecated and will be removed in 4.0. Please use Arb.bind instead",9 ReplaceWith("Arb.bind(arbA, arbB, arbC, arbD, arbE, arbF, arbG, arbH, arbI, arbJ) { a, b, c, d, e, f, g, h, i, j -> func(a, b, c, d, e, f, g, h, i, j) }"))10fun <A, B, C, D, E, F, G, H, I, J, R> bind(arbA: Arb<A>, arbB: Arb<B>, arbC: Arb<C>, arbD: Arb<D>, arbE: Arb<E>, arbF: Arb<F>, arbG: Arb<G>, arbH: Arb<H>, arbI: Arb<I>, arbJ: Arb<J>, func: (A, B, C, D, E, F, G, H, I, J) -> R): Arb<R> =11Arb.bind(arbA, arbB, arbC, arbD, arbE, arbF, arbG, arbH, arbI, arbJ) { a, b, c, d, e, f, g, h, i, j -> func(a, b, c, d, e, f, g, h, i, j) }12@Deprecated("This function is deprecated and will be removed in 4.0. Please use Arb.bind instead",13 ReplaceWith("Arb.bind
arbs
Using AI Code Generation
1 fun `test using kotest property`() {2 forAll<Int> { n ->3 }4 }5 fun `test using kotest property`() {6 forAll<Int> { n ->7 }8 }
arbs
Using AI Code Generation
1val arb = Arb.string(1..10, Arb.alpha())2val arb = Arb.string(1..10, Arb.alpha())3val s = arb.generate()4val seq = arb.generateSequence()5val list = arb.generateList(10)6val set = arb.generateSet(10)7val map = arb.generateMap(10, Arb.string(1..10, Arb.alpha()))8val seq = arb.generateSequence()9val list = arb.generateList(10)10val set = arb.generateSet(10)11val map = arb.generateMap(10, Arb.string(1..10, Arb.alpha()))12val seq = arb.generateSequence()13val list = arb.generateList(10)14val set = arb.generateSet(10)15val map = arb.generateMap(10, Arb.string(1..10, Arb.alpha()))16val seq = arb.generateSequence()17val list = arb.generateList(10)18val set = arb.generateSet(10)19val map = arb.generateMap(10, Arb.string(1..10, Arb.alpha()))20val seq = arb.generateSequence()21val list = arb.generateList(10)22val set = arb.generateSet(10)23val map = arb.generateMap(10, Arb.string(1..10, Arb.alpha()))
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!!