Best Spek code snippet using org.spekframework.intellij.domain.ScopeDescriptorCache.isSpekSubclass
ScopeDescriptorCache.kt
Source:ScopeDescriptorCache.kt
...26 }27 return null28 }29 fun fromClassOrObject(clz: KtClassOrObject): ScopeDescriptor.Group? {30 if (clz.isAbstract() || clz.isObjectLiteral() || !isSpekSubclass(clz)) {31 return null32 }33 val fqName = checkNotNull(clz.fqName).asString()34 var cached = cache.getOrDefault(fqName, null)35 if (cached == null || cached.first < clz.modificationStamp) {36 cached = clz.modificationStamp to buildDescriptor(clz)37 cache[fqName] = cached38 }39 return checkNotNull(cached).second40 }41 private fun buildDescriptor(clz: KtClassOrObject): ScopeDescriptor.Group {42 val info = KtClassInfoUtil.createClassOrObjectInfo(clz)43 val builder = PathBuilder()44 if (info.containingPackageFqName.asString().isNotBlank()) {45 builder.appendPackage(info.containingPackageFqName.asString())46 }47 val path = builder48 .append(checkNotNull(clz.fqName).shortName().asString())49 .build()50 val superTypeCall = clz.superTypeListEntries.filterIsInstance<KtSuperTypeCallEntry>()51 .firstOrNull()52 val children = mutableListOf<ScopeDescriptor>()53 if (superTypeCall != null) {54 val lambda = superTypeCall.valueArguments.map { it.getArgumentExpression() }55 .filterIsInstance<KtLambdaExpression>()56 .firstOrNull()57 if (lambda != null) {58 lambda.bodyExpression?.let { body ->59 children.addAll(buildScopes(path, body))60 }61 }62 }63 return ScopeDescriptor.Group(path, clz, false, true, children.toList()).apply {64 index[path.serialize()] = this65 }66 }67 private fun buildScopes(parent: Path, block: KtBlockExpression): List<ScopeDescriptor> {68 val callExpressions = PsiTreeUtil.getChildrenOfTypeAsList(block, KtCallExpression::class.java)69 return callExpressions.mapNotNull { callExpression ->70 val synonymContext = fetchSynonymContext(callExpression)71 when {72 synonymContext != null -> callExpression to synonymContext73 else -> null74 }75 }.mapNotNull { (callExpression, synonymContext) ->76 try {77 val description = synonymContext.constructDescription(callExpression)78 val path = PathBuilder(parent)79 .append(description)80 .build()81 val descriptor = when (synonymContext.synonym.type) {82 PsiSynonymType.GROUP -> {83 val lambdaArgument = callExpression.lambdaArguments.firstOrNull()84 val children = mutableListOf<ScopeDescriptor>()85 if (lambdaArgument != null) {86 val lambdaExpression = lambdaArgument.getLambdaExpression()87 if (lambdaExpression != null) {88 lambdaExpression.bodyExpression?.let { body ->89 children.addAll(buildScopes(path, body))90 }91 }92 }93 ScopeDescriptor.Group(94 path, callExpression, synonymContext.isExcluded(),95 synonymContext.isRunnable(), children.toList()96 )97 }98 PsiSynonymType.TEST -> ScopeDescriptor.Test(99 path, callExpression, synonymContext.isExcluded(), synonymContext.isRunnable()100 )101 }102 index[path.serialize()] = descriptor103 descriptor104 } catch (e: UnsupportedFeatureException) {105 // avoid ide from throwing up106 null107 }108 }109 }110 private fun fetchSynonymContext(callExpression: KtCallExpression): SynonymContext? {111 return fetchNamedFunction(callExpression)?.let(::extractSynonymContext)112 }113 private fun fetchNamedFunction(callExpression: KtCallExpression): KtNamedFunction? {114 val mainReference = callExpression.calleeExpression?.mainReference115 val resolved = mainReference?.resolve()116 if (resolved is KtNamedFunction) {117 return resolved118 }119 return null120 }121 private fun extractSynonymContext(function: KtNamedFunction): SynonymContext? {122 val lightMethod = function.toLightMethods().firstOrNull()123 if (lightMethod != null) {124 val synonym = lightMethod.annotations125 .filter { SYNONYM_CLASSES.contains(it.qualifiedName) }126 .map(::PsiSynonym)127 .firstOrNull()128 if (synonym != null) {129 val descriptions = lightMethod.annotations130 .filter { DESCRIPTIONS_CLASSES.contains(it.qualifiedName) }131 .map(::PsiDescriptions)132 .firstOrNull()133 if (descriptions != null) {134 return SynonymContext(synonym, descriptions)135 }136 }137 }138 return null139 }140 private fun isSpekSubclass(element: KtClassOrObject): Boolean {141 val superClass = getSuperClass(element)142 val fqName = superClass?.getKotlinFqName()143 if (fqName != null) {144 if (SPEK_CLASSES.contains(fqName.toString())) {145 return true146 } else {147 return isSpekSubclass(superClass)148 }149 }150 return false151 }152 private fun getSuperClass(element: KtClassOrObject): KtClassOrObject? {153 val superTypes = element.superTypeListEntries154 var superClass: KtClassOrObject? = null155 for (entry in superTypes) {156 if (entry is KtSuperTypeCallEntry) {157 val ref = entry.calleeExpression.constructorReferenceExpression158 ?.mainReference159 ?.resolve()160 superClass = when (ref) {161 is KtPrimaryConstructor -> ref.getContainingClassOrObject()...
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!!