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

org.cthul.matchers.diagnose.QuickMatcherBase Maven / Gradle / Ivy

Go to download

Provides hamcrest.org matchers for strings and exceptions, allows matching code blocks, and provides several utilities for combining matchers.

The newest version!
package org.cthul.matchers.diagnose;

import org.cthul.matchers.diagnose.result.MatchResult;
import org.cthul.matchers.diagnose.result.MatchResultMismatch;
import org.cthul.matchers.diagnose.result.MatchResultSuccess;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.hamcrest.StringDescription;

/**
 * Implements {@link QuickDiagnosingMatcher} interface, 
 * but matches and describes in separate steps.
 * 

* {@link QuickDiagnosingMatcher#matches(java.lang.Object, org.hamcrest.Description)} * is implemented as *

{@code 
 *   if (matches(item)) {
 *       return true;
 *   } else {
 *       describeMismatch(item, description);
 *       return false;
 *   }
 * }
*

* If you want to override {@link #matches(java.lang.Object, org.hamcrest.Description)}, * extend {@link QuickDiagnosingMatcherBase} instead. * @param */ public abstract class QuickMatcherBase extends BaseMatcher implements QuickDiagnosingMatcher { /** {@inheritDoc} */ @Override public abstract boolean matches(Object item); /** {@inheritDoc} */ @Override public abstract void describeMismatch(Object item, Description description); /** *

{@code 
     *   if (matches(item)) {
     *       return true;
     *   } else {
     *       describeMismatch(item, description);
     *       return false;
     *   }
     * }
* @param item * @param mismatch * @return match */ @Override public final boolean matches(Object item, Description mismatch) { if (matches(item)) { return true; } else { describeMismatch(item, mismatch); return false; } } @Override public MatchResult matchResult(I item) { StringDescription mismatch = new StringDescription(); if (matches(item, mismatch)) { return new MatchResultSuccess<>(item, this); } else { return new MatchResultMismatch<>(item, this, mismatch.toString()); } } }