Skip to content

Commit

Permalink
Merge pull request #186 from JetBrains/caching-mode-scope
Browse files Browse the repository at this point in the history
Introduce CachingMode.SCOPE
  • Loading branch information
raniejade authored Mar 9, 2017
2 parents e670147 + 9c5c054 commit 8da1a90
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,16 @@ import org.jetbrains.spek.meta.Experimental
@Experimental
enum class CachingMode {
/**
* Instance will be shared within the group it was declared.
* Each group will get their own unique instance. Nested groups will have
* their own unique instance as well.
*/
GROUP,

/**
* Instance will be shared within the group it was declared.
*/
SCOPE,

/**
* Each test will get their own unique instance.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,12 @@ class SpekTestEngine: HierarchicalTestEngine<SpekExecutionContext>() {
val fixtures: FixturesAdapter): Spec {

override fun <T> memoized(mode: CachingMode, factory: () -> T): LifecycleAware<T> {
return LifecycleAwareAdapter(mode, factory).apply {
val adapter = when (mode) {
CachingMode.GROUP -> LifecycleAwareAdapter.GroupCachingModeAdapter(factory)
CachingMode.TEST -> LifecycleAwareAdapter.TestCachingModeAdapter(factory)
CachingMode.SCOPE -> LifecycleAwareAdapter.ScopeCachingModeAdapter(root, factory)
}
return adapter.apply {
registerListener(this)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.jetbrains.spek.engine.lifecycle

import org.jetbrains.spek.api.lifecycle.ActionScope
import org.jetbrains.spek.api.lifecycle.CachingMode
import org.jetbrains.spek.api.lifecycle.GroupScope
import org.jetbrains.spek.api.lifecycle.LifecycleAware
import org.jetbrains.spek.api.lifecycle.LifecycleListener
Expand All @@ -11,7 +10,8 @@ import kotlin.reflect.KProperty
/**
* @author Ranie Jade Ramiso
*/
class LifecycleAwareAdapter<T>(val mode: CachingMode, val factory: () -> T): LifecycleAware<T>, LifecycleListener {
sealed class LifecycleAwareAdapter<T>(val factory: () -> T)
: LifecycleAware<T>, LifecycleListener {
var cached: T? = null

override fun getValue(thisRef: Any?, property: KProperty<*>) = invoke()
Expand All @@ -23,21 +23,38 @@ class LifecycleAwareAdapter<T>(val mode: CachingMode, val factory: () -> T): Lif
return cached!!
}

override fun afterExecuteTest(test: TestScope) {
if (test.parent !is ActionScope) {
if (mode == CachingMode.TEST) {
cached = null
class GroupCachingModeAdapter<T>(factory: () -> T): LifecycleAwareAdapter<T>(factory) {
val stack = mutableListOf<T?>()

override fun beforeExecuteGroup(group: GroupScope) {
stack.add(0, cached)
cached = null
}

override fun afterExecuteGroup(group: GroupScope) {
if (stack.isNotEmpty()) {
cached = stack.removeAt(0)
}
}
}

override fun afterExecuteGroup(group: GroupScope) {
if (mode == CachingMode.GROUP) {
cached = null
class ScopeCachingModeAdapter<T>(val group: GroupScope, factory: () -> T): LifecycleAwareAdapter<T>(factory) {
override fun afterExecuteGroup(group: GroupScope) {
if (this.group == group) {
cached = null
}
}
}

override fun afterExecuteAction(action: ActionScope) {
cached = null
class TestCachingModeAdapter<T>(factory: () -> T): LifecycleAwareAdapter<T>(factory) {
override fun afterExecuteTest(test: TestScope) {
if (test.parent !is ActionScope) {
cached = null
}
}

override fun afterExecuteAction(action: ActionScope) {
cached = null
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import org.junit.jupiter.api.Test
*/
class MemoizedTest: AbstractSpekTestEngineTest() {
@Test
fun testMemoizedTestCaching() {
fun memoizedTestCaching() {
class MemoizedSpec: Spek({
val foo = memoized {
listOf(1)
Expand Down Expand Up @@ -41,7 +41,7 @@ class MemoizedTest: AbstractSpekTestEngineTest() {
}

@Test
fun testMemoizedGroupCaching() {
fun memoizedGroupCaching() {
class MemoizedSpec: Spek({
val foo = memoized(CachingMode.GROUP) {
listOf(1)
Expand All @@ -50,12 +50,118 @@ class MemoizedTest: AbstractSpekTestEngineTest() {
var memoized1: List<Int>? = null
var memoized2: List<Int>? = null

test("first pass") {
memoized1 = foo()
group("group") {
test("first pass") {
memoized1 = foo()
}
}

test("second pass") {
memoized2 = foo()
group("another group") {
test("second pass") {
memoized2 = foo()
}

}


test("check") {
assertThat(memoized1, !sameInstance(memoized2))
}
})

val recorder = executeTestsForClass(MemoizedSpec::class)

assertThat(recorder.testFailureCount, equalTo(0))
}

@Test
fun memoizedNestedGroupCaching() {
class MemoizedSpec: Spek({
val foo = memoized(CachingMode.GROUP) {
listOf(1)
}

var memoized1: List<Int>? = null
var memoized2: List<Int>? = null

group("group") {
test("first pass") {
memoized1 = foo()
}

group("another group") {
test("second pass") {
memoized2 = foo()
}

}
}

test("check") {
assertThat(memoized1, !sameInstance(memoized2))
}
})

val recorder = executeTestsForClass(MemoizedSpec::class)

assertThat(recorder.testFailureCount, equalTo(0))
}

@Test
fun memoizedScopeCaching() {
class MemoizedSpec: Spek({
val foo = memoized(CachingMode.SCOPE) {
listOf(1)
}

var memoized1: List<Int>? = null
var memoized2: List<Int>? = null

group("group") {
test("first pass") {
memoized1 = foo()
}
}

group("another group") {
test("second pass") {
memoized2 = foo()
}

}


test("check") {
assertThat(memoized1, sameInstance(memoized2))
}
})

val recorder = executeTestsForClass(MemoizedSpec::class)

assertThat(recorder.testFailureCount, equalTo(0))
}

@Test
fun memoizedScopeCachingWithNestedGroups() {
class MemoizedSpec: Spek({
val foo = memoized(CachingMode.SCOPE) {
listOf(1)
}

var memoized1: List<Int>? = null
var memoized2: List<Int>? = null

group("group") {
test("first pass") {
memoized1 = foo()
}

group("another group") {
test("second pass") {
memoized2 = foo()
}

}
}

test("check") {
Expand All @@ -68,8 +174,9 @@ class MemoizedTest: AbstractSpekTestEngineTest() {
assertThat(recorder.testFailureCount, equalTo(0))
}


@Test
fun testMemoizedActionCaching() {
fun memoizedActionCaching() {
class MemoizedSpec: Spek({
val foo = memoized {
listOf(1)
Expand Down

0 comments on commit 8da1a90

Please sign in to comment.