forked from mockk/mockk
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mockk#510 | Fix problem with 2 matchers with the same type.
- Loading branch information
1 parent
54e29c1
commit d4f75a7
Showing
6 changed files
with
172 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
24 changes: 24 additions & 0 deletions
24
mockk/common/src/main/kotlin/io/mockk/impl/recording/SignatureMatchersList.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,24 @@ | ||
package io.mockk.impl.recording | ||
|
||
import io.mockk.Matcher | ||
|
||
class SignatureMatchersList { | ||
private var matchers = mutableListOf<SignatureWithMatcher>() | ||
|
||
fun add(signature: List<Any>, matcher: Matcher<*>) { | ||
matchers.add(SignatureWithMatcher(signature, matcher)) | ||
} | ||
|
||
fun remove(signature: List<Any?>): Matcher<*>? { | ||
val index = matchers.indexOfFirst { it.signature == signature } | ||
return if (index > -1) matchers.removeAt(index).matcher else null | ||
} | ||
|
||
fun isNotEmpty() = matchers.isNotEmpty() | ||
|
||
override fun toString(): String { | ||
return matchers.map { it.matcher }.toString() | ||
} | ||
|
||
private data class SignatureWithMatcher(val signature: List<Any>, val matcher: Matcher<*>) | ||
} |
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,41 @@ | ||
package io.mockk.gh | ||
|
||
import io.mockk.every | ||
import io.mockk.mockk | ||
import io.mockk.verify | ||
import kotlin.test.Test | ||
|
||
class Issue510Test { | ||
|
||
class ShopService { | ||
|
||
fun buyProducts(products: List<Product>) { | ||
println("You bought...") | ||
} | ||
|
||
fun addProductAndOrders(products: List<Product>, orders: List<Order>) { | ||
println("Add and orders...") | ||
} | ||
} | ||
|
||
data class Product(val name: String, val price: Int) | ||
data class Order(val name: String) | ||
|
||
@Test | ||
fun `should match with two arguments of type list`() { | ||
// given | ||
val shopService = mockk<ShopService>() | ||
val products = listOf(Product("raspberry", 2), Product("banana", 1)) | ||
val orders = listOf(Order("raspberry"), Order("banana")) | ||
|
||
every { | ||
shopService.addProductAndOrders(products = any(), orders = any()) | ||
} returns Unit | ||
|
||
// when | ||
shopService.addProductAndOrders(products, orders) | ||
|
||
// then | ||
verify { shopService.addProductAndOrders(products, orders) } | ||
} | ||
} |
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
94 changes: 94 additions & 0 deletions
94
mockk/common/src/test/kotlin/io/mockk/impl/recording/SignatureMatchersListTest.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,94 @@ | ||
package io.mockk.impl.recording | ||
|
||
import io.mockk.AllAnyMatcher | ||
import io.mockk.mockk | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertFalse | ||
import kotlin.test.assertNull | ||
import kotlin.test.assertTrue | ||
|
||
class SignatureMatchersListTest { | ||
|
||
@Test | ||
fun shouldAddMatcherForSignature() { | ||
// given | ||
val map = SignatureMatchersList() | ||
val signedMatcher = mockk<SignedMatcher>(relaxed = true) | ||
val signature = listOf(signedMatcher.signature) | ||
val matcher = signedMatcher.matcher | ||
|
||
// when | ||
map.add(signature, matcher) | ||
map.add(signature, matcher) | ||
|
||
// then | ||
assertTrue { map.isNotEmpty() } | ||
} | ||
|
||
@Test | ||
fun shouldNotContainAnyMatchers() { | ||
// given | ||
val map = SignatureMatchersList() | ||
|
||
// when && then | ||
assertFalse { map.isNotEmpty() } | ||
} | ||
|
||
@Test | ||
fun shouldNotRemoveFromEmptyList() { | ||
// given | ||
val map = SignatureMatchersList() | ||
val signedMatcher = mockk<SignedMatcher>(relaxed = true) | ||
val signature = listOf(signedMatcher.signature) | ||
|
||
// when | ||
val matcher = map.remove(signature) | ||
|
||
// then | ||
assertFalse { map.isNotEmpty() } | ||
assertNull(matcher) | ||
} | ||
|
||
@Test | ||
fun shouldRemoveFromList() { | ||
// given | ||
val map = SignatureMatchersList() | ||
val signedMatcher1 = mockk<SignedMatcher>(relaxed = true) | ||
val signedMatcher2 = mockk<SignedMatcher>(relaxed = true) | ||
val signature1 = listOf(signedMatcher1.signature) | ||
val signature2 = listOf(signedMatcher2.signature) | ||
val matcher1 = signedMatcher1.matcher | ||
val matcher2 = signedMatcher2.matcher | ||
|
||
// when | ||
map.add(signature1, matcher1) | ||
map.add(signature2, matcher2) | ||
|
||
// and | ||
val matcher = map.remove(signature1) | ||
|
||
// then | ||
assertTrue { map.isNotEmpty() } | ||
assertEquals(matcher, matcher1) | ||
} | ||
|
||
@Test | ||
fun shouldPrintListCorrectly() { | ||
// given | ||
val list = SignatureMatchersList() | ||
val signedMatcher = mockk<SignedMatcher>(relaxed = true) | ||
val signature = listOf(signedMatcher.signature) | ||
val matcher = AllAnyMatcher<Any>() | ||
val expectedMatchersListAsString = "[$matcher]" | ||
|
||
// when | ||
list.add(signature, matcher) | ||
|
||
// and | ||
val matchersAsString = list.toString() | ||
|
||
// then | ||
assertEquals(matchersAsString, expectedMatchersListAsString) | ||
} | ||
} |