Sometimes I wish there was some way to use matcher...
# kotest
e
Sometimes I wish there was some way to use matchers as predicates out-of-the-box. For instance, today I found myself doing
Copy code
myListOfJsonStuff.filter {
  runCatching {
    it shouldEqualSpecifiedJson """ { "type": "FOO" } """
  }.isSuccess
}
Anyone else done something similar? Or maybe figured some way to do this neatly with existing functionality?
s
Maybe we add a filter version of inspectors?
e
Yeah was thinking of suggesting something like
filterMatching(t: (T) -> Unit)
, or perhaps
filterMatching(matcher: Matcher<T>)
s
The former allows you to run multiple and matches how inspectors look
👍 1
e
Will add some test too before marking as ready.. trivial to implement given the inspector utilities 🙂 https://github.com/kotest/kotest/pull/4658
k
This MR uses the anti-pattern of exceptions as flow control. It's not usually a problem for a unit test, but it can slow down some unit tests due to the time it takes to construct an exception with all the stack trace information that's then thrown away. Therefore, would it be a good idea to also have
filterMatching(matcher: Matcher<T>)
which doesn't involve exceptions? On the other hand, users can already easily use the stdlib's
filter
for that.