Skip to content

Commit

Permalink
Cleanup dagger/functional/nullables tests.
Browse files Browse the repository at this point in the history
This CL moves all classes related to NullabilityTest into the test itself so that all necessary dependencies are encapsulated within the test. This helps readability when there are multiple tests in package so that you don't have to figure out which sources are used by which test.

RELNOTES=N/A
PiperOrigin-RevId: 506516080
  • Loading branch information
bcorso authored and Dagger Team committed Feb 2, 2023
1 parent 757d709 commit 9c1fd05
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 194 deletions.
10 changes: 2 additions & 8 deletions javatests/dagger/functional/nullables/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,11 @@ load("//:test_defs.bzl", "GenJavaTests")
package(default_visibility = ["//:src"])

GenJavaTests(
name = "nullables",
srcs = glob(["*.java"]),
name = "NullabilityTest",
srcs = ["NullabilityTest.java"],
javacopts = DOCLINT_HTML_AND_SYNTAX + DOCLINT_REFERENCES,
deps = [
"//:dagger_with_compiler",
"//third_party/java/auto:factory",
"//third_party/java/guava/base",
"//third_party/java/guava/collect",
"//third_party/java/guava/util/concurrent",
"//third_party/java/jsr305_annotations",
"//third_party/java/jsr330_inject",
"//third_party/java/junit",
"//third_party/java/truth",
],
Expand Down
30 changes: 0 additions & 30 deletions javatests/dagger/functional/nullables/NullComponent.java

This file was deleted.

This file was deleted.

57 changes: 0 additions & 57 deletions javatests/dagger/functional/nullables/NullFoo.java

This file was deleted.

46 changes: 0 additions & 46 deletions javatests/dagger/functional/nullables/NullModule.java

This file was deleted.

109 changes: 103 additions & 6 deletions javatests/dagger/functional/nullables/NullabilityTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,108 @@
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.fail;

import dagger.Component;
import dagger.Module;
import dagger.Provides;
import dagger.Reusable;
import javax.inject.Inject;
import javax.inject.Provider;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public class NullabilityTest {
@Test public void testNullability_provides() {
@interface Nullable {}

@Component(dependencies = NullComponent.class)
interface NullComponentWithDependency {
@Nullable String string();
Number number();
Provider<String> stringProvider();
Provider<Number> numberProvider();
}

@Component(modules = NullModule.class)
interface NullComponent {
@Nullable String string();
@Nullable Integer integer();
NullFoo nullFoo();
Number number();
Provider<String> stringProvider();
Provider<Number> numberProvider();
}

@Module
static class NullModule {
Number numberValue = null;
Integer integerCallCount = 0;

@Nullable
@Provides
String provideNullableString() {
return null;
}

@Provides
Number provideNumber() {
return numberValue;
}

@Nullable
@Provides
@Reusable
Integer provideNullReusableInteger() {
integerCallCount++;
return null;
}
}

@SuppressWarnings("BadInject") // This is just for testing purposes.
static class NullFoo {
final String string;
final Number number;
final Provider<String> stringProvider;
final Provider<Number> numberProvider;

@Inject
NullFoo(
@Nullable String string,
Number number,
Provider<String> stringProvider,
Provider<Number> numberProvider) {
this.string = string;
this.number = number;
this.stringProvider = stringProvider;
this.numberProvider = numberProvider;
}

String methodInjectedString;
Number methodInjectedNumber;
Provider<String> methodInjectedStringProvider;
Provider<Number> methodInjectedNumberProvider;
@Inject void inject(
@Nullable String string,
Number number,
Provider<String> stringProvider,
Provider<Number> numberProvider) {
this.methodInjectedString = string;
this.methodInjectedNumber = number;
this.methodInjectedStringProvider = stringProvider;
this.methodInjectedNumberProvider = numberProvider;
}

@Nullable @Inject String fieldInjectedString;
@Inject Number fieldInjectedNumber;
@Inject Provider<String> fieldInjectedStringProvider;
@Inject Provider<Number> fieldInjectedNumberProvider;
}

@Test
public void testNullability_provides() {
NullModule module = new NullModule();
NullComponent component = DaggerNullComponent.builder().nullModule(module).build();
NullComponent component =
DaggerNullabilityTest_NullComponent.builder().nullModule(module).build();

// Can't construct NullFoo because it depends on Number, and Number was null.
try {
Expand All @@ -53,9 +145,11 @@ public class NullabilityTest {
nullFoo.fieldInjectedNumberProvider);
}

@Test public void testNullability_reusuable() {
@Test
public void testNullability_reusuable() {
NullModule module = new NullModule();
NullComponent component = DaggerNullComponent.builder().nullModule(module).build();
NullComponent component =
DaggerNullabilityTest_NullComponent.builder().nullModule(module).build();

// Test that the @Nullable @Reusuable binding is cached properly even when the value is null.
assertThat(module.integerCallCount).isEqualTo(0);
Expand All @@ -65,7 +159,8 @@ public class NullabilityTest {
assertThat(module.integerCallCount).isEqualTo(1);
}

@Test public void testNullability_components() {
@Test
public void testNullability_components() {
NullComponent nullComponent = new NullComponent() {
@Override public Provider<String> stringProvider() {
return new Provider<String>() {
Expand Down Expand Up @@ -100,7 +195,9 @@ public class NullabilityTest {
}
};
NullComponentWithDependency component =
DaggerNullComponentWithDependency.builder().nullComponent(nullComponent).build();
DaggerNullabilityTest_NullComponentWithDependency.builder()
.nullComponent(nullComponent)
.build();
validate(false, component.string(), component.stringProvider(), component.numberProvider());

// Also validate that the component's number() method fails
Expand Down
19 changes: 0 additions & 19 deletions javatests/dagger/functional/nullables/Nullable.java

This file was deleted.

0 comments on commit 9c1fd05

Please sign in to comment.