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

io.github.olib963.javatest.matchers.internal.MatcherAssertion Maven / Gradle / Ivy

There is a newer version: 0.2.0
Show newest version
package io.github.olib963.javatest.matchers.internal;

import io.github.olib963.javatest.Assertion;
import io.github.olib963.javatest.AssertionResult;
import io.github.olib963.javatest.matchers.CheckedRunnable;
import io.github.olib963.javatest.matchers.Matcher;

public class MatcherAssertion implements Assertion {
    private final A value;
    private final Matcher matcher;
    public MatcherAssertion(A value, Matcher matcher) {
        this.value = value;
        this.matcher = matcher;
    }

    @Override
    public AssertionResult run() {
        var matchResult = matcher.matches(value);
        var expectedPrefix = "Expected {" + toString(value) + "} to " + matcher.describeExpected();
        var description = matchResult.mismatch.map(mismatch -> expectedPrefix + " but " + mismatch).orElse(expectedPrefix);
        return AssertionResult.of(matchResult.matches, description);
    }

    private String toString(Object value) {
        if (value == null) {
            return "null";
        } else if(value instanceof CheckedRunnable) {
            return "runnable";
        }
        return value.toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy