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
Changes from 1 commit
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
61 changes: 61 additions & 0 deletions nullaway/src/test/java/com/uber/nullaway/NullAwayTest.java
Expand Up @@ -1551,6 +1551,67 @@ 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: returning @Nullable expression from method with @NonNull",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This error message seems inappropriate, so need to make some changes in the code to have the correct error message.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comments on #307 and re-add this one plus that fix (assuming it works, of course)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done 👍

" .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() && 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