Skip to content

Commit 80fbdc9

Browse files
authored
Refactor ContractsTests to use Java text blocks (#1435)
Following up on my previous PR and the goal of issue #1425, I have refactored ContractsTests.java. - Migrated test data from external files to inline text blocks using addSourceLines. - Deleted physical test files: CheckContractPositiveCases.java and CheckContractNegativeCases.java. - Removed unnecessary @SuppressWarnings("deprecation") annotations. - Part of the ongoing effort to modernize tests (#1425). Thank you for contributing to NullAway! Please note that once you click "Create Pull Request" you will be asked to sign our [Uber Contributor License Agreement](https://cla-assistant.io/uber/NullAway) via [CLA assistant](https://cla-assistant.io/). Before pressing the "Create Pull Request" button, please provide the following: - [x] A description about what and why you are contributing, even if it's trivial. - [x] The issue number(s) or PR number(s) in the description if you are contributing in response to those. - [x] If applicable, unit tests. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Tests** * Consolidated external test-data into inline test cases within the test suite for simpler maintenance and clearer test flow. * Removed separate public test-data classes that previously existed as external resources. * Dropped a deprecation-suppression annotation from the public test class to reflect current warnings handling. <sub>✏️ Tip: You can customize this high-level summary in your review settings.</sub> <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent 4b4d1e8 commit 80fbdc9

File tree

3 files changed

+88
-140
lines changed

3 files changed

+88
-140
lines changed

nullaway/src/test/java/com/uber/nullaway/ContractsTests.java

Lines changed: 88 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import java.util.Arrays;
44
import org.junit.Test;
55

6-
@SuppressWarnings("deprecation")
76
public class ContractsTests extends NullAwayTestsBase {
87

98
@Test
@@ -14,7 +13,63 @@ public void checkContractPositiveCases() {
1413
temporaryFolder.getRoot().getAbsolutePath(),
1514
"-XepOpt:NullAway:AnnotatedPackages=com.uber",
1615
"-XepOpt:NullAway:CheckContracts=true"))
17-
.addSourceFile("testdata/CheckContractPositiveCases.java")
16+
.addSourceLines(
17+
"CheckContractPositiveCases.java",
18+
"""
19+
package com.uber;
20+
import javax.annotation.Nullable;
21+
import org.jetbrains.annotations.Contract;
22+
public class CheckContractPositiveCases {
23+
@Contract("_, !null -> !null")
24+
@Nullable
25+
Object foo(Object a, @Nullable Object b) {
26+
if (a.hashCode() % 2 == 0) {
27+
// BUG: Diagnostic contains: Method foo has @Contract
28+
return null;
29+
}
30+
return new Object();
31+
}
32+
@Contract("_, !null -> !null")
33+
@Nullable
34+
Object fooTwo(Object a, @Nullable Object b) {
35+
if (b != null) {
36+
// BUG: Diagnostic contains: Method fooTwo has @Contract(_, !null -> !null), but this appears
37+
// to be violated, as a @Nullable value may be returned when parameter b is non-null.
38+
return null;
39+
}
40+
return new Object();
41+
}
42+
@Contract("_, !null, _ -> !null")
43+
@Nullable
44+
Object fooThree(Object a, @Nullable Object b, Object c) {
45+
if (b != null) {
46+
// BUG: Diagnostic contains: Method fooThree has @Contract(_, !null, _ -> !null), but this
47+
// appears to be violated, as a @Nullable value may be returned when parameter b is non-null.
48+
return null;
49+
}
50+
return new Object();
51+
}
52+
@Contract("_, !null, !null, _ -> !null")
53+
@Nullable
54+
Object fooFour(Object a, @Nullable Object b, Object c, Object d) {
55+
if (b != null) {
56+
// BUG: Diagnostic contains: Method fooFour has @Contract(_, !null, !null, _ -> !null), but
57+
// this appears to be violated, as a @Nullable value may be returned when the contract
58+
// preconditions are true.
59+
return null;
60+
}
61+
return new Object();
62+
}
63+
@Nullable Object value = null;
64+
@Contract("_ -> !null")
65+
public @Nullable Object orElse(@Nullable Object other) {
66+
// Both contract and method signature assume 'other' is NULLABLE
67+
// BUG: Diagnostic contains: Method orElse has @Contract(_ -> !null), but this appears to be
68+
// violated, as a @Nullable value may be returned when the contract preconditions are true.
69+
return value != null ? value : other;
70+
}
71+
}
72+
""")
1873
.doTest();
1974
}
2075

@@ -54,7 +109,37 @@ public void checkContractNegativeCases() {
54109
temporaryFolder.getRoot().getAbsolutePath(),
55110
"-XepOpt:NullAway:AnnotatedPackages=com.uber",
56111
"-XepOpt:NullAway:CheckContracts=true"))
57-
.addSourceFile("testdata/CheckContractNegativeCases.java")
112+
.addSourceLines(
113+
"CheckContractNegativeCases.java",
114+
"""
115+
package com.uber;
116+
import javax.annotation.Nullable;
117+
import org.jetbrains.annotations.Contract;
118+
public class CheckContractNegativeCases {
119+
@Contract("_, !null -> !null")
120+
@Nullable
121+
Object foo(Object a, @Nullable Object b) {
122+
if (a.hashCode() % 2 == 0) {
123+
return b;
124+
}
125+
return new Object();
126+
}
127+
@Contract("_, !null -> !null")
128+
@Nullable
129+
Object fooTwo(Object a, @Nullable Object b) {
130+
if (b != null) {
131+
return b;
132+
}
133+
return new Object();
134+
}
135+
@Nullable Object value = null;
136+
@Contract("_ -> !null")
137+
public @Nullable Object orElse(Object other) {
138+
// 'other' is assumed to NONNULL using the information from method signature
139+
return value != null ? value : other;
140+
}
141+
}
142+
""")
58143
.doTest();
59144
}
60145

nullaway/src/test/resources/com/uber/nullaway/testdata/CheckContractNegativeCases.java

Lines changed: 0 additions & 55 deletions
This file was deleted.

nullaway/src/test/resources/com/uber/nullaway/testdata/CheckContractPositiveCases.java

Lines changed: 0 additions & 82 deletions
This file was deleted.

0 commit comments

Comments
 (0)