用于比较器的 Junit Matcher?

2022-09-04 02:52:00

几天来,我一直在使用Junit的匹配器功能。一切正常,但我正在寻找一个匹配器,它使用比较器进行比较,并且不依赖于对象等于方法。

我想更换

Assert.assertThat(one, CoreMatchers.equalTo(two)

使用类似的东西(伪代码)

Assert.assertThat(eins, CoreMatchers.equalTo(operand, new MyComparator())

您知道是否存在简单的开箱即用解决方案?我没有在谷歌中找到一个,也不想写一个。


答案 1

Hamcrest 2.0.0.0+ 现在支持此功能。

您可以使用 org.hamcrest.comparator.Comparator.ComparatorMatcherBuilder 类来实现此目的,例如:

ComparatorMatcherBuilder builder = ComparatorMatcherBuilder.comparedBy(equivalenceComparator);
Assert.assertThat(eins, builder.comparesEqualTo(operand));

答案 2

我在hamcrest 1.3中遇到了同样的问题,并通过编写一个匹配器来解决它,该匹配器遵循IsEqual-Matcher的代码,但使用给定的比较器而不是Object#equals()。

import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.hamcrest.Factory;
import org.hamcrest.Matcher;
import org.junit.Assert;

import java.lang.reflect.Array;
import java.util.Comparator;

/**
 * Is the value equal to another value, as tested by the
 * given Comparator?<br/>
 * Based on the example of {@link org.hamcrest.core.IsEqual}.
 *
 * @author Serhat Cinar
 */
public class IsEqualWithComparator<T> extends BaseMatcher<T> {
    private final Object expectedValue;
    private final Comparator<T> comparator;

    public IsEqualWithComparator(T equalArg, Comparator<T> comparator) {
        expectedValue = equalArg;
        this.comparator = comparator;
    }

    @Override
    public boolean matches(Object actualValue) {
        return areEqual(actualValue, expectedValue, comparator);
    }

    @Override
    public void describeTo(Description description) {
        description.appendValue(expectedValue);
    }

    private static boolean areEqual(Object actual, Object expected, Comparator comparator) {
        if (actual == null) {
            return expected == null;
        }

        if (expected != null && isArray(actual)) {
            return isArray(expected) && areArraysEqual(actual, expected, comparator);
        }

        return comparator.compare(actual, expected) == 0;
    }

    private static boolean areArraysEqual(Object actualArray, Object expectedArray, Comparator comparator) {
        return areArrayLengthsEqual(actualArray, expectedArray) && areArrayElementsEqual(actualArray, expectedArray, comparator);
    }

    private static boolean areArrayLengthsEqual(Object actualArray, Object expectedArray) {
        return Array.getLength(actualArray) == Array.getLength(expectedArray);
    }

    private static boolean areArrayElementsEqual(Object actualArray, Object expectedArray, Comparator comparator) {
        for (int i = 0; i < Array.getLength(actualArray); i++) {
            if (!areEqual(Array.get(actualArray, i), Array.get(expectedArray, i), comparator)) {
                return false;
            }
        }
        return true;
    }

    private static boolean isArray(Object o) {
        return o.getClass().isArray();
    }

    @Factory
    public static <T> Matcher<T> equalTo(T operand, Comparator<T> comparator) {
        return new IsEqualWithComparator<>(operand, comparator);
    }

    public static void main(String argv[]) {
        Assert.assertThat("abc", IsEqualWithComparator.equalTo("ABC", new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o1.equalsIgnoreCase(o2) ? 0 : -1;
            }
        }));
    }
}

推荐