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

com.codepoetics.fluvius.test.matchers.BasePropertyMatcher Maven / Gradle / Ivy

There is a newer version: 1.10
Show newest version
package com.codepoetics.fluvius.test.matchers;

import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeDiagnosingMatcher;

import static com.codepoetics.fluvius.test.matchers.IndentationControl.indent;
import static com.codepoetics.fluvius.test.matchers.IndentationControl.newline;
import static com.codepoetics.fluvius.test.matchers.IndentationControl.outdent;

abstract class BasePropertyMatcher extends TypeSafeDiagnosingMatcher {

  private final String entityType;

  protected BasePropertyMatcher(String entityType) {
    this.entityType = entityType;
  }

  protected boolean propertyMatches(String propertyName, T propertyValue, Matcher matcher, Description description) {
    if (matcher == null) {
      return true;
    }
    if (matcher.matches(propertyValue)) {
      return true;
    }
    newline(description).appendText(propertyName).appendText(": ");
    indent();
    matcher.describeMismatch(propertyValue, description);
    outdent();
    return false;
  }

  @Override
  public void describeTo(Description description) {
    description.appendText("A ").appendText(entityType);
    indent();
    describeProperties(new PropertyDescriber(description));
    outdent();
  }

  protected abstract void describeProperties(PropertyDescriber describer);
  protected abstract void checkProperties(T t, PropertyMismatchDescriber describer);

  @Override
  protected boolean matchesSafely(T t, Description description) {
    PropertyMismatchDescriber mismatchDescriber = new PropertyMismatchDescriber(description);
    checkProperties(t, mismatchDescriber);
    return mismatchDescriber.result();
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy