Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added tests for optional emptiness support with Rx #308

Merged
merged 2 commits into from Apr 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 7 additions & 5 deletions nullaway/src/main/java/com/uber/nullaway/NullAway.java
Expand Up @@ -639,12 +639,14 @@ private Description checkReturnExpression(
return Description.NO_MATCH;
}
if (mayBeNullExpr(state, retExpr)) {
String message = "returning @Nullable expression from method with @NonNull return type";
final ErrorMessage errorMessage =
new ErrorMessage(
MessageTypes.RETURN_NULLABLE,
"returning @Nullable expression from method with @NonNull return type");
handler.onPrepareErrorMessage(retExpr, state, errorMessage);

return errorBuilder.createErrorDescriptionForNullAssignment(
new ErrorMessage(MessageTypes.RETURN_NULLABLE, message),
retExpr,
state.getPath(),
buildDescription(tree));
errorMessage, retExpr, state.getPath(), buildDescription(tree));
}
return Description.NO_MATCH;
}
Expand Down
64 changes: 64 additions & 0 deletions nullaway/src/test/java/com/uber/nullaway/NullAwayTest.java
Expand Up @@ -1551,6 +1551,70 @@ public void OptionalEmptinessUncheckedTest() {
.doTest();
}

@Test
public void OptionalEmptinessRxPositiveTest() {
compilationHelper
.setArgs(
Arrays.asList(
"-d",
temporaryFolder.getRoot().getAbsolutePath(),
"-XepOpt:NullAway:AnnotatedPackages=com.uber,io.reactivex",
"-XepOpt:NullAway:UnannotatedSubPackages=com.uber.lib.unannotated",
"-XepOpt:NullAway:CheckOptionalEmptiness=true"))
.addSourceLines(
"TestPositive.java",
"package com.uber;",
"import java.util.Optional;",
"import io.reactivex.Observable;",
"public class TestPositive {",
" private static boolean perhaps() { return Math.random() > 0.5; }",
" void foo(Observable<Optional<String>> observable) {",
" observable",
" .filter(optional -> optional.isPresent() || perhaps())",
" // BUG: Diagnostic contains: Optional optional can be empty",
" .map(optional -> optional.get().toString());",
" observable",
" .filter(optional -> optional.isPresent() || perhaps())",
" // BUG: Diagnostic contains: Optional optional can be empty",
" .map(optional -> optional.get())",
" .map(irr -> irr.toString());",
" }",
"}")
.doTest();
}

@Test
public void OptionalEmptinessRxNegativeTest() {
compilationHelper
.setArgs(
Arrays.asList(
"-d",
temporaryFolder.getRoot().getAbsolutePath(),
"-XepOpt:NullAway:AnnotatedPackages=com.uber",
"-XepOpt:NullAway:UnannotatedSubPackages=com.uber.lib.unannotated",
"-XepOpt:NullAway:CheckOptionalEmptiness=true"))
.addSourceLines(
"TestNegative.java",
"package com.uber;",
"import java.util.Optional;",
"import io.reactivex.Observable;",
"public class TestNegative {",
" private static boolean perhaps() { return Math.random() > 0.5; }",
" void foo(Observable<Optional<String>> observable) {",
" observable",
" .filter(optional -> optional.isPresent())",
" .map(optional -> optional.get().toString());",
" observable",
" .filter(optional -> optional.isPresent() && perhaps())",
shubhamugare marked this conversation as resolved.
Show resolved Hide resolved
" .map(optional -> optional.get().toString());",
" observable",
" .filter(optional -> optional.isPresent() && perhaps())",
" .map(optional -> optional.get());",
" }",
"}")
.doTest();
}

@Test
public void testCastToNonNull() {
compilationHelper
Expand Down