Skip to content

Commit

Permalink
added combined tests
Browse files Browse the repository at this point in the history
  • Loading branch information
armughan11 committed Dec 26, 2024
1 parent eee9014 commit ea27598
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions nullaway/src/test/java/com/uber/nullaway/EnsuresNonNullTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -398,4 +398,60 @@ public void requiresNonNullStaticFieldInterpretation() {
"Item.java", "package com.uber;", "class Item {", " public void call() { }", "}")
.doTest();
}

@Test
public void requiresAndEnsuresNonNullOnSameMethod() {
defaultCompilationHelper
.addSourceLines(
"Foo.java",
"package com.uber;",
"import javax.annotation.Nullable;",
"import com.uber.nullaway.annotations.RequiresNonNull;",
"import com.uber.nullaway.annotations.EnsuresNonNull;",
"class Foo {",
" @Nullable static Item field1;",
" @Nullable static Item field2;",
"",
" @RequiresNonNull(\"field1\")",
" @EnsuresNonNull(\"field2\")",
" public void positiveEnsureNonnull() {",
" field1.call(); ",
" field2 = new Item(); ",
" }",
" @RequiresNonNull(\"field1\")",
" @EnsuresNonNull(\"field2\")",
" // BUG: Diagnostic contains: Method is annotated with @EnsuresNonNull but fails to ensure the following fields are non-null at exit",
" public void negativeEnsureNonnull() {",
" field1.call(); ",
" }",
" @RequiresNonNull(\"field1\")",
" @EnsuresNonNull(\"field1\")",
" public void combinedPositive() {",
" field1.call(); ",
" }",
" @RequiresNonNull(\"field1\")",
" @EnsuresNonNull(\"field1\")",
" // BUG: Diagnostic contains: Method is annotated with @EnsuresNonNull but fails to ensure the following fields are non-null at exit",
" public void combinedNegative() {",
" field1= null; ",
" }",
"",
" public void positiveCase() {",
" field1 = new Item(); ",
" positiveEnsureNonnull(); ",
" combinedPositive(); ",
" field2.call(); ",
" }",
"",
" public void negativeCase() {",
" // BUG: Diagnostic contains: Expected static field field1 to be non-null at call site",
" negativeEnsureNonnull();",
" // BUG: Diagnostic contains: Expected static field field1 to be non-null at call site",
" combinedNegative();",
" }",
"}")
.addSourceLines(
"Item.java", "package com.uber;", "class Item {", " public void call() { }", "}")
.doTest();
}
}

0 comments on commit ea27598

Please sign in to comment.