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 { private List patterns; public ResultContainsOnlyTheseErrors(List 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 Matcher containsOnly(String type, String file, String[] methods) { ArrayList patterns = new ArrayList<>(); for (String method : methods) { patterns.add(new ErrorPattern(type, file, method)); } return new ResultContainsOnlyTheseErrors(patterns); } public static Matcher containsOnly(List patterns) { return new ResultContainsOnlyTheseErrors(patterns); } }