Skip to content

Commit

Permalink
Merge pull request #631 from Endhuine/feature/reorganize_test
Browse files Browse the repository at this point in the history
feature[test]: refactored following issue tests: 31, 36, 47, 48, 51, 70
  • Loading branch information
Raibaz committed May 28, 2021
2 parents bd0a0c8 + 45e3f89 commit c32e42c
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 114 deletions.
35 changes: 0 additions & 35 deletions mockk/common/src/test/kotlin/io/mockk/gh/Issue48Test.kt

This file was deleted.

31 changes: 0 additions & 31 deletions mockk/common/src/test/kotlin/io/mockk/gh/Issue70Test.kt

This file was deleted.

28 changes: 26 additions & 2 deletions mockk/common/src/test/kotlin/io/mockk/it/CoroutinesTest.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.mockk.it

import io.mockk.*
import io.mockk.impl.InternalPlatform
import kotlin.test.Test
import kotlin.test.assertEquals

Expand Down Expand Up @@ -51,8 +50,33 @@ class CoroutinesTest {
}
}

/**
* See issue #48
*/
@Test
fun mockPrivateCoroutineCall() {
val myClassSpy = spyk<MockPrivateSuspendCls>(recordPrivateCalls = true)

every { myClassSpy["myPrivateCall"](5) } returns "something"

myClassSpy.publicCall()

verify { myClassSpy["myPrivateCall"](5) }
}

class MockCls {
suspend fun coOtherOp(a: Int = 1, b: Int = 2): Int = a + b
suspend fun coLambdaOp(a: Int, b: suspend () -> Int) = a + b()
}
}

class MockPrivateSuspendCls {
fun publicCall() {
InternalPlatformDsl.runCoroutine {
myPrivateCall(5)
}
}

private suspend fun myPrivateCall(arg1: Int) {
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.mockk.gh
package io.mockk.it

import io.mockk.MockKAnnotations
import io.mockk.every
Expand All @@ -8,7 +8,11 @@ import io.mockk.verify
import kotlin.test.BeforeTest
import kotlin.test.Test

class Issue47Test {
/**
* See issue #47
*/
class InjectMocksTest {

interface IFoo

class Foo : IFoo {
Expand All @@ -17,7 +21,6 @@ class Issue47Test {
}

abstract class AbstractBar<T : IFoo> {
// @Inject
lateinit var foo: T
}

Expand Down Expand Up @@ -46,4 +49,4 @@ class Issue47Test {

verify { foo.method() }
}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package io.mockk.gh
package io.mockk.it

import io.mockk.every
import io.mockk.mockk
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith

class Issue36Test {
/**
* see issue #36
*/
class IntTest {
abstract class Cls1<out R> {
abstract fun op(): R
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
package io.mockk.gh
package io.mockk.it

import io.mockk.every
import io.mockk.mockkClass
import io.mockk.verify
import kotlin.test.Test
import kotlin.test.assertEquals

class Issue31Test {
class MockkClassTest {

class MockCls {
fun op(a: Int, b: Int) = a + b
}

val mock = mockkClass(MockCls::class)

/**
* See issue #31
*/
@Test
fun exactlyZeroWithAny() {
every { mock.op(3, 4) } returns 5
Expand All @@ -21,4 +25,4 @@ class Issue31Test {

verify { mock.op(3, 4) }
}
}
}
51 changes: 37 additions & 14 deletions mockk/common/src/test/kotlin/io/mockk/it/PrivateFunctionsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,19 @@ import kotlin.test.assertEquals

@Suppress("UNUSED_PARAMETER")
class PrivateFunctionsTest {
class Abc {
fun y() = x()

private fun x() = "abc"
}

object Def {
fun y() = x()

private fun x() = "abc"
}
/**
* See issue #70
*/
@Test
fun mockPrivateMethodWithGeneric() {
val mock = spyk<GenericsCls>()

class MockCls {
fun y(a: Int, b: Int?, d: Def?) = x(a, b, d)
every {
mock["updateItemInDb"](any<Long>(), any<String>(), any()) as Unit
} just Runs

private fun x(a: Int, b: Int?, d: Def?) = "abc $a $b"
mock.pubCall()
}

@Test
Expand Down Expand Up @@ -78,4 +75,30 @@ class PrivateFunctionsTest {
verify { mock["x"](any<Int>(), any<Int>(), any<Def>()) }
}

}
class Abc {
fun y() = x()

private fun x() = "abc"
}

object Def {
fun y() = x()

private fun x() = "abc"
}

class MockCls {
fun y(a: Int, b: Int?, d: Def?) = x(a, b, d)

private fun x(a: Int, b: Int?, d: Def?) = "abc $a $b"
}

class GenericsCls {
private fun <T> updateItemInDb(id: Long, column: String, data: T) {
}

fun pubCall() {
updateItemInDb(1L, "abc", "data")
}
}
}
Original file line number Diff line number Diff line change
@@ -1,30 +1,18 @@
package io.mockk.gh
package io.mockk.it

import io.mockk.*
import io.mockk.Runs
import io.mockk.every
import io.mockk.just
import io.mockk.spyk
import io.mockk.verify
import kotlin.test.Test
import kotlin.test.assertEquals

class Issue51Test {
data class Person(var name: String)

class Team {
protected var person: Person = Person("Init")
get() = Person("Ben")
set(value) {
field = value
}

protected fun fn(arg: Int): Int = arg + 5
fun pubFn(arg: Int) = fn(arg)

var memberName: String
get() = person.name
set(value) {
person = Person(value)
}

}
class PrivatePropertiesTest {

/**
* See issue #51
*/
@Test
fun testPrivateProperty() {
val mock = spyk(Team(), recordPrivateCalls = true)
Expand All @@ -44,4 +32,23 @@ class Issue51Test {
verify { mock invoke "fn" withArguments listOf(5) }
}

}
data class Person(var name: String)

class Team {
protected var person: Person = Person("Init")
get() = Person("Ben")
set(value) {
field = value
}

protected fun fn(arg: Int): Int = arg + 5
fun pubFn(arg: Int) = fn(arg)

var memberName: String
get() = person.name
set(value) {
person = Person(value)
}

}
}

0 comments on commit c32e42c

Please sign in to comment.