Best Spek code snippet using org.spekframework.spek2.runtime.Collector.finalize
Collectors.kt
Source:Collectors.kt
...10 private val lifecycleManager: LifecycleManager,11 override val defaultCachingMode: CachingMode,12 override var defaultTimeout: Long13) : Root {14 private val finalizers = mutableListOf<() -> Unit>()15 private val ids = linkedMapOf<String, Int>()16 fun finalize() {17 finalizers.forEach { it.invoke() }18 finalizers.clear()19 }20 override fun <T> memoized(mode: CachingMode, factory: suspend () -> T): MemoizedValue<T> = memoized(mode, factory) { }21 override fun <T> memoized(mode: CachingMode, factory: suspend () -> T, destructor: suspend (T) -> Unit): MemoizedValue<T> {22 // scope values automatically register an afterXXX fixture23 // when de-initializing - this means that any afterXXX fixture24 // declared after the memoized can't access it.25 // this custom lifecycle aware object delays the registration26 // of the afterXXX fixtures called by MemoizedValueAdapter.27 // The fixtures are only registered when Collector.finalize() is invoked,28 // which happens after a group have been discovered.29 val lifecycleAware = object: LifecycleAware by this {30 override fun afterEachTest(fixture: Fixture) {31 with(this@Collector) {32 finalizers.add { afterEachTest(fixture) }33 }34 }35 override fun afterEachGroup(fixture: Fixture) {36 with(this@Collector) {37 finalizers.add { afterEachGroup(fixture) }38 }39 }40 override fun afterGroup(fixture: Fixture) {41 with(this@Collector) {42 finalizers.add { afterGroup(fixture) }43 }44 }45 }46 return MemoizedValueCreator(47 root,48 mode,49 lifecycleAware,50 factory,51 destructor52 )53 }54 override fun <T> memoized(): MemoizedValue<T> {55 return MemoizedValueReader(root)56 }57 override fun registerListener(listener: LifecycleListener) {58 lifecycleManager.addListener(listener)59 }60 override fun group(description: String, skip: Skip, defaultCachingMode: CachingMode, preserveExecutionOrder: Boolean, failFast: Boolean, body: GroupBody.() -> Unit) {61 // TODO: combine failFast and preserveExecutionOrder into a single parameter.62 require(failFast == preserveExecutionOrder) { "failFast and preserveExecutionOrder must have the same value!" }63 if (root.failFast) {64 throw AssertionError("Fail fast groups can only contain test scopes!")65 }66 val group = GroupScopeImpl(67 idFor(description),68 root.path.resolve(description),69 root,70 skip,71 lifecycleManager,72 preserveExecutionOrder,73 failFast74 )75 root.addChild(group)76 val cachingMode = if (defaultCachingMode == CachingMode.INHERIT) {77 this.defaultCachingMode78 } else {79 defaultCachingMode80 }81 val collector = Collector(group, lifecycleManager, cachingMode, defaultTimeout)82 try {83 require(description.isNotEmpty()) { "Empty description for group." }84 body.invoke(collector)85 collector.finalize()86 } catch (e: Throwable) {87 collector.beforeGroup { throw e }88 group.addChild(89 TestScopeImpl(90 idFor("Group Failure"),91 root.path.resolve("Group Failure"),92 root,93 defaultTimeout,94 {},95 skip,96 lifecycleManager97 )98 )99 }...
SpekRuntime.kt
Source:SpekRuntime.kt
...76 val classScope = GroupScopeImpl(ScopeId(ScopeType.Class, qualifiedName), path, null, Skip.No, lifecycleManager, false)77 val collector = Collector(classScope, lifecycleManager, CachingMode.TEST, getGlobalTimeoutSetting(DEFAULT_TIMEOUT))78 try {79 instance.root.invoke(collector)80 collector.finalize()81 } catch (e: Exception) {82 collector.beforeGroup { throw e }83 classScope.addChild(TestScopeImpl(84 ScopeId(ScopeType.Scope, "Discovery failure"),85 path.resolve("Discovery failure"),86 classScope,87 getGlobalTimeoutSetting(DEFAULT_TIMEOUT),88 {},89 Skip.No,90 lifecycleManager91 ))92 }93 return classScope94 }...
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!!