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

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

package ru.yandex.qatools.matchers.collection;

import ch.lambdaj.function.convert.StringConverter;
import org.hamcrest.Description;

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

import static java.lang.String.format;
import static java.util.Collections.frequency;

/**
 * Created with IntelliJ IDEA.
 * User: lanwen
 * Date: 27.05.13
 * Time: 0:30
 */
public final class MismatchHelper {
    // Util class
    private MismatchHelper() {
    }

    public static  StringConverter> asStringWithFind(final List> listToFind) {
        return new StringConverter>() {
            @Override
            public String convert(Wrapper nextExpected) {
                String elementFound = "list has only " + listToFind.size() + " items";
                if (nextExpected.getPosition() < listToFind.size()) {
                    elementFound = listToFind.get(nextExpected.getPosition()).toString();
                }
                return format("Expected %s on position [%d], but was - %s",
                        nextExpected, nextExpected.getPosition(), elementFound);
            }
        };
    }


    public static  StringConverter> asStringWithFrequency(
            final Collection> actualList,
            final Collection> expectedList) {

        return new StringConverter>() {
            @Override
            public String convert(Wrapper nextExpected) {
                return format("%s - expected [%d] times, but frequency was - [%d]",
                        nextExpected, frequency(expectedList, nextExpected), frequency(actualList, nextExpected));
            }
        };
    }


    @SuppressWarnings("unchecked")
    public static void appendMismatch(Description mismatchDescription, String comment, Collection collectionToPrint) {
        if (collectionToPrint.size() > 0) {
            mismatchDescription.appendText("\n(").appendText(comment)
                    .appendText(") [")
                    .appendValue(collectionToPrint.size())
                    .appendText("]:\n")
                    .appendValueList("-> ", "\n-> ", "", collectionToPrint);
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy