Best Kotest code snippet using io.kotest.framework.multiplatform.native.specs
SpecIrGenerationExtension.kt
Source: SpecIrGenerationExtension.kt
...31import 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
...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()...
entry.kt
Source: entry.kt
...5 // the method invoked to start the tests, must exist on TestEngineLauncher6 const val LaunchMethodName = "launch"7 // the FQN for the class used to launch the MPP engine8 const val TestEngineClassName = "io.kotest.engine.TestEngineLauncher"9 // the method invoked to add specs to the launcher, must exist on TestEngineLauncher10 const val WithSpecsMethodName = "withSpecs"11 // the method invoked to set the team city listener, must exist on TestEngineLauncher12 const val WithTeamCityListenerMethodName = "withTeamCityListener"13}...
Check out the latest blogs from LambdaTest on this topic:
The QA testing career includes following an often long, winding road filled with fun, chaos, challenges, and complexity. Financially, the spectrum is broad and influenced by location, company type, company size, and the QA tester’s experience level. QA testing is a profitable, enjoyable, and thriving career choice.
One of the most important skills for leaders to have is the ability to prioritize. To understand how we can organize all of the tasks that must be completed in order to complete a project, we must first understand the business we are in, particularly the project goals. There might be several project drivers that stimulate project execution and motivate a company to allocate the appropriate funding.
Unit and functional testing are the prime ways of verifying the JavaScript code quality. However, a host of tools are available that can also check code before or during its execution in order to test its quality and adherence to coding standards. With each tool having its unique features and advantages contributing to its testing capabilities, you can use the tool that best suits your need for performing JavaScript testing.
JavaScript is one of the most widely used programming languages. This popularity invites a lot of JavaScript development and testing frameworks to ease the process of working with it. As a result, numerous JavaScript testing frameworks can be used to perform unit testing.
These days, development teams depend heavily on feedback from automated tests to evaluate the quality of the system they are working on.
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!!