org.hamcrest.TypeSafeDiagnosingMatcher Java Examples

The following examples show how to use org.hamcrest.TypeSafeDiagnosingMatcher. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CommonMatchers.java    From JustJson with Apache License 2.0 6 votes vote down vote up
public static Matcher<JsonElement> withCharsMoreOrEqualTo(final int value) {
    return new TypeSafeDiagnosingMatcher<JsonElement>() {

        @Override
        protected boolean matchesSafely(JsonElement item, Description mismatchDescription) {
            //we do not care for the properties if parent item is not String
            if (!item.isString()) return true;
            if (item.asString().length() < value) {
                mismatchDescription.appendText("String length less than minimum value: " + value);
                return false;
            }
            return true;
        }

        @Override
        public void describeTo(Description description) {
            description.appendText("String minimum length");
        }
    };
}
 
Example #2
Source File: NextMatchers.java    From mobius with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a matcher that matches {@link Next} instances whose effects match the supplied effect
 * matcher.
 *
 * @param matcher the matcher to apply to the effects
 * @param <M> the model type
 * @param <F> the effect type
 */
public static <M, F> Matcher<Next<M, F>> hasEffects(Matcher<Iterable<F>> matcher) {
  return new TypeSafeDiagnosingMatcher<Next<M, F>>() {
    @Override
    protected boolean matchesSafely(Next<M, F> item, Description mismatchDescription) {
      if (!item.hasEffects()) {
        mismatchDescription.appendText("it had no effects");
        return false;

      } else if (!matcher.matches(item.effects())) {
        mismatchDescription.appendText("the effects were ");
        matcher.describeMismatch(item.effects(), mismatchDescription);
        return false;
      }
      return true;
    }

    @Override
    public void describeTo(Description description) {
      description.appendText("Next with effects ").appendDescriptionOf(matcher);
    }
  };
}
 
Example #3
Source File: NextMatchers.java    From mobius with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a matcher that matches {@link Next} instances without a model.
 *
 * @param <M> the model type
 * @param <F> the effect type
 */
public static <M, F> Matcher<Next<M, F>> hasNoModel() {
  return new TypeSafeDiagnosingMatcher<Next<M, F>>() {
    @Override
    protected boolean matchesSafely(Next<M, F> item, Description mismatchDescription) {
      if (item.hasModel()) {
        mismatchDescription.appendText("it had a model: " + item.modelUnsafe());
        return false;

      } else {
        return true;
      }
    }

    @Override
    public void describeTo(Description description) {
      description.appendText("Next without model");
    }
  };
}
 
Example #4
Source File: NextMatchers.java    From mobius with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a matcher that matches {@link Next} instances with a model that matches the supplied
 * model matcher.
 *
 * @param matcher the matcher to apply to the model
 * @param <M> the model type
 * @param <F> the effect type
 */
public static <M, F> Matcher<Next<M, F>> hasModel(Matcher<M> matcher) {
  return new TypeSafeDiagnosingMatcher<Next<M, F>>() {
    @Override
    protected boolean matchesSafely(Next<M, F> item, Description mismatchDescription) {
      if (!item.hasModel()) {
        mismatchDescription.appendText("it had no model");
        return false;

      } else if (!matcher.matches(item.modelUnsafe())) {
        mismatchDescription.appendText("the model ");
        matcher.describeMismatch(item.modelUnsafe(), mismatchDescription);
        return false;

      } else {
        return true;
      }
    }

    @Override
    public void describeTo(Description description) {
      description.appendText("Next with model ").appendDescriptionOf(matcher);
    }
  };
}
 
Example #5
Source File: NextMatchers.java    From mobius with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a matcher that matches {@link Next} instances with a model.
 *
 * @param <M> the model type
 * @param <F> the effect type
 */
public static <M, F> Matcher<Next<M, F>> hasModel() {
  return new TypeSafeDiagnosingMatcher<Next<M, F>>() {
    @Override
    protected boolean matchesSafely(Next<M, F> item, Description mismatchDescription) {
      if (!item.hasModel()) {
        mismatchDescription.appendText("it had no model");
        return false;
      }

      return true;
    }

    @Override
    public void describeTo(Description description) {
      description.appendText("Next with any model");
    }
  };
}
 
Example #6
Source File: NextMatchers.java    From mobius with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a matcher that matches {@link Next} instances with no effects.
 *
 * @param <M> the model type
 * @param <F> the effect type
 */
public static <M, F> Matcher<Next<M, F>> hasNoEffects() {
  return new TypeSafeDiagnosingMatcher<Next<M, F>>() {
    @Override
    protected boolean matchesSafely(Next<M, F> item, Description mismatchDescription) {
      if (item.hasEffects()) {
        mismatchDescription.appendText("it had effects: " + item.effects());
        return false;
      } else {
        return true;
      }
    }

    @Override
    public void describeTo(Description description) {
      description.appendText("Next without effects");
    }
  };
}
 
Example #7
Source File: MatcherHelper.java    From fullstop with Apache License 2.0 6 votes vote down vote up
public static <T> Matcher<? super Collection<T>> hasSize(final int expectedSize) {
    return new TypeSafeDiagnosingMatcher<Collection<?>>() {
        @Override
        protected boolean matchesSafely(final Collection<?> actual, final Description mismatchDescription) {
            final int actualSize = actual.size();
            if (actualSize == expectedSize) {
                return true;
            }
            else {
                mismatchDescription.appendText("size was ").appendValue(actualSize);
                return false;
            }
        }

        @Override
        public void describeTo(final Description description) {
            description.appendText("with size ").appendValue(expectedSize);
        }
    };
}
 
Example #8
Source File: ViolationMatchers.java    From fullstop with Apache License 2.0 6 votes vote down vote up
public static Matcher<Violation> hasType(final String expectedType) {
    return new TypeSafeDiagnosingMatcher<Violation>() {
        @Override
        protected boolean matchesSafely(final Violation violation, final Description mismatchDescription) {
            final String actualType = violation.getViolationType();
            if (!Objects.equals(actualType, expectedType)) {
                mismatchDescription.appendText("type was ").appendValue(actualType);
                return false;
            } else {
                return true;
            }
        }

        @Override
        public void describeTo(final Description description) {
            description.appendText("violation of type ").appendValue(expectedType);
        }
    };
}
 
Example #9
Source File: PersonMatchers.java    From mockito-cookbook with Apache License 2.0 6 votes vote down vote up
public static Matcher<Person> containsSiblings(final Person... siblings) {
	return new TypeSafeDiagnosingMatcher<Person>() {
		@Override
		public void describeTo(Description description) {
			description.appendText("Person should have siblings ").appendValue(siblings);
		}

		@Override
		protected boolean matchesSafely(Person person, Description mismatchDescription) {
			if (!person.getSiblings().containsAll(Arrays.asList(siblings))) {
				mismatchDescription.appendText("The person has size of siblings equal to ")
						.appendValue(person.getSiblings().size())
						.appendText(" and the person has siblings ")
						.appendValue(person.getSiblings());
				return false;
			}
			return true;
		}
	};
}
 
Example #10
Source File: FlinkSqlParserImplTest.java    From flink with Apache License 2.0 6 votes vote down vote up
public static BaseMatcher<SqlNode> validated(String validatedSql) {
	return new TypeSafeDiagnosingMatcher<SqlNode>() {
		@Override
		protected boolean matchesSafely(SqlNode item, Description mismatchDescription) {
			if (item instanceof ExtendedSqlNode) {
				try {
					((ExtendedSqlNode) item).validate();
				} catch (SqlValidateException e) {
					mismatchDescription.appendText("Could not validate the node. Exception: \n");
					mismatchDescription.appendValue(e);
				}

				String actual = item.toSqlString(null, true).getSql();
				return actual.equals(validatedSql);
			}
			mismatchDescription.appendText("This matcher can be applied only to ExtendedSqlNode.");
			return false;
		}

		@Override
		public void describeTo(Description description) {
			description.appendText("The validated node string representation should be equal to: \n");
			description.appendText(validatedSql);
		}
	};
}
 
Example #11
Source File: EventMatchers.java    From mapper with Apache License 2.0 6 votes vote down vote up
public static <EventT> Matcher<? super MatchingHandler<? extends EventT>> noEvents() {
  return new TypeSafeDiagnosingMatcher<MatchingHandler<? extends EventT>>() {
    @Override
    protected boolean matchesSafely(MatchingHandler<? extends EventT> item, Description mismatchDescription) {
      if (item.events.isEmpty()) {
        return true;
      } else {
        mismatchDescription.appendText("events happened: " + item.events);
        return false;
      }
    }

    @Override
    public void describeTo(Description description) {
      description.appendText("no events");
    }
  };
}
 
Example #12
Source File: BytesMatchers.java    From hbase with Apache License 2.0 6 votes vote down vote up
public static Matcher<byte[]> bytesAsStringBinary(final Matcher<String> matcher) {
  return new TypeSafeDiagnosingMatcher<byte[]>() {
    @Override protected boolean matchesSafely(byte[] item, Description mismatchDescription) {
      final String binary = Bytes.toStringBinary(item);
      if (matcher.matches(binary)) {
        return true;
      }
      mismatchDescription.appendText("was a byte[] with a Bytes.toStringBinary value ");
      matcher.describeMismatch(binary, mismatchDescription);
      return false;
    }

    @Override public void describeTo(Description description) {
      description
        .appendText("has a byte[] with a Bytes.toStringBinary value that ")
        .appendDescriptionOf(matcher);
    }
  };
}
 
Example #13
Source File: IncrementalAnalyserTest.java    From pitest with Apache License 2.0 6 votes vote down vote up
private Matcher<MutationResult> withStatus(final DetectionStatus status) {
  return new TypeSafeDiagnosingMatcher<MutationResult>() {

    @Override
    public void describeTo(final Description description) {
      description.appendText("a mutation result with status ").appendValue(status);
    }

    @Override
    protected boolean matchesSafely(final MutationResult item,
                                    final Description mismatchDescription) {
      mismatchDescription
              .appendText("a mutation result with status ")
              .appendValue(item.getStatus());

      return status.equals(item.getStatus());
    }
  };
}
 
Example #14
Source File: CommonMatchers.java    From JustJson with Apache License 2.0 6 votes vote down vote up
/**
 * General matcher
 *
 * @param validator
 * @param element
 * @return
 */
public static Matcher<JsonElement> isElementValid(final Validator validator, final JsonElement element) {
    return new TypeSafeDiagnosingMatcher<JsonElement>() {
        @Override
        protected boolean matchesSafely(JsonElement item, Description mismatchDescription) {
            if (!validator.isValid(element)) {
                mismatchDescription.appendText("element: " + element.toString() + ", does not validate by validator " + validator.getTitle());
                return false;
            }
            return true;
        }

        @Override
        public void describeTo(Description description) {
            description.appendText("is array item valid");
        }
    };
}
 
Example #15
Source File: CommonMatchers.java    From JustJson with Apache License 2.0 6 votes vote down vote up
public static Matcher<JsonElement> withCharsLessOrEqualTo(final int value) {
    return new TypeSafeDiagnosingMatcher<JsonElement>() {

        @Override
        protected boolean matchesSafely(JsonElement item, Description mismatchDescription) {
            //we do not care for the properties if parent item is not String
            if (!item.isString()) return true;
            if (item.asString().length() > value) {
                mismatchDescription.appendText("String length more than maximum value: " + value);
                return false;
            }
            return true;
        }

        @Override
        public void describeTo(Description description) {
            description.appendText("String maximum length");
        }
    };
}
 
Example #16
Source File: MatcherHelper.java    From fullstop with Apache License 2.0 6 votes vote down vote up
public static Matcher<? super CharSequence> empty() {
    return new TypeSafeDiagnosingMatcher<CharSequence>() {
        @Override
        protected boolean matchesSafely(final CharSequence item, final Description mismatchDescription) {
            if (item.length() == 0) {
                return true;
            }
            else {
                mismatchDescription.appendText("was ").appendValue(item);
                return false;
            }
        }

        @Override
        public void describeTo(final Description description) {
            description.appendText("empty string");
        }
    };
}
 
Example #17
Source File: CommonMatchers.java    From JustJson with Apache License 2.0 6 votes vote down vote up
public static Matcher<JsonElement> matchesPattern(final String value) {
    return new TypeSafeDiagnosingMatcher<JsonElement>() {

        @Override
        protected boolean matchesSafely(JsonElement item, Description mismatchDescription) {
            //we do not care for the properties if parent item is not String
            if (!item.isString()) return true;
            if (!Pattern.matches(value, item.asString())) {
                mismatchDescription.appendText("Pattern '" + value + "' does not match '" + item.asString() + "'");
                return false;
            }
            return true;
        }

        @Override
        public void describeTo(Description description) {
            description.appendText("Pattern match");
        }
    };
}
 
Example #18
Source File: CommonMatchers.java    From JustJson with Apache License 2.0 6 votes vote down vote up
public static Matcher<JsonElement> isLessThan(final double value) {
    return new TypeSafeDiagnosingMatcher<JsonElement>() {

        @Override
        protected boolean matchesSafely(JsonElement item, Description mismatchDescription) {
            //we do not care for the properties if parent item is not Number
            if (!item.isNumber()) return true;
            if (!(item.asDouble() < value)) {
                mismatchDescription.appendText("value is not less than exclusive maximum " + value);
                return false;
            }
            return true;
        }

        @Override
        public void describeTo(Description description) {
            description.appendText("exclusive maximum");
        }
    };
}
 
Example #19
Source File: CommonMatchers.java    From JustJson with Apache License 2.0 6 votes vote down vote up
public static Matcher<JsonElement> isLessOrEqualThan(final double value) {
    return new TypeSafeDiagnosingMatcher<JsonElement>() {

        @Override
        protected boolean matchesSafely(JsonElement item, Description mismatchDescription) {
            //we do not care for the properties if parent item is not Number
            if (!item.isNumber()) return true;
            if (!(item.asDouble() <= value)) {
                mismatchDescription.appendText("value is not less than maximum " + value);
                return false;
            }
            return true;
        }

        @Override
        public void describeTo(Description description) {
            description.appendText("maximum");
        }
    };
}
 
Example #20
Source File: CommonMatchers.java    From JustJson with Apache License 2.0 6 votes vote down vote up
public static Matcher<JsonElement> isMoreThan(final double value) {
    return new TypeSafeDiagnosingMatcher<JsonElement>() {

        @Override
        protected boolean matchesSafely(JsonElement item, Description mismatchDescription) {
            //we do not care for the properties if parent item is not Number
            if (!item.isNumber()) return true;
            if (!(item.asDouble() > value)) {
                mismatchDescription.appendText("value is not more than exclusive minimum " + value);
                return false;
            }
            return true;
        }

        @Override
        public void describeTo(Description description) {
            description.appendText("exclusive minimum");
        }
    };
}
 
Example #21
Source File: CommonMatchers.java    From JustJson with Apache License 2.0 6 votes vote down vote up
public static Matcher<JsonElement> isMoreOrEqualThan(final double value) {
    return new TypeSafeDiagnosingMatcher<JsonElement>() {

        @Override
        protected boolean matchesSafely(JsonElement item, Description mismatchDescription) {
            //we do not care for the properties if parent item is not Number
            if (!item.isNumber()) return true;
            if (!(item.asDouble() >= value)) {
                mismatchDescription.appendText("value is not more than minimum " + value);
                return false;
            }
            return true;
        }

        @Override
        public void describeTo(Description description) {
            description.appendText("minimum");
        }
    };
}
 
Example #22
Source File: CommonMatchers.java    From JustJson with Apache License 2.0 6 votes vote down vote up
public static Matcher<JsonElement> isMultipleOf(final double value) {
    return new TypeSafeDiagnosingMatcher<JsonElement>() {
        @Override
        protected boolean matchesSafely(JsonElement item, Description mismatchDescription) {
            //we do not care for the properties if parent item is not Number
            if (!item.isNumber()) return true;

            Number remainder = item.asDouble() % value;
            if (!remainder.equals(0) && !remainder.equals(0.0)) {
                mismatchDescription.appendText("value is not multipleOf " + value);
                return false;
            }

            return true;
        }

        @Override
        public void describeTo(Description description) {
            description.appendText("multipleOf");
        }
    };
}
 
Example #23
Source File: CommonMatchers.java    From JustJson with Apache License 2.0 6 votes vote down vote up
public static Matcher<JsonElement> isOfType(final String type) {
    return new TypeSafeDiagnosingMatcher<JsonElement>() {
        @Override
        protected boolean matchesSafely(JsonElement item, Description mismatchDescription) {
            if (type.equals(item.getJsonType()))
                return true;
            else {
                mismatchDescription.appendText(", mismatch type '" + item.getJsonType() + "'");
                return false;
            }
        }

        @Override
        public void describeTo(Description description) {
            description.appendText("\nMatch to type: " + type);
        }
    };
}
 
Example #24
Source File: CommonMatchers.java    From JustJson with Apache License 2.0 6 votes vote down vote up
public static Matcher<JsonElement> isOfType(final List<String> types) {
    return new TypeSafeDiagnosingMatcher<JsonElement>() {
        @Override
        protected boolean matchesSafely(JsonElement item, Description mismatchDescription) {
            if (types.contains(item.getJsonType()) || (item.getJsonType().equals(TYPE_INTEGER) && types.contains(TYPE_NUMBER)))
                return true;
            else {
                mismatchDescription.appendText(", mismatch type '" + item.getJsonType() + "'");
                return false;
            }
        }

        @Override
        public void describeTo(Description description) {
            description.appendText("\nMatch to one of types: " + types.toString());
        }
    };
}
 
Example #25
Source File: CommonMatchers.java    From JustJson with Apache License 2.0 6 votes vote down vote up
public static Matcher<JsonElement> isInEnums(final JsonArray enums) {
    return new TypeSafeDiagnosingMatcher<JsonElement>() {
        @Override
        protected boolean matchesSafely(JsonElement item, Description mismatchDescription) {

            if (enums.contains(item)) {
                return true;
            }

            mismatchDescription.appendText(", mismatch value '" + item.toString() + "'");
            return false;
        }

        @Override
        public void describeTo(Description description) {
            description.appendText("\nMatch to one of enum values: " + enums.toString());
        }
    };
}
 
Example #26
Source File: CommonMatchers.java    From JustJson with Apache License 2.0 6 votes vote down vote up
public static Matcher<JsonElement> areItemsValid(final Validator validator) {
    return new TypeSafeDiagnosingMatcher<JsonElement>() {
        @Override
        protected boolean matchesSafely(JsonElement item, Description mismatchDescription) {
            //we do not care for the properties if parent item is not JsonArray
            if (!item.isJsonArray()) return true;

            for (int i = 0; i < item.asJsonArray().length(); i++) {
                StringBuilder sb = new StringBuilder();
                if (!validator.validate(item.asJsonArray().opt(i), sb)) {
                    mismatchDescription.appendText("item at pos: " + i + ", does not validate by validator " + validator.getTitle())
                            .appendText("\nDetails: ")
                            .appendText(sb.toString());
                    return false;
                }
            }
            return true;
        }

        @Override
        public void describeTo(Description description) {
            description.appendText("are array items valid");
        }
    };
}
 
Example #27
Source File: CommonMatchers.java    From JustJson with Apache License 2.0 6 votes vote down vote up
public static Matcher<JsonElement> doesItemCountMatches(final int itemsCount) {
    return new TypeSafeDiagnosingMatcher<JsonElement>() {
        @Override
        protected boolean matchesSafely(JsonElement item, Description mismatchDescription) {
            //we do not care for the properties if parent item is not JsonArray
            if (!item.isJsonArray()) return true;


            if (item.asJsonArray().length() > itemsCount) {
                mismatchDescription.appendText("items in Json array more than defined");
                return false;
            }

            return true;
        }

        @Override
        public void describeTo(Description description) {
            description.appendText("array items max count");
        }
    };
}
 
Example #28
Source File: CommonMatchers.java    From JustJson with Apache License 2.0 6 votes vote down vote up
public static Matcher<JsonElement> areItemsUnique() {
    return new TypeSafeDiagnosingMatcher<JsonElement>() {
        @Override
        protected boolean matchesSafely(JsonElement item, Description mismatchDescription) {
            //we do not care for the items if parent item is not JsonArray
            if (!item.isJsonArray()) return true;

            JsonElement prevEl = null;
            for (JsonElement el : item.asJsonArray()) {
                if (prevEl != null && el.equals(prevEl)) {
                    mismatchDescription.appendText("items in Json array are not unique");
                    return false;
                }
                prevEl = el;
            }

            return true;
        }

        @Override
        public void describeTo(Description description) {
            description.appendText("unique items");
        }
    };
}
 
Example #29
Source File: CommonMatchers.java    From JustJson with Apache License 2.0 6 votes vote down vote up
public static Matcher<JsonElement> isPropertyPresent(final String property) {
    return new TypeSafeDiagnosingMatcher<JsonElement>() {
        @Override
        protected boolean matchesSafely(JsonElement item, Description mismatchDescription) {
            //we do not care for the properties if parent item is not JsonObject
            if (!item.isJsonObject()) return true;

            if (!item.asJsonObject().has(property)) {
                mismatchDescription.appendText(", does not exist in : " + item);
                return false;
            }
            return true;
        }

        @Override
        public void describeTo(Description description) {
            description.appendText("\nCheck if property '" + property + "' exists");
        }
    };
}
 
Example #30
Source File: CommonMatchers.java    From JustJson with Apache License 2.0 6 votes vote down vote up
public static Matcher<JsonElement> isPropertyValid(final Validator validator, final String property) {
    return new TypeSafeDiagnosingMatcher<JsonElement>() {
        @Override
        protected boolean matchesSafely(JsonElement item, Description mismatchDescription) {
            //we do not care for the properties if parent item is not JsonObject
            if (!item.isJsonObject()) return true;

            //we also dont care if the property is not actually there
            //if it is needed it will be handled by the "required" constraint on another matcher
            if (!item.asJsonObject().has(property)) return true;
            StringBuilder sb = new StringBuilder();
            if (!validator.validate(item.asJsonObject().opt(property), sb)) {
                mismatchDescription.appendText(", mismatch value: " + item.asJsonObject().opt(property))
                        .appendText("\nDetails: ")
                        .appendText(sb.toString());
                return false;
            }
            return true;
        }

        @Override
        public void describeTo(Description description) {
            description.appendText("\nMatch object property '" + property + "' with schema: " + ((SchemaValidator) validator).getSchema());
        }
    };
}