DefaultAsserter object. Add function to replace constructor invoke.

This commit is contained in:
Pavel Punegov
2018-12-17 19:26:06 +03:00
committed by Pavel Punegov
parent ac0f612781
commit 1c4ecd287d
4 changed files with 11 additions and 13 deletions
@@ -8,12 +8,17 @@ package kotlin.test
/** /**
* Default [Asserter] implementation to avoid dependency on JUnit or TestNG. * Default [Asserter] implementation to avoid dependency on JUnit or TestNG.
*/ */
// TODO: make object in 1.2 object DefaultAsserter : Asserter {
class DefaultAsserter : Asserter {
override fun fail(message: String?): Nothing { override fun fail(message: String?): Nothing {
if (message == null) if (message == null)
throw AssertionError() throw AssertionError()
else else
throw AssertionError(message) throw AssertionError(message)
} }
} }
@Deprecated("DefaultAsserter is an object now, constructor call is not required anymore",
ReplaceWith("DefaultAsserter", "kotlin.test.DefaultAsserter"))
@kotlin.js.JsName("DefaultAsserterConstructor")
@Suppress("FunctionName")
fun DefaultAsserter(): DefaultAsserter = DefaultAsserter
@@ -9,9 +9,4 @@ internal fun messagePrefix(message: String?) = if (message == null) "" else "$me
internal expect fun lookupAsserter(): Asserter internal expect fun lookupAsserter(): Asserter
@PublishedApi // required to get stable name as it's called from box tests @PublishedApi // required to get stable name as it's called from box tests
internal fun overrideAsserter(value: Asserter?): Asserter? { internal fun overrideAsserter(value: Asserter?): Asserter? = _asserter.also { _asserter = value }
// TODO: Replace with return _asserter.also { _asserter = value } after KT-17540 is fixed
val previous = _asserter
_asserter = value
return previous
}
@@ -199,7 +199,7 @@ private fun checkFailedAssertion(assertion: () -> Unit) {
@Suppress("INVISIBLE_MEMBER") @Suppress("INVISIBLE_MEMBER")
private fun withDefaultAsserter(block: () -> Unit) { private fun withDefaultAsserter(block: () -> Unit) {
val current = overrideAsserter(DefaultAsserter()) val current = overrideAsserter(DefaultAsserter)
try { try {
block() block()
} finally { } finally {
@@ -9,12 +9,10 @@ import java.util.ServiceLoader
private val contributors = ServiceLoader.load(AsserterContributor::class.java).toList() private val contributors = ServiceLoader.load(AsserterContributor::class.java).toList()
private val defaultAsserter = DefaultAsserter()
internal actual fun lookupAsserter(): Asserter { internal actual fun lookupAsserter(): Asserter {
for (contributor in contributors) { for (contributor in contributors) {
val asserter = contributor.contribute() val asserter = contributor.contribute()
if (asserter != null) return asserter if (asserter != null) return asserter
} }
return defaultAsserter return DefaultAsserter
} }