mercredi 28 janvier 2015

hamcrest TypeSafeMatcher fails matching of double array

I wrote this matcher to check the contents of a double[]:



@SuppressWarnings({ "rawtypes", "unchecked" })
public static Matcher<double[]> isArrayCloseTo(double[] expected) {
final double DELTA = 1e-10;
List<Matcher<Double>> matchers = new ArrayList<>();
for (double d : expected)
matchers.add(new IsCloseTo(d, DELTA));
return new IsArray(matchers.toArray(new Matcher[matchers.size()]));
}


I suppress those warnings because there's nothing I can do about an array not having a generic type. The method looks fine, but it always fails:



assertThat(new double[] { .1 }, isArrayCloseTo(new double[] { .1 })); //fails


The problem is in TypesafeMatcher, line 65: expectedType.isInstance(item), where expectedType is Object.class and item is [0.1].


I suspect that this problem has to do with the fact that I can't genericize the Matchers array I pass to IsArray, but I don't know how to fix this. Can anyone tell me how I should be matching an array of doubles?


Aucun commentaire:

Enregistrer un commentaire