-
Notifications
You must be signed in to change notification settings - Fork 89
Add collection filtering assertions analagous to filterIsInstance #546
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
base: main
Are you sure you want to change the base?
Changes from all commits
0deb0f8
3afef39
9e0cb3a
e3f15e9
0f43f7e
167e5f7
d2e7108
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -131,6 +131,34 @@ internal fun MutableList<*>.removeFirst(value: Any?) { | |
if (index > -1) removeAt(index) | ||
} | ||
|
||
/** | ||
* Asserts the collection contains at least one instance of a given type. | ||
* | ||
* ``` | ||
* assertThat(listOf<Any>("one", "two", 1)).havingInstanceOf<String>().each { | ||
* it.hasLength(3) | ||
* } | ||
* ``` | ||
*/ | ||
inline fun <reified T> Assert<Iterable<*>>.havingInstancesOf(): Assert<List<T>> { | ||
return transform("contains instance of ${T::class}") { actual -> | ||
actual.filterIsInstance<T>().also { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The sub-list allocation is necessary to provide a way to continue making assertions against the sublist. No? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, yeah, I missed the new list was propagated as a return value. I'm a bit hesitant about the API shape using a verb "contains" while the return value is a full filtering. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, agreed. I noticed the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess I'm a bit torn on whether this makes sense as a dedicated operator since it combines a transform with an assertion in a way that I don't think has precedent from other operators. We'll see what the actual maintainers think, though. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd argue there's precedent with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But it's atomic in nature. You cannot break it down into discrete steps. Whereas this one is very clearly a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fair. I'd find a shorthand for the transformation basically just as helpful. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A transformation without assertion might set a (good) precedent for other collection operators. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
if (it.isEmpty()) expected("to contain at least one instance of ${T::class} but was $actual") | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Asserts the collection does not contain an instance of a given type. | ||
* | ||
* ``` | ||
* assertThat(listOf<Any>("one", "two", 1)).doesNotContainInstanceOf<Double>() | ||
* ``` | ||
*/ | ||
inline fun <reified T> Assert<Iterable<*>>.notHavingInstancesOf() = given { actual -> | ||
if (actual.any { it is T }) expected("to not contain instances of ${T::class} but was $actual") | ||
} | ||
|
||
/** | ||
* Asserts on each item in the iterable. The given lambda will be run for each item. | ||
* | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I moved this note into the added section for consistency