All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ru.yandex.qatools.matchers.collection.HasSameItemsAsListMatcher Maven / Gradle / Ivy

There is a newer version: 1.4.1
Show newest version
package ru.yandex.qatools.matchers.collection;

import ch.lambdaj.collection.LambdaList;
import org.hamcrest.*;

import java.util.Collection;
import java.util.List;

import static ch.lambdaj.collection.LambdaCollections.with;
import static org.hamcrest.Matchers.*;
import static ru.yandex.qatools.matchers.collection.HasSameItemsAsCollectionMatcher.hasSameItemsAsCollection;
import static ru.yandex.qatools.matchers.collection.MismatchHelper.appendMismatch;
import static ru.yandex.qatools.matchers.collection.WrapperConverter.wrap;

/**
 * Created with IntelliJ IDEA.
 * User: lanwen
 * Date: 25.05.13
 * Time: 21:35
 */
public class HasSameItemsAsListMatcher extends TypeSafeDiagnosingMatcher> {
    private List expected;

    @SuppressWarnings("unchecked")
    private WrapperFactory wrapperFactory = (WrapperFactory) ObjectMethodsWrapper.standardMethods();

    private boolean sortcheck = false;

    public HasSameItemsAsListMatcher(List expect) {
        this.expected = expect;
    }

    public HasSameItemsAsListMatcher useWrapperFactory(WrapperFactory wrapperFactory) {
        if (wrapperFactory == null) {
            throw new IllegalArgumentException("Wrapper factory can't be null");
        }
        this.wrapperFactory = wrapperFactory;
        return this;
    }

    public HasSameItemsAsListMatcher sameSorted() {
        sortcheck = true;
        return this;
    }


    @Override
    protected boolean matchesSafely(List actual, Description mismatchDescription) {
        LambdaList> wrappedActual = with(actual).convert(wrap(actual, wrapperFactory));
        LambdaList> wrappedExpected = with(expected).convert(wrap(expected, wrapperFactory));

        LambdaList> inActualButNotInExpected = wrappedActual.clone().remove(isIn(wrappedExpected));
        LambdaList> inExpectedButNotInActual = wrappedExpected.clone().remove(isIn(wrappedActual));

        appendMismatch(mismatchDescription, "In Actual but not in Expected", inActualButNotInExpected);
        appendMismatch(mismatchDescription, "In Expected but not in Actual", inExpectedButNotInActual);

        Matcher>> collectionMatcher = hasSameItemsAsCollection(
                wrappedExpected.remove(not(isIn(wrappedActual)))
        );
        collectionMatcher.describeMismatch(wrappedActual.remove(not(isIn(wrappedExpected))), mismatchDescription);

        int notsorted = 0;
        if (sortcheck) {
            LambdaList> notCorrectlySorted = wrappedExpected.clone()
                    .remove(sortedEqual(wrappedActual));
            appendMismatch(mismatchDescription, "Not sorted correctly",
                    notCorrectlySorted.convert(MismatchHelper.asStringWithFind(wrappedActual)).distinct());
            notsorted = notCorrectlySorted.size();
        }

        return inActualButNotInExpected.size()
                + inExpectedButNotInActual.size()
                + notsorted == 0 && collectionMatcher.matches(wrappedActual);
    }


    @Override
    public void describeTo(Description description) {
        description.appendText("lists contains same items").appendText(sortcheck ? " and sorted equally" : "");
    }

    private Matcher> sortedEqual(final List> expected) {
        return new FeatureMatcher, Boolean>(is(true), "", "") {
            @Override
            protected Boolean featureValueOf(Wrapper actual) {
                //Can be replaced with condition as ~ expected.prev == item.prev && expected.next == item.next
                return expected.indexOf(actual) == actual.getPosition();
            }
        };
    }


    @Factory
    public static  HasSameItemsAsListMatcher hasSameItemsAsList(List correctList) {
        return new HasSameItemsAsListMatcher<>(correctList);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy