Best Kotest code snippet using io.kotest.property.arbitrary.filter
IorModuleTest.kt
Source:IorModuleTest.kt
...18import 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()...
ValidatedModuleTest.kt
Source:ValidatedModuleTest.kt
...16import 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<*, *> ->...
EitherModuleTest.kt
Source:EitherModuleTest.kt
...16import 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<*, *> ->...
SnowflakeTest.kt
Source:SnowflakeTest.kt
...7import io.kotest.matchers.string.shouldEndWith8import io.kotest.property.Arb9import io.kotest.property.Exhaustive10import io.kotest.property.arbitrary.arbitrary11import io.kotest.property.arbitrary.filter12import io.kotest.property.arbitrary.long13import io.kotest.property.checkAll14import io.kotest.property.exhaustive.azstring15import org.tesserakt.diskordin.core.data.Snowflake.ConstructionError.LessThenDiscordEpoch16import org.tesserakt.diskordin.core.data.Snowflake.ConstructionError.NotNumber17import kotlin.random.nextLong18private const val MIN_SNOWFLAKE = 4194305L19@ExperimentalUnsignedTypes20class SnowflakeTest : FunSpec() {21 private val snowflakes = arbitrary { it.random.nextLong(MIN_SNOWFLAKE..Long.MAX_VALUE) }22 init {23 test("Snowflake.toString should return number in string") {24 checkAll(snowflakes) {25 it.asSnowflake().toString() shouldBe "$it"26 }27 }28 context("Non-safe converts should throw errors comparing their rules") {29 test("String, that hasn't digits") {30 checkAll(Exhaustive.azstring(1..30)) {31 shouldThrow<IllegalArgumentException> {32 it.asSnowflake()33 }.message shouldEndWith "cannot be represented as Snowflake"34 }35 }36 test("Number, that less then 0") {37 checkAll(Arb.long(max = 0).filter { it < 0 }) {38 shouldThrow<IllegalArgumentException> {39 it.asSnowflake()40 }.message shouldBe "id must be greater than 0"41 }42 }43 test("Number, that less then $MIN_SNOWFLAKE") {44 checkAll(arbitrary { it.random.nextLong(MIN_SNOWFLAKE) }) {45 shouldThrow<IllegalArgumentException> {46 it.asSnowflake()47 }.message shouldBe "id must be greater than ${MIN_SNOWFLAKE - 1}"48 }49 }50 }51 context("Safe converts should wrap error in data-type") {...
ArticleInformationTest.kt
Source:ArticleInformationTest.kt
...7import 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})...
Arb.kt
Source:Arb.kt
...3import 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 }...
LeapYearTest.kt
Source:LeapYearTest.kt
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})...
PagingState.kt
Source:PagingState.kt
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}...
filter
Using AI Code Generation
1 property("filter") {2 Arb.filter(Arb.int(1..10)) { it % 2 == 0 }.take(100).forEach {3 }4 }5 property("combine") {6 val a = Arb.int(1..10)7 val b = Arb.int(1..10)8 val c = Arb.combine(a, b) { a, b -> a + b }9 c.take(100).forEach {10 it.shouldBeLessThanOrEqual(20)11 }12 }13 property("create") {14 val a = Arb.create { it.random.nextInt(1, 10) }15 a.take(100).forEach {16 it.shouldBeLessThanOrEqual(10)17 }18 }19 property("create edge cases") {20 val a = Arb.createEdgecases(1, 2, 3, 4, 5)21 a.take(100).forEach {22 it.shouldBeOneOf(1, 2, 3, 4, 5)23 }24 }25 property("create edge cases") {26 val a = Arb.createEdgecases(1, 2, 3, 4, 5)27 a.take(100).forEach {28 it.shouldBeOneOf(1, 2, 3, 4, 5)29 }30 }31 property("create edge cases") {32 val a = Arb.createEdgecases(1, 2, 3, 4, 5)33 a.take(100).forEach {34 it.shouldBeOneOf(1, 2, 3, 4, 5)35 }36 }37 property("create edge cases") {38 val a = Arb.createEdgecases(1, 2, 3, 4, 5)
filter
Using AI Code Generation
1 val smallInts = Arb.filter(Arb.int(), { it > 0 && it < 100 })2 val smallInts = Arb.filter(Arb.int(), { it > 0 && it < 100 })3 val smallInts = Arb.filter(Arb.int(), { it > 0 && it < 100 })4 val smallInts = Arb.filter(Arb.int(), { it > 0 && it < 100 })5 val smallInts = Arb.filter(Arb.int(), { it > 0 && it < 100 })6 val smallInts = Arb.filter(Arb.int(), { it > 0 && it < 100 })7 val smallInts = Arb.filter(Arb.int(), { it > 0 && it < 100 })8 val smallInts = Arb.filter(Arb.int(), { it > 0 && it < 100 })9 val smallInts = Arb.filter(Arb.int(), { it > 0 && it < 100 })10 val smallInts = Arb.filter(Arb.int(), { it > 0 && it < 100 })11 val smallInts = Arb.filter(Arb.int(), { it > 0 && it < 100 })12 val smallInts = Arb.filter(Arb.int(), { it > 0 && it < 100 })13 val smallInts = Arb.filter(Arb.int(), { it > 0 &&
filter
Using AI Code Generation
1val filter = Filter { it > 4 }2val intGen = Gen.int().filter(filter)3val intGen = Gen.int().filter { it > 4 }4val filter = Filter { it > 4 }5val intGen = Gen.int().filter(filter)6val intGen = Gen.int().filter { it > 4 }7val filter = Filter { it > 4 }8val intGen = Gen.int().filter(filter)9val intGen = Gen.int().filter { it > 4 }10val filter = Filter { it > 4 }11val intGen = Gen.int().filter(filter)12val intGen = Gen.int().filter { it > 4 }13val filter = Filter { it > 4 }14val intGen = Gen.int().filter(filter)15val intGen = Gen.int().filter { it > 4 }16val filter = Filter { it > 4 }17val intGen = Gen.int().filter(filter)18val intGen = Gen.int().filter { it > 4 }19val filter = Filter { it > 4 }20val intGen = Gen.int().filter(filter)21val intGen = Gen.int().filter { it > 4 }22val filter = Filter { it > 4 }23val intGen = Gen.int().filter(filter)24val intGen = Gen.int().filter { it > 4 }25val filter = Filter { it > 4 }26val intGen = Gen.int().filter(filter)27val intGen = Gen.int().filter { it > 4 }28val filter = Filter { it > 4 }29val intGen = Gen.int().filter(filter)30val intGen = Gen.int().filter { it > 4 }
filter
Using AI Code Generation
1val intList = Arb.filter(1..100) { it % 2 == 0 }.list(10).generate()2val intList = Arb.filter(1..100) { it % 2 == 0 }.list(10).generate()3val intList = Arb.filter(1..100) { it % 2 == 0 }.list(10).generate()4val intList = Arb.filter(1..100) { it % 2 == 0 }.list(10).generate()5val intList = Arb.filter(1..100) { it % 2 == 0 }.list(10).generate()6val intList = Arb.filter(1..100) { it % 2 == 0 }.list(10).generate()7val intList = Arb.filter(1..100) { it % 2 == 0 }.list(10).generate()8val intList = Arb.filter(1..100) { it % 2 == 0 }.list(10).generate()9val intList = Arb.filter(1..100) { it % 2 == 0 }.list(10).generate()10val intList = Arb.filter(1..100) { it % 2 == 0 }.list(10).generate()
filter
Using AI Code Generation
1val sample = Gen.string().filter { it.length > 3 }.random()2println(sample)3val sample2 = Gen.string().filter { it.length > 3 }.random()4println(sample2)5val sample3 = Gen.string().filter { it.length > 3 }.random()6println(sample3)7val sample4 = Gen.string().filter { it.length > 3 }.random()8println(sample4)9val sample5 = Gen.string().filter { it.length > 3 }.random()10println(sample5)11val sample6 = Gen.string().filter { it.length > 3 }.random()12println(sample6)13val sample7 = Gen.string().filter { it.length > 3 }.random()14println(sample7)15val sample8 = Gen.string().filter { it.length > 3 }.random()16println(sample8)
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!!