Skip to content

Commit

Permalink
Migrate more tests to XProcessing testing APIs.
Browse files Browse the repository at this point in the history
This CL also adds golden files for SwitchingProvidersTest in default mode.

RELNOTES=N/A
PiperOrigin-RevId: 474382303
  • Loading branch information
bcorso authored and Dagger Team committed Sep 14, 2022
1 parent a15f8df commit e11fa81
Show file tree
Hide file tree
Showing 13 changed files with 973 additions and 50 deletions.
124 changes: 74 additions & 50 deletions javatests/dagger/internal/codegen/SwitchingProviderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,42 @@
import static com.google.testing.compile.CompilationSubject.assertThat;
import static dagger.internal.codegen.Compilers.compilerWithOptions;

import androidx.room.compiler.processing.util.Source;
import com.google.common.collect.ImmutableList;
import com.google.testing.compile.Compilation;
import com.google.testing.compile.Compiler;
import com.google.testing.compile.JavaFileObjects;
import dagger.testing.compile.CompilerTests;
import dagger.testing.golden.GoldenFileRule;
import javax.tools.JavaFileObject;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

@RunWith(JUnit4.class)
@RunWith(Parameterized.class)
public class SwitchingProviderTest {
@Parameters(name = "{0}")
public static ImmutableList<Object[]> parameters() {
return CompilerMode.TEST_PARAMETERS;
}

@Rule public GoldenFileRule goldenFileRule = new GoldenFileRule();

private final CompilerMode compilerMode;

public SwitchingProviderTest(CompilerMode compilerMode) {
this.compilerMode = compilerMode;
}

@Test
public void switchingProviderTest() throws Exception {
ImmutableList.Builder<JavaFileObject> javaFileObjects = ImmutableList.builder();
ImmutableList.Builder<Source> sources = ImmutableList.builder();
StringBuilder entryPoints = new StringBuilder();
for (int i = 0; i <= 100; i++) {
String bindingName = "Binding" + i;
javaFileObjects.add(
JavaFileObjects.forSourceLines(
sources.add(
CompilerTests.javaSource(
"test." + bindingName,
"package test;",
"",
Expand All @@ -55,8 +67,8 @@ public void switchingProviderTest() throws Exception {
entryPoints.append(String.format(" Provider<%1$s> get%1$sProvider();\n", bindingName));
}

javaFileObjects.add(
JavaFileObjects.forSourceLines(
sources.add(
CompilerTests.javaSource(
"test.TestComponent",
"package test;",
"",
Expand All @@ -68,13 +80,17 @@ public void switchingProviderTest() throws Exception {
entryPoints.toString(),
"}"));

Compilation compilation = compilerWithAndroidMode().compile(javaFileObjects.build());
assertThat(compilation).succeededWithoutWarnings();
assertThat(compilation)
.generatedSourceFile("test.DaggerTestComponent")
.hasSourceEquivalentTo(goldenFileRule.goldenFile("test.DaggerTestComponent"));
CompilerTests.daggerCompiler(sources.build())
.withProcessingOptions(compilerMode.processorOptions())
.compile(
subject -> {
subject.hasErrorCount(0);
subject.hasWarningCount(0);
subject.generatedSource(goldenFileRule.goldenSource("test/DaggerTestComponent"));
});
}

// TODO(b/231189307): Convert this to XProcessing testing APIs once erasure usage is fixed.
@Test
public void unscopedBinds() throws Exception {
JavaFileObject module =
Expand Down Expand Up @@ -110,13 +126,15 @@ public void unscopedBinds() throws Exception {
" Provider<CharSequence> charSequenceProvider();",
"}");

Compilation compilation = compilerWithAndroidMode().compile(module, component);
Compilation compilation =
compilerWithOptions(compilerMode.javacopts()).compile(module, component);
assertThat(compilation).succeeded();
assertThat(compilation)
.generatedSourceFile("test.DaggerTestComponent")
.hasSourceEquivalentTo(goldenFileRule.goldenFile("test.DaggerTestComponent"));
}

// TODO(b/231189307): Convert this to XProcessing testing APIs once erasure usage is fixed.
@Test
public void scopedBinds() throws Exception {
JavaFileObject module =
Expand Down Expand Up @@ -155,7 +173,8 @@ public void scopedBinds() throws Exception {
" Provider<CharSequence> charSequenceProvider();",
"}");

Compilation compilation = compilerWithAndroidMode().compile(module, component);
Compilation compilation =
compilerWithOptions(compilerMode.javacopts()).compile(module, component);
assertThat(compilation).succeeded();
assertThat(compilation)
.generatedSourceFile("test.DaggerTestComponent")
Expand All @@ -164,8 +183,8 @@ public void scopedBinds() throws Exception {

@Test
public void emptyMultibindings_avoidSwitchProviders() throws Exception {
JavaFileObject module =
JavaFileObjects.forSourceLines(
Source module =
CompilerTests.javaSource(
"test.TestModule",
"package test;",
"",
Expand All @@ -179,8 +198,8 @@ public void emptyMultibindings_avoidSwitchProviders() throws Exception {
" @Multibinds Set<String> set();",
" @Multibinds Map<String, String> map();",
"}");
JavaFileObject component =
JavaFileObjects.forSourceLines(
Source component =
CompilerTests.javaSource(
"test.TestComponent",
"package test;",
"",
Expand All @@ -195,23 +214,26 @@ public void emptyMultibindings_avoidSwitchProviders() throws Exception {
" Provider<Map<String, String>> mapProvider();",
"}");

Compilation compilation = compilerWithAndroidMode().compile(module, component);
assertThat(compilation).succeeded();
assertThat(compilation)
.generatedSourceFile("test.DaggerTestComponent")
.hasSourceEquivalentTo(goldenFileRule.goldenFile("test.DaggerTestComponent"));
CompilerTests.daggerCompiler(module, component)
.withProcessingOptions(compilerMode.processorOptions())
.compile(
subject -> {
subject.hasErrorCount(0);
subject.hasWarningCount(0);
subject.generatedSource(goldenFileRule.goldenSource("test/DaggerTestComponent"));
});
}

@Test
public void memberInjectors() throws Exception {
JavaFileObject foo =
JavaFileObjects.forSourceLines(
Source foo =
CompilerTests.javaSource(
"test.Foo",
"package test;",
"",
"class Foo {}");
JavaFileObject component =
JavaFileObjects.forSourceLines(
Source component =
CompilerTests.javaSource(
"test.TestComponent",
"package test;",
"",
Expand All @@ -224,29 +246,32 @@ public void memberInjectors() throws Exception {
" Provider<MembersInjector<Foo>> providerOfMembersInjector();",
"}");

Compilation compilation = compilerWithAndroidMode().compile(foo, component);
assertThat(compilation).succeeded();
assertThat(compilation)
.generatedSourceFile("test.DaggerTestComponent")
.hasSourceEquivalentTo(goldenFileRule.goldenFile("test.DaggerTestComponent"));
CompilerTests.daggerCompiler(foo, component)
.withProcessingOptions(compilerMode.processorOptions())
.compile(
subject -> {
subject.hasErrorCount(0);
subject.hasWarningCount(0);
subject.generatedSource(goldenFileRule.goldenSource("test/DaggerTestComponent"));
});
}

@Test
public void optionals() throws Exception {
JavaFileObject present =
JavaFileObjects.forSourceLines(
Source present =
CompilerTests.javaSource(
"test.Present",
"package test;",
"",
"class Present {}");
JavaFileObject absent =
JavaFileObjects.forSourceLines(
Source absent =
CompilerTests.javaSource(
"test.Absent",
"package test;",
"",
"class Absent {}");
JavaFileObject module =
JavaFileObjects.forSourceLines(
Source module =
CompilerTests.javaSource(
"test.TestModule",
"package test;",
"",
Expand All @@ -261,8 +286,8 @@ public void optionals() throws Exception {
"",
" @Provides static Present p() { return new Present(); }",
"}");
JavaFileObject component =
JavaFileObjects.forSourceLines(
Source component =
CompilerTests.javaSource(
"test.TestComponent",
"package test;",
"",
Expand All @@ -276,14 +301,13 @@ public void optionals() throws Exception {
" Provider<Optional<Absent>> providerOfOptionalOfAbsent();",
"}");

Compilation compilation = compilerWithAndroidMode().compile(present, absent, module, component);
assertThat(compilation).succeeded();
assertThat(compilation)
.generatedSourceFile("test.DaggerTestComponent")
.hasSourceEquivalentTo(goldenFileRule.goldenFile("test.DaggerTestComponent"));
}

private Compiler compilerWithAndroidMode() {
return compilerWithOptions(CompilerMode.FAST_INIT_MODE.javacopts());
CompilerTests.daggerCompiler(present, absent, module, component)
.withProcessingOptions(compilerMode.processorOptions())
.compile(
subject -> {
subject.hasErrorCount(0);
subject.hasWarningCount(0);
subject.generatedSource(goldenFileRule.goldenSource("test/DaggerTestComponent"));
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package test;

import dagger.internal.DaggerGenerated;
import dagger.internal.MapFactory;
import dagger.internal.SetFactory;
import java.util.Map;
import java.util.Set;
import javax.annotation.processing.Generated;
import javax.inject.Provider;

@DaggerGenerated
@Generated(
value = "dagger.internal.codegen.ComponentProcessor",
comments = "https://dagger.dev"
)
@SuppressWarnings({
"unchecked",
"rawtypes"
})
final class DaggerTestComponent {
private DaggerTestComponent() {
}

public static Builder builder() {
return new Builder();
}

public static TestComponent create() {
return new Builder().build();
}

static final class Builder {
private Builder() {
}

public TestComponent build() {
return new TestComponentImpl();
}
}

private static final class TestComponentImpl implements TestComponent {
private final TestComponentImpl testComponentImpl = this;

private TestComponentImpl() {


}

@Override
public Provider<Set<String>> setProvider() {
return SetFactory.<String>empty();
}

@Override
public Provider<Map<String, String>> mapProvider() {
return MapFactory.<String, String>emptyMapProvider();
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package test;

import dagger.MembersInjector;
import dagger.internal.DaggerGenerated;
import dagger.internal.InstanceFactory;
import dagger.internal.MembersInjectors;
import javax.annotation.processing.Generated;
import javax.inject.Provider;

@DaggerGenerated
@Generated(
value = "dagger.internal.codegen.ComponentProcessor",
comments = "https://dagger.dev"
)
@SuppressWarnings({
"unchecked",
"rawtypes"
})
final class DaggerTestComponent {
private DaggerTestComponent() {
}

public static Builder builder() {
return new Builder();
}

public static TestComponent create() {
return new Builder().build();
}

static final class Builder {
private Builder() {
}

public TestComponent build() {
return new TestComponentImpl();
}
}

private static final class TestComponentImpl implements TestComponent {
private final TestComponentImpl testComponentImpl = this;

private Provider<MembersInjector<Foo>> fooMembersInjectorProvider;

private TestComponentImpl() {

initialize();

}

@SuppressWarnings("unchecked")
private void initialize() {
this.fooMembersInjectorProvider = InstanceFactory.create(MembersInjectors.<Foo>noOp());
}

@Override
public Provider<MembersInjector<Foo>> providerOfMembersInjector() {
return fooMembersInjectorProvider;
}
}
}

0 comments on commit e11fa81

Please sign in to comment.