下面列出了org.hamcrest.Description#appendText ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private static Matcher<Button> closeButtonForChart(final Chart chart) {
return new AbstractMatcher<Button>() {
@Override
protected boolean doMatch(Object item) {
if (!(item instanceof Button)) {
return false;
}
Button button = (Button) item;
return (button.getParent() == chart);
}
@Override
public void describeTo(Description description) {
description.appendText("delete button for custom chart");
}
};
}
private static Matcher<View> childAtPosition(
final Matcher<View> parentMatcher, final int position) {
return new TypeSafeMatcher<View>() {
@Override
public void describeTo(Description description) {
description.appendText("Child at position " + position + " in parent ");
parentMatcher.describeTo(description);
}
@Override
public boolean matchesSafely(View view) {
ViewParent parent = view.getParent();
return parent instanceof ViewGroup && parentMatcher.matches(parent)
&& view.equals(((ViewGroup) parent).getChildAt(position));
}
};
}
/**
* Checks if array is non-empty. This matcher can be replaced with JUnit 4.8
*/
public static Matcher<Object[]> hasItemInArray() {
return new TypeSafeMatcher<Object[]>() {
@Override
public boolean matchesSafely(Object[] arrayToTest) {
if (arrayToTest == null || arrayToTest.length == 0) {
return false;
}
return true;
}
@Override
public void describeTo(Description description) {
description.appendText("non-empty array");
}
};
}
@Override
public void describeMismatch(Object file, Description description) {
List<String> lines = readLinesSafely(file, description);
if (lines != null) {
lineMatchingDelegate.describeMismatch(lines, description);
description.appendText(" in file ");
description.appendValue(file);
description.appendText(" with lines ");
description.appendValue(readLinesSafely(file, Description.NONE));
}
}
@Override
protected boolean matchesSafely(final OperatorBackPressureStats stats, final Description mismatchDescription) {
if (!isBackPressureRatioCorrect(stats)) {
mismatchDescription.appendText("Not all subtask back pressure ratios in " + getBackPressureRatios(stats) + " are " + expectedBackPressureRatio);
return false;
}
return true;
}
@Override
public void describeTo(Description description) {
StringBuilder sb = new StringBuilder();
sb.append("not updated");
if (excludedNames.size() > 0) {
sb.append(" except {");
sb.append(String.join(", ", excludedNames));
sb.append("}");
}
description.appendText(sb.toString());
}
@Override
public void describeMismatchSafely(TableAndQuery tableAndQuery, Description description) {
String info;
if (!response.getJobComplete()) {
// query job not complete
info = String.format("The query job hasn't completed. Got response: %s", response);
} else {
// checksum mismatch
info =
String.format(
"was (%s).%n" + "\tTotal number of rows are: %d.%n" + "\tQueried data details:%s",
actualChecksum, response.getTotalRows(), formatRows(TOTAL_FORMATTED_ROWS));
}
description.appendText(info);
}
private void appendQuoting(Description description) {
if (wanted instanceof String) {
description.appendText("\"");
} else if (wanted instanceof Character) {
description.appendText("'");
}
}
public static Matcher<View> withTextColor(final int color) {
Checks.checkNotNull(color);
return new BoundedMatcher<View, TextView>(TextView.class) {
@Override
public boolean matchesSafely(TextView warning) {
return color == warning.getCurrentTextColor();
}
@Override
public void describeTo(Description description) {
description.appendText("with text color: ");
}
};
}
@Override
public void describeTo(Description description) {
if (this.expectedContents != null) {
this.expectedContents.describeTo(description);
}
else {
description
.appendText(this.templateFormat.getFileExtension() + " snippet");
}
}
@Override
public void describeMismatch(Object item, Description description) {
description.appendText("was ");
if (!(item instanceof IgnoreFilePattern.PathNameMatchResult)) {
description.appendValue(item);
} else {
IgnoreFilePattern.PathNameMatchResult res = (IgnoreFilePattern.PathNameMatchResult) item;
description.appendText("isMatch? ").appendValue(res.isMatch)
.appendText(" requiresMore? ").appendValue(res.requiresMore)
.appendText(" isLast? ").appendValue(res.isLastElementMatch)
.appendText(" next ").appendValue(res.next);
}
}
public static <T> ArgumentMatcher<List> eq(final List<T> expected) {
return new ArgumentMatcher<List>() {
@Override
public boolean matches(Object argument) {
return argument.equals(expected);
}
@Override
public void describeTo(Description description) {
description.appendText(expected.toString());
}
};
}
/**
* Matches the contents of a native forward mapping action.
*
* @param node JSON action to match
* @param description object used for recording errors
* @return true if the contents match, false otherwise
*/
private boolean matchNativeForwardAction(JsonNode node, Description description) {
NativeForwardMappingAction actionToMatch = (NativeForwardMappingAction) action;
final String jsonType = node.get(MappingActionCodec.TYPE).textValue();
if (!actionToMatch.type().name().equals(jsonType)) {
description.appendText("type was " + jsonType);
return false;
}
return true;
}
@Override
public Matcher<Object> isNull() {
return new BaseMatcher<Object>() {
@Override
public boolean matches(Object item) {
return item == null;
}
@Override
public void describeTo(Description description) {
description.appendText("is null");
}
};
}
@Override
public void describeTo(Description description) {
String expectedJson = convertObjectToJsonString(expectedEvent);
description.appendText("expected to look like " + expectedJson);
}
@Override
protected boolean matchesSafely(JsonNode jsonRegion, Description description) {
// check id
String jsonRegionId = jsonRegion.get("id").asText();
String regionId = region.id().toString();
if (!jsonRegionId.equals(regionId)) {
description.appendText("region id was " + jsonRegionId);
return false;
}
// check type
String jsonType = jsonRegion.get("type").asText();
String type = region.type().toString();
if (!jsonType.equals(type)) {
description.appendText("type was " + jsonType);
return false;
}
// check name
String jsonName = jsonRegion.get("name").asText();
String name = region.name();
if (!jsonName.equals(name)) {
description.appendText("name was " + jsonName);
return false;
}
// check size of master array
JsonNode jsonMasters = jsonRegion.get("masters");
if (jsonMasters.size() != region.masters().size()) {
description.appendText("masters size was " + jsonMasters.size());
return false;
}
// check master
for (Set<NodeId> set : region.masters()) {
boolean masterFound = false;
for (int masterIndex = 0; masterIndex < jsonMasters.size(); masterIndex++) {
masterFound = checkEquality(jsonMasters.get(masterIndex), set);
}
if (!masterFound) {
description.appendText("master not found " + set.toString());
return false;
}
}
return true;
}
@Override
public void describeTo(Description description) {
description.appendText(record.toString());
}
/**
* @see org.hamcrest.SelfDescribing#describeTo(org.hamcrest.Description)
*/
@Override
public void describeTo(Description description) {
description.appendText("Should have failed");
}
@Override
public void describeMismatch(Object item, Description mismatchDescription)
{
String differences = super.getDifferences();
mismatchDescription.appendText(null != differences ? differences : item.toString());
}
@Override
protected void describeMismatchSafely(Map<K, V> actual, Description mismatchDescription) {
mismatchDescription.appendText(String.valueOf(difference(actual, expected)));
}