Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
isMockKMock + return set of mocks #221
- Loading branch information
Showing
16 changed files
with
295 additions
and
31 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
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
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
10 changes: 10 additions & 0 deletions
10
mockk/common/src/main/kotlin/io/mockk/impl/stub/MockType.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,10 @@ | ||
package io.mockk.impl.stub | ||
|
||
enum class MockType { | ||
REGULAR, | ||
SPY, | ||
OBJECT, | ||
STATIC, | ||
CONSTRUCTOR, | ||
TEMPORARY | ||
} |
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
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,39 @@ | ||
package io.mockk.gh | ||
|
||
import io.mockk.every | ||
import io.mockk.isMockKMock | ||
import io.mockk.mockk | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertTrue | ||
|
||
class Issue221Test { | ||
interface Foo { | ||
fun getTasks(): Set<Task> | ||
|
||
fun getTask(): Task | ||
} | ||
|
||
interface Task { | ||
fun getSubTask(): Task | ||
|
||
fun doIt(): Int | ||
} | ||
|
||
|
||
@Test | ||
fun returnsSetOfMocks() { | ||
val foo = mockk<Foo>() | ||
every { foo.getTasks() } returns setOf(mockk(), mockk()) | ||
|
||
val tasks = foo.getTasks() | ||
|
||
assertEquals(2, tasks.size) | ||
val task1 = tasks.first() | ||
val task2 = tasks.drop(1).first() | ||
|
||
assertTrue(isMockKMock(task1)) | ||
assertTrue(isMockKMock(task2)) | ||
} | ||
|
||
} |
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,68 @@ | ||
package io.mockk.it | ||
|
||
import io.mockk.* | ||
import kotlin.test.Test | ||
import kotlin.test.assertFalse | ||
import kotlin.test.assertTrue | ||
|
||
class MockTypesTest { | ||
enum class MockType { | ||
REGULAR, | ||
SPY, | ||
OBJECT, | ||
STATIC, | ||
CONSTRUCTOR | ||
} | ||
|
||
class TestCls | ||
|
||
@Test | ||
fun regularMock() { | ||
assertOnlyOfType(mockk(), MockType.REGULAR) | ||
} | ||
|
||
@Test | ||
fun spyMock() { | ||
assertOnlyOfType(spyk(), MockType.SPY) | ||
} | ||
|
||
@Test | ||
fun objectMock() { | ||
val test = Any() | ||
mockkObject(test) { | ||
assertOnlyOfType(test, MockType.OBJECT) | ||
} | ||
} | ||
|
||
@Test | ||
fun staticMock() { | ||
mockkStatic(TestCls::class) { | ||
assertOnlyOfType(TestCls::class, MockType.STATIC) | ||
} | ||
} | ||
|
||
@Test | ||
fun constructorMock() { | ||
mockkConstructor(TestCls::class) { | ||
assertOnlyOfType(TestCls::class, MockType.CONSTRUCTOR) | ||
} | ||
} | ||
|
||
fun assertOnlyOfType(mock: Any, singleType: MockType) { | ||
for (type in MockType.values()) { | ||
if (singleType == type) { | ||
assertTrue(isOfMockType(mock, type), "mock is not of type $singleType") | ||
} else { | ||
assertFalse(isOfMockType(mock, type), "mock should be of type $singleType, but it is as well $type") | ||
} | ||
} | ||
} | ||
|
||
fun isOfMockType(mock: Any, type: MockType) = when (type) { | ||
MockType.REGULAR -> isMockKMock(mock) | ||
MockType.SPY -> isMockKMock(mock, regular = false, spy = true) | ||
MockType.OBJECT -> isMockKMock(mock, regular = false, objectMock = true) | ||
MockType.STATIC -> isMockKMock(mock, regular = false, staticMock = true) | ||
MockType.CONSTRUCTOR -> isMockKMock(mock, regular = false, constructorMock = true) | ||
} | ||
} |
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
Oops, something went wrong.