Skip to content

Commit

Permalink
Add test case for unsound map reassignment handling (#541)
Browse files Browse the repository at this point in the history
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
msridhar authored Jan 6, 2022
1 parent 6c66a93 commit bde8d50
Showing 1 changed file with 48 additions and 0 deletions.
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();
}
}

0 comments on commit bde8d50

Please sign in to comment.