Best Kotest code snippet using io.kotest.property.exhaustive.char
ChangeGenerators.kt
Source:ChangeGenerators.kt
1package liquibase.ext.generators2import io.kotest.property.Arb3import io.kotest.property.arbitrary.arbitrary4import io.kotest.property.arbitrary.bind5import io.kotest.property.arbitrary.bool6import io.kotest.property.arbitrary.int7import io.kotest.property.arbitrary.map8import io.kotest.property.arbitrary.next9import io.kotest.property.arbitrary.orNull10import io.kotest.property.arbitrary.stringPattern11import io.kotest.property.arbitrary.take12import io.kotest.property.exhaustive.exhaustive13import io.mockk.every14import io.mockk.spyk15import liquibase.change.AddColumnConfig16import liquibase.change.core.CreateIndexChange17import liquibase.change.core.DropColumnChange18import liquibase.change.core.DropForeignKeyConstraintChange19import liquibase.change.core.DropIndexChange20import liquibase.change.core.DropPrimaryKeyChange21import liquibase.change.core.DropUniqueConstraintChange22import liquibase.database.core.DerbyDatabase23import liquibase.database.core.FirebirdDatabase24import liquibase.database.core.H2Database25import liquibase.database.core.HsqlDatabase26import liquibase.database.core.MSSQLDatabase27import liquibase.database.core.MySQLDatabase28import liquibase.database.core.OracleDatabase29import liquibase.database.core.PostgresDatabase30import liquibase.database.core.SQLiteDatabase31import liquibase.database.core.SybaseDatabase32import liquibase.ext.rewrites.CreateIndexOnline33import liquibase.ext.rewrites.DropColumnOnline34import liquibase.ext.rewrites.DropForeignKeyConstraintOnline35import liquibase.ext.rewrites.DropIndexOnline36import liquibase.ext.rewrites.DropPrimaryKeyConstraintOnline37import liquibase.ext.rewrites.DropUniqueConstraintOnline38object ChangeGenerators {39 val alphaNumArb = Arb.stringPattern("[a-zA-Z0-9]+")40 val columnsArb = arbitrary { rs ->41 val n = Arb.int(1, 3).next(rs)42 val cols = alphaNumArb.take(n, rs).toList()43 cols.map { c ->44 AddColumnConfig().apply {45 name = c46 }47 }48 }49 val createIndexCombinedArb = Arb.bind(alphaNumArb.orNull(), alphaNumArb, alphaNumArb, columnsArb) {50 schema, table, index, cols ->51 Pair(52 CreateIndexOnline().apply {53 schemaName = schema54 tableName = table55 indexName = index56 columns = cols57 },58 CreateIndexChange().apply {59 schemaName = schema60 tableName = table61 indexName = index62 columns = cols63 }64 )65 }66 val dropColumnCombinedArb = Arb.bind(alphaNumArb.orNull(), alphaNumArb, columnsArb) {67 schema, table, cols ->68 Pair(69 DropColumnOnline().apply {70 schemaName = schema71 tableName = table72 columns = cols73 },74 DropColumnChange().apply {75 schemaName = schema76 tableName = table77 columns = cols78 }79 )80 }81 val dropForeignKeyCombinedArb = Arb.bind(alphaNumArb.orNull(), alphaNumArb, alphaNumArb) {82 schema, table, constraint ->83 Pair(84 DropForeignKeyConstraintOnline().apply {85 baseTableSchemaName = schema86 baseTableName = table87 constraintName = constraint88 },89 DropForeignKeyConstraintChange().apply {90 baseTableSchemaName = schema91 baseTableName = table92 constraintName = constraint93 }94 )95 }96 val dropIndexCombinedArb = Arb.bind(alphaNumArb.orNull(), alphaNumArb, alphaNumArb) {97 schema, table, index ->98 Pair(99 DropIndexOnline().apply {100 schemaName = schema101 tableName = table102 indexName = index103 },104 DropIndexChange().apply {105 schemaName = schema106 tableName = table107 indexName = index108 }109 )110 }111 val dropPrimaryKeyCombinedArb = Arb.bind(alphaNumArb.orNull(), alphaNumArb, alphaNumArb, Arb.bool()) {112 schema, table, constraint, dropIdx ->113 Pair(114 DropPrimaryKeyConstraintOnline().apply {115 schemaName = schema116 tableName = table117 constraintName = constraint118 dropIndex = dropIdx119 },120 DropPrimaryKeyChange().apply {121 schemaName = schema122 tableName = table123 constraintName = constraint124 dropIndex = dropIdx125 }126 )127 }128 val dropUniqueConstraintCombinedArb = Arb.bind(alphaNumArb.orNull(), alphaNumArb, alphaNumArb) {129 schema, table, constraint ->130 Pair(131 DropUniqueConstraintOnline().apply {132 schemaName = schema133 tableName = table134 constraintName = constraint135 },136 DropUniqueConstraintChange().apply {137 schemaName = schema138 tableName = table139 constraintName = constraint140 }141 )142 }143 val otherDatabases = listOf(144 MySQLDatabase(), PostgresDatabase(), MSSQLDatabase(), H2Database(), HsqlDatabase(), SybaseDatabase(),145 FirebirdDatabase(), DerbyDatabase(), SQLiteDatabase()146 ).exhaustive()147 val compatibleDatabases = Arb.int(19, 30).map { createOracleSpy(it) }148 val incompatibleDatabases = Arb.int(1, 18).map { createOracleSpy(it) }149 val incompatibleOracleSpy = createOracleSpy(12, 'c')150 val compatibleOracleSpy = createOracleSpy(19, 'c')151 private fun createOracleSpy(ver: Int, suffix: Char? = null) = spyk<OracleDatabase>().apply {152 every { databaseMajorVersion } returns ver153 every { databaseProductVersion } returns "Oracle Database ${ver}$suffix Enterprise Edition Release"154 }155}...
ResolversTest.kt
Source:ResolversTest.kt
1package org.tesserakt.diskordin.commands.resolver2import arrow.core.Either3import arrow.core.sequenceEither4import io.kotest.assertions.arrow.core.shouldBeLeft5import io.kotest.assertions.arrow.core.shouldBeRight6import io.kotest.core.spec.style.FunSpec7import io.kotest.matchers.collections.shouldContainInOrder8import io.kotest.matchers.should9import io.kotest.matchers.shouldBe10import io.kotest.matchers.types.beOfType11import io.kotest.property.Arb12import io.kotest.property.Gen13import io.kotest.property.PropertyTesting14import io.kotest.property.RandomSource15import io.kotest.property.arbitrary.string16import io.kotest.property.arbitrary.stringPattern17import io.kotest.property.exhaustive.exhaustive18import org.tesserakt.diskordin.commands.CommandContext19import org.tesserakt.diskordin.core.data.DeferredIdentified20import org.tesserakt.diskordin.core.data.EagerIdentified21import org.tesserakt.diskordin.core.entity.IMessage22import org.tesserakt.diskordin.core.entity.IMessageChannel23import org.tesserakt.diskordin.core.entity.IUser24import java.math.BigDecimal25import java.math.BigInteger26class ResolversTest : FunSpec() {27 private val testCount = PropertyTesting.defaultIterationCount.coerceAtMost(256)28 private suspend fun <T : Any, C : CommandContext> test(29 resolver: TypeResolver<T, C>,30 badInput: Gen<String>,31 goodInput: Gen<String>,32 ctx: C33 ): Pair<Either<ParseError, List<T>>, Either<ParseError, List<T>>> {34 val badResult = badInput.generate(RandomSource.default())35 .take(testCount).toList().map { resolver.parse(ctx, it.value) }.sequenceEither()36 val goodResult = goodInput.generate(RandomSource.default())37 .take(testCount).toList().map { resolver.parse(ctx, it.value) }.sequenceEither()38 return badResult to goodResult39 }40 init {41 val fakeContext = object : CommandContext {42 override val message: EagerIdentified<IMessage>43 get() = error("Fake")44 override val author: EagerIdentified<IUser>45 get() = error("Fake")46 override val commandArgs: Array<String>47 get() = error("Fake")48 override val channel: DeferredIdentified<IMessageChannel>49 get() = error("Fake")50 }51 test("Boolean resolver") {52 val bad = Arb.string(0, 100)53 val good = listOf("true", "TRuE", "FalSe", "false").exhaustive()54 val (fail, success) = test(BooleanResolver(), bad, good, fakeContext)55 fail.shouldBeLeft() should beOfType<BooleanResolver.BooleanConversionError>()56 success.shouldBeRight() shouldContainInOrder listOf(true, true, false, false)57 }58 test("String resolver") {59 val bad = Arb.string(0..100)60 val good = Arb.string(0..100)61 val (fail, success) = test(StringResolver(), bad, good, fakeContext)62 fail.shouldBeRight()63 success.shouldBeRight()64 }65 test("Char resolver") {66 val bad = Arb.string(2..100)67 val good = Arb.stringPattern(".")68 val (fail, success) = test(CharResolver(), bad, good, fakeContext)69 fail.shouldBeLeft() shouldBe beOfType<CharResolver.LengthError>()70 success.shouldBeRight()71 }72 context("Number resolvers") {73 fun generateNumberValues(maxSize: Int) =74 Arb.stringPattern("-?\\d{1,$maxSize}")75 suspend fun <N : Number> generateAndTest(maxValue: N, resolver: TypeResolver<N, CommandContext>) {76 val length = BigDecimal(maxValue.toString()).toPlainString().length - 177 println("Next length is $length")78 val bad = Arb.string(0, length)79 val (fail, success) = test(resolver, bad, generateNumberValues(length), fakeContext)80 fail.shouldBeLeft()81 success.shouldBeRight()82 }83 test("Int") { generateAndTest(Int.MAX_VALUE, IntResolver()) }84 test("Long") { generateAndTest(Long.MAX_VALUE, LongResolver()) }85 test("Short") { generateAndTest(Short.MAX_VALUE, ShortResolver()) }86 test("Byte") { generateAndTest(Byte.MAX_VALUE, ByteResolver()) }87 test("Float") { generateAndTest(Float.MAX_VALUE, FloatResolver()) }88 test("Double") { generateAndTest(Double.MAX_VALUE, DoubleResolver()) }89 test("BigInteger") { generateAndTest(BigInteger.valueOf(10).pow(1024), BigIntegerResolver()) }90 test("BigDecimal") { generateAndTest(BigDecimal(10).pow(1024), BigDecimalResolver()) }91 }92 }93}...
MainTest.kt
Source:MainTest.kt
...5import io.kotest.core.spec.style.describeSpec6import io.kotest.matchers.shouldBe7import io.kotest.property.Arb8import io.kotest.property.Exhaustive9import io.kotest.property.arbitrary.char10import io.kotest.property.arbitrary.int11import io.kotest.property.exhaustive.azstring12import io.kotest.property.forAll13class MainTest : DescribeSpec({14 include(createPasswordTest(::createRangePasswordFromRow))15 describe("Create range password instance") {16 it("Exception if low is greater than high") {17 shouldThrowAny { createRangePasswordFromRow("5-3 a: abcde") }18 }19 }20 include(createPasswordTest(::createPositionPasswordFromRow))21 describe("Create position password instance") {22 it("Exception if positions are less than 1") {23 forAll(Arb.int(-1000..0), Arb.int(-1000..0)) { pos1, pos2 ->24 shouldThrowAny {25 createPositionPasswordFromRow("$pos1-$pos2 a: abcde")26 }27 true28 }29 }30 }31 describe("Range password instance validity") {32 it("Always fails if both requirements 0 and letter in string") {33 forAll<Char, String> { letter, password ->34 !createRangePasswordFromRow("0-0 $letter: $letter${password.filter { it.isLetterOrDigit() }}").isValid()35 }36 }37 it("Succeeds if count is in range") {38 val max = 10039 forAll(Arb.int(1 until max), Arb.char()) {size, letter ->40 createRangePasswordFromRow("1-$max $letter: ${letter.toString().repeat(size)}").isValid()41 }42 }43 it("Fails if count is out of range") {44 val max = 10045 forAll(Arb.int(1 until max), Arb.char()) {size, letter ->46 !createRangePasswordFromRow("0-0 $letter: ${letter.toString().repeat(size)}").isValid()47 }48 }49 it("Succeeds with example row") {50 withClue("Password has 1 'a' so it is within range") {51 createRangePasswordFromRow("1-3 a: abcde").isValid() shouldBe true52 }53 }54 }55 describe("Position password instance validity") {56 it("Always invalid with same first and second position") {57 val maxLen = 1058 forAll(Arb.int(1..maxLen), Arb.char(), Exhaustive.azstring(maxLen..maxLen)) { pos, char, string ->59 !createPositionPasswordFromRow("$pos-$pos $char: $string").isValid()60 }61 }62 it("Valid with only one position containing letter") {63 withClue("Position 1 contains a and position 3 does not.") {64 createPositionPasswordFromRow("1-3 a: abcde").isValid() shouldBe true65 }66 }67 it("Invalid with no positions containing letter") {68 withClue("neither position 1 nor position 3 contains b.") {69 createPositionPasswordFromRow("1-3 b: cdefg").isValid() shouldBe false70 }71 }72 it("Invalid with both positions containing letter") {73 withClue("both position 2 and position 9 contain c.") {...
GeneratorTests.kt
Source:GeneratorTests.kt
...22 //使ç¨åºå±çæå¨Arbitrary æ ééæºçæå¨23 "use generator" {24 Arb.int(1, 200).take(10).toList().run(::println)25 Arb.intArray(Arb.int(2, 8), Arb.int(200..400)).take(10).map { it.toList() }.toList().run(::println)26 Arb.char('a'..'z').take(10).toList().run(::println)27 Arb.stringPattern("\\w+[0-9]").take(10).toList().run(::println)28 Arb.list(Arb.int(), 1..5).take(10).toList().run(::println)29 }30 //使ç¨æéçæå¨ Exhaustive31 "use exhaustive" {32 Exhaustive.enum<Season>().values.run(::println)33 Exhaustive.ints(1..10).values.run(::println)34 }35 //使ç¨ç»åçæå¨36 "use complex operation" {37 Arb.choice(Arb.int(1..10), Arb.double(20.0..50.0)).take(10).toList().run(::println)38 Arb.bind(Arb.string(), Arb.int()) { name, age ->39 Person(name, age).run(::println)40 }...
Codepoints.kt
Source:Codepoints.kt
...8val underscoreReplacers = underscoreReplacerList.exhaustive()9val notUnderscoreReplacers = Arb.codepoints()10 .filterNot { it.value in underscoreReplacerList }11 .map { it.value }12private val charClassMap = listOf(13 '|' to 1,14 '/' to 2, '\\' to 2,15 '[' to 3, ']' to 3,16 '{' to 4, '}' to 4,17 '(' to 5, ')' to 5,18 '<' to 6, '>' to 6,19).map { (key, value) -> key.toInt() to value }20val charClassMembers = charClassMap.exhaustive()21val notCharClassMembers = Arb.codepoints()22 .filterNot { it.value in charClassMap.map { (key, _) -> key } }23 .map { it.value }24private val oppositePairList = listOf(25 '[' to ']',26 ']' to '[',27 '{' to '}',28 '}' to '{',29 '(' to ')',30 ')' to '(',31).map { (key, value) -> key.toInt() to value.toInt() }32val oppositePairs = oppositePairList.exhaustive()33val notOppositePairs = arbitrary { source ->34 val left = Arb.codepoints().sample(source)35 val right = Arb.codepoints().sample(source)36 left.value.value to right.value.value // Ew...
CharTest.kt
Source:CharTest.kt
...5import io.kotest.property.Exhaustive6import io.kotest.property.exhaustive.az7class CharTest : FunSpec() {8 init {9 test("Exhaustive.az() should return all chars in a..z") {10 Exhaustive.az().values.toSet().shouldHaveSize(26)11 ('a'..'z').forEach {12 Exhaustive.az().values.shouldContain(it)13 }14 }15 }16}...
char.kt
Source:char.kt
1package io.kotest.property.exhaustive2import io.kotest.property.Exhaustive3/**4 * Returns an Exhaustive which returns the characters a to z.5 */6fun Exhaustive.Companion.az(): Exhaustive<Char> {7 return ('a'..'z').map { it }.exhaustive()8}...
char
Using AI Code Generation
1val chars = char()2val strings = string()3val ints = int()4val longs = long()5val floats = float()6val doubles = double()7val bigIntegers = bigInteger()8val bigDecimals = bigDecimal()9val booleans = boolean()10val bytes = byte()11val shorts = short()12val charRanges = charRange('a', 'b')13val intRanges = intRange(1, 10)14val longRanges = longRange(1, 10)15val floatRanges = floatRange(1f, 10f)16val doubleRanges = doubleRange(1.0, 10.0)17val bigIntegerRanges = bigIntegerRange(BigInteger.ONE, BigInteger.TEN)18val bigDecimalRanges = bigDecimalRange(BigDecimal.ONE, BigDecimal.TEN)19val byteRanges = byteRange(1, 10)
char
Using AI Code Generation
1val chars = char('a', 'z')2val chars = char('a', 'z')3val chars = char('a', 'z')4val chars = char('a', 'z')5val chars = char('a', 'z')6val chars = char('a', 'z')7val chars = char('a', 'z')8val chars = char('a', 'z')9val chars = char('a', 'z')10val chars = char('a', 'z')11val chars = char('a', 'z')
char
Using AI Code Generation
1 val chars = char()2 val charsFromRange = char('a'..'z')3 val charsFromChars = char('a', 'b', 'c')4 val charsFromCharsList = char(listOf('a', 'b', 'c'))5 val charsFromCharsArray = char(arrayOf('a', 'b', 'c'))6 val charsFromCharsSequence = char(sequenceOf('a', 'b', 'c'))7 val ints = int()8 val intsFromRange = int(1..10)9 val intsFromInts = int(1, 2, 3)10 val intsFromIntsList = int(listOf(1, 2, 3))11 val intsFromIntsArray = int(arrayOf(1, 2, 3))12 val intsFromIntsSequence = int(sequenceOf(1, 2, 3))13 val longs = long()14 val longsFromRange = long(1L..10L)15 val longsFromLongs = long(1L, 2L, 3L)16 val longsFromLongsList = long(listOf(1L, 2L, 3L))17 val longsFromLongsArray = long(arrayOf(1L, 2L, 3L))18 val longsFromLongsSequence = long(sequenceOf(1L, 2L, 3L))19 val doubles = double()20 val doublesFromRange = double(1.0..10.0)21 val doublesFromDoubles = double(1.0, 2.0, 3.0)22 val doublesFromDoublesList = double(listOf(1.0, 2.0, 3.0))23 val doublesFromDoublesArray = double(arrayOf(1.0, 2.0, 3.0))24 val doublesFromDoublesSequence = double(sequenceOf(1.0, 2.0, 3.0))
char
Using AI Code Generation
1 fun `test all characters`(){2 val chars = char().exhaustive().toList()3 chars.forEach {4 println(it)5 }6 }7 fun `test all strings`(){8 val strings = string().exhaustive().toList()9 strings.forEach {10 println(it)11 }12 }13As you can see, the exhaustive() extension function is used to generate all possible values of a type of data. 14In the first test, we use the exhaustive() extension function to generate all possible values of the Int type. 15In the second test, we use the exhaustive() extension function to generate all possible values of the Char type. 16You can find the exhaustive() extension function in the io.kotest.property.exhaustive package. 17If you want to generate all possible values of other types of data, you can use the exhaustive() extension function in the io.kotest.property.exhaustive package. 18The exhaustive() extension function is not limited to generating all possible values of a type of data.
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!!