Skip to content

Commit

Permalink
Tests for features we never knew we had
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddenton committed Aug 23, 2023
1 parent f6762e8 commit 359cbaf
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 12 deletions.
30 changes: 21 additions & 9 deletions mock4k/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,20 @@ Super-simple and super-fast Mocking library. Use when you really don't want to i
- ✅ Simple
- ✅ Fast
- ✅ Relaxed/Strict modes
- ✅ Coroutines
- ✅ Functions
- ❌ Verification
- ❌ Argument capture
-Partials
-Partial mocking
- ❌ Spies
- ❌ Any evil Powermock nonsense

### Supplementary features:
- ❌ Wannabe-functional
- ❌ Multiplatform
- ❌ Spring-support
-Coroutines
-Multiplatform
- ❌ Annotations
- ❌ Blockchain
- ❌ AI-powered

## Installation

Expand All @@ -44,7 +46,8 @@ interface Wallet {

class AppleStore(private val wallet: Wallet) {
fun buyMacBook() = wallet.pay("MacBook", 9999)
fun seeGenius() {}
suspend fun seeGenius() = "hello!"
fun buyMacbookUsing(pay: (String, Int) -> Int?) = pay("MacBook", 9999)
}

class AppleStoreTest {
Expand All @@ -67,12 +70,21 @@ class AppleStoreTest {
val appleStore = AppleStore(mock(MockMode.Relaxed))
assertThat(appleStore.buyMacBook(), equalTo(null))
}

@Test
fun `see genius`() {
// when there are no calls expected
val appleStore = AppleStore(mock()).seeGenius()
assertThat(appleStore.buyMacBook(), equalTo(Unit))
val appleStore = AppleStore(mock(Relaxed))

runBlocking {
// suspend calls
assertThat(appleStore.seeGenius(), equalTo("hello!"))
}
}

@Test
fun `bus using someone else's money`() {
val appleStore = AppleStore(mock())
appleStore.buyMacbookUsing(mock())
}
}
```
Expand Down
4 changes: 4 additions & 0 deletions mock4k/build.gradle
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
description = 'ForkHandles: The very cheapest mocking framework platform.'

dependencies {
testImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.3")
}
41 changes: 38 additions & 3 deletions mock4k/src/test/kotlin/dev/forkhandles/mock4k/MockTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.natpryce.hamkrest.assertion.assertThat
import com.natpryce.hamkrest.equalTo
import dev.forkhandles.mock4k.MockMode.Relaxed
import dev.forkhandles.mock4k.MockMode.Strict
import kotlinx.coroutines.runBlocking
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.fail

Expand All @@ -14,9 +15,12 @@ interface Wallet {

class AppleStore(private val wallet: Wallet) {
fun buyMacBook() = wallet.pay("MacBook", 9999)
fun seeGenius(): String? = if (true) "sorted" else null
suspend fun seeGenius(): String = "sorted"
fun buyMacbookUsing(fn: (String, Int) -> Int?) = fn("MacBook", 9999)
suspend fun buyMacbookLater() = wallet.pay("MacBook", 9999)
}


class MockTest {
@Test
fun `mock call is strict by default`() {
Expand Down Expand Up @@ -48,14 +52,45 @@ class MockTest {
assertThat(appleStore.buyMacBook(), absent())
}

@Test
fun `suspend mock call supported`() {
val appleStore = AppleStore(mock())

runBlocking {
assertThat(appleStore.seeGenius(), equalTo("sorted"))
}
}

@Test
fun `function mock supported`() {
try {
AppleStore(mock()).buyMacbookUsing(mock())
fail("didn't throw")
} catch (e: UnstubbedCall) {
assertThat(e.message, equalTo("Unstubbed call: Function2.invoke(MacBook, 9999)"))
}
}

@Test
fun `fails on unexpected call`() {
try {
val appleStore = AppleStore(mock())
appleStore.buyMacBook()
AppleStore(mock<Wallet>()).buyMacBook()
fail("didn't throw")
} catch (e: UnstubbedCall) {
assertThat(e.message, equalTo("Unstubbed call: Wallet.pay(MacBook, 9999)"))
}
}

@Test
fun `suspend mock call failure`() {

runBlocking {
try {
AppleStore(mock<Wallet>()).buyMacbookLater()
fail("didn't throw")
} catch (e: UnstubbedCall) {
assertThat(e.message, equalTo("Unstubbed call: Wallet.pay(MacBook, 9999)"))
}
}
}
}

0 comments on commit 359cbaf

Please sign in to comment.