Skip to content

Commit

Permalink
Wire Contraints into ArgumentMatchers
Browse files Browse the repository at this point in the history
  • Loading branch information
bitPogo committed Feb 27, 2022
1 parent 547fb7f commit ee3b487
Show file tree
Hide file tree
Showing 12 changed files with 574 additions and 75 deletions.
7 changes: 7 additions & 0 deletions kmock/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ kotlin {
).contains(sourceSet.name)
}

all {
languageSettings.apply {
optIn("kotlin.ExperimentalUnsignedTypes")
optIn("kotlin.RequiresOptIn")
}
}

val commonMain by getting {
dependencies {
implementation(Dependency.multiplatform.kotlin.common)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ interface KMockContract {
}

fun interface MatcherConstraint {
fun matches(value: Any?): Boolean
fun matches(actual: Any?): Boolean
}

data class Reference(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,92 @@

package tech.antibytes.kmock.verification

import tech.antibytes.kmock.KMockContract
import tech.antibytes.kmock.KMockContract.GetOrSet
import tech.antibytes.kmock.verification.contraints.eq

internal fun Array<out Any?>?.hasBeenCalledWithVoid(): Boolean = this == null

private fun wrapValue(value: Any?): KMockContract.MatcherConstraint {
return if (value is KMockContract.MatcherConstraint) {
value
} else {
eq(value)
}
}

internal fun Array<out Any?>?.hasBeenCalledWith(vararg values: Any?): Boolean {
return when {
this == null -> values.isEmpty()
values.isEmpty() -> true
else -> values.all { value -> this.contains(value) }
else -> {
var lastMatch = 0

for (value in values) {
var matched = false
val expected = wrapValue(value)

for (idx in lastMatch until this.size) {
val actual = this[idx]

if (expected.matches(actual)) {
matched = true
lastMatch = idx
break
}
}

if (!matched) {
return false
}
}

return true
}
}
}

internal fun Array<out Any?>?.hasBeenStrictlyCalledWith(vararg values: Any?): Boolean {
return this?.contentDeepEquals(values) ?: values.isEmpty()
return when {
this == null && values.isEmpty() -> true
this == null -> false
values.size != this.size -> false
else -> {

for (idx in values.indices) {
val expected = wrapValue(values[idx])

if (!expected.matches(this[idx])) {
return false
}
}

return true
}
}
}

internal fun Array<out Any?>?.hasBeenCalledWithout(vararg values: Any?): Boolean {
return if (this == null) {
values.isNotEmpty()
} else {
values.none { value -> this.contains(value) }
for (value in values) {
var matched = false
val expected = wrapValue(value)

for (actual in this) {
if (expected.matches(actual)) {
matched = true
break
}
}

if (matched) {
return false
}
}

return true
}
}

Expand All @@ -37,6 +102,9 @@ internal fun GetOrSet.wasSet(): Boolean = this is GetOrSet.Set
internal fun GetOrSet.wasSetTo(value: Any?): Boolean {
return when (this) {
!is GetOrSet.Set -> false
else -> this.value == value
else -> {
val expected = wrapValue(value)
return expected.matches(this.value)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import kotlin.reflect.KClass
class any(
private val expected: KClass<*>? = null
) : KMockContract.MatcherConstraint {
override fun matches(value: Any?): Boolean {
override fun matches(actual: Any?): Boolean {
return when {
expected == null -> true
value == null -> false
else -> value::class == expected
actual == null -> false
else -> actual::class == expected
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,29 @@
package tech.antibytes.kmock.verification.contraints

import tech.antibytes.kmock.KMockContract
import kotlin.reflect.KClass

@OptIn(ExperimentalUnsignedTypes::class)
class eq(
private val expected: Any?
) : KMockContract.MatcherConstraint {
override fun matches(value: Any?): Boolean = expected == value
override fun matches(actual: Any?): Boolean {
return when {
expected is Array<*> && actual is Array<*> -> expected.contentDeepEquals(actual)
expected is ByteArray && actual is ByteArray -> expected.contentEquals(actual)
expected is ShortArray && actual is ShortArray -> expected.contentEquals(actual)
expected is IntArray && actual is IntArray -> expected.contentEquals(actual)
expected is LongArray && actual is LongArray -> expected.contentEquals(actual)
expected is FloatArray && actual is FloatArray -> expected.contentEquals(actual)
expected is DoubleArray && actual is DoubleArray -> expected.contentEquals(actual)
expected is CharArray && actual is CharArray -> expected.contentEquals(actual)
expected is BooleanArray && actual is BooleanArray -> expected.contentEquals(actual)

expected is UByteArray && actual is UByteArray -> expected.contentEquals(actual)
expected is UShortArray && actual is UShortArray -> expected.contentEquals(actual)
expected is UIntArray && actual is UIntArray -> expected.contentEquals(actual)
expected is ULongArray && actual is ULongArray -> expected.contentEquals(actual)

else -> expected == actual
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@
package tech.antibytes.kmock.verification.contraints

import tech.antibytes.kmock.KMockContract
import kotlin.reflect.KClass

class isNot(
private val illegal: Any? = null
) : KMockContract.MatcherConstraint {
override fun matches(value: Any?): Boolean = illegal != value
override fun matches(actual: Any?): Boolean = illegal != actual
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ import tech.antibytes.kmock.KMockContract
class isNotSame(
private val illegal: Any?
) : KMockContract.MatcherConstraint {
override fun matches(value: Any?): Boolean = illegal !== value
override fun matches(actual: Any?): Boolean = illegal !== actual
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ import tech.antibytes.kmock.KMockContract
class isSame(
private val expected: Any?
) : KMockContract.MatcherConstraint {
override fun matches(value: Any?): Boolean = expected === value
override fun matches(actual: Any?): Boolean = expected === actual
}
Loading

0 comments on commit ee3b487

Please sign in to comment.