-
Notifications
You must be signed in to change notification settings - Fork 300
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test case for unsound map reassignment handling (#541)
It might be useful to have a suite of test cases showing where NullAway is unsound, for documentation purposes. Here I just add one for re-assigning a `Map` variable, but we could easily add more in the future.
- Loading branch information
Showing
1 changed file
with
48 additions
and
0 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
nullaway/src/test/java/com/uber/nullaway/NullAwayUnsoundnessTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.uber.nullaway; | ||
|
||
import com.google.errorprone.CompilationTestHelper; | ||
import java.util.Arrays; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.TemporaryFolder; | ||
|
||
/** Unit tests showing cases where NullAway is unsound. Useful for documentation purposes. */ | ||
public class NullAwayUnsoundnessTests { | ||
|
||
@Rule public final TemporaryFolder temporaryFolder = new TemporaryFolder(); | ||
|
||
private CompilationTestHelper defaultCompilationHelper; | ||
|
||
@Before | ||
public void setup() { | ||
defaultCompilationHelper = | ||
CompilationTestHelper.newInstance(NullAway.class, getClass()) | ||
.setArgs( | ||
Arrays.asList( | ||
"-d", | ||
temporaryFolder.getRoot().getAbsolutePath(), | ||
"-XepOpt:NullAway:AnnotatedPackages=com.uber")); | ||
} | ||
|
||
@Test | ||
public void mapReassignUnsound() { | ||
defaultCompilationHelper | ||
.addSourceLines( | ||
"Test.java", | ||
"package com.uber;", | ||
"import java.util.*;", | ||
"public class Test {", | ||
" public void mapReassignUnsound(Map m, Object o) {", | ||
" if (m.containsKey(o)) {", | ||
" // NullAway is currently unsound for this case. It ignores", | ||
" // the re-assignment of m and still assumes m.get(o) is non-null", | ||
" // on the subsequent line.", | ||
" m = new HashMap();", | ||
" m.get(o).toString();", | ||
" }", | ||
" }", | ||
"}") | ||
.doTest(); | ||
} | ||
} |