Skip to content

Conversation

HaydenMeloche
Copy link
Contributor

This adds some transformers allowing you to utilize Collection asserts on parts of a map.

Example for keys:

assertThat(mapOf("key" to "value")).havingKeys().containsOnly("key")

Example for values:

assertThat(mapOf("key" to "value")).havingValues().containsOnly("value")

Given that there is already a Map<K,V>.isNotEmpty() I thought it made sense to have these transformers throw an error if their corresponding parts have no values.

fixes #527

@JakeWharton
Copy link
Contributor

I thought it made sense to have these transformers throw an error if their corresponding parts have no values.

Do any other "having"-style functions also perform an implicit assertion?

Really my only criticism of this approach is that there's something pure in the "having" suite of functions being purely transformational in contrast to actual validation functions. There's nothing strictly wrong with doing assertThat(map).havingKeys().isEmpty(), after all, even if it's a bit redundant.

@JakeWharton
Copy link
Contributor

I did find some examples, such as key on a map (which should become havingKey if #522 moves forward). So retaining the implicit assertion here seems fine 👍 .

@HaydenMeloche
Copy link
Contributor Author

@JakeWharton I admittedly was on the fence here (and I'm definitely open to removing the assertion). Map<K,V>.key() was ultimately the reason I decided to leave in the assertion

@jzbrooks
Copy link
Collaborator

This seems analogous to #546 (function names in that PR notwithstanding).

Copy link

@emartynov emartynov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One comment and the rest is LGTM

@Test
fun havingValues_empty_list_fails() {
val error = assertFailsWith<AssertionError> {
assertThat(emptyMap<String, String>()).havingKeys()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it be havingValues()?

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

Successfully merging this pull request may close these issues.

Assert on map should have keys() and values() functions

4 participants