Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

isAny() doesn't check the type #10

Closed
glureau opened this issue Mar 10, 2022 · 3 comments
Closed

isAny() doesn't check the type #10

glureau opened this issue Mar 10, 2022 · 3 comments

Comments

@glureau
Copy link

glureau commented Mar 10, 2022

interface A
interface B : A
class Foo {
  fun consume(a: A) { println(a) }
}
// in tests
@Test fun testMe() {
  val mockFoo = MockFoo()
  mockFoo.consume(object: A {})
  verify { mockFoo.consume(isAny<A>()) }
  // is equivalent to 
  verify { mockFoo.consume(isAny<B>()) }
  // and both will be pass even if the param is not an instance of B
}

I expect the check isAny<B>() to fail when an instance that doesn't implement B is used.

I think isAny() should check the type instead of returning Success all the time, something like

    inline fun <reified T> isAny(capture: MutableList<T>? = null): ArgConstraint<T> =
        ArgConstraint(capture, "isAny") { if (it is T) ArgConstraint.Result.Success else ArgConstraint.Result.Failure({ "..." }) }

(An alternative could be to provide a isInstanceOf<T>() next to isAny() if we don't want to change the current behavior.)

@glureau
Copy link
Author

glureau commented Mar 10, 2022

I used isValid {} as a workaround, it's not a blocker, but I was surprise by the current behavior.

@SalomonBrys
Copy link
Member

isAny always returns true because everything is Any in Kotlin.
I'll introduce isInstanceOf

@SalomonBrys
Copy link
Member

1.4.0 introduces the isInstanceOf constraint.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants