From f64be4596b46bcd2e8ff6694c71e6197ace16f5a Mon Sep 17 00:00:00 2001 From: Martino Luca Date: Mon, 1 Aug 2016 04:34:19 -0700 Subject: [PATCH] Implement containsOnlyLines matcher for tests Reviewed By: jvillard Differential Revision: D3648820 fbshipit-source-id: c4332f1 --- .../endtoend/c/infer/NullDereferenceTest.java | 6 +++--- .../endtoend/c/infer/OffsetOfExprTest.java | 4 ++-- .../tests/endtoend/c/infer/VAArgExprTest.java | 4 ++-- .../objc/infer/UpdateDictNPETest.java | 3 --- .../objc/linters/AssignPointerTest.java | 13 ++++++------ .../objc/linters/AtomicPropertyTest.java | 18 ++++++++-------- .../endtoend/objc/linters/NSNumber2Test.java | 4 ++-- .../endtoend/objc/linters/NSNumberTest.java | 4 ++-- .../objc/linters/RegisteredObserver3.java | 4 ++-- .../objc/linters/StrongDelegateTest.java | 4 ++-- .../linters/BlockCaptureCXXRefTest.java | 12 +++++------ .../objcpp/linters/GlobalVarTest.java | 4 ++-- .../matchers/ResultContainsLineNumbers.java | 21 ++++++++++++++----- 13 files changed, 55 insertions(+), 46 deletions(-) diff --git a/infer/tests/endtoend/c/infer/NullDereferenceTest.java b/infer/tests/endtoend/c/infer/NullDereferenceTest.java index a41b7df1a..6f625c833 100644 --- a/infer/tests/endtoend/c/infer/NullDereferenceTest.java +++ b/infer/tests/endtoend/c/infer/NullDereferenceTest.java @@ -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)); } } diff --git a/infer/tests/endtoend/c/infer/OffsetOfExprTest.java b/infer/tests/endtoend/c/infer/OffsetOfExprTest.java index 6450e55f5..1f3d5ffef 100644 --- a/infer/tests/endtoend/c/infer/OffsetOfExprTest.java +++ b/infer/tests/endtoend/c/infer/OffsetOfExprTest.java @@ -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})); } } diff --git a/infer/tests/endtoend/c/infer/VAArgExprTest.java b/infer/tests/endtoend/c/infer/VAArgExprTest.java index 6be0af71b..aaf259412 100644 --- a/infer/tests/endtoend/c/infer/VAArgExprTest.java +++ b/infer/tests/endtoend/c/infer/VAArgExprTest.java @@ -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})); } } diff --git a/infer/tests/endtoend/objc/infer/UpdateDictNPETest.java b/infer/tests/endtoend/objc/infer/UpdateDictNPETest.java index 1f39bdc01..732b159e4 100644 --- a/infer/tests/endtoend/objc/infer/UpdateDictNPETest.java +++ b/infer/tests/endtoend/objc/infer/UpdateDictNPETest.java @@ -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; diff --git a/infer/tests/endtoend/objc/linters/AssignPointerTest.java b/infer/tests/endtoend/objc/linters/AssignPointerTest.java index 8e53f980e..5718b5844 100644 --- a/infer/tests/endtoend/objc/linters/AssignPointerTest.java +++ b/infer/tests/endtoend/objc/linters/AssignPointerTest.java @@ -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 + })); } } diff --git a/infer/tests/endtoend/objc/linters/AtomicPropertyTest.java b/infer/tests/endtoend/objc/linters/AtomicPropertyTest.java index 54295e8e8..388522caf 100644 --- a/infer/tests/endtoend/objc/linters/AtomicPropertyTest.java +++ b/infer/tests/endtoend/objc/linters/AtomicPropertyTest.java @@ -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 + })); } } diff --git a/infer/tests/endtoend/objc/linters/NSNumber2Test.java b/infer/tests/endtoend/objc/linters/NSNumber2Test.java index 961f73491..9c2229b29 100644 --- a/infer/tests/endtoend/objc/linters/NSNumber2Test.java +++ b/infer/tests/endtoend/objc/linters/NSNumber2Test.java @@ -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})); } } diff --git a/infer/tests/endtoend/objc/linters/NSNumberTest.java b/infer/tests/endtoend/objc/linters/NSNumberTest.java index 0ccc97b8a..593fb1dfc 100644 --- a/infer/tests/endtoend/objc/linters/NSNumberTest.java +++ b/infer/tests/endtoend/objc/linters/NSNumberTest.java @@ -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}) ); } diff --git a/infer/tests/endtoend/objc/linters/RegisteredObserver3.java b/infer/tests/endtoend/objc/linters/RegisteredObserver3.java index 875c746e7..09268f159 100644 --- a/infer/tests/endtoend/objc/linters/RegisteredObserver3.java +++ b/infer/tests/endtoend/objc/linters/RegisteredObserver3.java @@ -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})); } } diff --git a/infer/tests/endtoend/objc/linters/StrongDelegateTest.java b/infer/tests/endtoend/objc/linters/StrongDelegateTest.java index 8c2aab24a..ef0e60fe1 100644 --- a/infer/tests/endtoend/objc/linters/StrongDelegateTest.java +++ b/infer/tests/endtoend/objc/linters/StrongDelegateTest.java @@ -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})); } } diff --git a/infer/tests/endtoend/objcpp/linters/BlockCaptureCXXRefTest.java b/infer/tests/endtoend/objcpp/linters/BlockCaptureCXXRefTest.java index 77012b949..0568db506 100644 --- a/infer/tests/endtoend/objcpp/linters/BlockCaptureCXXRefTest.java +++ b/infer/tests/endtoend/objcpp/linters/BlockCaptureCXXRefTest.java @@ -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 + })); } } diff --git a/infer/tests/endtoend/objcpp/linters/GlobalVarTest.java b/infer/tests/endtoend/objcpp/linters/GlobalVarTest.java index 8bf95ad74..59b125143 100644 --- a/infer/tests/endtoend/objcpp/linters/GlobalVarTest.java +++ b/infer/tests/endtoend/objcpp/linters/GlobalVarTest.java @@ -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})); } } diff --git a/infer/tests/utils/matchers/ResultContainsLineNumbers.java b/infer/tests/utils/matchers/ResultContainsLineNumbers.java index 4154c6325..0090cbabb 100644 --- a/infer/tests/utils/matchers/ResultContainsLineNumbers.java +++ b/infer/tests/utils/matchers/ResultContainsLineNumbers.java @@ -21,19 +21,26 @@ import utils.InferResults; public class ResultContainsLineNumbers extends BaseMatcher { - 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 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 { } public static Matcher containsLines(int[] lines) { - return new ResultContainsLineNumbers(lines); + return new ResultContainsLineNumbers(lines, false); + } + + public static Matcher containsOnlyLines(int[] lines) { + return new ResultContainsLineNumbers(lines, true); } private int[] findLineNumbersInReport(InferResults results) {