Best Kotest code snippet using io.kotest.property.arbitrary.file.Arb.Companion.lines
ParseProperty.kt
Source:ParseProperty.kt
1package net.torommo.logspy2import io.kotest.core.spec.style.StringSpec3import io.kotest.property.Arb4import io.kotest.property.arbitrary.Codepoint5import io.kotest.property.arbitrary.arbitrary6import io.kotest.property.arbitrary.bind7import io.kotest.property.arbitrary.element8import io.kotest.property.arbitrary.filter9import io.kotest.property.arbitrary.int10import io.kotest.property.arbitrary.map11import io.kotest.property.arbitrary.merge12import io.kotest.property.arbitrary.single13import io.kotest.property.arbitrary.string14import io.kotest.property.checkAll15import kotlin.random.Random16import kotlin.random.nextInt17import kotlin.streams.asSequence18import kotlin.text.CharCategory.DECIMAL_DIGIT_NUMBER19import kotlin.text.CharCategory.LETTER_NUMBER20import kotlin.text.CharCategory.LOWERCASE_LETTER21import kotlin.text.CharCategory.MODIFIER_LETTER22import kotlin.text.CharCategory.OTHER_LETTER23import kotlin.text.CharCategory.TITLECASE_LETTER24import kotlin.text.CharCategory.UPPERCASE_LETTER25import net.torommo.logspy.ExceptionCreationAction.ADD_SUPPRESSED_TO_ROOT_EXCEPTION26import net.torommo.logspy.ExceptionCreationAction.RANDOM_ACTION_ON_RANDOM_SUPPRESSED_IN_ROOT_EXCEPTION27import net.torommo.logspy.ExceptionCreationAction.SET_NEW_ROOT_EXCEPTION28import org.junit.jupiter.api.Assertions.assertDoesNotThrow29import org.slf4j.Logger30import org.slf4j.LoggerFactory31class ParseProperty :32 StringSpec(33 {34 "logger name parsability" {35 checkAll(36 arbitrary(PositionIndenpendentStringShrinker(1)) { rs ->37 arbJavaIdentifier.merge(arbKotlinIdentifier).single(rs)38 }39 ) { loggerName ->40 val logger = LoggerFactory.getLogger(loggerName)41 LogstashStdoutSpyProvider().createFor(loggerName)42 .use {43 logger.error("Test")44 assertDoesNotThrow(it::events);45 }46 }47 }48 "log level parsability" {49 checkAll(arbLevel) { logAction ->50 val logger = LoggerFactory.getLogger("test")51 LogstashStdoutSpyProvider().createFor("test")52 .use {53 logAction(logger, "Test")54 assertDoesNotThrow(it::events);55 }56 }57 }58 "log message parsability" {59 checkAll(60 arbitrary(PositionIndenpendentStringShrinker()) { rs -> arbMessage.single(rs) }61 ) { message ->62 val logger = LoggerFactory.getLogger("test")63 LogstashStdoutSpyProvider().createFor("test")64 .use {65 logger.error(message)66 assertDoesNotThrow(it::events);67 }68 }69 }70 "exception message parsability" {71 val logger = LoggerFactory.getLogger("test")72 checkAll(73 arbitrary(PositionIndenpendentStringShrinker()) { rs -> arbMessage.single(rs) }74 ) { message ->75 LogstashStdoutSpyProvider().createFor("test")76 .use {77 val exception = RuntimeException(message)78 exception.stackTrace = emptyArray()79 logger.error("test", exception)80 assertDoesNotThrow(it::events);81 }82 }83 }84 "stack trace parsability" {85 val logger = LoggerFactory.getLogger("test")86 checkAll(87 arbitrary(ArrayShrinker()) { rs ->88 arbKotlinStackTraceElements.merge(arbJavaStackTraceElements).single(rs)89 }90 ) { element: Array<StackTraceElement> ->91 LogstashStdoutSpyProvider().createFor("test")92 .use {93 val exception = RuntimeException("test message")94 exception.stackTrace = element95 logger.error("test", exception)96 assertDoesNotThrow(it::events);97 }98 }99 }100 "exception tree parsability" {101 val logger = LoggerFactory.getLogger("test")102 checkAll(arbExceptionTree) { exception: Throwable ->103 LogstashStdoutSpyProvider().createFor("test")104 .use {105 logger.error("test", exception)106 assertDoesNotThrow(it::events);107 }108 }109 }110 }111 )112fun Arb.Companion.printableAscii(): Arb<Codepoint> =113 arbitrary(listOf(Codepoint('a'.code))) { rs ->114 val printableChars = (' '.code..'~'.code).asSequence()115 val codepoints = printableChars.map { Codepoint(it) }.toList()116 val ints = Arb.int(codepoints.indices)117 codepoints[ints.sample(rs).value]118 }119fun Arb.Companion.printableMultilinesIndentedAscii(): Arb<Codepoint> =120 arbitrary(listOf(Codepoint('a'.code))) { rs ->121 val indentings = sequenceOf(0xB)122 val endOfLines = sequenceOf(0xA, 0xD)123 val printableChars = (' '.code..'~'.code).asSequence()124 val codepoints = (indentings + endOfLines + printableChars).map { Codepoint(it) }.toList()125 val ints = Arb.int(codepoints.indices)126 codepoints[ints.sample(rs).value]127 }128fun Arb.Companion.unicode(): Arb<Codepoint> =129 arbitrary(listOf(Codepoint('a'.code))) { rs ->130 val ints = Arb.int(Character.MIN_CODE_POINT..Character.MAX_CODE_POINT)131 Codepoint(ints.sample(rs).value)132 }133val arbLevel =134 Arb.element(135 listOf<(Logger, String) -> Unit>(136 Logger::error,137 Logger::warn,138 Logger::info,139 Logger::debug,140 Logger::trace141 )142 )143val arbMessage =144 Arb.string(0, 1024, Arb.printableMultilinesIndentedAscii())145 .merge(Arb.string(0, 1024, Arb.unicode()))146val arbJavaIdentifier =147 Arb.string(minSize = 1, codepoints = Arb.printableAscii())148 .merge(Arb.string(minSize = 1, codepoints = Arb.unicode()))149 .filter { Character.isJavaIdentifierStart(it.codePoints().asSequence().first()) }150 .filter {151 it.codePoints()152 .asSequence()153 .all { codePoint -> Character.isJavaIdentifierPart(codePoint) }154 }155val arbJavaFileName = arbJavaIdentifier.map { "${it}.java" }156val arbJavaStackTraceElement =157 Arb.bind(arbJavaIdentifier, arbJavaIdentifier, arbJavaFileName, Arb.int(-65536, 65535))158 { className, methodName, fileName, lineNumber ->159 StackTraceElement(className, methodName, fileName, lineNumber)160 }161val arbJavaStackTraceElements = Arb.array(arbJavaStackTraceElement, 0..7)162val arbKotlinIdentifier =163 Arb.string(minSize = 1, codepoints = Arb.printableAscii())164 .merge(Arb.string(minSize = 1, codepoints = Arb.unicode()))165 .filter { isUnescapedIdentifier(it) || isEscapedIdentifier(it) }166val arbKotlinFileName = arbKotlinIdentifier.map { "${it}.kt" };167val arbKotlinStackTraceElement =168 Arb.bind(arbKotlinIdentifier, arbKotlinIdentifier, arbKotlinFileName, Arb.int(-65536, 65535))169 { className, methodName, fileName, lineNumber ->170 StackTraceElement(className, methodName, fileName, lineNumber)171 }172val arbKotlinStackTraceElements = Arb.array(arbKotlinStackTraceElement, 0..7)173val arbExceptionTree: Arb<Throwable> =174 arbitrary { rs ->175 val throwableGenerator = arbFlatException.generate(rs).iterator()176 val result = throwableGenerator.next().value177 repeat(rs.random.nextInt(1..7)) {178 val exceptionToPlace = throwableGenerator.next().value179 addExceptionToRandomPlace(rs.random, result, exceptionToPlace)180 }181 result182 }183private fun addExceptionToRandomPlace(random: Random, aggregator: Throwable, addition: Throwable) {184 when (ExceptionCreationAction.values()[random.nextInt(ExceptionCreationAction.values().size)]) {185 SET_NEW_ROOT_EXCEPTION -> {186 rootCauseOf(aggregator).initCause(addition)187 }188 ADD_SUPPRESSED_TO_ROOT_EXCEPTION -> {189 rootCauseOf(aggregator).addSuppressed(addition)190 }191 RANDOM_ACTION_ON_RANDOM_SUPPRESSED_IN_ROOT_EXCEPTION -> {192 val rootCause = rootCauseOf(aggregator)193 if (rootCause.suppressed.isNotEmpty()) {194 addExceptionToRandomPlace(195 random,196 rootCause.suppressed[random.nextInt(rootCause.suppressed.size)],197 addition198 )199 }200 }201 }202}203private tailrec fun rootCauseOf(candidate: Throwable): Throwable {204 return if (candidate.cause == null) {205 candidate206 } else {207 rootCauseOf(candidate.cause!!)208 }209}210private enum class ExceptionCreationAction {211 SET_NEW_ROOT_EXCEPTION,212 ADD_SUPPRESSED_TO_ROOT_EXCEPTION,213 RANDOM_ACTION_ON_RANDOM_SUPPRESSED_IN_ROOT_EXCEPTION214}215val arbFlatException =216 Arb.bind(217 Arb.string(0, 255, Arb.printableMultilinesIndentedAscii()),218 arbKotlinStackTraceElements.merge(arbJavaStackTraceElements)219 ) { message, stackTrace ->220 val result: Exception = RuntimeException(message)221 result.stackTrace = stackTrace222 result223 }224private fun isUnescapedIdentifier(string: String): Boolean {225 return isLetterOrUnderscore(string.first()) &&226 string.toCharArray().all { char -> isLetterOrUnderscore(char) || isUnicodeDigit(char) }227}228private fun isLetterOrUnderscore(char: Char): Boolean {229 return LOWERCASE_LETTER.contains(char) || UPPERCASE_LETTER.contains(char) ||230 TITLECASE_LETTER.contains(char) || LETTER_NUMBER.contains(char) ||231 MODIFIER_LETTER.contains(char) || OTHER_LETTER.contains(char) || char == '_'232}233private fun isUnicodeDigit(char: Char): Boolean {234 return DECIMAL_DIGIT_NUMBER.contains(char)235}236private fun isEscapedIdentifier(string: String): Boolean {237 return string.all { it != '\r' && it != '\n' && it != '`' }238}...
file.kt
Source:file.kt
1package io.kotest.property.arbitrary2import io.kotest.property.Arb3import java.io.File4import java.nio.charset.Charset5import kotlin.random.Random6/**7 * Returns an [Arb] where each value is a randomly created File object.8 * The file objects do not necessarily exist on disk.9 */10fun Arb.Companion.file(): Arb<File> = Arb.string(1..100).map { File(it) }11/**12 * Returns an [Arb] where each value is a randomly chosen File object from given directory.13 * If the Directory does not exist or is empty, this function will throw an error.14 *15 * If recursive is true(default value is false) it gives files from inner directories as well recursively.16 */17fun Arb.Companion.file(directoryName: String, recursive: Boolean = false): Arb<File> {18 fun randomFile(files: Sequence<File>, random: Random): File {19 val allFiles = files.toList()20 if (allFiles.isEmpty()) error("No files to enumerate")21 return allFiles.shuffled(random).first()22 }23 return arbitrary {24 val fileTreeWalk = File(directoryName).walk()25 when (recursive) {26 true -> randomFile(fileTreeWalk.maxDepth(Int.MAX_VALUE), it.random)27 else -> randomFile(fileTreeWalk.maxDepth(1), it.random)28 }29 }30}31/**32 * Returns an [Arb] where each value is a randomly chosen line in the given file.33 */34fun Arb.Companion.lines(file: File, charset: Charset = Charsets.UTF_8): Arb<String> {35 val contents = file.readLines(charset)36 return arbitrary {37 contents.shuffled(it.random).first()38 }39}...
Arb.Companion.lines
Using AI Code Generation
1val lines = Arb.lines()2val strings = Arb.strings()3val stringsWithLength = Arb.strings(10)4val stringsWithLengthAndCharacters = Arb.strings(10, "abc")5val stringsWithLengthAndCharactersAndSeed = Arb.strings(10, "abc", 10)6val stringsWithLengthAndCharactersAndSeedAndSource = Arb.strings(10, "abc", 10, RandomSource.Default)7val stringsWithLengthAndCharactersAndSeedAndSourceAndCharset = Arb.strings(10, "abc", 10, RandomSource.Default, Charsets.UTF_8)8val stringsWithLengthAndCharactersAndSeedAndSourceAndCharsetAndPrefix = Arb.strings(10, "abc", 10, RandomSource.Default, Charsets.UTF_8, "prefix")
Arb.Companion.lines
Using AI Code Generation
1val lines = Arb.lines(100, 1000)2val lines = Arb.lines(100, 1000, 10, 100)3val lines = Arb.lines(100, 1000, 10, 100, 10, 100)4val lines = Arb.lines(100, 1000, 10, 100, 10, 100, 10, 100)5val lines = Arb.lines(100, 1000, 10, 100, 10, 100, 10, 100, 10, 100)6val lines = Arb.lines(100, 1000, 10, 100, 10, 100, 10, 100, 10, 100, 10, 100)
Arb.Companion.lines
Using AI Code Generation
1Arb . Companion . lines ( 10 , 10 , 10 ) . edgecases ( 10 ) . forEach { println ( it ) }2Arb . Companion . strings ( 10 , 10 , 10 ) . edgecases ( 10 ) . forEach { println ( it ) }3Arb . Companion . lines ( 10 , 10 , 10 ) . edgecases ( 10 ) . forEach { println ( it ) }4Arb . Companion . strings ( 10 , 10 , 10 ) . edgecases ( 10 ) . forEach { println ( it ) }5Arb . Companion . lines ( 10 , 10 , 10 ) . edgecases ( 10 ) . forEach { println ( it ) }6Arb . Companion . strings ( 10 , 10 , 10 ) . edgecases ( 10 ) . forEach { println ( it ) }7Arb . Companion . lines ( 10 , 10
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!!