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

How to do either-or assertions? #450

Open
hakanai opened this issue Apr 5, 2023 · 2 comments
Open

How to do either-or assertions? #450

hakanai opened this issue Apr 5, 2023 · 2 comments

Comments

@hakanai
Copy link

hakanai commented Apr 5, 2023

I have a value where it can either be null, or if non-null it must match a pattern.

So I'd like to be able to do something like

assertThat(value).any(
    { a -> a.isNull() },
    { a -> isNotNull().matches(Regex("""some pattern""")) }
)

The existing code I'm converting from Java and AssertJ is using anyOf(Condition...) to do this.

I guess matchesPredicate is possible as a last resort, but it removes all potential for reusing existing assertions. :(

@evant
Copy link
Collaborator

evant commented Apr 5, 2023

Could be a useful addition, but in the meantime I think you'd be best served with a custom assertion. More manual work but you could get a better message out of it.

@hakanai
Copy link
Author

hakanai commented Apr 7, 2023

If it were just the one place in the tests using it, I'd probably hack up a custom assertion for that place, but there are several occurrences all with different rules for what options they accept.

I wish I could figure out how to extract a message from the inner assertions. I haven't looked at how all {} is doing it yet, but any is the direct counterpart to all so the two should probably work similarly.

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

2 participants