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

Make shouldNotBeNull return non-null instance #90

Closed
AndreasVolkmann opened this issue Jan 11, 2018 · 8 comments
Closed

Make shouldNotBeNull return non-null instance #90

AndreasVolkmann opened this issue Jan 11, 2018 · 8 comments

Comments

@AndreasVolkmann
Copy link
Collaborator

Sometimes you want to test whether a nullable instance is actually null.
Most often I want to run futher tests on this instance afterwards.

It would be nice if the shouldNotBeNull fun returned a non null type when the test passes (and throws if not).

Sample implementation:

fun <T : Any> T?.shouldNotBeNull(): T = this ?: throw AssertionError("Expected this to not be null")

This allows one to write the following code:

val user = userSource.findUser(details.email).shouldNotBeNull()
user.email.shouldNotBeBlank()
articleSource.insertArticle(user, it)

Without having to !! every user reference.

@jcornaz
Copy link
Contributor

jcornaz commented Jan 12, 2018

Totaly agree, I often encounter this use case too.

@MarkusAmshove
Copy link
Owner

If you want you can review the change before I release it :-)

@MarkusAmshove
Copy link
Owner

This might also be something worth it for almost all other assertions, to return the value on their happy path

@MarkusAmshove
Copy link
Owner

A good candidate would be

infix fun Any?.shouldBeInstanceOf(className: Class<*>) = assertTrue("Expected $this to be an instance of $className", className.isInstance(this))

to

inline fun <reified T: Any> Any.shouldBeInstanceOf() : T = if(this::class.isInstance(T::class)) this as T else throw AssertionError("Expected $this to be an instance of ${T::class.qualifiedName}")

which has to be a separate method to not break existing code.

This would allow:

open class Base {
    fun doSomething() = println("Base")
}

class Child : Base() {
    fun different() = println("Child")
}

fun main(args: Array<String>) {
    val base : Base = Child()
    // base.different() - Doesn't compile, not of type Child
    val child = base.shouldBeInstanceOf<Child>()
    child.different()
}```

@AndreasVolkmann
Copy link
Collaborator Author

Exactly, it should help wherever possible.

Why did you put this as in next release and needs migration to 2.0 ?

@MarkusAmshove
Copy link
Owner

in next release means that it'll the in the next version that is released (pushed/merged to master, waiting to be released).

Migration to 2.0 is just to keep track of changes that aren't in the 2.0 branch yet. They have to be migrated by hand, because the repository and project structure changes for 2.0.
It makes sure that 2.0 will be on par with the latest 1.x version feature wise ;-)

@MarkusAmshove
Copy link
Owner

I'm tracking the other stuff in #91 to collect most assertions which make sense.

I hope to get everything ready and released this weekend :)

@MarkusAmshove
Copy link
Owner

I've just released these changes with v1.34

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

No branches or pull requests

3 participants