Skip to content

Commit

Permalink
Removes restriction to type Any on expect
Browse files Browse the repository at this point in the history
  • Loading branch information
Yves Bonjour committed Mar 13, 2018
1 parent 7a08c07 commit 5827e9f
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/main/kotlin/ch/yvu/rxpect/RXpect.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ object RXpect {
defaultValueGenerator("Please provide a return value as follows expectSubscribe(mock.foo()).thenReturn(returnValue)"))
.build()

inline fun <reified T : Any> expect(methodCall: T): DefaultExpectation<T> =
inline fun <reified T> expect(methodCall: T): DefaultExpectation<T> =
DefaultExpectationBuilderImpl(
methodCall,
defaultValueGenerator("Please provide a return value as follows expect(mock.foo()).thenReturn(returnValue)"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package ch.yvu.rxpect.expect
import ch.yvu.rxpect.setupExpectation
import com.nhaarman.mockitokotlin2.whenever

class DefaultExpectationBuilderImpl<T : Any>(
class DefaultExpectationBuilderImpl<T>(
private val methodCall: T,
private val defaultAnswer: () -> T
) : DefaultExpectationBuilder<T> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package ch.yvu.rxpect.mockito

inline fun <reified T : Any> defaultValueGenerator(messageIfNoDefaultValue: String): () -> T =
inline fun <reified T> defaultValueGenerator(messageIfNoDefaultValue: String): () -> T =
{
defaultValue()
?: throw IllegalStateException(messageIfNoDefaultValue)
}

inline fun <reified T : Any> defaultValue(): T? =
inline fun <reified T> defaultValue(): T? =
when (T::class) {
Unit::class -> {
Unit as T
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ class DefaultExpectationBuilderTest {
expectation.verify()
}

@Test
fun fulfilledExpectationWithReturnValueWithoutDefaultVAlue() {
val mock: TestClass = mock()
val foo: Foo = mock()
val expectation = expect(mock.foo2()).thenReturn(foo)

mock.foo2()

expectation.verify()
}

@Test(expected = WantedButNotInvoked::class)
fun unfulfilledExpectation() {
val mock: TestClass = mock()
Expand All @@ -37,5 +48,8 @@ class DefaultExpectationBuilderTest {

interface TestClass {
fun foo(value: String): Int
fun foo2(): Foo
}

interface Foo
}

0 comments on commit 5827e9f

Please sign in to comment.