-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Sergej Shafarenka
authored
Apr 22, 2020
1 parent
f8cd90f
commit a9c1e4f
Showing
6 changed files
with
108 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
knot3/src/test/kotlin/de/halfbit/knot3/utils/RxPluginsException.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package de.halfbit.knot3.utils | ||
|
||
import io.reactivex.rxjava3.plugins.RxJavaPlugins | ||
import org.hamcrest.core.IsEqual | ||
import org.junit.Assert | ||
import org.junit.rules.TestRule | ||
import org.junit.runner.Description | ||
import org.junit.runners.model.Statement | ||
import kotlin.reflect.KClass | ||
|
||
class RxPluginsException private constructor() : TestRule { | ||
|
||
private var expectedType: KClass<*>? = null | ||
private var expectedInstance: Throwable? = null | ||
|
||
fun expect(type: KClass<*>) { | ||
expectedType = type | ||
} | ||
|
||
fun expect(instance: Throwable) { | ||
expectedInstance = instance | ||
} | ||
|
||
override fun apply(base: Statement, description: Description): Statement { | ||
return ExpectedExceptionStatement(base) | ||
} | ||
|
||
private inner class ExpectedExceptionStatement( | ||
private val base: Statement | ||
) : Statement() { | ||
override fun evaluate() { | ||
var observedException: Throwable? = null | ||
val backup = RxJavaPlugins.getErrorHandler() | ||
RxJavaPlugins.setErrorHandler { | ||
observedException = it | ||
} | ||
try { | ||
base.evaluate() | ||
} finally { | ||
RxJavaPlugins.setErrorHandler(backup) | ||
} | ||
when { | ||
expectedInstance != null -> { | ||
Assert.assertThat(observedException, IsEqual(expectedInstance)) | ||
} | ||
expectedType != null -> { | ||
val observedType = observedException?.let { it::class } | ||
Assert.assertThat(observedType, IsEqual(expectedType)) | ||
} | ||
} | ||
} | ||
} | ||
|
||
companion object { | ||
fun none() = RxPluginsException() | ||
} | ||
} |