Skip to content

Commit

Permalink
Migrate more tests to XProcessing testing APIs.
Browse files Browse the repository at this point in the history
RELNOTES=N/A
PiperOrigin-RevId: 474870628
  • Loading branch information
bcorso authored and Dagger Team committed Sep 16, 2022
1 parent 1099dd4 commit bc94a5a
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 80 deletions.
3 changes: 2 additions & 1 deletion java/dagger/internal/codegen/base/MapType.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.TypeName;
import dagger.internal.codegen.javapoet.TypeNames;
import dagger.internal.codegen.xprocessing.XTypes;
import dagger.spi.model.Key;

/** Information about a {@link java.util.Map} type. */
Expand All @@ -43,7 +44,7 @@ private XType type() {

/** {@code true} if the map type is the raw {@link java.util.Map} type. */
public boolean isRawType() {
return type().getTypeArguments().isEmpty();
return XTypes.isRawParameterizedType(type());
}

/**
Expand Down
3 changes: 2 additions & 1 deletion java/dagger/internal/codegen/base/SetType.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.TypeName;
import dagger.internal.codegen.javapoet.TypeNames;
import dagger.internal.codegen.xprocessing.XTypes;
import dagger.spi.model.Key;

/** Information about a {@link java.util.Set} type. */
Expand All @@ -42,7 +43,7 @@ private XType type() {

/** {@code true} if the set type is the raw {@link java.util.Set} type. */
public boolean isRawType() {
return type().getTypeArguments().isEmpty();
return XTypes.isRawParameterizedType(type());
}

/** Returns the element type. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.google.common.util.concurrent.ListenableFuture;
import dagger.internal.codegen.binding.InjectionAnnotations;
import dagger.internal.codegen.javapoet.TypeNames;
import dagger.internal.codegen.xprocessing.XTypes;
import java.util.Optional;
import java.util.Set;
import javax.inject.Inject;
Expand Down Expand Up @@ -116,7 +117,7 @@ protected void checkSetValuesType() {

private Optional<XType> unwrapListenableFuture(XType type) {
if (isTypeOf(type, TypeNames.LISTENABLE_FUTURE)) {
if (type.getTypeArguments().isEmpty()) {
if (XTypes.isRawParameterizedType(type)) {
report.addError("@Produces methods cannot return a raw ListenableFuture");
return Optional.empty();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,19 @@

package dagger.internal.codegen;

import static com.google.testing.compile.CompilationSubject.assertThat;
import static dagger.internal.codegen.Compilers.daggerCompiler;
import static dagger.internal.codegen.DaggerModuleMethodSubject.Factory.assertThatMethodInUnannotatedClass;
import static dagger.internal.codegen.DaggerModuleMethodSubject.Factory.assertThatModuleMethod;

import androidx.room.compiler.processing.util.Source;
import com.google.common.collect.ImmutableList;
import com.google.testing.compile.Compilation;
import com.google.testing.compile.JavaFileObjects;
import dagger.Module;
import dagger.producers.ProducerModule;
import dagger.testing.compile.CompilerTests;
import java.lang.annotation.Annotation;
import java.util.Collection;
import javax.inject.Inject;
import javax.inject.Qualifier;
import javax.inject.Singleton;
import javax.tools.JavaFileObject;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
Expand Down Expand Up @@ -124,8 +121,8 @@ public void intoMap() {
*/
@Test
public void intoMapWithComponent() {
JavaFileObject module =
JavaFileObjects.forSourceLines(
Source module =
CompilerTests.javaSource(
"test.TestModule",
"package test;",
"",
Expand All @@ -137,8 +134,8 @@ public void intoMapWithComponent() {
"interface TestModule {",
" @BindsOptionalOf @IntoMap Object object();",
"}");
JavaFileObject component =
JavaFileObjects.forSourceLines(
Source component =
CompilerTests.javaSource(
"test.TestComponent",
"package test;",
"",
Expand All @@ -147,12 +144,17 @@ public void intoMapWithComponent() {
"@Component(modules = TestModule.class)",
"interface TestComponent {}");

Compilation compilation = daggerCompiler().compile(module, component);
assertThat(compilation).failed();
assertThat(compilation)
.hadErrorContaining("cannot have multibinding annotations")
.inFile(module)
.onLineContaining("object();");
CompilerTests.daggerCompiler(module, component)
.compile(
subject -> {
subject.hasErrorCount(2);
subject.hasErrorContaining("test.TestModule has errors")
.onSource(component)
.onLineContaining("@Component(modules = TestModule.class)");
subject.hasErrorContaining("cannot have multibinding annotations")
.onSource(module)
.onLineContaining("object()");
});
}

/** An injectable value object. */
Expand Down
40 changes: 16 additions & 24 deletions javatests/dagger/internal/codegen/DaggerModuleMethodSubject.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,19 @@
package dagger.internal.codegen;

import static com.google.common.truth.Truth.assertAbout;
import static com.google.testing.compile.CompilationSubject.assertThat;
import static dagger.internal.codegen.Compilers.daggerCompiler;

import com.google.common.collect.FluentIterable;
import androidx.room.compiler.processing.util.Source;
import com.google.common.collect.ImmutableList;
import com.google.common.truth.FailureMetadata;
import com.google.common.truth.Subject;
import com.google.common.truth.Truth;
import com.google.testing.compile.Compilation;
import com.google.testing.compile.JavaFileObjects;
import dagger.Module;
import dagger.producers.ProducerModule;
import dagger.testing.compile.CompilerTests;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Arrays;
import java.util.List;
import javax.tools.JavaFileObject;

/** A {@link Truth} subject for testing Dagger module methods. */
final class DaggerModuleMethodSubject extends Subject {
Expand All @@ -43,29 +39,25 @@ static final class Factory implements Subject.Factory<DaggerModuleMethodSubject,

/** Starts a clause testing a Dagger {@link Module @Module} method. */
static DaggerModuleMethodSubject assertThatModuleMethod(String method) {
return assertAbout(daggerModuleMethod())
return assertAbout(new Factory())
.that(method)
.withDeclaration("@Module abstract class %s { %s }");
}

/** Starts a clause testing a Dagger {@link ProducerModule @ProducerModule} method. */
static DaggerModuleMethodSubject assertThatProductionModuleMethod(String method) {
return assertAbout(daggerModuleMethod())
return assertAbout(new Factory())
.that(method)
.withDeclaration("@ProducerModule abstract class %s { %s }");
}

/** Starts a clause testing a method in an unannotated class. */
static DaggerModuleMethodSubject assertThatMethodInUnannotatedClass(String method) {
return assertAbout(daggerModuleMethod())
return assertAbout(new Factory())
.that(method)
.withDeclaration("abstract class %s { %s }");
}

static Factory daggerModuleMethod() {
return new Factory();
}

private Factory() {}

@Override
Expand All @@ -86,7 +78,7 @@ public DaggerModuleMethodSubject createSubject(FailureMetadata failureMetadata,
"import java.util.*;",
"import javax.inject.*;");
private String declaration;
private ImmutableList<JavaFileObject> additionalSources = ImmutableList.of();
private ImmutableList<Source> additionalSources = ImmutableList.of();

private DaggerModuleMethodSubject(FailureMetadata failureMetadata, String subject) {
super(failureMetadata, subject);
Expand Down Expand Up @@ -135,7 +127,7 @@ DaggerModuleMethodSubject withDeclaration(String declaration) {
}

/** Additional source files that must be compiled with the module. */
DaggerModuleMethodSubject withAdditionalSources(JavaFileObject... sources) {
DaggerModuleMethodSubject withAdditionalSources(Source... sources) {
this.additionalSources = ImmutableList.copyOf(sources);
return this;
}
Expand All @@ -146,14 +138,15 @@ DaggerModuleMethodSubject withAdditionalSources(JavaFileObject... sources) {
*/
void hasError(String errorSubstring) {
String source = moduleSource();
JavaFileObject module = JavaFileObjects.forSourceLines("test.TestModule", source);
Compilation compilation =
daggerCompiler().compile(FluentIterable.from(additionalSources).append(module));
assertThat(compilation).failed();
assertThat(compilation)
.hadErrorContaining(errorSubstring)
.inFile(module)
.onLine(methodLine(source));
Source module = CompilerTests.javaSource("test.TestModule", source);
CompilerTests.daggerCompiler(
ImmutableList.<Source>builder().add(module).addAll(additionalSources).build())
.compile(
subject ->
subject
.hasErrorContaining(errorSubstring)
.onSource(module)
.onLine(methodLine(source)));
}

private int methodLine(String source) {
Expand All @@ -180,5 +173,4 @@ private String moduleSource() {
writer.println();
return stringWriter.toString();
}

}
81 changes: 43 additions & 38 deletions javatests/dagger/internal/codegen/ModuleFactoryGeneratorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@
import androidx.room.compiler.processing.util.Source;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
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;
Expand Down Expand Up @@ -119,13 +117,14 @@ public void providesMethodReturnsProduced() {

@Test public void modulesWithTypeParamsMustBeAbstract() {
Source moduleFile =
CompilerTests.javaSource("test.TestModule",
"package test;",
"",
"import dagger.Module;",
"",
"@Module",
"final class TestModule<A> {}");
CompilerTests.javaSource(
"test.TestModule",
"package test;",
"",
"import dagger.Module;",
"",
"@Module",
"final class TestModule<A> {}");
CompilerTests.daggerCompiler(moduleFile)
.compile(
subject -> {
Expand All @@ -137,16 +136,18 @@ public void providesMethodReturnsProduced() {
}

@Test public void provideOverriddenByNoProvide() {
JavaFileObject parent = JavaFileObjects.forSourceLines("test.Parent",
"package test;",
"",
"import dagger.Module;",
"import dagger.Provides;",
"",
"@Module",
"class Parent {",
" @Provides String foo() { return null; }",
"}");
Source parent =
CompilerTests.javaSource(
"test.Parent",
"package test;",
"",
"import dagger.Module;",
"import dagger.Provides;",
"",
"@Module",
"class Parent {",
" @Provides String foo() { return null; }",
"}");
assertThatModuleMethod("String foo() { return null; }")
.withDeclaration("@Module class %s extends Parent { %s }")
.withAdditionalSources(parent)
Expand All @@ -156,16 +157,18 @@ public void providesMethodReturnsProduced() {
}

@Test public void provideOverriddenByProvide() {
JavaFileObject parent = JavaFileObjects.forSourceLines("test.Parent",
"package test;",
"",
"import dagger.Module;",
"import dagger.Provides;",
"",
"@Module",
"class Parent {",
" @Provides String foo() { return null; }",
"}");
Source parent =
CompilerTests.javaSource(
"test.Parent",
"package test;",
"",
"import dagger.Module;",
"import dagger.Provides;",
"",
"@Module",
"class Parent {",
" @Provides String foo() { return null; }",
"}");
assertThatModuleMethod("@Provides String foo() { return null; }")
.withDeclaration("@Module class %s extends Parent { %s }")
.withAdditionalSources(parent)
Expand All @@ -175,15 +178,17 @@ public void providesMethodReturnsProduced() {
}

@Test public void providesOverridesNonProvides() {
JavaFileObject parent = JavaFileObjects.forSourceLines("test.Parent",
"package test;",
"",
"import dagger.Module;",
"",
"@Module",
"class Parent {",
" String foo() { return null; }",
"}");
Source parent =
CompilerTests.javaSource(
"test.Parent",
"package test;",
"",
"import dagger.Module;",
"",
"@Module",
"class Parent {",
" String foo() { return null; }",
"}");
assertThatModuleMethod("@Provides String foo() { return null; }")
.withDeclaration("@Module class %s extends Parent { %s }")
.withAdditionalSources(parent)
Expand Down

0 comments on commit bc94a5a

Please sign in to comment.