How to use EngineContext.toProjectContext method of io.kotest.engine.interceptors.EngineContext class

Best Kotest code snippet using io.kotest.engine.interceptors.EngineContext.EngineContext.toProjectContext

SpecExecutor.kt

Source:SpecExecutor.kt Github

copy

Full Screen

1package io.kotest.engine.spec2import io.kotest.common.ExperimentalKotest3import io.kotest.common.Platform4import io.kotest.common.flatMap5import io.kotest.common.platform6import io.kotest.core.concurrency.CoroutineDispatcherFactory7import io.kotest.core.config.ProjectConfiguration8import io.kotest.core.spec.DslDrivenSpec9import io.kotest.core.spec.Spec10import io.kotest.core.spec.SpecRef11import io.kotest.core.test.TestCase12import io.kotest.core.test.TestResult13import io.kotest.engine.interceptors.EngineContext14import io.kotest.engine.interceptors.toProjectContext15import io.kotest.engine.listener.TestEngineListener16import io.kotest.engine.spec.interceptor.ApplyExtensionsInterceptor17import io.kotest.engine.spec.interceptor.ConfigurationInContextInterceptor18import io.kotest.engine.spec.interceptor.EnabledIfSpecInterceptor19import io.kotest.engine.spec.interceptor.FinalizeSpecInterceptor20import io.kotest.engine.spec.interceptor.IgnoreNestedSpecStylesInterceptor21import io.kotest.engine.spec.interceptor.IgnoredSpecInterceptor22import io.kotest.engine.spec.interceptor.PrepareSpecInterceptor23import io.kotest.engine.spec.interceptor.ProjectContextInterceptor24import io.kotest.engine.spec.interceptor.RequiresTagSpecInterceptor25import io.kotest.engine.spec.interceptor.SpecExtensionInterceptor26import io.kotest.engine.spec.interceptor.SpecFilterInterceptor27import io.kotest.engine.spec.interceptor.SpecFinishedInterceptor28import io.kotest.engine.spec.interceptor.SpecRefExtensionInterceptor29import io.kotest.engine.spec.interceptor.SpecRefInterceptor30import io.kotest.engine.spec.interceptor.SpecStartedInterceptor31import io.kotest.engine.spec.interceptor.SystemPropertySpecFilterInterceptor32import io.kotest.engine.spec.interceptor.TagsExcludedSpecInterceptor33import io.kotest.mpp.Logger34import io.kotest.mpp.bestName35import kotlin.reflect.KClass36/**37 * Executes a single [SpecRef].38 *39 * Uses a [TestEngineListener] to notify of events in the spec lifecycle.40 *41 * The spec executor has two levels of interceptors:42 * [io.kotest.engine.spec.interceptor.SpecRefInterceptor] are executed before the spec is created.43 * [io.kotest.engine.spec.interceptor.SpecInterceptor] are executed after the spec is created.44 *45 */46@ExperimentalKotest47class SpecExecutor(48 private val defaultCoroutineDispatcherFactory: CoroutineDispatcherFactory,49 private val context: EngineContext,50) {51 private val logger = Logger(SpecExecutorDelegate::class)52 private val extensions = SpecExtensions(context.configuration.registry)53 private val listener = context.listener54 suspend fun execute(ref: SpecRef) {55 logger.log { Pair(ref.kclass.bestName(), "Received $ref") }56 referenceInterceptors(ref)57 }58 suspend fun execute(kclass: KClass<out Spec>) {59 execute(SpecRef.Reference(kclass))60 }61 private suspend fun referenceInterceptors(ref: SpecRef) {62 val interceptors = listOfNotNull(63 if (platform == Platform.JVM) EnabledIfSpecInterceptor(listener, context.configuration.registry) else null,64 IgnoredSpecInterceptor(listener, context.configuration.registry),65 SpecFilterInterceptor(listener, context.configuration.registry),66 SystemPropertySpecFilterInterceptor(listener, context.configuration.registry),67 TagsExcludedSpecInterceptor(listener, context.configuration),68 if (platform == Platform.JVM) RequiresTagSpecInterceptor(listener, context.configuration, context.configuration.registry) else null,69 SpecRefExtensionInterceptor(context.configuration.registry),70 SpecStartedInterceptor(listener),71 SpecFinishedInterceptor(listener),72 if (platform == Platform.JVM) ApplyExtensionsInterceptor(context.configuration.registry) else null,73 PrepareSpecInterceptor(context.configuration.registry),74 FinalizeSpecInterceptor(context.configuration.registry),75 )76 val innerExecute: suspend (SpecRef) -> Result<Map<TestCase, TestResult>> = {77 createInstance(ref).flatMap { specInterceptors(it) }78 }79 logger.log { Pair(ref.kclass.bestName(), "Executing ${interceptors.size} reference interceptors") }80 interceptors.foldRight(innerExecute) { ext: SpecRefInterceptor, fn: suspend (SpecRef) -> Result<Map<TestCase, TestResult>> ->81 { ref -> ext.intercept(ref, fn) }82 }.invoke(ref)83 }84 private suspend fun specInterceptors(spec: Spec): Result<Map<TestCase, TestResult>> {85 val interceptors = listOfNotNull(86 if (platform == Platform.JS) IgnoreNestedSpecStylesInterceptor(listener, context.configuration.registry) else null,87 ProjectContextInterceptor(context.toProjectContext()),88 SpecExtensionInterceptor(context.configuration.registry),89 ConfigurationInContextInterceptor(context.configuration),90 )91 val initial: suspend (Spec) -> Result<Map<TestCase, TestResult>> = {92 try {93 val delegate = createSpecExecutorDelegate(listener, defaultCoroutineDispatcherFactory, context.configuration)94 logger.log { Pair(spec::class.bestName(), "delegate=$delegate") }95 Result.success(delegate.execute(spec))96 } catch (t: Throwable) {97 logger.log { Pair(spec::class.bestName(), "Error executing spec $t") }98 Result.failure(t)99 }100 }101 logger.log { Pair(spec::class.bestName(), "Executing ${interceptors.size} spec interceptors") }102 return interceptors.foldRight(initial) { ext, fn ->103 { spec -> ext.intercept(spec, fn) }104 }.invoke(spec)105 }106 /**107 * Creates an instance of the given [SpecRef], notifies users of the instantiation event108 * or instantiation failure, and returns a Result with the error or spec.109 *110 * After this method is called the spec is sealed.111 */112 private suspend fun createInstance(ref: SpecRef): Result<Spec> =113 ref.instance(context.configuration.registry)114 .onFailure { extensions.specInstantiationError(ref.kclass, it) }115 .flatMap { spec -> extensions.specInstantiated(spec).map { spec } }116 .onSuccess { if (it is DslDrivenSpec) it.seal() }117}118interface SpecExecutorDelegate {119 suspend fun execute(spec: Spec): Map<TestCase, TestResult>120}121@ExperimentalKotest122internal expect fun createSpecExecutorDelegate(123 listener: TestEngineListener,124 defaultCoroutineDispatcherFactory: CoroutineDispatcherFactory,125 configuration: ProjectConfiguration,126): SpecExecutorDelegate...

Full Screen

Full Screen

EngineInterceptor.kt

Source:EngineInterceptor.kt Github

copy

Full Screen

1package io.kotest.engine.interceptors2import io.kotest.common.KotestInternal3import io.kotest.core.TagExpression4import io.kotest.core.config.ProjectConfiguration5import io.kotest.core.project.ProjectContext6import io.kotest.core.project.TestSuite7import io.kotest.engine.EngineResult8import io.kotest.engine.listener.CompositeTestEngineListener9import io.kotest.engine.listener.NoopTestEngineListener10import io.kotest.engine.listener.TestEngineListener11/**12 * Internal pipeline that intercepts calls to the engine.13 *14 * This can be used to execute code before or after the engine15 * and permits changing the [EngineContext].16 */17@KotestInternal18interface EngineInterceptor {19 suspend fun intercept(context: EngineContext, execute: suspend (EngineContext) -> EngineResult): EngineResult20}21data class EngineContext(22 val suite: TestSuite,23 val listener: TestEngineListener,24 val tags: TagExpression,25 val configuration: ProjectConfiguration,26) {27 constructor(configuration: ProjectConfiguration) : this(28 TestSuite.empty,29 NoopTestEngineListener,30 TagExpression.Empty,31 configuration32 )33 companion object {34 val empty = EngineContext(TestSuite.empty, NoopTestEngineListener, TagExpression.Empty, ProjectConfiguration())35 }36 /**37 * Returns this [EngineContext] with the given [listener] added via a [CompositeTestEngineListener].38 */39 fun mergeListener(listener: TestEngineListener): EngineContext {40 return EngineContext(suite, CompositeTestEngineListener(listOf(this.listener, listener)), tags, configuration)41 }42 fun withTestSuite(suite: TestSuite): EngineContext {43 return EngineContext(suite, listener, tags, configuration)44 }45 fun withListener(listener: TestEngineListener): EngineContext {46 return EngineContext(suite, listener, tags, configuration)47 }48 fun withConfiguration(c: ProjectConfiguration): EngineContext {49 return EngineContext(suite, listener, tags, c)50 }51 fun withTags(tags: TagExpression): EngineContext {52 return EngineContext(suite, listener, tags, configuration)53 }54}55fun ProjectContext.toEngineContext(context: EngineContext): EngineContext {56 return EngineContext(57 suite,58 context.listener,59 tags,60 configuration61 )62}63fun EngineContext.toProjectContext(): ProjectContext {64 return ProjectContext(65 suite,66 tags,67 configuration68 )69}...

Full Screen

Full Screen

ProjectExtensionEngineInterceptor.kt

Source:ProjectExtensionEngineInterceptor.kt Github

copy

Full Screen

1package io.kotest.engine.interceptors2import io.kotest.common.KotestInternal3import io.kotest.core.extensions.ProjectExtension4import io.kotest.core.project.ProjectContext5import io.kotest.engine.EngineResult6/**7 * An [EngineInterceptor] that invokes any [ProjectExtension]s before the engine begins execution.8 *9 * Project extensions can adapt the [ProjectContext] which is the public API version of10 * the [EngineContext]. Any changes to the project context are reflected downstream in11 * the engine context passed to the execute function.12 */13@KotestInternal14internal object ProjectExtensionEngineInterceptor : EngineInterceptor {15 override suspend fun intercept(16 context: EngineContext,17 execute: suspend (EngineContext) -> EngineResult18 ): EngineResult {19 var result: EngineResult = EngineResult.empty20 val initial: suspend (ProjectContext) -> Unit = { result = execute(it.toEngineContext(context)) }21 val chain = context22 .configuration23 .registry24 .all()25 .filterIsInstance<ProjectExtension>()26 .foldRight(initial) { extension, acc: suspend (ProjectContext) -> Unit ->27 {28 extension.interceptProject(context.toProjectContext()) { acc(it) }29 }30 }31 return try {32 chain.invoke(context.toProjectContext())33 result34 } catch (t: Throwable) {35 result.addError(t)36 }37 }38}...

Full Screen

Full Screen

EngineContext.toProjectContext

Using AI Code Generation

copy

Full Screen

1import io.kotest.core.spec.style.StringSpec2import io.kotest.engine.interceptors.EngineContext3import io.kotest.matchers.shouldBe4class EngineContextTest : StringSpec({5 "toProjectContext" {6 val context = EngineContext()7 val projectContext = context.toProjectContext()8 }9})10import io.kotest.core.spec.style.StringSpec11import io.kotest.engine.interceptors.EngineContext12import io.kotest.matchers.shouldBe13class EngineContextTest : StringSpec({14 "toProjectContext" {15 val context = EngineContext()16 val projectContext = context.toProjectContext()17 }18})19import io.kotest.core.spec.style.StringSpec20import io.kotest.engine.interceptors.EngineContext21import io.kotest.matchers.shouldBe22class EngineContextTest : StringSpec({23 "toProjectContext" {24 val context = EngineContext()25 val projectContext = context.toProjectContext()26 }27})28import io.kotest.core.spec.style.StringSpec29import io.kotest.engine.interceptors.EngineContext30import io.kotest.matchers.shouldBe31class EngineContextTest : StringSpec({32 "toProjectContext" {33 val context = EngineContext()34 val projectContext = context.toProjectContext()35 }36})37import io.kotest.core.spec.style.StringSpec38import io.kotest.engine.interceptors.EngineContext39import io.kotest.matchers.shouldBe40class EngineContextTest : StringSpec({41 "toProjectContext" {42 val context = EngineContext()43 val projectContext = context.toProjectContext()44 }45})

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 Kotest 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