Best Spek code snippet using org.spekframework.intellij.producerType.IdePlatformKind.toProducerType
SpekRunConfigurationProducer.kt
Source:SpekRunConfigurationProducer.kt
1package org.spekframework.intellij2import com.intellij.execution.actions.ConfigurationContext3import com.intellij.execution.actions.RunConfigurationProducer4import com.intellij.openapi.module.Module5import com.intellij.openapi.project.Project6import com.intellij.openapi.roots.ModuleRootManager7import com.intellij.openapi.util.Ref8import com.intellij.psi.PsiDirectory9import com.intellij.psi.PsiElement10import com.intellij.psi.PsiPackage11import org.jetbrains.kotlin.config.KotlinFacetSettings12import org.jetbrains.kotlin.config.KotlinFacetSettingsProvider13import org.jetbrains.kotlin.idea.caches.project.implementingModules14import org.jetbrains.kotlin.idea.core.getPackage15import org.jetbrains.kotlin.idea.util.module16import org.jetbrains.kotlin.platform.IdePlatformKind17import org.jetbrains.kotlin.platform.impl.CommonIdePlatformKind18import org.jetbrains.kotlin.psi.KtCallExpression19import org.jetbrains.kotlin.psi.KtClassOrObject20import org.spekframework.intellij.domain.ScopeDescriptorCache21import org.spekframework.intellij.support.SpekCommonProgramRunConfigurationParameters.GeneratedNameHint22import org.spekframework.intellij.util.maybeGetContext23import org.spekframework.spek2.runtime.scope.Path24import org.spekframework.spek2.runtime.scope.PathBuilder25abstract class SpekRunConfigurationProducer(val producerType: ProducerType, type: SpekConfigurationType)26 : RunConfigurationProducer<SpekRunConfiguration<*>>(type) {27 override fun isConfigurationFromContext(configuration: SpekRunConfiguration<*>,28 context: ConfigurationContext): Boolean {29 val descriptorCache = checkNotNull(30 context.project.getComponent(ScopeDescriptorCache::class.java)31 )32 return context.psiLocation?.let {33 var nameHint: GeneratedNameHint? = null34 val path = if (it is PsiDirectory) {35 getPathFromDir(context, it)36 nameHint = GeneratedNameHint.PACKAGE37 } else if (it is PsiPackage) {38 getPathFromPackage(it)39 nameHint = GeneratedNameHint.PACKAGE40 } else {41 val elementContext = maybeGetContext(it)42 val descriptor = when (elementContext) {43 is KtClassOrObject -> {44 nameHint = GeneratedNameHint.CLASS45 descriptorCache.fromClassOrObject(elementContext)46 }47 is KtCallExpression -> {48 nameHint = GeneratedNameHint.SCOPE49 descriptorCache.fromCallExpression(elementContext)50 }51 else -> null52 }53 if (descriptor != null && !descriptor.excluded && descriptor.runnable) {54 descriptor.path55 } else {56 null57 }58 }59 configuration.data.path == path && configuration.data.generatedNameHint == nameHint60 } ?: false61 }62 override fun setupConfigurationFromContext(configuration: SpekRunConfiguration<*>,63 context: ConfigurationContext,64 sourceElement: Ref<PsiElement>): Boolean {65 val descriptorCache = checkNotNull(66 context.project.getComponent(ScopeDescriptorCache::class.java)67 )68 return sourceElement.get().let {69 var nameHint: GeneratedNameHint? = null70 val path = if (it is PsiDirectory) {71 nameHint = GeneratedNameHint.PACKAGE72 getPathFromDir(context, it)73 } else if (it is PsiPackage) {74 nameHint = GeneratedNameHint.PACKAGE75 getPathFromPackage(it)76 } else {77 val elementContext = maybeGetContext(it)78 val descriptor = when (elementContext) {79 is KtClassOrObject -> {80 nameHint = GeneratedNameHint.CLASS81 descriptorCache.fromClassOrObject(elementContext)82 }83 is KtCallExpression -> {84 nameHint = GeneratedNameHint.SCOPE85 descriptorCache.fromCallExpression(elementContext)86 }87 else -> null88 }89 if (descriptor != null && !descriptor.excluded && descriptor.runnable) {90 descriptor.path91 } else {92 null93 }94 }95 if (path != null) {96 configuration.data.path = path97 configuration.data.generatedNameHint = nameHint98 val kotlinFacetSettings = KotlinFacetSettingsProvider.getInstance(context.project)99 .getInitializedSettings(context.module)100 var canRun = false101 if (isPlatformSupported(kotlinFacetSettings.platform!!.kind)) {102 configuration.configureForModule(context.module)103 configuration.data.producerType = producerType104 canRun = true105 configuration.data.producerType = producerType106 } else if (kotlinFacetSettings.platform!!.kind == CommonIdePlatformKind) {107 val result = findSupportedModule(context.project, context.module)108 if (result != null) {109 val (module, moduleKotlinFacetSettings) = result110 configuration.configureForModule(module)111 configuration.data.producerType = moduleKotlinFacetSettings.platform!!.kind.toProducerType()112 canRun = true113 }114 }115 canRun116 } else {117 false118 }119 }120 }121 private fun getPathFromDir(context: ConfigurationContext, dir: PsiDirectory): Path? {122 if (context.module != null) {123 val moduleRootManager = ModuleRootManager.getInstance(context.module)124 if (context.module == dir.module || moduleRootManager.isDependsOn(dir.module)) {125 val psiPackage = dir.getPackage()126 if (psiPackage != null) {127 return getPathFromPackage(psiPackage)128 }129 }130 }131 return null132 }133 private fun getPathFromPackage(pkg: PsiPackage): Path {134 if (pkg.qualifiedName.isEmpty()) {135 return PathBuilder().build()136 }137 return PathBuilder()138 .appendPackage(pkg.qualifiedName)139 .build()140 }141 private fun findSupportedModule(project: Project, commonModule: Module): Pair<Module, KotlinFacetSettings>? {142 val kotlinFacetSettingsProvider = KotlinFacetSettingsProvider.getInstance(project)143 return commonModule.implementingModules144 .map { it to kotlinFacetSettingsProvider.getInitializedSettings(it) }145 .firstOrNull {146 isPlatformSupported(it.second.platform!!.kind)147 }148 }149 private fun isPlatformSupported(kind: IdePlatformKind<*>) = kind.toProducerType() == producerType150}...
producerType.kt
Source:producerType.kt
1package org.spekframework.intellij2import org.jetbrains.kotlin.platform.IdePlatformKind3import org.jetbrains.kotlin.platform.impl.CommonIdePlatformKind4import org.jetbrains.kotlin.platform.impl.JsIdePlatformKind5import org.jetbrains.kotlin.platform.impl.JvmIdePlatformKind6import org.jetbrains.kotlin.platform.impl.NativeIdePlatformKind7enum class ProducerType {8 COMMON,9 JVM,10 NATIVE,11 JS12}13fun IdePlatformKind<*>.toProducerType(): ProducerType {14 return when (this) {15 CommonIdePlatformKind -> ProducerType.COMMON16 JvmIdePlatformKind -> ProducerType.JVM17 NativeIdePlatformKind -> ProducerType.NATIVE18 JsIdePlatformKind -> ProducerType.JS19 else -> throw IllegalArgumentException("Unsupported platform kind: ${this}")20 }21}...
IdePlatformKind.toProducerType
Using AI Code Generation
1val platformType = IdePlatformKind.toProducerType(platformKind)2val platform = platformType.java.newInstance()3val spek = Spek(platform)4val root = spek.discover()5val engine = SpekEngine(root)6val listener = object : TestEngineListener {7override fun executionStarted(test: Test) {8}9override fun executionFinished(test: Test, result: TestResult) {10}11override fun executionSkipped(test: Test, reason: String) {12}13override fun executionIgnored(test: Test) {14}15}16engine.execute(listener)17}18}19}20private fun getPlatformKind(): IdePlatformKind<*> {21val platformManager = IdePlatformKind::class.java.getDeclaredField("PLATFORM_MANAGER")22val manager = platformManager.get(null)23val getPlatformKind = manager.javaClass.getDeclaredMethod("getPlatformKind", String::class.java)24return getPlatformKind.invoke(manager, "IntelliJ IDEA Community Edition") as IdePlatformKind<*>25}26}
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!!