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

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

package com.codepoetics.fluvius.test.matchers;

import com.codepoetics.fluvius.api.scratchpad.Key;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.hamcrest.Matcher;

import java.util.HashMap;
import java.util.Map;

public class RecordingMatcher {

  private final Map recordedValues = new HashMap<>();

  public  Matcher record(final Key key) {
    return record(key.getName());
  }

  public  Matcher record(final String name) {
    return new BaseMatcher() {
      @Override
      public void describeTo(Description description) {
        description.appendText("(recorded as ").appendValue(name).appendText(")");
      }

      @Override
      public boolean matches(Object o) {
        recordedValues.put(name, o);
        return true;
      }
    };
  }

  public  Matcher equalsRecorded(final Key key) {
    return equalsRecorded(key.getName());
  }

  public  Matcher equalsRecorded(final String name) {
    return new BaseMatcher() {
      @Override
      public boolean matches(Object o) {
        return recordedValues.containsKey(name) && recordedValues.get(name).equals(o);
      }

      @Override
      public void describeTo(Description description) {
        description.appendText("equals value recorded as ").appendValue(name);
        if (recordedValues.containsKey(name)) {
          description.appendText(" (").appendValue(recordedValues.get(name)).appendText(")");
        } else {
          description.appendText(" (no value recorded)");
        }
      }
    };
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy