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

com.github.karsaig.approvalcrest.jupiter.matcher.MatchersImpl Maven / Gradle / Ivy

The newest version!
package com.github.karsaig.approvalcrest.jupiter.matcher;

import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.junit.jupiter.api.TestInfo;

import com.github.karsaig.approvalcrest.FileMatcherConfig;
import com.github.karsaig.approvalcrest.jupiter.Junit5InfoBasedTestMeta;
import com.github.karsaig.approvalcrest.jupiter.JunitJupiterTestMeta;
import com.github.karsaig.approvalcrest.matcher.ContentMatcher;
import com.github.karsaig.approvalcrest.matcher.DiagnosingCustomisableMatcher;
import com.github.karsaig.approvalcrest.matcher.JsonMatcher;
import com.github.karsaig.approvalcrest.matcher.TestMetaInformation;

public class MatchersImpl {

    private static final MatcherFactory MATCHER_FACTORY = new MatcherFactory();

    protected TestMetaInformation getTestMetaInformation() {
        return new JunitJupiterTestMeta();
    }

    protected Junit5InfoBasedTestMeta getTestMetaInformation(TestInfo testInfo) {
        return new Junit5InfoBasedTestMeta(testInfo);
    }

    public  DiagnosingCustomisableMatcher sameBeanAs(T expected) {
        return MATCHER_FACTORY.beanMatcher(expected);
    }

    public  JsonMatcher sameJsonAsApproved() {
        return Matchers.sameJsonAsApproved(getTestMetaInformation());
    }

    public  JsonMatcher sameJsonAsApproved(TestMetaInformation testMetaInformation) {
        return MATCHER_FACTORY.jsonMatcher(testMetaInformation, new FileMatcherConfig());
    }

    public  JsonMatcher sameJsonAsApproved(TestInfo testInfo) {
        return getUniqueIndex(testInfo)
                .map(s -> MATCHER_FACTORY.jsonMatcher(getTestMetaInformation(testInfo), new FileMatcherConfig()).withUniqueId(s))
                .orElse(MATCHER_FACTORY.jsonMatcher(getTestMetaInformation(testInfo), new FileMatcherConfig()));
    }

    public  ContentMatcher sameContentAsApproved() {
        return Matchers.sameContentAsApproved(getTestMetaInformation());
    }

    public  ContentMatcher sameContentAsApproved(TestMetaInformation testMetaInformation) {
        return MATCHER_FACTORY.contentMatcher(testMetaInformation, new FileMatcherConfig());
    }

    public  ContentMatcher sameContentAsApproved(TestInfo testInfo) {
        return getUniqueIndex(testInfo)
                .map(s -> MATCHER_FACTORY.contentMatcher(getTestMetaInformation(testInfo), new FileMatcherConfig()).withUniqueId(s))
                .orElse(MATCHER_FACTORY.contentMatcher(getTestMetaInformation(testInfo), new FileMatcherConfig()));
    }

    private static final Pattern TEST_INDEX_MATCHER = Pattern.compile("^\\[(\\d+)].*");

    private static Optional getUniqueIndex(TestInfo testInfo) {
        Matcher m = TEST_INDEX_MATCHER.matcher(testInfo.getDisplayName());
        if (m.matches()) {
            return Optional.of(m.group(1));
        }
        return Optional.empty();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy