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

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

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

IorModuleTest.kt

Source:IorModuleTest.kt Github

copy

Full Screen

1package arrow.integrations.jackson.module2import arrow.core.Ior3import arrow.core.Option4import arrow.core.bothIor5import arrow.core.leftIor6import arrow.core.rightIor7import arrow.core.test.generators.ior8import arrow.core.test.generators.option9import com.fasterxml.jackson.annotation.JsonProperty10import com.fasterxml.jackson.databind.ObjectMapper11import com.fasterxml.jackson.module.kotlin.registerKotlinModule12import io.kotest.assertions.throwables.shouldNotThrowAny13import io.kotest.core.spec.style.FunSpec14import io.kotest.matchers.shouldBe15import io.kotest.property.Arb16import io.kotest.property.arbitrary.Codepoint17import io.kotest.property.arbitrary.alphanumeric18import io.kotest.property.arbitrary.arbitrary19import io.kotest.property.arbitrary.az20import io.kotest.property.arbitrary.boolean21import io.kotest.property.arbitrary.enum22import io.kotest.property.arbitrary.filter23import io.kotest.property.arbitrary.int24import io.kotest.property.arbitrary.orNull25import io.kotest.property.arbitrary.pair26import io.kotest.property.arbitrary.string27import io.kotest.property.checkAll28class IorModuleTest : FunSpec() {29 init {30 context("serialization/deserialization") {31 test("should round-trip on mandatory types") {32 checkAll(arbTestClass) { it.shouldRoundTrip(mapper) }33 }34 test("should serialize in the expected format") {35 checkAll(arbTestClassJsonString) { it.shouldRoundTripOtherWay<TestClass>(mapper) }36 }37 test("should round-trip nullable types") {38 checkAll(Arb.ior(arbFoo.orNull(), arbBar.orNull())) { ior: Ior<Foo?, Bar?> ->39 ior.shouldRoundTrip(mapper)40 }41 }42 test("should round-trip nested ior types") {43 checkAll(44 Arb.ior(Arb.ior(arbFoo, Arb.int()).orNull(), Arb.ior(Arb.string(), arbBar.orNull()))45 ) { ior: Ior<Ior<Foo, Int>?, Ior<String, Bar?>> -> ior.shouldRoundTrip(mapper) }46 }47 test("should serialize with configurable left / right field name") {48 checkAll(49 Arb.pair(Arb.string(10, Codepoint.az()), Arb.string(10, Codepoint.az())).filter {50 it.first != it.second51 }52 ) { (leftFieldName, rightFieldName) ->53 val mapper =54 ObjectMapper()55 .registerKotlinModule()56 .registerArrowModule(iorModuleConfig = IorModuleConfig(leftFieldName, rightFieldName))57 mapper.writeValueAsString(5.leftIor()) shouldBe """{"$leftFieldName":5}"""58 mapper.writeValueAsString("hello".rightIor()) shouldBe """{"$rightFieldName":"hello"}"""59 mapper.writeValueAsString(Pair(5, "hello").bothIor()) shouldBe60 """{"$leftFieldName":5,"$rightFieldName":"hello"}"""61 }62 }63 test("should round-trip with configurable left / right field name") {64 checkAll(65 Arb.pair(66 Arb.string(10, Codepoint.az()),67 Arb.string(10, Codepoint.az()),68 )69 .filter { it.first != it.second },70 arbTestClass71 ) { (leftFieldName, rightFieldName), testClass ->72 val mapper =73 ObjectMapper()74 .registerKotlinModule()75 .registerArrowModule(76 eitherModuleConfig = EitherModuleConfig(leftFieldName, rightFieldName)77 )78 testClass.shouldRoundTrip(mapper)79 }80 }81 test("should round-trip with wildcard types") {82 checkAll(Arb.ior(Arb.int(1..10), Arb.string(10, Codepoint.az()))) { original: Ior<*, *> ->83 val mapper = ObjectMapper().registerKotlinModule().registerArrowModule()84 val serialized = mapper.writeValueAsString(original)85 val deserialized: Ior<*, *> = shouldNotThrowAny {86 mapper.readValue(serialized, Ior::class.java)87 }88 deserialized shouldBe original89 }90 }91 }92 }93 private enum class IorPolarity {94 Left,95 Both,96 Right97 }98 private val arbTestClassJsonString = arbitrary {99 when (Arb.enum<IorPolarity>().bind()) {100 IorPolarity.Left -> {101 val foo = arbFoo.bind()102 """103 {104 "ior": {105 "left": {106 "foo": ${foo.fooValue.orNull()},107 "otherValue": ${mapper.writeValueAsString(foo.otherValue)}108 }109 }110 }111 """.trimIndent()112 }113 IorPolarity.Both -> {114 val foo = arbFoo.bind()115 val bar = arbBar.bind()116 """117 {118 "ior": {119 "left": {120 "foo": ${foo.fooValue.orNull()},121 "otherValue": ${mapper.writeValueAsString(foo.otherValue)}122 },123 "right": {124 "first": ${bar.first},125 "second": "${bar.second}",126 "third": ${bar.third}127 }128 }129 }130 """.trimIndent()131 }132 IorPolarity.Right -> {133 val bar = arbBar.bind()134 """135 {136 "ior": {137 "right": {138 "first": ${bar.first},139 "second": "${bar.second}",140 "third": ${bar.third}141 }142 }143 }144 """.trimIndent()145 }146 }147 }148 private data class Foo(149 @get:JsonProperty("foo") val fooValue: Option<Int>,150 val otherValue: String151 )152 private data class Bar(val first: Int, val second: String, val third: Boolean)153 private data class TestClass(val ior: Ior<Foo, Bar>)154 private val arbFoo: Arb<Foo> = arbitrary {155 Foo(Arb.option(Arb.int()).bind(), Arb.string().bind())156 }157 private val arbBar: Arb<Bar> = arbitrary {158 Bar(Arb.int().bind(), Arb.string(0..100, Codepoint.alphanumeric()).bind(), Arb.boolean().bind())159 }160 private val arbTestClass: Arb<TestClass> = arbitrary { TestClass(Arb.ior(arbFoo, arbBar).bind()) }161 private val mapper = ObjectMapper().registerKotlinModule().registerArrowModule()162}...

Full Screen

Full Screen

ValidatedModuleTest.kt

Source:ValidatedModuleTest.kt Github

copy

Full Screen

1package arrow.integrations.jackson.module2import arrow.core.Option3import arrow.core.Validated4import arrow.core.invalid5import arrow.core.test.generators.option6import arrow.core.test.generators.validated7import arrow.core.valid8import com.fasterxml.jackson.annotation.JsonProperty9import com.fasterxml.jackson.databind.ObjectMapper10import com.fasterxml.jackson.module.kotlin.registerKotlinModule11import io.kotest.assertions.throwables.shouldNotThrowAny12import io.kotest.core.spec.style.FunSpec13import io.kotest.matchers.shouldBe14import io.kotest.property.Arb15import io.kotest.property.arbitrary.Codepoint16import io.kotest.property.arbitrary.alphanumeric17import io.kotest.property.arbitrary.arbitrary18import io.kotest.property.arbitrary.az19import io.kotest.property.arbitrary.boolean20import io.kotest.property.arbitrary.filter21import io.kotest.property.arbitrary.int22import io.kotest.property.arbitrary.orNull23import io.kotest.property.arbitrary.pair24import io.kotest.property.arbitrary.string25import io.kotest.property.checkAll26class ValidatedModuleTest : FunSpec() {27 init {28 context("json serialization / deserialization") {29 test("should round-trip") { checkAll(arbTestClass) { it.shouldRoundTrip(mapper) } }30 test("should round-trip nullable types") {31 checkAll(Arb.validated(arbFoo.orNull(), arbBar.orNull())) { validated: Validated<Foo?, Bar?>32 ->33 validated.shouldRoundTrip(mapper)34 }35 }36 test("should round-trip other way") {37 checkAll(arbTestClassJsonString) { it.shouldRoundTripOtherWay<TestClass>(mapper) }38 }39 test("should round-trip nested validated types") {40 checkAll(Arb.validated(Arb.int(), Arb.validated(Arb.int(), Arb.string()).orNull())) {41 validated: Validated<Int, Validated<Int, String>?> ->42 validated.shouldRoundTrip(mapper)43 }44 }45 test("should serialize with configurable invalid / valid field name") {46 checkAll(47 Arb.pair(Arb.string(10, Codepoint.az()), Arb.string(10, Codepoint.az())).filter {48 it.first != it.second49 }50 ) { (invalidFieldName, validFieldName) ->51 val mapper =52 ObjectMapper()53 .registerKotlinModule()54 .registerArrowModule(55 validatedModuleConfig = ValidatedModuleConfig(invalidFieldName, validFieldName)56 )57 mapper.writeValueAsString(5.invalid()) shouldBe """{"$invalidFieldName":5}"""58 mapper.writeValueAsString("hello".valid()) shouldBe """{"$validFieldName":"hello"}"""59 }60 }61 test("should round-trip with configurable invalid / valid field name") {62 checkAll(63 Arb.pair(Arb.string(10, Codepoint.az()), Arb.string(10, Codepoint.az())).filter {64 it.first != it.second65 },66 arbTestClass67 ) { (invalidFieldName, validFieldName), testClass ->68 val mapper =69 ObjectMapper()70 .registerKotlinModule()71 .registerArrowModule(EitherModuleConfig(invalidFieldName, validFieldName))72 testClass.shouldRoundTrip(mapper)73 }74 }75 test("should round-trip with wildcard types") {76 checkAll(Arb.validated(Arb.int(1..10), Arb.string(10, Codepoint.az()))) {77 original: Validated<*, *> ->78 val mapper = ObjectMapper().registerKotlinModule().registerArrowModule()79 val serialized = mapper.writeValueAsString(original)80 val deserialized: Validated<*, *> = shouldNotThrowAny {81 mapper.readValue(serialized, Validated::class.java)82 }83 deserialized shouldBe original84 }85 }86 }87 }88 private val arbTestClassJsonString = arbitrary {89 if (Arb.boolean().bind()) {90 val foo = arbFoo.bind()91 """92 {93 "validated": {94 "invalid": {95 "foo": ${foo.fooValue.orNull()},96 "otherValue": ${mapper.writeValueAsString(foo.otherValue)}97 }98 }99 }100 """.trimIndent()101 } else {102 val bar = arbBar.bind()103 """104 {105 "validated": {106 "valid": {107 "first": ${bar.first},108 "second": "${bar.second}",109 "third": ${bar.third}110 }111 }112 }113 """.trimIndent()114 }115 }116 private data class Foo(117 @get:JsonProperty("foo") val fooValue: Option<Int>,118 val otherValue: String119 )120 private data class Bar(val first: Int, val second: String, val third: Boolean)121 private data class TestClass(val validated: Validated<Foo, Bar>)122 private val arbFoo: Arb<Foo> = arbitrary {123 Foo(Arb.option(Arb.int()).bind(), Arb.string().bind())124 }125 private val arbBar: Arb<Bar> = arbitrary {126 Bar(Arb.int().bind(), Arb.string(0..100, Codepoint.alphanumeric()).bind(), Arb.boolean().bind())127 }128 private val arbTestClass: Arb<TestClass> = arbitrary {129 TestClass(Arb.validated(arbFoo, arbBar).bind())130 }131 private val mapper = ObjectMapper().registerKotlinModule().registerArrowModule()132}...

Full Screen

Full Screen

EitherModuleTest.kt

Source:EitherModuleTest.kt Github

copy

Full Screen

1package arrow.integrations.jackson.module2import arrow.core.Either3import arrow.core.Option4import arrow.core.left5import arrow.core.right6import arrow.core.test.generators.either7import arrow.core.test.generators.option8import com.fasterxml.jackson.annotation.JsonProperty9import com.fasterxml.jackson.databind.ObjectMapper10import com.fasterxml.jackson.module.kotlin.registerKotlinModule11import io.kotest.assertions.throwables.shouldNotThrowAny12import io.kotest.core.spec.style.FunSpec13import io.kotest.matchers.shouldBe14import io.kotest.property.Arb15import io.kotest.property.arbitrary.Codepoint16import io.kotest.property.arbitrary.alphanumeric17import io.kotest.property.arbitrary.arbitrary18import io.kotest.property.arbitrary.az19import io.kotest.property.arbitrary.boolean20import io.kotest.property.arbitrary.filter21import io.kotest.property.arbitrary.int22import io.kotest.property.arbitrary.orNull23import io.kotest.property.arbitrary.pair24import io.kotest.property.arbitrary.string25import io.kotest.property.checkAll26class EitherModuleTest : FunSpec() {27 init {28 context("either serialization/deserialization") {29 test("should round-trip on mandatory types") {30 checkAll(arbTestClass) { it.shouldRoundTrip(mapper) }31 }32 test("should serialize in the expected format") {33 checkAll(arbTestClassJsonString) { it.shouldRoundTripOtherWay<TestClass>(mapper) }34 }35 test("should round-trip nullable types") {36 checkAll(Arb.either(arbFoo.orNull(), arbBar.orNull())) { either: Either<Foo?, Bar?> ->37 either.shouldRoundTrip(mapper)38 }39 }40 test("should round-trip nested either types") {41 checkAll(42 Arb.either(43 Arb.either(arbFoo, Arb.int()).orNull(),44 Arb.either(Arb.string(), arbBar.orNull())45 )46 ) { either: Either<Either<Foo, Int>?, Either<String, Bar?>> ->47 either.shouldRoundTrip(mapper)48 }49 }50 test("should serialize with configurable left / right field name") {51 checkAll(52 Arb.pair(Arb.string(10, Codepoint.az()), Arb.string(10, Codepoint.az())).filter {53 it.first != it.second54 }55 ) { (leftFieldName, rightFieldName) ->56 val mapper =57 ObjectMapper()58 .registerKotlinModule()59 .registerArrowModule(EitherModuleConfig(leftFieldName, rightFieldName))60 mapper.writeValueAsString(5.left()) shouldBe """{"$leftFieldName":5}"""61 mapper.writeValueAsString("hello".right()) shouldBe """{"$rightFieldName":"hello"}"""62 }63 }64 test("should round-trip with configurable left / right field name") {65 checkAll(66 Arb.pair(67 Arb.string(10, Codepoint.az()),68 Arb.string(10, Codepoint.az()),69 )70 .filter { it.first != it.second },71 arbTestClass72 ) { (leftFieldName, rightFieldName), testClass ->73 val mapper =74 ObjectMapper()75 .registerKotlinModule()76 .registerArrowModule(77 eitherModuleConfig = EitherModuleConfig(leftFieldName, rightFieldName)78 )79 testClass.shouldRoundTrip(mapper)80 }81 }82 test("should round-trip on wildcard types") {83 val mapper = ObjectMapper().registerArrowModule()84 checkAll(Arb.either(Arb.int(1..10), Arb.string(5))) { original: Either<*, *> ->85 val serialized = mapper.writeValueAsString(original)86 val deserialized = shouldNotThrowAny { mapper.readValue(serialized, Either::class.java) }87 deserialized shouldBe original88 }89 }90 }91 }92 private val arbTestClassJsonString = arbitrary {93 if (Arb.boolean().bind()) {94 val foo = arbFoo.bind()95 """96 {97 "either": {98 "left": {99 "foo": ${foo.fooValue.orNull()},100 "otherValue": ${mapper.writeValueAsString(foo.otherValue)}101 }102 }103 }104 """.trimIndent()105 } else {106 val bar = arbBar.bind()107 """108 {109 "either": {110 "right": {111 "first": ${bar.first},112 "second": "${bar.second}",113 "third": ${bar.third}114 }115 }116 }117 """.trimIndent()118 }119 }120 private data class Foo(121 @get:JsonProperty("foo") val fooValue: Option<Int>,122 val otherValue: String123 )124 private data class Bar(val first: Int, val second: String, val third: Boolean)125 private data class TestClass(val either: Either<Foo, Bar>)126 private val arbFoo: Arb<Foo> = arbitrary {127 Foo(Arb.option(Arb.int()).bind(), Arb.string().bind())128 }129 private val arbBar: Arb<Bar> = arbitrary {130 Bar(Arb.int().bind(), Arb.string(0..100, Codepoint.alphanumeric()).bind(), Arb.boolean().bind())131 }132 private val arbTestClass: Arb<TestClass> = arbitrary {133 TestClass(Arb.either(arbFoo, arbBar).bind())134 }135 private val mapper = ObjectMapper().registerKotlinModule().registerArrowModule()136}...

Full Screen

Full Screen

ArticleInformationTest.kt

Source:ArticleInformationTest.kt Github

copy

Full Screen

1package com.sierisimo.devto2import com.sierisimo.devto.data.ArticleInformation3import com.sierisimo.devto.data.articleOf4import com.sierisimo.devto.data.requireValidBody5import com.sierisimo.devto.data.requireValidTitle6import io.kotest.assertions.throwables.shouldThrow7import io.kotest.core.spec.style.FunSpec8import io.kotest.core.spec.style.ShouldSpec9import io.kotest.matchers.shouldBe10import io.kotest.property.Arb11import io.kotest.property.arbitrary.filter12import io.kotest.property.arbitrary.list13import io.kotest.property.arbitrary.single14import io.kotest.property.arbitrary.string15import io.kotest.property.forAll16internal class ArticleOfTest : ShouldSpec({17 should("fail when title is empty") {18 shouldThrow<IllegalArgumentException> {19 articleOf(20 title = "",21 markdown = ""22 )23 }24 }25 should("fail when markdown is empty") {26 shouldThrow<IllegalArgumentException> {27 articleOf(28 title = "Test",29 markdown = ""30 )31 }32 }33 should("add corresponding and valid tags as list to the final article") {34 forAll(35 Arb.string(minSize = 1).filter { it.isNotBlank() },36 Arb.string(minSize = 1).filter { it.isNotBlank() },37 Arb.list(Arb.string())38 ) { title, desc, tagList ->39 articleOf(40 title,41 desc,42 tagList43 ) == ArticleInformation(44 title,45 desc,46 tagList.filter { it.isNotBlank() })47 }48 }49})50internal class ArticleInformationTest : FunSpec({51 context("The ArticleInformation") {52 test("fails validation when title is empty") {53 val article =54 ArticleInformation("", Arb.string().single(), emptyList())55 val exception = shouldThrow<IllegalArgumentException> { article.requireValidTitle() }56 exception.message shouldBe "Title cannot be blank or empty for a new article"57 }58 test("fail validation when body is empty") {59 val article =60 ArticleInformation(61 Arb.string(minSize = 1).filter { it.isNotBlank() }.single(), "", emptyList()62 )63 val exception = shouldThrow<IllegalArgumentException> { article.requireValidBody() }64 exception.message shouldBe "Creation of article requires a body in markdown text"65 }66 }67})...

Full Screen

Full Screen

Arb.kt

Source:Arb.kt Github

copy

Full Screen

1package utils2import io.kotest.property.Arb3import io.kotest.property.Exhaustive4import io.kotest.property.arbitrary.arbitrary5import io.kotest.property.arbitrary.byte6import io.kotest.property.arbitrary.byteArrays7import io.kotest.property.arbitrary.filter8import io.kotest.property.arbitrary.int9import io.kotest.property.arbitrary.merge10import kotlin.random.nextUInt11import kotlin.random.nextULong12operator fun <A> Arb<A>.plus(other: Arb<A>) =13 merge(other)14operator fun <A> Arb<A>.minus(other: Exhaustive<A>) =15 filter { it !in other.values }16operator fun <V : Comparable<V>> Arb<V>.minus(range: ClosedRange<V>) =17 filter { it !in range }18fun Arb.Companion.byteArrays(length: Int) =19 byteArrays(length..length)20fun Arb.Companion.byteArrays(length: IntRange) =21 Arb.byteArrays(Arb.int(length), Arb.byte())22fun Arb.Companion.uint(range: UIntRange = UInt.MIN_VALUE..UInt.MAX_VALUE) = arbitrary(listOf(range.first, range.last)) {23 it.random.nextUInt(range)24}25fun Arb.Companion.ulong() = arbitrary(listOf(ULong.MIN_VALUE, ULong.MAX_VALUE)) {26 it.random.nextULong()27}28inline fun <reified T> Arb.Companion.arrayOf(value: Arb<T>, length: IntRange): Arb<Array<T>> = arbitrary { rs ->29 Array(rs.random.nextInt(length.first, length.last)) {30 value.sample(rs).value31 }32}...

Full Screen

Full Screen

LeapYearTest.kt

Source:LeapYearTest.kt Github

copy

Full Screen

1import io.kotest.core.spec.style.ExpectSpec2import io.kotest.matchers.shouldBe3import io.kotest.property.Arb4import io.kotest.property.arbitrary.filter5import io.kotest.property.arbitrary.filterNot6import io.kotest.property.arbitrary.int7import io.kotest.property.checkAll8class LeapYearTest : ExpectSpec({9 context("A year is not a leap year") {10 expect("if not divisible by 4") {11 checkAll(Arb.int().filterNot { it isDivisibleBy 4 }) { year ->12 isLeapYear(year) shouldBe false13 }14 }15 expect("if divisible by 100 but not by 400") {16 checkAll(Arb.int().filter { (it isDivisibleBy 100) && !(it isDivisibleBy 400) }) { year ->17 isLeapYear(year) shouldBe false18 }19 }20 }21 context("A year is a leap year") {22 expect("if divisible by 4 but not by 100") {23 checkAll(Arb.int().filter { (it isDivisibleBy 4) && !(it isDivisibleBy 100) }) { year ->24 isLeapYear(year) shouldBe true25 }26 }27 expect("if divisible by 400") {28 checkAll(Arb.int().filter { it isDivisibleBy 400 }) { year ->29 isLeapYear(year) shouldBe true30 }31 }32 }33})...

Full Screen

Full Screen

PagingState.kt

Source:PagingState.kt Github

copy

Full Screen

1package uk.co.appsplus.bootstrap.testing.arbs.ext2import io.kotest.property.Arb3import io.kotest.property.arbitrary.enum4import io.kotest.property.arbitrary.filterNot5import io.kotest.property.arbitrary.of6import uk.co.appsplus.bootstrap.ui.pagination.PagingState7fun Arb.Companion.loadingState(8 onlyStates: List<PagingState>? = null,9 removedStates: List<PagingState> = emptyList()10): Arb<PagingState> {11 return onlyStates?.let {12 Arb.of(it)13 } ?: Arb.enum<PagingState>()14 .filterNot { it in removedStates }15}...

Full Screen

Full Screen

Arb.filter

Using AI Code Generation

copy

Full Screen

1 fun testFilter() {2 Arb.filter(Arb.int()) { it > 0 }.checkAll { it > 0 }3 }4 fun testFilterNot() {5 Arb.filterNot(Arb.int()) { it > 0 }.checkAll { it <= 0 }6 }7 fun testFilterNot1() {8 Arb.filterNot(Arb.int(), { it > 0 }).checkAll { it <= 0 }9 }10 fun testFilter1() {11 Arb.filter(Arb.int(), { it > 0 }).checkAll { it > 0 }12 }13 fun testFilterNot2() {14 Arb.filterNot(Arb.int(), { it > 0 }, 10).checkAll { it <= 0 }15 }16 fun testFilter2() {17 Arb.filter(Arb.int(), { it > 0 }, 10).checkAll { it > 0 }18 }19 fun testFilterNot3() {20 Arb.filterNot(Arb.int(), { it > 0 }, 10, 10).checkAll { it <= 0 }21 }22 fun testFilter3() {23 Arb.filter(Arb.int(), { it > 0 }, 10, 10).checkAll { it > 0 }24 }25 fun testFilterNot4() {26 Arb.filterNot(Arb.int(), { it > 0 }, 10, 10, 10).checkAll { it <= 0 }27 }

Full Screen

Full Screen

Arb.filter

Using AI Code Generation

copy

Full Screen

1fun `filtering an arbitrary`() {2 val arb = Arb.int().filter { it > 0 }3 arb.checkAll { it > 0 }4}5val arb = Arb.int().list(1..10).filter { it.all { it > 0 } }6arb.checkAll { it.all { it > 0 } }7val arb = Arb.int().map(1..10).filter { it.all { it.value > 0 } }8arb.checkAll { it.all { it.value > 0 } }9val arb = Arb.int().set(1..10).filter { it.all { it > 0 } }10arb.checkAll { it.all { it > 0 } }11val arb = Arb.int().sequence(1..10).filter { it.all { it > 0 } }12arb.checkAll { it.all { it > 0 } }13val arb = Arb.int().pair().filter { it.first > 0 && it.second > 0 }14arb.checkAll { it.first > 0 && it.second > 0 }15val arb = Arb.int().triple().filter { it.first > 0 && it.second > 0 && it.third > 0 }16arb.checkAll { it.first > 0 && it.second > 0 && it.third > 0 }17val arb = Arb.int().quad().filter { it.first > 0 && it.second > 0 && it.third > 0 && it.fourth > 0 }18arb.checkAll { it.first > 0 && it.second > 0 && it.third > 0 && it.fourth > 0 }19val arb = Arb.int().quint().filter { it.first > 0 && it.second > 0 && it.third > 0 && it.fourth > 0 && it.fifth > 0 }20arb.checkAll { it.first > 0 && it.second > 0 && it.third > 0 && it.fourth > 0 && it.fifth >

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