r

    rocketraman

    1 year ago
    Just checking out Strikt. My previous preferred library was hamkrest. My first assertion is to validate the count of elements in a list. I see
    exactly
    ,
    atMost
    , and
    atLeast
    , but all of these take an additional predicate. Is the preferred approach to do this?
    expectThat(it).get { count() }.isEqualTo(3)
    Is there any particular reason that there isn't a count matcher directly on list?
    robfletcher

    robfletcher

    1 year ago
    expectThat(it).hasSize(3)
    should work for you
    r

    rocketraman

    1 year ago
    Its actually an
    Iterable
    , not a
    List
    . No
    hasSize
    matcher seems to be available.
    robfletcher

    robfletcher

    1 year ago
    Okay, that’s because
    Iterable
    doesn’t define a
    size
    property. That’s added on
    Collection
    . It wouldn’t make sense for all iterables (they can be unbounded, for example). For your case
    expectThat(it.toList()).hasSize(3)
    is probably the way to go.
    I mean, Strikt could add a
    hasSize
    for
    Assertion.Builder<Iterable<*>>
    but like I say it would not necessarily even make sense for every iterable
    r

    rocketraman

    1 year ago
    The Kotlin stdlib does have a
    count
    function on
    Iterable
    . Normally unbounded iterables are represented as a
    Sequence
    .
    robfletcher

    robfletcher

    1 year ago
    hmm, I could easily move
    hasSize
    up to work on Iterable I guess
    r

    rocketraman

    1 year ago
    I think that makes sense, based on the extensions available in stdlib. Want me to create a GH issue?
    robfletcher

    robfletcher

    1 year ago
    but it might make more sense to add a
    count()
    function so you could do
    expectThat(it).count().isEqualTo(3)
    to make it clear what the underlying API mapping is
    r

    rocketraman

    1 year ago
    Makes sense to me
    Out of curiosity, why does
    hasSize
    exist? Couldn't it work the same way?
    Oh because List has a sizeproperty
    robfletcher

    robfletcher

    1 year ago
    yeah, well…
    Collection
    does
    which is what
    count()
    delegates to if called on a
    Collection
    otherwise it iterates over all the elements
    r

    rocketraman

    1 year ago
    Yup
    robfletcher

    robfletcher

    1 year ago
    I think the Strikt function would just be
    fun Builder<out Iterable<*>>.count(): Builder<Int> =
      get(Iterable<*>::count)
    r

    rocketraman

    1 year ago
    I can confirm that worked. I was going to submit a PR but I see you beat me to it with a release of 0.28.1. 🙌