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

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

@ -10,7 +10,7 @@
package endtoend.c;
import static org.hamcrest.MatcherAssert.assertThat;
import static utils.matchers.ResultContainsLineNumbers.containsLines;
import static utils.matchers.ResultContainsLineNumbers.containsOnlyLines;
import com.google.common.collect.ImmutableList;
@ -53,7 +53,7 @@ public class VAArgExprTest {
assertThat(
"Results should contain the correct " + DIVIDE_BY_ZERO,
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 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;

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

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

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

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

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

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

@ -10,7 +10,7 @@
package endtoend.objcpp.linters;
import static org.hamcrest.MatcherAssert.assertThat;
import static utils.matchers.ResultContainsLineNumbers.containsLines;
import static utils.matchers.ResultContainsLineNumbers.containsOnlyLines;
import com.google.common.collect.ImmutableList;
@ -53,7 +53,7 @@ public class GlobalVarTest {
assertThat(
"Results should contain " + GLOBAL_VARIABLE_INITIALIZED_WITH_FUNCTION_OR_METHOD_CALL,
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> {
int[] lines;
private int[] lines;
private boolean strict;
public ResultContainsLineNumbers(int[] lines) {
public ResultContainsLineNumbers(
int[] lines,
boolean strict) {
this.lines = lines;
this.strict = strict;
}
@Override
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;
for (int line : lines) {
boolean isContained = false;
for (InferError error : results.getErrors()) {
for (InferError error : errors) {
isContained = isContained || line == error.getErrorLine();
}
allContained = allContained && isContained;
@ -58,7 +65,11 @@ public class ResultContainsLineNumbers extends BaseMatcher<InferResults> {
}
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) {

Loading…
Cancel
Save