Skip to content

Commit

Permalink
Merge pull request #1104 from casplattel/expression_body
Browse files Browse the repository at this point in the history
chore: converting functions to expression bodies
  • Loading branch information
Raibaz committed Jun 28, 2023
2 parents 6a49ba7 + 538230f commit 47e4e39
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,55 +16,53 @@ abstract class RecordedBlockEvaluator(
scope: S,
mockBlock: (S.() -> T)?,
coMockBlock: (suspend S.() -> T)?
) {
try {
val callRecorderInstance = callRecorder()
) = try {
val callRecorderInstance = callRecorder()

val block: () -> T = when {
mockBlock != null -> {
{ scope.mockBlock() }
}
coMockBlock != null -> {
{ InternalPlatformDsl.runCoroutine { scope.coMockBlock() } }
}
else -> {
{ throw MockKException("You should specify either 'mockBlock' or 'coMockBlock'") }
}
val block: () -> T = when {
mockBlock != null -> {
{ scope.mockBlock() }
}
coMockBlock != null -> {
{ InternalPlatformDsl.runCoroutine { scope.coMockBlock() } }
}
else -> {
{ throw MockKException("You should specify either 'mockBlock' or 'coMockBlock'") }
}
}

val blockWithRethrow = enhanceWithRethrow(block, callRecorderInstance::isLastCallReturnsNothing)
val blockWithRethrow = enhanceWithRethrow(block, callRecorderInstance::isLastCallReturnsNothing)

val autoHinter = autoHinterFactory()
val autoHinter = autoHinterFactory()

try {
autoHinter.autoHint(
callRecorderInstance,
0,
64,
blockWithRethrow
)
} catch (npe: NothingThrownNullPointerException) {
// skip
}

val n = callRecorderInstance.estimateCallRounds()
for (i in 1 until n) {
try {
autoHinter.autoHint(
callRecorderInstance,
0,
64,
i,
n,
blockWithRethrow
)
} catch (npe: NothingThrownNullPointerException) {
// skip
}

val n = callRecorderInstance.estimateCallRounds()
for (i in 1 until n) {
try {
autoHinter.autoHint(
callRecorderInstance,
i,
n,
blockWithRethrow
)
} catch (npe: NothingThrownNullPointerException) {
// skip
}
}
callRecorderInstance.round(n, n)
callRecorderInstance.done()
} catch (ex: Throwable) {
throw InternalPlatform.prettifyRecordingException(ex)
}
callRecorderInstance.round(n, n)
callRecorderInstance.done()
} catch (ex: Throwable) {
throw InternalPlatform.prettifyRecordingException(ex)
}

private class NothingThrownNullPointerException : RuntimeException()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package io.mockk.impl.instantiation
import kotlin.reflect.KClass

open class AnyValueGenerator {
open fun anyValue(cls: KClass<*>, isNullable: Boolean, orInstantiateVia: () -> Any?): Any? {
return when (cls) {
open fun anyValue(cls: KClass<*>, isNullable: Boolean, orInstantiateVia: () -> Any?): Any? =
when (cls) {
Boolean::class -> false
Byte::class -> 0.toByte()
Short::class -> 0.toShort()
Expand Down Expand Up @@ -33,6 +33,5 @@ open class AnyValueGenerator {

else -> orInstantiateVia()
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ package io.mockk.impl.log
import io.mockk.impl.recording.CommonCallRecorder

class SafeToString(val callrecorderGetter: () -> CommonCallRecorder) {
operator fun invoke(logger: Logger): Logger {
return SafeLogger(logger, callrecorderGetter)
}
operator fun invoke(logger: Logger): Logger = SafeLogger(logger, callrecorderGetter)

fun <T> exec(block: () -> T): T = callrecorderGetter().safeExec(block)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,13 @@ class CommonIdentityHashMapOf<K, V> : MutableMap<K, V> {
override val size: Int
get() = map.size

override fun containsKey(key: K): Boolean {
return map.containsKey(ref(key))
}
override fun containsKey(key: K): Boolean = map.containsKey(ref(key))

override fun containsValue(value: V): Boolean {
return map.containsValue(value)
}
override fun containsValue(value: V): Boolean = map.containsValue(value)

override fun get(key: K): V? {
return map[ref(key)]
}
override fun get(key: K): V? = map[ref(key)]

override fun isEmpty(): Boolean {
return map.isEmpty()
}
override fun isEmpty(): Boolean = map.isEmpty()

override val entries: MutableSet<MutableMap.MutableEntry<K, V>>
get() = throw UnsupportedOperationException("entries")
Expand All @@ -38,17 +30,11 @@ class CommonIdentityHashMapOf<K, V> : MutableMap<K, V> {
map.clear()
}

override fun put(key: K, value: V): V? {
return map.put(ref(key), value)
}
override fun put(key: K, value: V): V? = map.put(ref(key), value)

override fun putAll(from: Map<out K, V>) {
throw UnsupportedOperationException("putAll")
}
override fun putAll(from: Map<out K, V>): Unit = throw UnsupportedOperationException("putAll")

override fun remove(key: K): V? {
return map.remove(ref(key))
}
override fun remove(key: K): V? = map.remove(ref(key))

private fun ref(key: K) = if (key == null) null else InternalPlatform.ref(key)
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@ class CommonRef(override val value: Any) : Ref {
return value === other.value
}

override fun hashCode(): Int {
return if (InternalPlatform.isPassedByValue(value::class)) {
override fun hashCode(): Int =
if (InternalPlatform.isPassedByValue(value::class)) {
value.hashCode()
} else {
InternalPlatformDsl.identityHashCode(value)
}
}

override fun toString(): String = "Ref(${value::class.simpleName}@${InternalPlatform.hkd(value)})"
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ class ChainedCallDetector(safeToString: SafeToString) {

log.trace { "Processing call #$callN: ${zeroCall.method.toStr()}" }

fun buildMatcher(isStart: Boolean, zeroCallValue: Any?, matcherBySignature: Matcher<*>?): Matcher<*> {
return if (matcherBySignature == null) {
fun buildMatcher(isStart: Boolean, zeroCallValue: Any?, matcherBySignature: Matcher<*>?): Matcher<*> =
if (matcherBySignature == null) {
if (allAny)
ConstantMatcher(true)
else {
Expand All @@ -41,7 +41,6 @@ class ChainedCallDetector(safeToString: SafeToString) {
matcherBySignature
}
}
}

fun regularArgument(nArgument: Int): Matcher<*> {
val signature = callInAllRounds.map {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,5 @@ import io.mockk.impl.recording.CommonCallRecorder
import io.mockk.impl.stub.Stub

class SafeLoggingState(recorder: CommonCallRecorder) : CallRecordingState(recorder) {
override fun call(invocation: Invocation): Any? {
return (invocation.stub as Stub).stdObjectAnswer(invocation)
}
}
override fun call(invocation: Invocation): Any? = (invocation.stub as Stub).stdObjectAnswer(invocation)
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ open class MockKStub(
}
}

protected open fun defaultAnswer(invocation: Invocation): Any? {
return stdObjectFunctions(invocation.self, invocation.method, invocation.args) {
protected open fun defaultAnswer(invocation: Invocation): Any? =
stdObjectFunctions(invocation.self, invocation.method, invocation.args) {
if (shouldRelax(invocation)) {
if (invocation.method.returnsUnit) return Unit
return gatewayAccess.anyValueGenerator().anyValue(
Expand All @@ -92,7 +92,6 @@ open class MockKStub(
" among the configured answers: ($configuredAnswers)")
}
}
}

private fun shouldRelax(invocation: Invocation) = when {
relaxed -> true
Expand Down

0 comments on commit 47e4e39

Please sign in to comment.