下面列出了org.hamcrest.Description#appendValueList ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private boolean matchesQueryParameter(RecordedRequest item, Description mismatchDescription) {
HttpUrl requestUrl = item.getRequestUrl();
if (requestUrl.querySize() == 0) {
mismatchDescription.appendText(" query was empty");
return false;
}
String queryParamValue = requestUrl.queryParameter(first);
if (second.equals(queryParamValue)) {
return true;
}
mismatchDescription.appendValueList("Query parameters were {", ", ", "}.",
requestUrl.query().split("&"));
return false;
}
private boolean hasQueryParameter(RecordedRequest item, Description mismatchDescription) {
String path = item.getPath();
boolean hasQuery = path.indexOf("?") > 0;
if (!hasQuery) {
mismatchDescription.appendText(" query was empty");
return false;
}
String query = path.substring(path.indexOf("?") + 1, path.length());
String[] parameters = query.split("&");
for (String p : parameters) {
if (p.startsWith(String.format("%s=", first))) {
return true;
}
}
mismatchDescription.appendValueList("Query parameters were {", ", ", "}.", parameters);
return false;
}
@Override
protected boolean matchesSafely(List<String> items, Description mismatchDescriptor) {
int count = 0;
for (String item : items) {
if (stringMatcher.matches(item)) {
count++;
}
}
boolean okay = count == expectedCount;
if (!okay) {
mismatchDescriptor.appendText("was matched " + count + " times in the following list:");
String separator = String.format("%n") + " ";
mismatchDescriptor.appendValueList(separator, separator, "", items);
}
return okay;
}
public static Matcher<Git> hasTag(final String tag) {
return new TypeSafeDiagnosingMatcher<Git>() {
@Override
protected boolean matchesSafely(Git repo, Description mismatchDescription) {
try {
mismatchDescription.appendValueList("a git repo with tags: ", ", ", "", repo.getRepository().getTags().keySet());
return GitHelper.hasLocalTag(repo, tag);
} catch (Exception e) {
throw new RuntimeException("Couldn't access repo", e);
}
}
@Override
public void describeTo(Description description) {
description.appendText("a git repo with the tag " + tag);
}
};
}
public static Matcher<Git> hasCleanWorkingDirectory() {
return new TypeSafeDiagnosingMatcher<Git>() {
@Override
protected boolean matchesSafely(Git git, Description mismatchDescription) {
try {
Status status = git.status().call();
if (!status.isClean()) {
String start = "Uncommitted changes in ";
String end = " at " + git.getRepository().getWorkTree().getAbsolutePath();
mismatchDescription.appendValueList(start, ", ", end, status.getUncommittedChanges());
}
return status.isClean();
} catch (GitAPIException e) {
throw new RuntimeException("Error checking git status", e);
}
}
@Override
public void describeTo(Description description) {
description.appendText("A git directory with no staged or unstaged changes");
}
};
}
public <T> Matcher<Set<? super T>> are(final T... ts) {
final Collection<?> c1 = Arrays.asList(ts);
return new BaseMatcher<Set<? super T>>() {
public boolean matches(Object o) {
Collection<?> c2 = (Collection<?>) o;
return c1.containsAll(c2) && c2.containsAll(c1);
}
public void describeTo(Description description) {
description.appendText("elements: ");
description.appendValueList("(", ",", ")", ts);
}
};
}
@Override
public void describeTo(final Description description) {
description.appendText("UserException of type: ")
.appendValue(expectedType.toString());
if (expectedMessage != null) {
description.appendText(" with message that contains: \"")
.appendText(expectedMessage)
.appendText("\"");
}
if (expectedContexts != null) {
description.appendValueList(" with contexts that contain every one of {", ",", "}", expectedContexts);
}
}
@Override
protected void describeMismatchSafely(final UserException e, final Description description) {
description.appendText("UserException thrown was of type: ")
.appendValue(e.getErrorType().toString());
if (expectedMessage != null) {
description.appendText(" with message: \"")
.appendText(e.getMessage())
.appendText("\"");
}
if (expectedContexts != null) {
description.appendValueList(" with contexts {", ",", "}", e.getContextStrings());
}
}
@Override
@SuppressWarnings("unchecked")
public void describeMismatch(Object item, Description description) {
describeTo(description);
description.appendValueList(" but actual event reasons were {", ", ", "}.",
((List<Event>) item).stream().map(evt -> {
String objRef = "";
if (evt.getInvolvedObject() != null) {
objRef = " involved object: " + evt.getInvolvedObject().getKind() + "/" + evt.getInvolvedObject().getName();
}
return evt.getReason() + " (" + evt.getType() + " " + evt.getMessage() + objRef + ")\n";
})
.collect(Collectors.toList()));
}
private void context(String sourceCode, String methodName, Description mismatchDescription) {
if (!sourceCode.contains(methodName)) {
mismatchDescription.appendText("You did not appear to invoke the method at all.");
} else {
String[] lines = sourceCode.split("\\n");
mismatchDescription.appendText("Actual invocations: ");
mismatchDescription.appendValueList("[", ",", "]",
Arrays.stream(lines).filter(l -> l.contains(methodName)).map(String::trim).collect(toList()));
}
}
@Override
protected void describeMismatchSafely(WebDriver item, Description mismatchDescription) {
mismatchDescription.appendText("was only ");
List<String> cookNames = new LinkedList<>();
for (Cookie cook : item.manage().getCookies()) {
cookNames.add(cook.getName());
}
mismatchDescription.appendValueList("[", ", ", "]", cookNames);
}
@Override
public void describeTo(Description description) {
description.appendValueList("<[", ", ", "]>", executionStages);
}
@Override
public void describeTo(Description description) {
description.appendValueList("[", ",", "]", this.substrings);
}
@Override
protected void describeMismatchSafely(Collection<DockerPort> unavailablePorts, Description mismatchDescription) {
mismatchDescription.appendValueList("These ports were unavailable:\n", "\n", ".", buildClosedPortsErrorMessage(unavailablePorts));
}
@Override
public void describeTo(Description description) {
description.appendValueList("The resource should contain at least one the following events {", ", ", "}. ", eventReasons);
}
@Override
public void describeTo(Description description) {
description.appendValueList("The resource should contain all of the following events {", ", ", "}. ", eventReasons);
}
private void appendCollection(Description description, Collection<?> collection) {
description.appendValueList("{", ", ", "}", elementsToStringSorted(collection));
}
@Override
public void describeTo(Description description) {
description.appendValueList("a collection which contains ", ", ", ".", items);
}
@Override
public void describeTo(Description description) {
description.appendValueList("a collection which contains ", ", ", ".", items);
}
/**
* Generates a description of the object. The description may be part of a
* a description of a larger object of which this is just a component, so it
* should be worded appropriately.
*
* @param description The description to be built or appended to.
*/
@Override
public void describeTo(Description description) {
description.appendValueList("The resource should not contain no events with any of the reasons {", ", ", "}, ",
prohibitedEvents);
}