io.github.olib963.javatest.matchers.internal.MatcherAssertion Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of javatest-matchers Show documentation
Show all versions of javatest-matchers Show documentation
Matchers to create assertions from common conditions
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();
}
}