Add matcher for length of a sequence

Bug #900567 reported by Andrew Bennetts
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
testtools
Triaged
Wishlist
Unassigned

Bug Description

bzrlib has a nice assertLength helper which helpfully shows the contents of the sequence if the length is not the expected value. But it's not a matcher, so for more complex cases tests resort to ad hoc assertions that look like:

    if len(self.hpss_calls) < 34 and len(self.hpss_calls) > 47:
        self.fail("Expected hpss_calls to have between 34 and 47 elements, got %d (%r)"
            % (len(self.hpss_calls), self.hpss_calls))

It would be nice if there were a matcher for this, so that could be written like:

    self.assertThat(
        self.hpss_calls, Length(MatchesAny(LessThan(34), GreaterThan(47))))

(And so get the nice formatted errors etc for free.)

Tags: matchers
Revision history for this message
Jonathan Lange (jml) wrote : Re: [Bug 900567] [NEW] Add matcher for length of a sequence

On Tue, Dec 6, 2011 at 2:11 AM, Andrew Bennetts
<email address hidden> wrote:
> Public bug reported:
>
> bzrlib has a nice assertLength helper which helpfully shows the contents
> of the sequence if the length is not the expected value.  But it's not a
> matcher, so for more complex cases tests resort to ad hoc assertions
> that look like:
>
>    if len(self.hpss_calls) < 34 and len(self.hpss_calls) > 47:
>        self.fail("Expected hpss_calls to have between 34 and 47 elements, got %d (%r)"
>            % (len(self.hpss_calls), self.hpss_calls))
>
> It would be nice if there were a matcher for this, so that could be
> written like:
>
>    self.assertThat(
>        self.hpss_calls, Length(MatchesAny(LessThan(34), GreaterThan(47))))
>
> (And so get the nice formatted errors etc for free.)

Cheapest way to do this:

  Length = lambda matcher: AfterPreprocessing(len, matcher)

jml

Jonathan Lange (jml)
Changed in testtools:
status: New → Triaged
importance: Undecided → Wishlist
Jonathan Lange (jml)
tags: added: matchers
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.