org.jetbrains.spek.engine.memoized.SubjectAdapter.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of spek-junit-platform-engine Show documentation
Show all versions of spek-junit-platform-engine Show documentation
Spek TestEngine for the JUnit Platform
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) {
var subject = subjectMap[group]
if (subject != null && subject.mode == CachingMode.GROUP) {
subject.reset()
}
}
override fun afterExecuteTest(test: TestExtensionContext) {
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!!)
}
}
}