dart-lang / test

A library for writing unit tests in Dart.
https://pub.dev/packages/test
495 stars 213 forks source link

Create containsAllIn matcher #2369

Closed alanrussian closed 6 years ago

alanrussian commented 6 years ago

One of the matchers I use often in Java's truth library is "containsAllIn". I've found myself needing something like this in Dart a few times now.

A comparison of matchers in Truth vs. this package (credit @natebosch):

containsExactlyElementsIn(...) -> unorderedEquals(...) containsExactlyElementsIn(...).inOrder() -> orderedEquals(...) containsAll(Iterable matchers) => allOf(matchers.map(contains).toList()) containsAllOf(...).inOrder() -> containsAllInOrder(...) containsAllIn -> Does not exist

natebosch commented 6 years ago

On thing that makes this tricky is that to match the semantics of other Iterable matchers in this package we'd want to support some looseness which means that a given element in the checked value could match more than one element of the expectation.

expect([2,1], containsAll([greaterThan(0), greaterThan(1)]);

My first greedy approach to solve this didn't work since the 2 matched the greaterThan(0) and failed to find a match for greaterThan(1). We might need to treat it like a bipartite matching problem.

natebosch commented 6 years ago

looks like unorderedMatches suffers the same problem https://github.com/dart-lang/test/issues/2370

alanrussian commented 6 years ago

Thanks for the quick fix, Nate! :-)