Best Spek code snippet using org.spekframework.spek2.runtime.util.ClassUtil
Path.kt
Source:Path.kt
1package org.spekframework.spek2.runtime.scope2import org.spekframework.spek2.runtime.util.Base643import org.spekframework.spek2.runtime.util.ClassUtil4import kotlin.reflect.KClass5val Path.isRoot: Boolean6 get() {7 return name.isEmpty() && parent == null8 }9// handles two cases10// classToPath -> discoveryRequest.path11// 1: my.package/MyClass -> my.package/MyClass/description12// 2: my.package/MyClass/description -> my.package/MyClass13fun Path.intersects(path: Path) = this.isParentOf(path) || path.isParentOf(this)14data class Path(val name: String, val parent: Path?) {15 private val serialized by lazy {16 serialize(this)17 }18 private val humanReadable by lazy {19 serialize(this, false)20 }21 private val encoded by lazy {22 encode(name)23 }24 fun resolve(name: String) = Path(name, this)25 fun isParentOf(path: Path): Boolean {26 var current: Path? = path27 while (current != null) {28 if (current == this) {29 return true30 }31 current = current.parent32 }33 return false34 }35 fun serialize(): String = serialized36 override fun toString(): String {37 return humanReadable38 }39 companion object {40 const val PATH_SEPARATOR = '/'41 private fun serialize(path: Path, encoded: Boolean = true): String {42 return if (path.parent == null) {43 // this will be an empty string44 path.name45 } else {46 val name = if (encoded) {47 path.encoded48 } else {49 path.name50 }51 "${serialize(path.parent, encoded)}$PATH_SEPARATOR$name".trimStart(PATH_SEPARATOR)52 }53 }54 fun encode(name: String): String {55 return Base64.encodeToString(name)56 }57 fun decode(name: String): String {58 return Base64.decodeToString(name)59 }60 }61}62class PathBuilder(private var parent: Path) {63 constructor() : this(ROOT)64 fun appendPackage(packageName: String): PathBuilder {65 packageName.split('.')66 .forEach { part -> append(part) }67 return this68 }69 fun append(name: String): PathBuilder {70 parent = Path(name, parent)71 return this72 }73 fun build(): Path = parent74 companion object {75 val ROOT: Path = Path("", null)76 fun from(clz: KClass<*>): PathBuilder {77 val (packageName, className) = ClassUtil.extractPackageAndClassNames(clz)78 val builder = PathBuilder()79 if (packageName.isNotEmpty()) {80 builder.appendPackage(packageName)81 }82 return builder83 .append(className)84 }85 fun parse(path: String): PathBuilder {86 var builder = PathBuilder()87 path.split(Path.PATH_SEPARATOR)88 .forEach { builder = builder.append(Path.decode(it)) }89 return builder90 }91 }...
SpekRuntime.kt
Source:SpekRuntime.kt
...6import org.spekframework.spek2.runtime.execution.DiscoveryResult7import org.spekframework.spek2.runtime.execution.ExecutionRequest8import org.spekframework.spek2.runtime.lifecycle.LifecycleManager9import org.spekframework.spek2.runtime.scope.*10import org.spekframework.spek2.runtime.util.ClassUtil11private const val DEFAULT_TIMEOUT = 10000L12class SpekRuntime {13 fun discover(discoveryRequest: DiscoveryRequest): DiscoveryResult {14 val scopes = discoveryRequest.context.getTests()15 .map { testInfo ->16 val matchingPath = discoveryRequest.paths.firstOrNull { it.intersects(testInfo.path) }17 testInfo to matchingPath18 }19 .filter { (_, matchingPath) -> matchingPath != null }20 .map { (testInfo, matchingPath) ->21 checkNotNull(matchingPath)22 val spec = resolveSpec(testInfo.createInstance(), testInfo.path)23 spec.filterBy(matchingPath)24 spec25 }26 .filter { spec -> !spec.isEmpty() }27 return DiscoveryResult(scopes)28 }29 fun execute(request: ExecutionRequest) = Executor().execute(request)30 private fun resolveSpec(instance: Spek, path: Path): GroupScopeImpl {31 val fixtures = FixturesAdapter()32 val lifecycleManager = LifecycleManager().apply {33 addListener(fixtures)34 }35 val (packageName, className) = ClassUtil.extractPackageAndClassNames(instance::class)36 val qualifiedName = if (packageName.isNotEmpty()) {37 "$packageName.$className"38 } else {39 className40 }41 val classScope = GroupScopeImpl(ScopeId(ScopeType.Class, qualifiedName), path, null, Skip.No, lifecycleManager, false)42 val collector = Collector(classScope, lifecycleManager, fixtures, CachingMode.TEST, DEFAULT_TIMEOUT)43 try {44 instance.root.invoke(collector)45 } catch (e: Exception) {46 collector.beforeGroup { throw e }47 classScope.addChild(TestScopeImpl(48 ScopeId(ScopeType.Scope, "Discovery failure"),49 path.resolve("Discovery failure"),...
ClassUtil.kt
Source:ClassUtil.kt
1package org.spekframework.spek2.runtime.util2import kotlin.reflect.KClass3object ClassUtil {4 fun extractPackageAndClassNames(clz: KClass<*>): Pair<String, String> {5 val qualifiedName = checkNotNull(clz.qualifiedName)6 val className = checkNotNull(clz.simpleName)7 var packageName = qualifiedName.removeSuffix(className)8 .trimEnd('.')9 return packageName to className10 }11}...
ClassUtil
Using AI Code Generation
1import org.spekframework.spek2.runtime.util.ClassUtil 2import org.spekframework.spek2.runtime.util.ClassUtilKt3import org.spekframework.spek2.runtime.scope.TestScopeImpl 4import org.spekframework.spek2.runtime.scope.TestScopeImplKt5import org.spekframework.spek2.runtime.scope.TestScopeImpl 6import org.spekframework.spek2.runtime.scope.TestScopeImplKt7import org.spekframework.spek2.runtime.scope.TestScopeImpl 8import org.spekframework.spek2.runtime.scope.TestScopeImplKt9import org.spekframework.spek2.runtime.scope.TestScopeImpl 10import org.spekframework.spek2.runtime.scope.TestScopeImplKt11import org.spekframework.spek2.runtime.scope.TestScopeImpl 12import org.spekframework.spek2.runtime.scope.TestScopeImplKt13import org.spekframework.spek2.runtime.scope.TestScopeImpl 14import org.spekframework.spek2.runtime.scope.TestScopeImplKt15import org.spekframework.spek2.runtime.scope.TestScopeImpl 16import org.spekframework.spek2.runtime.scope.TestScopeImplKt17import org.spekframework.spek2.runtime.scope.TestScopeImpl 18import org.spekframework.spek2.runtime.scope.TestScopeImplKt19import org.spekframework.spek2.runtime.scope.TestScopeImpl 20import org.spekframework.spek2.runtime.scope.TestScopeImplKt21import org.spekframework.spek2.runtime.scope.TestScopeImpl 22import org.spekframework
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!!