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

Feature Request: Add description to matchesPredicate assertion #514

Open
jdsee opened this issue Feb 11, 2024 · 7 comments
Open

Feature Request: Add description to matchesPredicate assertion #514

jdsee opened this issue Feb 11, 2024 · 7 comments
Labels

Comments

@jdsee
Copy link

jdsee commented Feb 11, 2024

Hi 👋

It would be great if the matchesPredicate assertion would allow a description parameter to be displayed on an assertion error instead of satisfy the predicate.

It could look something like this:

assertThat(x).matchesPredicate("some very special requirements") { it.isSpecial() }

which would then result in the error message:

expected X(special=false) to satisfy some very special requirements

I'm happy to submit a PR for this as soon as I find time for it.

@evant
Copy link
Collaborator

evant commented Feb 12, 2024

@evant evant added the question label Feb 12, 2024
@jdsee
Copy link
Author

jdsee commented Feb 12, 2024

Yes, the predicate matcher already exists. My request is to accept an optional description to improve the currently hardcoded error message.

Or am I still missing something? 😃

@evant
Copy link
Collaborator

evant commented Feb 13, 2024

Ah gotcha, I'm a little hesitant to add assertion-specific message overriding. There is some limited ability to override the message, for example you could set the name so that:

assertThat(12, name = "divisible by 5").matchesPredicate { it % 5 }

would give you

org.opentest4j.AssertionFailedError: divisible by 5
    expected 12 to satisfy the predicate

otherwise, I would really recommend using a custom assertion, where you can set the message exactly how you want

fun Assert<X>.isSpecial() = given { actual ->
    if (!it.isSpecial()) expected("${actual} to satisfy some very special requirements")
}

assertThat(x).isSpecial()

@saket
Copy link

saket commented Feb 16, 2024

I'm migrating my project to assertk and I have the same feature request. How do I replace my Truth.assertWithMessage() calls? I was using them as a way to describe my assertions. Here's an example:

https://github.com/saket/telephoto/blob/bdff7e6b09fb3a9785a3b54e0455e6fab6108539/zoomable-image/core/src/androidTest/kotlin/me/saket/telephoto/zoomable/ZoomableImageTest.kt#L610-L612

Using custom assertions to include messages feels a bit odd.

@saket
Copy link

saket commented Feb 16, 2024

Perhaps I can use Assert.all(message) as a stop-gap?

@evant
Copy link
Collaborator

evant commented Feb 16, 2024

yeah if something is going to be added it would be more general like assertWithMessage(), there's been a bit of discussion on adding to/customizing the error message but nothing's been settled on. (see #352)

@saket
Copy link

saket commented Feb 16, 2024

Subscribed to #352!

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

No branches or pull requests

3 participants