WrapRule to simplify test rule writing (since we use Kotlin)

This commit is contained in:
Vladimir Krivosheev
2015-10-06 08:08:51 +02:00
parent 24f607c6c8
commit ac121bf6a2
3 changed files with 41 additions and 29 deletions
@@ -11,6 +11,14 @@
<item name='org.junit.rules.TestRule org.junit.runners.model.Statement apply(org.junit.runners.model.Statement, org.junit.runner.Description)'>
<annotation name='org.jetbrains.annotations.NotNull'/>
</item>
<item
name='org.junit.rules.TestRule org.junit.runners.model.Statement apply(org.junit.runners.model.Statement, org.junit.runner.Description) 0'>
<annotation name='org.jetbrains.annotations.NotNull'/>
</item>
<item
name='org.junit.rules.TestRule org.junit.runners.model.Statement apply(org.junit.runners.model.Statement, org.junit.runner.Description) 1'>
<annotation name='org.jetbrains.annotations.NotNull'/>
</item>
<item name='org.junit.rules.TestWatcher void finished(org.junit.runner.Description) 0'>
<annotation name='org.jetbrains.annotations.NotNull'/>
</item>
@@ -8,15 +8,11 @@ import com.intellij.openapi.components.StoragePathMacros
import com.intellij.openapi.components.service
import com.intellij.openapi.components.stateStore
import com.intellij.openapi.project.ProjectManager
import com.intellij.testFramework.ProjectRule
import com.intellij.testFramework.RuleChain
import com.intellij.testFramework.TemporaryDirectory
import com.intellij.testFramework.deleteRecursively
import com.intellij.testFramework.*
import org.assertj.core.api.Assertions.assertThat
import org.junit.ClassRule
import org.junit.Rule
import org.junit.Test
import org.junit.rules.ExternalResource
import java.nio.file.Paths
internal class DefaultProjectStoreTest {
@@ -30,17 +26,11 @@ internal class DefaultProjectStoreTest {
private val ruleChain = RuleChain(
tempDirManager,
object : ExternalResource() {
private var isDoNotSave = false
override fun before() {
val app = ApplicationManagerEx.getApplicationEx()
isDoNotSave = app.isDoNotSave
app.doNotSave(false)
}
override fun after() {
val app = ApplicationManagerEx.getApplicationEx()
WrapRule {
val app = ApplicationManagerEx.getApplicationEx()
val isDoNotSave = app.isDoNotSave
app.doNotSave(false);
{
try {
app.doNotSave(isDoNotSave)
}
@@ -49,17 +39,11 @@ internal class DefaultProjectStoreTest {
}
}
},
object : ExternalResource() {
private var externalDependenciesManager: ExternalDependenciesManager? = null
override fun before() {
val defaultProject = ProjectManager.getInstance().defaultProject
externalDependenciesManager = defaultProject.service<ExternalDependenciesManager>()
externalDependenciesManager!!.allDependencies = requiredPlugins
}
override fun after() {
externalDependenciesManager?.allDependencies = emptyList()
WrapRule {
val externalDependenciesManager = ProjectManager.getInstance().defaultProject.service<ExternalDependenciesManager>()
externalDependenciesManager.allDependencies = requiredPlugins
{
externalDependenciesManager.allDependencies = emptyList()
}
}
)
@@ -245,7 +245,7 @@ class DisposeModulesRule(private val projectRule: ProjectRule) : ExternalResourc
try {
moduleManager.disposeModule(module)
}
catch(e: Throwable) {
catch (e: Throwable) {
if (errors == null) {
errors = SmartList()
}
@@ -256,4 +256,24 @@ class DisposeModulesRule(private val projectRule: ProjectRule) : ExternalResourc
CompoundRuntimeException.throwIfNotEmpty(errors)
}
}
}
}
/**
* Only and only if "before" logic in case of exception doesn't require "after" logic - must be no side effects if "before" finished abnormally.
* So, should be one task per rule.
*/
class WrapRule(private val before: () -> () -> Unit) : TestRule {
override final fun apply(base: Statement, description: Description): Statement {
return object : Statement() {
override fun evaluate() {
val after = before()
try {
base.evaluate()
}
finally {
after()
}
}
}
}
}