Implement containsOnlyLines matcher for tests

Reviewed By: jvillard

Differential Revision: D3648820

fbshipit-source-id: c4332f1
master
Martino Luca 8 years ago committed by Facebook Github Bot 2
parent aed5e6bee8
commit f64be4596b

@ -10,8 +10,8 @@
package endtoend.c.infer; package endtoend.c.infer;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static utils.matchers.ResultContainsLineNumbers.containsLines;
import static utils.matchers.ResultContainsExactly.containsExactly; import static utils.matchers.ResultContainsExactly.containsExactly;
import static utils.matchers.ResultContainsLineNumbers.containsOnlyLines;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
@ -65,11 +65,11 @@ public class NullDereferenceTest {
@Test @Test
public void whenInferRunsOnSimpleNpe_interprocThenCorrectLineIsReported() public void whenInferRunsOnSimpleNpe_interprocThenCorrectLineIsReported()
throws InterruptedException, IOException, InferException { throws InterruptedException, IOException, InferException {
int[] lines = {20, 32}; int[] lines = {20, 32, 49, 60, 66, 79, 88, 93, 122};
assertThat( assertThat(
"Results should contain null pointer dereference error", "Results should contain null pointer dereference error",
inferResults, inferResults,
containsLines(lines)); containsOnlyLines(lines));
} }
} }

@ -10,7 +10,7 @@
package endtoend.c; package endtoend.c;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static utils.matchers.ResultContainsLineNumbers.containsLines; import static utils.matchers.ResultContainsLineNumbers.containsOnlyLines;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
@ -53,7 +53,7 @@ public class OffsetOfExprTest {
assertThat( assertThat(
"Results should contain the correct " + DIVIDE_BY_ZERO, "Results should contain the correct " + DIVIDE_BY_ZERO,
inferResults, inferResults,
containsLines(new int[] {21, 23})); containsOnlyLines(new int[]{21, 23}));
} }
} }

@ -10,7 +10,7 @@
package endtoend.c; package endtoend.c;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static utils.matchers.ResultContainsLineNumbers.containsLines; import static utils.matchers.ResultContainsLineNumbers.containsOnlyLines;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
@ -53,7 +53,7 @@ public class VAArgExprTest {
assertThat( assertThat(
"Results should contain the correct " + DIVIDE_BY_ZERO, "Results should contain the correct " + DIVIDE_BY_ZERO,
inferResults, inferResults,
containsLines(new int[] {18, 20})); containsOnlyLines(new int[]{18, 20}));
} }
} }

@ -11,9 +11,6 @@ package endtoend.objc.infer;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static utils.matchers.ResultContainsExactly.containsExactly; import static utils.matchers.ResultContainsExactly.containsExactly;
import static utils.matchers.ResultContainsLineNumbers.containsLines;
import static utils.matchers.ResultContainsNoErrorInMethod.doesNotContain;
import static utils.matchers.ResultContainsErrorInMethod.contains;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;

@ -10,7 +10,7 @@
package endtoend.objc.linters; package endtoend.objc.linters;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static utils.matchers.ResultContainsLineNumbers.containsLines; import static utils.matchers.ResultContainsLineNumbers.containsOnlyLines;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
@ -55,11 +55,12 @@ public class AssignPointerTest {
assertThat( assertThat(
"Results should contain the correct " + ASSIGN_POINTER_WARNING, "Results should contain the correct " + ASSIGN_POINTER_WARNING,
inferResults, inferResults,
containsLines(new int[]{ containsOnlyLines(
18, new int[]{
20, 18,
22 20,
})); 22
}));
} }
} }

@ -10,8 +10,7 @@
package endtoend.objc.linters; package endtoend.objc.linters;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static utils.matchers.ResultContainsExactly.containsExactly; import static utils.matchers.ResultContainsLineNumbers.containsOnlyLines;
import static utils.matchers.ResultContainsLineNumbers.containsLines;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
@ -53,13 +52,14 @@ public class AtomicPropertyTest {
assertThat( assertThat(
"Results should contain the correct " + DIRECT_ATOMIC_PROPERTY_ACCESS, "Results should contain the correct " + DIRECT_ATOMIC_PROPERTY_ACCESS,
inferResults, inferResults,
containsLines(new int[]{ containsOnlyLines(
77, new int[]{
82, 77,
86, 82,
98, 86,
99 98,
})); 99
}));
} }
} }

@ -10,7 +10,7 @@
package endtoend.objc.linters; package endtoend.objc.linters;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static utils.matchers.ResultContainsLineNumbers.containsLines; import static utils.matchers.ResultContainsLineNumbers.containsOnlyLines;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
@ -51,7 +51,7 @@ public class NSNumber2Test {
assertThat( assertThat(
"Results should contain " + BAD_POINTER_COMPARISON, "Results should contain " + BAD_POINTER_COMPARISON,
inferResults, inferResults,
containsLines(new int[] {17, 26, 33})); containsOnlyLines(new int[]{17, 26, 33}));
} }
} }

@ -10,7 +10,7 @@
package endtoend.objc.linters; package endtoend.objc.linters;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static utils.matchers.ResultContainsLineNumbers.containsLines; import static utils.matchers.ResultContainsLineNumbers.containsOnlyLines;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
@ -51,7 +51,7 @@ public class NSNumberTest {
assertThat( assertThat(
"Results should contain " + BAD_POINTER_COMPARISON, "Results should contain " + BAD_POINTER_COMPARISON,
inferResults, inferResults,
containsLines(new int[]{13, 18, 23, 43}) containsOnlyLines(new int[]{13, 18, 23, 43})
); );
} }

@ -10,7 +10,7 @@
package endtoend.objc.linters; package endtoend.objc.linters;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static utils.matchers.ResultContainsLineNumbers.containsLines; import static utils.matchers.ResultContainsLineNumbers.containsOnlyLines;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
@ -56,6 +56,6 @@ public class RegisteredObserver3 {
assertThat( assertThat(
"Results should contain " + REGISTERED_OBSERVER, "Results should contain " + REGISTERED_OBSERVER,
inferResults, inferResults,
containsLines(new int[]{14})); containsOnlyLines(new int[]{14}));
} }
} }

@ -10,7 +10,7 @@
package endtoend.objc.linters; package endtoend.objc.linters;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static utils.matchers.ResultContainsLineNumbers.containsLines; import static utils.matchers.ResultContainsLineNumbers.containsOnlyLines;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
@ -52,7 +52,7 @@ public class StrongDelegateTest {
assertThat( assertThat(
"Results should contain " + STRONG_DELEGATE_WARNING, "Results should contain " + STRONG_DELEGATE_WARNING,
inferResults, inferResults,
containsLines(new int[]{15, 19, 21, 23, 25})); containsOnlyLines(new int[]{15, 19, 21, 23, 25}));
} }
} }

@ -10,8 +10,7 @@
package endtoend.objcpp.linters; package endtoend.objcpp.linters;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static utils.matchers.ResultContainsExactly.containsExactly; import static utils.matchers.ResultContainsLineNumbers.containsOnlyLines;
import static utils.matchers.ResultContainsLineNumbers.containsLines;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
@ -51,9 +50,10 @@ public class BlockCaptureCXXRefTest {
assertThat( assertThat(
"Results should contain the correct " + REFERENCE_CAPTURED, "Results should contain the correct " + REFERENCE_CAPTURED,
inferResults, inferResults,
containsLines(new int[]{ containsOnlyLines(
20, new int[]{
37 20,
})); 37
}));
} }
} }

@ -10,7 +10,7 @@
package endtoend.objcpp.linters; package endtoend.objcpp.linters;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static utils.matchers.ResultContainsLineNumbers.containsLines; import static utils.matchers.ResultContainsLineNumbers.containsOnlyLines;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
@ -53,7 +53,7 @@ public class GlobalVarTest {
assertThat( assertThat(
"Results should contain " + GLOBAL_VARIABLE_INITIALIZED_WITH_FUNCTION_OR_METHOD_CALL, "Results should contain " + GLOBAL_VARIABLE_INITIALIZED_WITH_FUNCTION_OR_METHOD_CALL,
inferResults, inferResults,
containsLines(new int[]{30, 32, 34})); containsOnlyLines(new int[]{30, 32, 34}));
} }
} }

@ -21,19 +21,26 @@ import utils.InferResults;
public class ResultContainsLineNumbers extends BaseMatcher<InferResults> { public class ResultContainsLineNumbers extends BaseMatcher<InferResults> {
int[] lines; private int[] lines;
private boolean strict;
public ResultContainsLineNumbers(int[] lines) { public ResultContainsLineNumbers(
int[] lines,
boolean strict) {
this.lines = lines; this.lines = lines;
this.strict = strict;
} }
@Override @Override
public boolean matches(Object o) { public boolean matches(Object o) {
InferResults results = (InferResults) o; Vector<InferError> errors = ((InferResults) o).getErrors();
if (strict && lines.length != errors.size()) {
return false;
}
boolean allContained = true; boolean allContained = true;
for (int line : lines) { for (int line : lines) {
boolean isContained = false; boolean isContained = false;
for (InferError error : results.getErrors()) { for (InferError error : errors) {
isContained = isContained || line == error.getErrorLine(); isContained = isContained || line == error.getErrorLine();
} }
allContained = allContained && isContained; allContained = allContained && isContained;
@ -58,7 +65,11 @@ public class ResultContainsLineNumbers extends BaseMatcher<InferResults> {
} }
public static <T> Matcher<InferResults> containsLines(int[] lines) { public static <T> Matcher<InferResults> containsLines(int[] lines) {
return new ResultContainsLineNumbers(lines); return new ResultContainsLineNumbers(lines, false);
}
public static <T> Matcher<InferResults> containsOnlyLines(int[] lines) {
return new ResultContainsLineNumbers(lines, true);
} }
private int[] findLineNumbersInReport(InferResults results) { private int[] findLineNumbersInReport(InferResults results) {

Loading…
Cancel
Save