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

com.github.alkedr.matchers.reporting.ReportingMatchers Maven / Gradle / Ivy

package com.github.alkedr.matchers.reporting;

import com.github.alkedr.matchers.reporting.sub.value.checkers.SubValuesChecker;
import com.github.alkedr.matchers.reporting.sub.value.extractors.SubValuesExtractor;
import com.github.alkedr.matchers.reporting.sub.value.extractors.SubValuesExtractors;
import org.hamcrest.Matcher;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.*;
import java.util.function.Supplier;

import static com.github.alkedr.matchers.reporting.sub.value.checkers.SubValueCheckers.compositeSubValuesCheckerSupplier;
import static com.github.alkedr.matchers.reporting.sub.value.checkers.SubValueCheckers.matcherSubValuesChecker;
import static com.github.alkedr.matchers.reporting.sub.value.checkers.SubValueCheckers.noOpSubValuesChecker;
import static com.github.alkedr.matchers.reporting.sub.value.extractors.SubValuesExtractors.*;
import static java.util.Arrays.asList;
import static org.hamcrest.CoreMatchers.equalTo;

// используй static import
public enum ReportingMatchers {
    ;

    // TODO: unchecked() ?
    public static  ReportingMatcher noOp() {
        //noinspection unchecked
        return (ReportingMatcher) NoOpMatcher.INSTANCE;
    }


    // оборачивает переданный ему матчер если он не reporting.
    public static  ReportingMatcher toReportingMatcher(Matcher matcher) {
        return matcher instanceof ReportingMatcher ? (ReportingMatcher) matcher : new ReportingMatcherAdapter<>(matcher);
    }

    public static  Iterable> toReportingMatchers(Iterable> matchers) {
        // TODO: конвертировать на лету?
        Collection> result = new ArrayList<>();
        for (Matcher matcher : matchers) {
            result.add(toReportingMatcher(matcher));
        }
        return result;
    }


    @SafeVarargs
    public static  ReportingMatcher sequence(ReportingMatcher... matchers) {
        return sequence(asList(matchers));
    }

    public static  ReportingMatcher sequence(Iterable> matchers) {
        return new SequenceMatcher<>(matchers);
    }


    @SafeVarargs
    public static  ReportingMatcher merge(ReportingMatcher... matchers) {
        return merge(asList(matchers));
    }

    public static  ReportingMatcher merge(Iterable> matchers) {
        return new MergingMatcher<>(sequence(matchers));
    }


    public static  ReportingMatcher present() {
        //noinspection unchecked
        return (ReportingMatcher) PresentMatcher.INSTANCE;
    }

    public static  ReportingMatcher absent() {
        //noinspection unchecked
        return (ReportingMatcher) AbsentMatcher.INSTANCE;
    }



    public static  ReportingMatcher value(SubValuesExtractor subValuesExtractor, S value) {
        return value(subValuesExtractor, equalTo(value));
    }

    public static  ReportingMatcher value(SubValuesExtractor subValuesExtractor, Matcher matcher) {
        return value(subValuesExtractor, toReportingMatcher(matcher));
    }

    public static  ReportingMatcher value(SubValuesExtractor subValuesExtractor, ReportingMatcher reportingMatcher) {
        return subValuesMatcher(subValuesExtractor, matcherSubValuesChecker(reportingMatcher));
    }

    @SafeVarargs   // TODO: требовать наличия хотя бы одного матчера!
    public static  ReportingMatcher value(SubValuesExtractor subValuesExtractor, Matcher... matchers) {
        return value(subValuesExtractor, asList(matchers));
    }

    public static  ReportingMatcher value(SubValuesExtractor subValuesExtractor, Iterable> matchers) {
        return value(subValuesExtractor, merge(toReportingMatchers(matchers)));
    }


    public static  ReportingMatcher value(String nameForReport, SubValuesExtractor subValuesExtractor, S value) {
        return value(renamed(subValuesExtractor, nameForReport), value);
    }

    public static  ReportingMatcher value(String nameForReport, SubValuesExtractor subValuesExtractor, Matcher matcher) {
        return value(renamed(subValuesExtractor, nameForReport), matcher);
    }

    public static  ReportingMatcher value(String nameForReport, SubValuesExtractor subValuesExtractor, ReportingMatcher reportingMatcher) {
        return value(renamed(subValuesExtractor, nameForReport), reportingMatcher);
    }

    @SafeVarargs
    public static  ReportingMatcher value(String nameForReport, SubValuesExtractor subValuesExtractor, Matcher... matchers) {
        return value(renamed(subValuesExtractor, nameForReport), matchers);
    }

    public static  ReportingMatcher value(String nameForReport, SubValuesExtractor subValuesExtractor, Iterable> matchers) {
        return value(renamed(subValuesExtractor, nameForReport), matchers);
    }



    public static  ReportingMatcher field(Field field, S value) {
        return value(SubValuesExtractors.field(field), value);
    }

    public static  ReportingMatcher field(Field field, Matcher matcher) {
        return value(SubValuesExtractors.field(field), matcher);
    }

    public static  ReportingMatcher field(Field field, ReportingMatcher reportingMatcher) {
        return value(SubValuesExtractors.field(field), reportingMatcher);
    }

    @SafeVarargs
    public static  ReportingMatcher field(Field field, Matcher... matchers) {
        return value(SubValuesExtractors.field(field), matchers);
    }

    public static  ReportingMatcher field(Field field, Iterable> matchers) {
        return value(SubValuesExtractors.field(field), matchers);
    }


    public static  ReportingMatcher field(String fieldName, S value) {
        return value(SubValuesExtractors.fieldByName(fieldName), value);
    }

    public static  ReportingMatcher field(String fieldName, Matcher matcher) {
        return value(SubValuesExtractors.fieldByName(fieldName), matcher);
    }

    public static  ReportingMatcher field(String fieldName, ReportingMatcher reportingMatcher) {
        return value(SubValuesExtractors.fieldByName(fieldName), reportingMatcher);
    }

    @SafeVarargs
    public static  ReportingMatcher field(String fieldName, Matcher... matchers) {
        return value(SubValuesExtractors.fieldByName(fieldName), matchers);
    }

    public static  ReportingMatcher field(String fieldName, Iterable> matchers) {
        return value(SubValuesExtractors.fieldByName(fieldName), matchers);
    }


    public static  ReportingMatcher field(String nameForReport, Field field, S value) {
        return value(nameForReport, SubValuesExtractors.field(field), value);
    }

    public static  ReportingMatcher field(String nameForReport, Field field, Matcher matcher) {
        return value(nameForReport, SubValuesExtractors.field(field), matcher);
    }

    public static  ReportingMatcher field(String nameForReport, Field field, ReportingMatcher reportingMatcher) {
        return value(nameForReport, SubValuesExtractors.field(field), reportingMatcher);
    }

    @SafeVarargs
    public static  ReportingMatcher field(String nameForReport, Field field, Matcher... matchers) {
        return value(nameForReport, SubValuesExtractors.field(field), matchers);
    }

    public static  ReportingMatcher field(String nameForReport, Field field, Iterable> matchers) {
        return value(nameForReport, SubValuesExtractors.field(field), matchers);
    }


    public static  ReportingMatcher field(String nameForReport, String fieldName, S value) {
        return value(nameForReport, SubValuesExtractors.fieldByName(fieldName), value);
    }

    public static  ReportingMatcher field(String nameForReport, String fieldName, Matcher matcher) {
        return value(nameForReport, SubValuesExtractors.fieldByName(fieldName), matcher);
    }

    public static  ReportingMatcher field(String nameForReport, String fieldName, ReportingMatcher reportingMatcher) {
        return value(nameForReport, SubValuesExtractors.fieldByName(fieldName), reportingMatcher);
    }

    @SafeVarargs
    public static  ReportingMatcher field(String nameForReport, String fieldName, Matcher... matchers) {
        return value(nameForReport, SubValuesExtractors.fieldByName(fieldName), matchers);
    }

    public static  ReportingMatcher field(String nameForReport, String fieldName, Iterable> matchers) {
        return value(nameForReport, SubValuesExtractors.fieldByName(fieldName), matchers);
    }



    public static  ReportingMatcher method(MethodInvocation invocation, S value) {
        return value(SubValuesExtractors.method(invocation.method, invocation.arguments), value);
    }

    public static  ReportingMatcher method(MethodInvocation invocation, Matcher matcher) {
        return value(SubValuesExtractors.method(invocation.method, invocation.arguments), matcher);
    }

    public static  ReportingMatcher method(MethodInvocation invocation, ReportingMatcher reportingMatcher) {
        return value(SubValuesExtractors.method(invocation.method, invocation.arguments), reportingMatcher);
    }

    @SafeVarargs
    public static  ReportingMatcher method(MethodInvocation invocation, Matcher... matchers) {
        return value(SubValuesExtractors.method(invocation.method, invocation.arguments), matchers);
    }

    public static  ReportingMatcher method(MethodInvocation invocation, Iterable> matchers) {
        return value(SubValuesExtractors.method(invocation.method, invocation.arguments), matchers);
    }


    public static  ReportingMatcher method(MethodByNameInvocation invocation, S value) {
        return value(SubValuesExtractors.methodByName(invocation.methodName, invocation.arguments), value);
    }

    public static  ReportingMatcher method(MethodByNameInvocation invocation, Matcher matcher) {
        return value(SubValuesExtractors.methodByName(invocation.methodName, invocation.arguments), matcher);
    }

    public static  ReportingMatcher method(MethodByNameInvocation invocation, ReportingMatcher reportingMatcher) {
        return value(SubValuesExtractors.methodByName(invocation.methodName, invocation.arguments), reportingMatcher);
    }

    @SafeVarargs
    public static  ReportingMatcher method(MethodByNameInvocation invocation, Matcher... matchers) {
        return value(SubValuesExtractors.methodByName(invocation.methodName, invocation.arguments), matchers);
    }

    public static  ReportingMatcher method(MethodByNameInvocation invocation, Iterable> matchers) {
        return value(SubValuesExtractors.methodByName(invocation.methodName, invocation.arguments), matchers);
    }


    public static  ReportingMatcher method(String nameForReport, MethodInvocation invocation, S value) {
        return value(nameForReport, SubValuesExtractors.method(invocation.method, invocation.arguments), value);
    }

    public static  ReportingMatcher method(String nameForReport, MethodInvocation invocation, Matcher matcher) {
        return value(nameForReport, SubValuesExtractors.method(invocation.method, invocation.arguments), matcher);
    }

    public static  ReportingMatcher method(String nameForReport, MethodInvocation invocation, ReportingMatcher reportingMatcher) {
        return value(nameForReport, SubValuesExtractors.method(invocation.method, invocation.arguments), reportingMatcher);
    }

    @SafeVarargs
    public static  ReportingMatcher method(String nameForReport, MethodInvocation invocation, Matcher... matchers) {
        return value(nameForReport, SubValuesExtractors.method(invocation.method, invocation.arguments), matchers);
    }

    public static  ReportingMatcher method(String nameForReport, MethodInvocation invocation, Iterable> matchers) {
        return value(nameForReport, SubValuesExtractors.method(invocation.method, invocation.arguments), matchers);
    }


    public static  ReportingMatcher method(String nameForReport, MethodByNameInvocation invocation, S value) {
        return value(nameForReport, SubValuesExtractors.methodByName(invocation.methodName, invocation.arguments), value);
    }

    public static  ReportingMatcher method(String nameForReport, MethodByNameInvocation invocation, Matcher matcher) {
        return value(nameForReport, SubValuesExtractors.methodByName(invocation.methodName, invocation.arguments), matcher);
    }

    public static  ReportingMatcher method(String nameForReport, MethodByNameInvocation invocation, ReportingMatcher reportingMatcher) {
        return value(nameForReport, SubValuesExtractors.methodByName(invocation.methodName, invocation.arguments), reportingMatcher);
    }

    @SafeVarargs
    public static  ReportingMatcher method(String nameForReport, MethodByNameInvocation invocation, Matcher... matchers) {
        return value(nameForReport, SubValuesExtractors.methodByName(invocation.methodName, invocation.arguments), matchers);
    }

    public static  ReportingMatcher method(String nameForReport, MethodByNameInvocation invocation, Iterable> matchers) {
        return value(nameForReport, SubValuesExtractors.methodByName(invocation.methodName, invocation.arguments), matchers);
    }


    public static MethodInvocation invocation(Method method, Object... arguments) {
        return new MethodInvocation(method, arguments);
    }

    public static MethodByNameInvocation invocation(String methodName, Object... arguments) {
        return new MethodByNameInvocation(methodName, arguments);
    }



    public static  ReportingMatcher getter(Method method, S value) {
        return value(SubValuesExtractors.getter(method), value);
    }

    public static  ReportingMatcher getter(Method method, Matcher matcher) {
        return value(SubValuesExtractors.getter(method), matcher);
    }

    public static  ReportingMatcher getter(Method method, ReportingMatcher reportingMatcher) {
        return value(SubValuesExtractors.getter(method), reportingMatcher);
    }

    @SafeVarargs
    public static  ReportingMatcher getter(Method method, Matcher... matchers) {
        return value(SubValuesExtractors.getter(method), matchers);
    }

    public static  ReportingMatcher getter(Method method, Iterable> matchers) {
        return value(SubValuesExtractors.getter(method), matchers);
    }


    public static  ReportingMatcher getter(String methodName, S value) {
        return value(SubValuesExtractors.getterByName(methodName), value);
    }

    public static  ReportingMatcher getter(String methodName, Matcher matcher) {
        return value(SubValuesExtractors.getterByName(methodName), matcher);
    }

    public static  ReportingMatcher getter(String methodName, ReportingMatcher reportingMatcher) {
        return value(SubValuesExtractors.getterByName(methodName), reportingMatcher);
    }

    @SafeVarargs
    public static  ReportingMatcher getter(String methodName, Matcher... matchers) {
        return value(SubValuesExtractors.getterByName(methodName), matchers);
    }

    public static  ReportingMatcher getter(String methodName, Iterable> matchers) {
        return value(SubValuesExtractors.getterByName(methodName), matchers);
    }



    public static  ReportingMatcher arrayElement(int index, T value) {
        return value(SubValuesExtractors.arrayElement(index), value);
    }

    public static  ReportingMatcher arrayElement(int index, Matcher matcher) {
        return value(SubValuesExtractors.arrayElement(index), matcher);
    }

    public static  ReportingMatcher arrayElement(int index, ReportingMatcher reportingMatcher) {
        return value(SubValuesExtractors.arrayElement(index), reportingMatcher);
    }

    @SafeVarargs
    public static  ReportingMatcher arrayElement(int index, Matcher... matchers) {
        return value(SubValuesExtractors.arrayElement(index), matchers);
    }

    public static  ReportingMatcher arrayElement(int index, Iterable> matchers) {
        return value(SubValuesExtractors.arrayElement(index), matchers);
    }


    public static  ReportingMatcher> iterableElement(int index, T value) {
        return value(SubValuesExtractors.iterableElement(index), value);
    }

    public static  ReportingMatcher> iterableElement(int index, Matcher matcher) {
        return value(SubValuesExtractors.iterableElement(index), matcher);
    }

    public static  ReportingMatcher> iterableElement(int index, ReportingMatcher reportingMatcher) {
        return value(SubValuesExtractors.iterableElement(index), reportingMatcher);
    }

    @SafeVarargs
    public static  ReportingMatcher> iterableElement(int index, Matcher... matchers) {
        return value(SubValuesExtractors.iterableElement(index), matchers);
    }

    public static  ReportingMatcher> iterableElement(int index, Iterable> matchers) {
        return value(SubValuesExtractors.iterableElement(index), matchers);
    }


    public static  ReportingMatcher> listElement(int index, T value) {
        return value(SubValuesExtractors.listElement(index), value);
    }

    public static  ReportingMatcher> listElement(int index, Matcher matcher) {
        return value(SubValuesExtractors.listElement(index), matcher);
    }

    public static  ReportingMatcher> listElement(int index, ReportingMatcher reportingMatcher) {
        return value(SubValuesExtractors.listElement(index), reportingMatcher);
    }

    @SafeVarargs
    public static  ReportingMatcher> listElement(int index, Matcher... matchers) {
        return value(SubValuesExtractors.listElement(index), matchers);
    }

    public static  ReportingMatcher> listElement(int index, Iterable> matchers) {
        return value(SubValuesExtractors.listElement(index), matchers);
    }



    public static  ReportingMatcher> entry(K key, V value) {
        return value(SubValuesExtractors.hashMapValueForKey(key), value);
    }

    public static  ReportingMatcher> entry(K key, Matcher matcher) {
        return value(SubValuesExtractors.hashMapValueForKey(key), matcher);
    }

    public static  ReportingMatcher> entry(K key, ReportingMatcher reportingMatcher) {
        return value(SubValuesExtractors.hashMapValueForKey(key), reportingMatcher);
    }

    @SafeVarargs
    public static  ReportingMatcher> entry(K key, Matcher... matchers) {
        return value(SubValuesExtractors.hashMapValueForKey(key), matchers);
    }

    public static  ReportingMatcher> entry(K key, Iterable> matchers) {
        return value(SubValuesExtractors.hashMapValueForKey(key), matchers);
    }



    @SafeVarargs
    public static  ReportingMatcher> iterable(Supplier... subValuesCheckerSuppliers) {
        return iterable(asList(subValuesCheckerSuppliers));
    }

    public static  ReportingMatcher> iterable(Iterable> subValuesCheckerSuppliers) {
        return subValuesMatcher(iterableElements(), subValuesCheckerSuppliers);
    }

    @SafeVarargs
    public static  ReportingMatcher> iterator(Supplier... subValuesCheckerSuppliers) {
        return iterator(asList(subValuesCheckerSuppliers));
    }

    public static  ReportingMatcher> iterator(Iterable> subValuesCheckerSuppliers) {
        return subValuesMatcher(iteratorElements(), subValuesCheckerSuppliers);
    }

    @SafeVarargs
    public static  ReportingMatcher array(Supplier... subValuesCheckerSuppliers) {
        return array(asList(subValuesCheckerSuppliers));
    }

    public static  ReportingMatcher array(Iterable> subValuesCheckerSuppliers) {
        return subValuesMatcher(arrayElements(), subValuesCheckerSuppliers);
    }

    @SafeVarargs
    public static  ReportingMatcher> hashMap(Supplier... subValuesCheckerSuppliers) {
        return hashMap(asList(subValuesCheckerSuppliers));
    }

    public static  ReportingMatcher> hashMap(Iterable> subValuesCheckerSuppliers) {
        return subValuesMatcher(hashMapEntries(), subValuesCheckerSuppliers);
    }


    @SafeVarargs
    public static  ReportingMatcher subValuesMatcher(SubValuesExtractor subValuesExtractor, Supplier... subValuesCheckerSuppliers) {
        return subValuesMatcher(subValuesExtractor, asList(subValuesCheckerSuppliers));
    }

    public static  ReportingMatcher subValuesMatcher(SubValuesExtractor subValuesExtractor, Iterable> subValuesCheckerSuppliers) {
        return new SubValuesMatcher<>(subValuesExtractor, compositeSubValuesCheckerSupplier(subValuesCheckerSuppliers));
    }



    public static  ReportingMatcher displayAll(SubValuesExtractor subValuesExtractor) {
        return new SubValuesMatcher<>(subValuesExtractor, noOpSubValuesChecker());
    }


    public static  ReportingMatcher uncheckedElementsAreFails(Matcher matcher) {
        return uncheckedElementsAreFails((ReportingMatcher) toReportingMatcher(matcher));
    }

    public static  ReportingMatcher uncheckedElementsAreFails(ReportingMatcher matcher) {
        return new UncheckedElementsAreFailsMatcher<>(matcher);
    }

    @SafeVarargs
    public static  ReportingMatcher uncheckedElementsAreFails(Matcher... matchers) {
        return uncheckedElementsAreFails(asList(matchers));
    }

    public static  ReportingMatcher uncheckedElementsAreFails(Iterable> matchers) {
        return uncheckedElementsAreFails(merge(toReportingMatchers(matchers)));
    }



    public static class MethodInvocation {
        final Method method;
        final Object[] arguments;

        MethodInvocation(Method method, Object... arguments) {
            this.method = method;
            this.arguments = arguments;
        }
    }

    public static class MethodByNameInvocation {
        final String methodName;
        final Object[] arguments;

        MethodByNameInvocation(String methodName, Object... arguments) {
            this.methodName = methodName;
            this.arguments = arguments;
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy