All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jetbrains.spek.engine.memoized.SubjectAdapter.kt Maven / Gradle / Ivy

There is a newer version: 1.1.5
Show newest version
package org.jetbrains.spek.engine.memoized

import org.jetbrains.spek.api.memoized.CachingMode
import org.jetbrains.spek.extension.GroupExtensionContext
import org.jetbrains.spek.extension.TestExtensionContext
import org.jetbrains.spek.extension.execution.AfterExecuteGroup
import org.jetbrains.spek.extension.execution.AfterExecuteTest
import java.util.*

/**
 * Adapter for [subjects][Subject] as a pseudo-extension.
 *
 * @author Ranie Jade Ramiso
 */
class SubjectAdapter: AfterExecuteGroup, AfterExecuteTest {
    private val subjectMap: MutableMap> = WeakHashMap()

    override fun afterExecuteGroup(group: GroupExtensionContext) {
        val subject = subjectMap[group]

        if (subject != null && subject.mode == CachingMode.GROUP) {
            subject.reset()
        }
    }

    override fun afterExecuteTest(test: TestExtensionContext) {
        if (!test.parent.lazy) {
            resetSubjects(test.parent)
        }
    }

    fun  registerSubject(mode: CachingMode, group: GroupExtensionContext, factory: () -> T): SubjectImpl {
        val subject = SubjectImpl(mode, factory)
        subjectMap.put(group, subject)
        return subject
    }

    fun resetSubjects(group: GroupExtensionContext) {
        val subject = subjectMap[group]

        if (subject != null && subject.mode == CachingMode.TEST) {
            subject.reset()
        }

        if (group.parent != null) {
            resetSubjects(group.parent!!)
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy