Question

I am attempting to use the Hamcrest matchers that come with the Flashbuilder 4.7 environment. I have 2 arrays, array A and array B. What I want to do is make sure that all of the members of B are found in A regardless of order. I'm looking for something that works kind of like this.

var a:Array = new Array( 1, 2, 3, 4);
var b:Array = new Array( 1, 2, 3, 4 );

//Both arrays contain the same values so this should
//return true
assertThat( a , hasEachAndEveryLastOneInsideOfIt(b));

Right now I've tried 'allOf' and 'hasItems' but I'm not quite able to get a grip on the syntax.

Was it helpful?

Solution 2

Here's a gist to handle this with a custom hamcrest matcher.

Usage : assertThat( a , arrayExact(b) );

The Matcher class :

https://gist.github.com/jamieowen/5480802

And the shorthand "arrayExact()" function access:

https://gist.github.com/jamieowen/5480819

It should also match 2d arrays as well.

OTHER TIPS

This should work:

assertThat(a, hasItems.apply(null, b))

Of course, this checks that all items in b are contained in a, meaning that a can contain other additional values. If you wanted to assert that a and b also have the same number of values, then assertEquals(a.length, b.length) is fine by me.

The trick here is to use Function.apply() because hasItems() does not expect an array but ...rest style arguments.

As a side note, allOf() is meant to create a list of matchers for the tested value.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top