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

Add transformers Map<K,V>.havingKeys() and Map<K,V>.havingValues() #557

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- the receiver types for `isTrue()`, `isFalse()`, and `isInstanceOf()` have been widened to operate on nullable values.
- signature of `isIn` and `isNotIn` has been changed to ensure at least two items are supplied.
- added assertions `isIn(Iterable<T>)` and `isNotIn(Iterable<T>)`
- added transformers `Map<K,V>.havingKeys()` and `Map<K,V>.havingValues()`

## [0.28.1] 2024-04-17

Expand Down
30 changes: 30 additions & 0 deletions assertk/src/commonMain/kotlin/assertk/assertions/map.kt
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,33 @@ fun <K, V> Assert<Map<K, V>>.key(key: K): Assert<V> = transform(appendName(show(
expected("to have key:${show(key)}")
}
}

/**
* Returns an assert that has a collection of the keys in the map.
* ```
* assertThat(mapOf("key" to "value")).havingKeys().containsOnly("key")
* ```
* @see havingValues
*/
fun <K, V> Assert<Map<K, V>>.havingKeys(): Assert<Set<K>> = transform {
if (it.isEmpty()) {
expected("map to not be empty")
} else {
it.keys
}
}

/**
* Returns an assert that has a collection of the values in the map.
* ```
* assertThat(mapOf("key" to "value")).havingValues().containsOnly("value")
* ```
* @see havingKeys
*/
fun <K, V> Assert<Map<K, V>>.havingValues(): Assert<Collection<V>> = transform {
if (it.isEmpty()) {
expected("map to not be empty")
} else {
it.values
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -287,4 +287,34 @@ class MapTest {
assertEquals("expected [subject] to have key:<\"wrong\">", error.message)
}
//endregion

//region havingKeys
@Test
fun havingKeys_empty_list_fails() {
val error = assertFailsWith<AssertionError> {
assertThat(emptyMap<String, String>()).havingKeys()
}
assertEquals("expected map to not be empty", error.message)
}

@Test
fun havingKeys_assertion_passes() {
assertThat(mapOf("key" to "value")).havingKeys().containsOnly("key")
}
//endregion

//region havingValues
@Test
fun havingValues_empty_list_fails() {
val error = assertFailsWith<AssertionError> {
assertThat(emptyMap<String, String>()).havingKeys()
}
assertEquals("expected map to not be empty", error.message)
}

@Test
fun havingValues_assertion_passes() {
assertThat(mapOf("key" to "value")).havingValues().containsOnly("value")
}
//endregion
}