Best Kotest code snippet using io.kotest.framework.multiplatform.native.specs.IrFile.specs
SpecIrGenerationExtension.kt
Source:SpecIrGenerationExtension.kt
1package io.kotest.framework.multiplatform.native2import org.jetbrains.kotlin.backend.common.IrElementTransformerVoidWithContext3import org.jetbrains.kotlin.backend.common.extensions.IrGenerationExtension4import org.jetbrains.kotlin.backend.common.extensions.IrPluginContext5import org.jetbrains.kotlin.backend.common.ir.addChild6import org.jetbrains.kotlin.backend.common.lower.DeclarationIrBuilder7import org.jetbrains.kotlin.cli.common.messages.MessageCollector8import org.jetbrains.kotlin.cli.common.toLogger9import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET10import org.jetbrains.kotlin.ir.builders.IrBlockBuilder11import org.jetbrains.kotlin.ir.builders.IrSingleStatementBuilder12import org.jetbrains.kotlin.ir.builders.Scope13import org.jetbrains.kotlin.ir.builders.declarations.addGetter14import org.jetbrains.kotlin.ir.builders.declarations.buildField15import org.jetbrains.kotlin.ir.builders.declarations.buildProperty16import org.jetbrains.kotlin.ir.builders.irBlock17import org.jetbrains.kotlin.ir.builders.irBlockBody18import org.jetbrains.kotlin.ir.builders.irCall19import org.jetbrains.kotlin.ir.builders.irVararg20import org.jetbrains.kotlin.ir.declarations.IrClass21import org.jetbrains.kotlin.ir.declarations.IrFile22import org.jetbrains.kotlin.ir.declarations.IrModuleFragment23import org.jetbrains.kotlin.ir.declarations.name24import org.jetbrains.kotlin.ir.util.constructors25import org.jetbrains.kotlin.ir.util.file26import org.jetbrains.kotlin.ir.util.getSimpleFunction27import org.jetbrains.kotlin.ir.util.irCall28import org.jetbrains.kotlin.ir.util.kotlinFqName29import org.jetbrains.kotlin.name.FqName30import org.jetbrains.kotlin.name.Name31import java.util.concurrent.CopyOnWriteArrayList32class SpecIrGenerationExtension(private val messageCollector: MessageCollector) : IrGenerationExtension {33 override fun generate(moduleFragment: IrModuleFragment, pluginContext: IrPluginContext) {34 moduleFragment.transform(object : IrElementTransformerVoidWithContext() {35 val specs = CopyOnWriteArrayList<IrClass>()36 override fun visitModuleFragment(declaration: IrModuleFragment): IrModuleFragment {37 val fragment = super.visitModuleFragment(declaration)38 messageCollector.toLogger().log("Detected ${specs.size} native specs:")39 specs.forEach {40 messageCollector.toLogger().log(it.kotlinFqName.asString())41 }42 if (specs.isEmpty()) return fragment43 val file = declaration.files.first()44 val launcherClass = pluginContext.referenceClass(FqName(EntryPoint.TestEngineClassName))45 ?: error("Cannot find ${EntryPoint.TestEngineClassName} class reference")46 val launcherConstructor = launcherClass.constructors.first { it.owner.valueParameters.isEmpty() }47 val launchFn = launcherClass.getSimpleFunction(EntryPoint.LaunchMethodName)48 ?: error("Cannot find function ${EntryPoint.LaunchMethodName}")49 val withSpecsFn = launcherClass.getSimpleFunction(EntryPoint.WithSpecsMethodName)50 ?: error("Cannot find function ${EntryPoint.WithSpecsMethodName}")51 val withTeamCityListenerMethodNameFn =52 launcherClass.getSimpleFunction(EntryPoint.WithTeamCityListenerMethodName)53 ?: error("Cannot find function ${EntryPoint.WithTeamCityListenerMethodName}")54 val eagerAnnotationName = FqName("kotlin.native.EagerInitialization")55 val eagerAnnotation = pluginContext.referenceClass(eagerAnnotationName)56 ?: error("Cannot find eager initialisation annotation class $eagerAnnotationName")57 val eagerAnnotationConstructor = eagerAnnotation.constructors.single()58 val launcher = pluginContext.irFactory.buildProperty {59 name = Name.identifier(EntryPoint.LauncherValName)60 }.apply {61 parent = file62 annotations += IrSingleStatementBuilder(pluginContext, Scope(this.symbol), UNDEFINED_OFFSET, UNDEFINED_OFFSET).build { irCall(eagerAnnotationConstructor) }63 backingField = pluginContext.irFactory.buildField {64 type = pluginContext.irBuiltIns.unitType65 isFinal = true66 isExternal = false67 isStatic = true // top level vals must be static68 name = Name.identifier(EntryPoint.LauncherValName)69 }.also { field ->70 field.correspondingPropertySymbol = this@apply.symbol71 field.initializer = pluginContext.irFactory.createExpressionBody(startOffset, endOffset) {72 this.expression = DeclarationIrBuilder(pluginContext, field.symbol).irBlock {73 +irCall(launchFn).also { launch ->74 launch.dispatchReceiver = irCall(withTeamCityListenerMethodNameFn).also { withTeamCity ->75 withTeamCity.dispatchReceiver = irCall(withSpecsFn).also { withSpecs ->76 withSpecs.dispatchReceiver = irCall(launcherConstructor)77 withSpecs.putValueArgument(78 0,79 irVararg(80 pluginContext.irBuiltIns.stringType,81 specs.map { irCall(it.constructors.first()) }82 )83 )84 }85 }86 }87 }88 }89 }90 addGetter {91 returnType = pluginContext.irBuiltIns.unitType92 }.also { func ->93 func.body = DeclarationIrBuilder(pluginContext, func.symbol).irBlockBody {94 }95 }96 }97 file.addChild(launcher)98 return fragment99 }100 override fun visitFileNew(declaration: IrFile): IrFile {101 super.visitFileNew(declaration)102 val specs = declaration.specs()103 messageCollector.toLogger().log("${declaration.name} contains ${specs.size} spec(s): ${specs.joinToString(", ") { it.kotlinFqName.asString() }}")104 this.specs.addAll(specs)105 return declaration106 }107 }, null)108 }109}...
specs.kt
Source:specs.kt
1package io.kotest.framework.multiplatform.native2import org.jetbrains.kotlin.ir.declarations.IrClass3import org.jetbrains.kotlin.ir.declarations.IrFile4import org.jetbrains.kotlin.ir.types.IrType5import org.jetbrains.kotlin.ir.types.classFqName6import org.jetbrains.kotlin.ir.types.getClass7val specClasses = listOf(8 "io.kotest.core.spec.style.BehaviorSpec",9 "io.kotest.core.spec.style.DescribeSpec",10 "io.kotest.core.spec.style.ExpectSpec",11 "io.kotest.core.spec.style.FeatureSpec",12 "io.kotest.core.spec.style.FreeSpec",13 "io.kotest.core.spec.style.FunSpec",14 "io.kotest.core.spec.style.ShouldSpec",15 "io.kotest.core.spec.style.StringSpec",16 "io.kotest.core.spec.style.WordSpec",17)18/**19 * Returns any specs declared at the top level in this file.20 */21fun IrFile.specs() = declarations.filterIsInstance<IrClass>().filter { it.isSpecClass() }22/**23 * Recursively returns all supertypes for an [IrClass] to the top of the type tree.24 */25fun IrClass.superTypes(): List<IrType> =26 this.superTypes + this.superTypes.flatMap { it.getClass()?.superTypes() ?: emptyList() }27/**28 * Returns true if any of the parents of this class are a spec class.29 */30fun IrClass.isSpecClass() =31 superTypes().mapNotNull { it.classFqName?.asString() }.intersect(specClasses).isNotEmpty()...
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!!