Best Kotest code snippet using io.kotest.framework.multiplatform.js.specs.IrFile.specs
SpecIrGenerationExtension.kt
Source:SpecIrGenerationExtension.kt
1package io.kotest.framework.multiplatform.js2import 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.IrStatement10import org.jetbrains.kotlin.ir.builders.declarations.buildFun11import org.jetbrains.kotlin.ir.builders.irBlockBody12import org.jetbrains.kotlin.ir.builders.irCall13import org.jetbrains.kotlin.ir.builders.irVararg14import org.jetbrains.kotlin.ir.declarations.IrClass15import org.jetbrains.kotlin.ir.declarations.IrFile16import org.jetbrains.kotlin.ir.declarations.IrModuleFragment17import org.jetbrains.kotlin.ir.declarations.IrSimpleFunction18import org.jetbrains.kotlin.ir.declarations.name19import org.jetbrains.kotlin.ir.expressions.IrCall20import org.jetbrains.kotlin.ir.util.constructors21import org.jetbrains.kotlin.ir.util.getSimpleFunction22import org.jetbrains.kotlin.ir.util.kotlinFqName23import org.jetbrains.kotlin.name.FqName24import org.jetbrains.kotlin.name.Name25import java.util.concurrent.CopyOnWriteArrayList26class SpecIrGenerationExtension(private val messageCollector: MessageCollector) : IrGenerationExtension {27 override fun generate(moduleFragment: IrModuleFragment, pluginContext: IrPluginContext) {28 moduleFragment.transform(object : IrElementTransformerVoidWithContext() {29 val specs = CopyOnWriteArrayList<IrClass>()30 var configs = CopyOnWriteArrayList<IrClass>()31 override fun visitModuleFragment(declaration: IrModuleFragment): IrModuleFragment {32 val fragment = super.visitModuleFragment(declaration)33 messageCollector.toLogger().log("Detected ${configs.size} configs:")34 configs.forEach {35 messageCollector.toLogger().log(it.kotlinFqName.asString())36 }37 messageCollector.toLogger().log("Detected ${specs.size} JS specs:")38 specs.forEach {39 messageCollector.toLogger().log(it.kotlinFqName.asString())40 }41 if (specs.isEmpty()) return fragment42 val file = declaration.files.first()43 val launcherClass = pluginContext.referenceClass(FqName(EntryPoint.TestEngineClassName))44 ?: error("Cannot find ${EntryPoint.TestEngineClassName} class reference")45 val launcherConstructor = launcherClass.constructors.first { it.owner.valueParameters.isEmpty() }46 val promiseFn = launcherClass.getSimpleFunction(EntryPoint.PromiseMethodName)47 ?: error("Cannot find function ${EntryPoint.PromiseMethodName}")48 val withSpecsFn = launcherClass.getSimpleFunction(EntryPoint.WithSpecsMethodName)49 ?: error("Cannot find function ${EntryPoint.WithSpecsMethodName}")50 val withConfigFn = launcherClass.getSimpleFunction(EntryPoint.WithConfigMethodName)51 ?: error("Cannot find function ${EntryPoint.WithConfigMethodName}")52 val main = pluginContext.irFactory.buildFun {53 name = Name.identifier("main")54 returnType = pluginContext.irBuiltIns.unitType55 }.also { func: IrSimpleFunction ->56 func.body = DeclarationIrBuilder(pluginContext, func.symbol).irBlockBody {57 +irCall(promiseFn).also { promise: IrCall ->58 promise.dispatchReceiver = irCall(withSpecsFn).also { withSpecs ->59 withSpecs.putValueArgument(60 0,61 irVararg(62 pluginContext.irBuiltIns.stringType,63 specs.map { irCall(it.constructors.first()) }64 )65 )66 withSpecs.dispatchReceiver = irCall(withConfigFn).also { withConfig ->67 withConfig.putValueArgument(68 0,69 irVararg(70 pluginContext.irBuiltIns.stringType,71 configs.map { irCall(it.constructors.first()) }72 )73 )74 withConfig.dispatchReceiver = irCall(launcherConstructor)75 }76 }77 }78 }79 }80// val launcher = pluginContext.irFactory.buildProperty {81// name = Name.identifier(EntryPoint.LauncherValName)82// }.apply {83// parent = file84// backingField = pluginContext.irFactory.buildField {85// type = pluginContext.irBuiltIns.unitType86// isFinal = true87// isExternal = false88// isStatic = true // top level vals must be static89// name = Name.identifier(EntryPoint.LauncherValName)90// }.also { field ->91// field.correspondingPropertySymbol = this@apply.symbol92// field.initializer = pluginContext.irFactory.createExpressionBody(startOffset, endOffset) {93// this.expression = DeclarationIrBuilder(pluginContext, field.symbol).irBlock {94// +irCall(promiseFn).also { promise: IrCall ->95// promise.dispatchReceiver = irCall(withSpecsFn).also { withSpecs ->96// withSpecs.putValueArgument(97// 0,98// irVararg(99// pluginContext.irBuiltIns.stringType,100// specs.map { irCall(it.constructors.first()) }101// )102// )103// withSpecs.dispatchReceiver = irCall(withConfigFn).also { withConfig ->104// withConfig.putValueArgument(105// 0,106// irVararg(107// pluginContext.irBuiltIns.stringType,108// configs.map { irCall(it.constructors.first()) }109// )110// )111// withConfig.dispatchReceiver = irCall(launcherConstructor)112// }113// }114// }115// }116// }117// }118//119// addGetter {120// returnType = pluginContext.irBuiltIns.unitType121// }.also { func: IrSimpleFunction ->122// func.body = DeclarationIrBuilder(pluginContext, func.symbol).irBlockBody {123// }124// }125// }126 file.addChild(main)127 return fragment128 }129 override fun visitClassNew(declaration: IrClass): IrStatement {130 super.visitClassNew(declaration)131 if (declaration.isProjectConfig()) configs.add(declaration)132 return declaration133 }134 override fun visitFileNew(declaration: IrFile): IrFile {135 super.visitFileNew(declaration)136 val specs = declaration.specs()137 messageCollector.toLogger()138 .log("${declaration.name} contains ${specs.size} spec(s): ${specs.joinToString(", ") { it.kotlinFqName.asString() }}")139 this.specs.addAll(specs)140 return declaration141 }142 }, null)143 }144}...
specs.kt
Source:specs.kt
1package io.kotest.framework.multiplatform.js2import 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.getClass7import org.jetbrains.kotlin.name.FqName8val specClasses = listOf(9 "io.kotest.core.spec.style.BehaviorSpec",10 "io.kotest.core.spec.style.DescribeSpec",11 "io.kotest.core.spec.style.ExpectSpec",12 "io.kotest.core.spec.style.FeatureSpec",13 "io.kotest.core.spec.style.FreeSpec",14 "io.kotest.core.spec.style.FunSpec",15 "io.kotest.core.spec.style.ShouldSpec",16 "io.kotest.core.spec.style.StringSpec",17 "io.kotest.core.spec.style.WordSpec",18)19val abstractProjectConfigFqName = FqName("io.kotest.core.config.AbstractProjectConfig")20/**21 * Returns any specs declared at the top level in this file.22 */23fun IrFile.specs() = declarations.filterIsInstance<IrClass>().filter { it.isSpecClass() }24/**25 * Returns true fi this IrClass is a project config26 */27fun IrClass.isProjectConfig() = superTypes().any { it.classFqName == abstractProjectConfigFqName }28/**29 * Recursively returns all supertypes for an [IrClass] to the top of the type tree.30 */31fun IrClass.superTypes(): List<IrType> =32 this.superTypes + this.superTypes.flatMap { it.getClass()?.superTypes() ?: emptyList() }33/**34 * Returns true if any of the parents of this class are a spec class.35 */36fun IrClass.isSpecClass() =37 superTypes().mapNotNull { it.classFqName?.asString() }.intersect(specClasses).isNotEmpty()...
IrFile.specs
Using AI Code Generation
1import io.kotest.core.spec.style.FunSpec2expect class IrFileSpec(body: IrFileSpec.() -> Unit) : FunSpec3actual class IrFileSpec actual constructor(body: IrFileSpec.() -> Unit) : FunSpec(body)4import io.kotest.core.spec.style.FunSpec5expect class IrFileSpec(body: IrFileSpec.() -> Unit) : FunSpec6actual class IrFileSpec actual constructor(body: IrFileSpec.() -> Unit) : FunSpec(body)7import io.kotest.core.spec.style.FunSpec8expect class IrFileSpec(body: IrFileSpec.() -> Unit) : FunSpec9actual class IrFileSpec actual constructor(body: IrFileSpec.() -> Unit) : FunSpec(body)10import io.kotest.core.spec.style.FunSpec11expect class IrFileSpec(body: IrFileSpec.() -> Unit) : FunSpec12actual class IrFileSpec actual constructor(body: IrFileSpec.() -> Unit) : FunSpec(body)13import io.kotest.core.spec.style.FunSpec14expect class IrFileSpec(body: IrFileSpec.() -> Unit) : FunSpec15actual class IrFileSpec actual constructor(body: IrFileSpec.() -> Unit) : FunSpec(body)16import io.kotest.core.spec.style.FunSpec17expect class IrFileSpec(body: IrFileSpec.() -> Unit) : FunSpec18actual class IrFileSpec actual constructor(body: IrFileSpec.() -> Unit) : FunSpec(body)19import io.kotest.core.spec.style.FunSpec20expect class IrFileSpec(body: IrFileSpec.() -> Unit) : FunSpec21actual class IrFileSpec actual constructor(body: IrFileSpec.() -> Unit)
IrFile.specs
Using AI Code Generation
1import io.kotest.core.spec.style.FunSpec2import io.kotest.matchers.shouldBe3import io.kotest.core.spec.style.scopes.RootContext4import io.kotest.framework.multiplatform.js.specs5class MySpec : FunSpec() {6override fun RootContext.specs() {7test("a test") {8}9}10}11import io.kotest.core.spec.style.FunSpec12import io.kotest.matchers.shouldBe13import io.kotest.core.spec.style.scopes.RootContext14import io.kotest.framework.multiplatform.js.specs15class MySpec : FunSpec() {16override fun RootContext.specs() {17test("a test") {18}19}20}
IrFile.specs
Using AI Code Generation
1}2}3}4}5}6}7}8}9val specs = IrFile.specs("iosSimulatorX64") {
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!!