Skip to content

Commit

Permalink
- custom matcher readme update
Browse files Browse the repository at this point in the history
 - objenesis update
 - coroutines update
  • Loading branch information
oleksiyp committed Apr 19, 2020
1 parent 4d5b765 commit 4e65812
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 77 deletions.
74 changes: 0 additions & 74 deletions COUNTLESS_EXAMPLES.md

This file was deleted.

67 changes: 67 additions & 0 deletions README.md
Expand Up @@ -959,6 +959,73 @@ Then you need to throw some exception as a behaviour:
every { quit(1) } throws Exception("this is a test")
```

### Custom matchers

Example of a custom matcher that compares list without order:

```kotlin

@Test
fun test() {
class MockCls {
fun op(a: List<Int>) = a.reversed()
}

val mock = mockk<MockCls>()

every { mock.op(any()) } returns listOf(5, 6, 9)

println(mock.op(listOf(1, 2, 3)))

verify { mock.op(matchListWithoutOrder(3, 2, 1)) }

}

data class ListWithoutOrderMatcher<T>(
val expectedList: List<T>,
val refEq: Boolean
) : Matcher<List<T>> {
val map = buildCountsMap(expectedList, refEq)

override fun match(arg: List<T>?): Boolean {
if (arg == null) return false
return buildCountsMap(arg, refEq) == map
}

private fun buildCountsMap(list: List<T>, ref: Boolean): Map<Any?, Int> {
val map = mutableMapOf<Any?, Int>()

for (item in list) {
val key = when {
item == null -> nullKey
refEq -> InternalPlatform.ref(item)
else -> item
}
map.compute(key, { _, value -> (value ?: 0) + 1 })
}

return map
}

override fun toString() = "matchListWithoutOrder($expectedList)"

@Suppress("UNCHECKED_CAST")
override fun substitute(map: Map<Any, Any>): Matcher<List<T>> {
return copy(expectedList = expectedList.map { map.getOrDefault(it as Any?, it) } as List<T>)
}

companion object {
val nullKey = Any()
}
}

inline fun <reified T : List<E>, E : Any> MockKMatcherScope.matchListWithoutOrder(
vararg items: E,
refEq: Boolean = true
): T = match(ListWithoutOrderMatcher(listOf(*items), refEq))

```

## Settings file

To adjust parameters globaly there is a posibility to specify few settings in a resource file.
Expand Down
5 changes: 2 additions & 3 deletions build.gradle
Expand Up @@ -4,10 +4,9 @@ buildscript {
ext.android_gradle_version = '3.6.3'
ext.android_build_tools_version = '28.0.3'
ext.byte_buddy_version = '1.10.9'
ext.coroutines_version = '1.0.0'
ext.coroutines_version = '1.3.3'
ext.dexmaker_version = '2.21.0'
ext.objenesis_version = '3.0.1'
ext.junit_version = '4.12'
ext.objenesis_version = '3.1'
ext.junit_jupiter_version = '5.6.2'
ext.junit_vintage_version = '5.6.2'
ext.dokka_version = '0.10.1'
Expand Down

0 comments on commit 4e65812

Please sign in to comment.