How to use IrFile.addTopLevelInitializer method of org.spekframework.spek2.kotlin.SpekExtension class

Best Spek code snippet using org.spekframework.spek2.kotlin.SpekExtension.IrFile.addTopLevelInitializer

SpekExtension.kt

Source:SpekExtension.kt Github

copy

Full Screen

1package org.spekframework.spek2.kotlin2import org.jetbrains.kotlin.backend.common.descriptors.synthesizedName3import 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.backend.common.serialization.knownBuiltins8import org.jetbrains.kotlin.descriptors.*9import org.jetbrains.kotlin.ir.IrElement10import org.jetbrains.kotlin.ir.builders.*11import org.jetbrains.kotlin.ir.builders.declarations.addField12import org.jetbrains.kotlin.ir.declarations.*13import org.jetbrains.kotlin.ir.declarations.impl.IrFunctionImpl14import org.jetbrains.kotlin.ir.descriptors.WrappedFieldDescriptor15import org.jetbrains.kotlin.ir.descriptors.WrappedSimpleFunctionDescriptor16import org.jetbrains.kotlin.ir.expressions.IrBlock17import org.jetbrains.kotlin.ir.expressions.IrExpression18import org.jetbrains.kotlin.ir.expressions.IrStatementOrigin19import org.jetbrains.kotlin.ir.expressions.impl.IrBlockImpl20import org.jetbrains.kotlin.ir.expressions.impl.IrClassReferenceImpl21import org.jetbrains.kotlin.ir.expressions.impl.IrExpressionBodyImpl22import org.jetbrains.kotlin.ir.expressions.impl.IrFunctionReferenceImpl23import org.jetbrains.kotlin.ir.symbols.IrFieldSymbol24import org.jetbrains.kotlin.ir.symbols.impl.IrFieldSymbolImpl25import org.jetbrains.kotlin.ir.symbols.impl.IrSimpleFunctionSymbolImpl26import org.jetbrains.kotlin.ir.types.IrType27import org.jetbrains.kotlin.ir.types.getClass28import org.jetbrains.kotlin.ir.types.typeWith29import org.jetbrains.kotlin.ir.util.constructors30import org.jetbrains.kotlin.ir.util.defaultType31import org.jetbrains.kotlin.ir.util.fqNameForIrSerialization32import org.jetbrains.kotlin.ir.visitors.IrElementVisitorVoid33import org.jetbrains.kotlin.ir.visitors.acceptChildrenVoid34import org.jetbrains.kotlin.name.ClassId35import org.jetbrains.kotlin.name.FqName36import org.jetbrains.kotlin.name.Name37// TODO: this extension is broken starting from 1.3.7x38class SpekExtension : IrGenerationExtension {39 override fun generate(moduleFragment: IrModuleFragment, pluginContext: IrPluginContext) {40 moduleFragment.files.forEach { file ->41 val spekCollector = SpekCollector(file, pluginContext)42 file.acceptChildrenVoid(spekCollector)43 spekCollector.generateRegistrations()44 }45 }46}47private class SpekCollector(48 private val file: IrFile,49 private val pluginContext: IrPluginContext50) : IrElementVisitorVoid {51 private val spekClassName = "org.spekframework.spek2.Spek"52 private var collectedSpeks = mutableListOf<IrClass>()53 override fun visitElement(element: IrElement) {54 element.acceptChildrenVoid(this)55 }56 override fun visitClass(declaration: IrClass) {57 super.visitClass(declaration)58 if (!declaration.isSpek) {59 return60 }61 if (declaration.kind != ClassKind.OBJECT) {62 if (!declaration.isAbstract) {63 //commonContext.reportWarning("Declaration ${declaration.name} inherits from $spekClassName but is not an object (it has kind ${declaration.kind}) and so will be not be run.", file, declaration)64 }65 return66 }67 collectedSpeks.add(declaration)68 }69 fun generateRegistrations() {70 collectedSpeks.forEach { generateRegistration(it) }71 }72 private fun generateRegistration(declaration: IrClass) {73 val testInfoClass = pluginContext.referenceClass(FqName("org.spekframework.spek2.launcher.TestInfo"))74 requireNotNull(testInfoClass) { "Unable to find org.spekframework.spek2.launcher.TestInfo" }75 //val testInfoClassDescriptor = pluginContext.moduleDescriptor.findClassAcrossModuleDependencies(ClassId.topLevel(FqName("org.spekframework.spek2.launcher.TestInfo")))!!76 //val testInfoClass = pluginContext.symbolTable.referenceClass(testInfoClassDescriptor)77 DeclarationIrBuilder(pluginContext, file.symbol, file.startOffset, file.endOffset).run {78 val primaryConstructor = testInfoClass.constructors.first()79 val call = irCall(primaryConstructor).apply {80 // TODO: should both of the IrType parameters below be declaration.defaultType?81 // Should one be the equivalent of KClass<Spek> or KClass<DerivedSpek>?82 val classType = pluginContext.irBuiltIns.kClassClass.typeWith(declaration.defaultType)83 val classReference = IrClassReferenceImpl(startOffset, endOffset, classType, declaration.symbol, classType)84 putValueArgument(0, classReference)85 val factoryType = primaryConstructor.owner.valueParameters[1].type86 val factoryBlock = createFactoryLambdaBlock(declaration, factoryType)87 putValueArgument(1, factoryBlock)88 }89 file.addTopLevelInitializer(call, pluginContext)90 }91 }92 private fun createFactoryLambdaBlock(declaration: IrClass, factoryType: IrType): IrBlock {93 return IrBlockImpl(94 declaration.startOffset,95 declaration.endOffset,96 factoryType,97 IrStatementOrigin.LAMBDA98 ).apply {99 val factory = createFactoryLambda(declaration)100 val factoryReference = IrFunctionReferenceImpl(101 factory.startOffset,102 factory.endOffset,103 factoryType,104 factory.symbol,105 0,106 0,107 null,108 IrStatementOrigin.LAMBDA109 )110 statements.add(factory)111 statements.add(factoryReference)112 }113 }114 private fun createFactoryLambda(declaration: IrClass): IrFunction = WrappedSimpleFunctionDescriptor().let { descriptor ->115 IrFunctionImpl(116 declaration.startOffset,117 declaration.endOffset,118 IrDeclarationOrigin.LOCAL_FUNCTION_FOR_LAMBDA,119 IrSimpleFunctionSymbolImpl(descriptor),120 Name.special("<anonymous>"),121 DescriptorVisibilities.LOCAL,122 Modality.FINAL,123 declaration.defaultType,124 isInline = false,125 isExternal = false,126 isTailrec = false,127 isSuspend = false,128 isExpect = false,129 isOperator = false,130 isFakeOverride = false,131 isInfix = false,132 ).apply {133 descriptor.bind(this)134 parent = this@SpekCollector.file135 body = DeclarationIrBuilder(pluginContext, symbol, symbol.owner.startOffset, symbol.owner.endOffset).irBlockBody {136 +irReturn(irGetObject(declaration.symbol))137 }138 }139 }140 private val IrClass.isSpek: Boolean141 get() = superTypes.any {142 it.getClass()?.fqNameForIrSerialization?.asString() == spekClassName143 }144 private val IrClass.isAbstract: Boolean145 get() = this.modality == Modality.ABSTRACT146}147private var topLevelInitializersCounter = 0148// originally taken from org.jetbrains.kotlin.ir.util/IrUtils2.kt but reworked149// 1. creates a top level field150// 2. creates a property using the previously declared field as its backing field151// Previously a top level field would cause the object to created but now it requires a property152private fun IrFile.addTopLevelInitializer(expression: IrExpression, pluginContext: IrPluginContext) {153 val threadLocalAnnotation = pluginContext.irBuiltIns.builtIns.builtInsModule.findClassAcrossModuleDependencies(154 ClassId.topLevel(FqName("kotlin.native.concurrent.ThreadLocal")))!!155 val t = pluginContext.symbols.externalSymbolTable.referenceClass(threadLocalAnnotation)156 val fieldName = "topLevelInitializer${topLevelInitializersCounter++}".synthesizedName157 val descriptor = WrappedFieldDescriptor()158 pluginContext.irFactory.createField(159 startOffset,160 endOffset,161 IrDeclarationOrigin.PROPERTY_BACKING_FIELD,162 IrFieldSymbolImpl(descriptor),163 fieldName,164 expression.type,165 DescriptorVisibilities.PRIVATE,166 true,167 false,168 true169 ).apply {170 addChild(this)171 descriptor.bind(this)172 parent = this@addTopLevelInitializer173 initializer = IrExpressionBodyImpl(startOffset, endOffset, expression)174 annotations += DeclarationIrBuilder(pluginContext, this.symbol, startOffset, endOffset).irCallConstructor(t.constructors.first(), emptyList())175 }176// Don't need a property anymore for a field to be initialized (in 1.4), previously this was needed.177// addProperty {178// name = fieldName179// visibility = Visibilities.PRIVATE180// origin = IrDeclarationOrigin.DEFINED181// }.also { property ->182// property.backingField = field183// property.getter = buildFun {184// origin = IrDeclarationOrigin.DEFAULT_PROPERTY_ACCESSOR185// name = "get-${fieldName.identifier}".synthesizedName186// returnType = field.type187// visibility = Visibilities.PRIVATE188// }.also { func ->189// func.parent = this@addTopLevelInitializer190// func.body = DeclarationIrBuilder(pluginContext, func.symbol, func.startOffset, func.endOffset).irBlockBody {191// +irReturn(irGetField(null, field))192// }193// }194// }195}...

Full Screen

Full Screen

IrFile.addTopLevelInitializer

Using AI Code Generation

copy

Full Screen

1 fun IrFile.addTopLevelInitializer(initializer: IrExpressionBody) {2 val initializerBlock = IrBlockImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET, context.irBuiltIns.unitType, IrStatementOrigin.INITIALIZER_BLOCK)3 initializerBlock.statements.add(IrExpressionBodyImpl(initializer.expression))4 this.declarations.add(initializerBlock)5 }6 override fun generate(moduleFragment: IrModuleFragment, pluginContext: IrPluginContext) {7 val initializer = IrExpressionBodyImpl(IrConstImpl.string(8 moduleFragment.files.forEach {9 it.addTopLevelInitializer(initializer)10 }11 }

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Spek automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful