You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
2.1 KiB

package utils.matchers;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import java.util.ArrayList;
import java.util.List;
import utils.InferError;
import utils.InferResults;
public class ResultContainsOnlyTheseErrors extends BaseMatcher<InferResults> {
private List<ErrorPattern> patterns;
public ResultContainsOnlyTheseErrors(List<ErrorPattern> patterns) {
this.patterns = patterns;
}
private boolean mathTypeAndAnyMethod(InferError e) {
for (ErrorPattern pattern : patterns) {
if (pattern.match(e)) {
return true;
}
}
return false;
}
@Override
public boolean matches(Object o) {
InferResults results = (InferResults) o;
for (InferError error : results.getErrors()) {
if (!mathTypeAndAnyMethod(error)) {
return false;
}
}
return true;
}
@Override
public void describeTo(Description description) {
String errorsString = "";
for (ErrorPattern pattern : patterns) {
errorsString =
errorsString + "\t" + pattern.toString() + "\n";
}
description.appendText("Only these errors: \n" + errorsString);
}
@Override
public void describeMismatch(Object item, Description description) {
InferResults results = (InferResults) item;
String unexpectedErrors = "";
for (InferError error : results.getErrors()) {
if (!mathTypeAndAnyMethod(error)) {
unexpectedErrors = unexpectedErrors + "\t" + error + "\n";
}
}
description.appendText(
"Infer reported the following unexpected errors: \n" +
unexpectedErrors + "\n" + results.inferCmdToString());
}
public static <T> Matcher<InferResults> containsOnly(String type, String file, String[] methods) {
ArrayList<ErrorPattern> patterns = new ArrayList<>();
for (String method : methods) {
patterns.add(new ErrorPattern(type, file, method));
}
return new ResultContainsOnlyTheseErrors(patterns);
}
public static <T> Matcher<InferResults> containsOnly(List<ErrorPattern> patterns) {
return new ResultContainsOnlyTheseErrors(patterns);
}
}