Skip to content

Commit bc94a5a

Browse files
bcorsoDagger Team
authored andcommitted
Migrate more tests to XProcessing testing APIs.
RELNOTES=N/A PiperOrigin-RevId: 474870628
1 parent 1099dd4 commit bc94a5a

File tree

6 files changed

+82
-80
lines changed

6 files changed

+82
-80
lines changed

java/dagger/internal/codegen/base/MapType.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import com.squareup.javapoet.ClassName;
2727
import com.squareup.javapoet.TypeName;
2828
import dagger.internal.codegen.javapoet.TypeNames;
29+
import dagger.internal.codegen.xprocessing.XTypes;
2930
import dagger.spi.model.Key;
3031

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

4445
/** {@code true} if the map type is the raw {@link java.util.Map} type. */
4546
public boolean isRawType() {
46-
return type().getTypeArguments().isEmpty();
47+
return XTypes.isRawParameterizedType(type());
4748
}
4849

4950
/**

java/dagger/internal/codegen/base/SetType.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.squareup.javapoet.ClassName;
2626
import com.squareup.javapoet.TypeName;
2727
import dagger.internal.codegen.javapoet.TypeNames;
28+
import dagger.internal.codegen.xprocessing.XTypes;
2829
import dagger.spi.model.Key;
2930

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

4344
/** {@code true} if the set type is the raw {@link java.util.Set} type. */
4445
public boolean isRawType() {
45-
return type().getTypeArguments().isEmpty();
46+
return XTypes.isRawParameterizedType(type());
4647
}
4748

4849
/** Returns the element type. */

java/dagger/internal/codegen/validation/ProducesMethodValidator.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import com.google.common.util.concurrent.ListenableFuture;
3131
import dagger.internal.codegen.binding.InjectionAnnotations;
3232
import dagger.internal.codegen.javapoet.TypeNames;
33+
import dagger.internal.codegen.xprocessing.XTypes;
3334
import java.util.Optional;
3435
import java.util.Set;
3536
import javax.inject.Inject;
@@ -116,7 +117,7 @@ protected void checkSetValuesType() {
116117

117118
private Optional<XType> unwrapListenableFuture(XType type) {
118119
if (isTypeOf(type, TypeNames.LISTENABLE_FUTURE)) {
119-
if (type.getTypeArguments().isEmpty()) {
120+
if (XTypes.isRawParameterizedType(type)) {
120121
report.addError("@Produces methods cannot return a raw ListenableFuture");
121122
return Optional.empty();
122123
} else {

javatests/dagger/internal/codegen/BindsOptionalOfMethodValidationTest.java

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,19 @@
1616

1717
package dagger.internal.codegen;
1818

19-
import static com.google.testing.compile.CompilationSubject.assertThat;
20-
import static dagger.internal.codegen.Compilers.daggerCompiler;
2119
import static dagger.internal.codegen.DaggerModuleMethodSubject.Factory.assertThatMethodInUnannotatedClass;
2220
import static dagger.internal.codegen.DaggerModuleMethodSubject.Factory.assertThatModuleMethod;
2321

22+
import androidx.room.compiler.processing.util.Source;
2423
import com.google.common.collect.ImmutableList;
25-
import com.google.testing.compile.Compilation;
26-
import com.google.testing.compile.JavaFileObjects;
2724
import dagger.Module;
2825
import dagger.producers.ProducerModule;
26+
import dagger.testing.compile.CompilerTests;
2927
import java.lang.annotation.Annotation;
3028
import java.util.Collection;
3129
import javax.inject.Inject;
3230
import javax.inject.Qualifier;
3331
import javax.inject.Singleton;
34-
import javax.tools.JavaFileObject;
3532
import org.junit.Test;
3633
import org.junit.runner.RunWith;
3734
import org.junit.runners.Parameterized;
@@ -124,8 +121,8 @@ public void intoMap() {
124121
*/
125122
@Test
126123
public void intoMapWithComponent() {
127-
JavaFileObject module =
128-
JavaFileObjects.forSourceLines(
124+
Source module =
125+
CompilerTests.javaSource(
129126
"test.TestModule",
130127
"package test;",
131128
"",
@@ -137,8 +134,8 @@ public void intoMapWithComponent() {
137134
"interface TestModule {",
138135
" @BindsOptionalOf @IntoMap Object object();",
139136
"}");
140-
JavaFileObject component =
141-
JavaFileObjects.forSourceLines(
137+
Source component =
138+
CompilerTests.javaSource(
142139
"test.TestComponent",
143140
"package test;",
144141
"",
@@ -147,12 +144,17 @@ public void intoMapWithComponent() {
147144
"@Component(modules = TestModule.class)",
148145
"interface TestComponent {}");
149146

150-
Compilation compilation = daggerCompiler().compile(module, component);
151-
assertThat(compilation).failed();
152-
assertThat(compilation)
153-
.hadErrorContaining("cannot have multibinding annotations")
154-
.inFile(module)
155-
.onLineContaining("object();");
147+
CompilerTests.daggerCompiler(module, component)
148+
.compile(
149+
subject -> {
150+
subject.hasErrorCount(2);
151+
subject.hasErrorContaining("test.TestModule has errors")
152+
.onSource(component)
153+
.onLineContaining("@Component(modules = TestModule.class)");
154+
subject.hasErrorContaining("cannot have multibinding annotations")
155+
.onSource(module)
156+
.onLineContaining("object()");
157+
});
156158
}
157159

158160
/** An injectable value object. */

javatests/dagger/internal/codegen/DaggerModuleMethodSubject.java

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,19 @@
1717
package dagger.internal.codegen;
1818

1919
import static com.google.common.truth.Truth.assertAbout;
20-
import static com.google.testing.compile.CompilationSubject.assertThat;
21-
import static dagger.internal.codegen.Compilers.daggerCompiler;
2220

23-
import com.google.common.collect.FluentIterable;
21+
import androidx.room.compiler.processing.util.Source;
2422
import com.google.common.collect.ImmutableList;
2523
import com.google.common.truth.FailureMetadata;
2624
import com.google.common.truth.Subject;
2725
import com.google.common.truth.Truth;
28-
import com.google.testing.compile.Compilation;
29-
import com.google.testing.compile.JavaFileObjects;
3026
import dagger.Module;
3127
import dagger.producers.ProducerModule;
28+
import dagger.testing.compile.CompilerTests;
3229
import java.io.PrintWriter;
3330
import java.io.StringWriter;
3431
import java.util.Arrays;
3532
import java.util.List;
36-
import javax.tools.JavaFileObject;
3733

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

4440
/** Starts a clause testing a Dagger {@link Module @Module} method. */
4541
static DaggerModuleMethodSubject assertThatModuleMethod(String method) {
46-
return assertAbout(daggerModuleMethod())
42+
return assertAbout(new Factory())
4743
.that(method)
4844
.withDeclaration("@Module abstract class %s { %s }");
4945
}
5046

5147
/** Starts a clause testing a Dagger {@link ProducerModule @ProducerModule} method. */
5248
static DaggerModuleMethodSubject assertThatProductionModuleMethod(String method) {
53-
return assertAbout(daggerModuleMethod())
49+
return assertAbout(new Factory())
5450
.that(method)
5551
.withDeclaration("@ProducerModule abstract class %s { %s }");
5652
}
5753

5854
/** Starts a clause testing a method in an unannotated class. */
5955
static DaggerModuleMethodSubject assertThatMethodInUnannotatedClass(String method) {
60-
return assertAbout(daggerModuleMethod())
56+
return assertAbout(new Factory())
6157
.that(method)
6258
.withDeclaration("abstract class %s { %s }");
6359
}
6460

65-
static Factory daggerModuleMethod() {
66-
return new Factory();
67-
}
68-
6961
private Factory() {}
7062

7163
@Override
@@ -86,7 +78,7 @@ public DaggerModuleMethodSubject createSubject(FailureMetadata failureMetadata,
8678
"import java.util.*;",
8779
"import javax.inject.*;");
8880
private String declaration;
89-
private ImmutableList<JavaFileObject> additionalSources = ImmutableList.of();
81+
private ImmutableList<Source> additionalSources = ImmutableList.of();
9082

9183
private DaggerModuleMethodSubject(FailureMetadata failureMetadata, String subject) {
9284
super(failureMetadata, subject);
@@ -135,7 +127,7 @@ DaggerModuleMethodSubject withDeclaration(String declaration) {
135127
}
136128

137129
/** Additional source files that must be compiled with the module. */
138-
DaggerModuleMethodSubject withAdditionalSources(JavaFileObject... sources) {
130+
DaggerModuleMethodSubject withAdditionalSources(Source... sources) {
139131
this.additionalSources = ImmutableList.copyOf(sources);
140132
return this;
141133
}
@@ -146,14 +138,15 @@ DaggerModuleMethodSubject withAdditionalSources(JavaFileObject... sources) {
146138
*/
147139
void hasError(String errorSubstring) {
148140
String source = moduleSource();
149-
JavaFileObject module = JavaFileObjects.forSourceLines("test.TestModule", source);
150-
Compilation compilation =
151-
daggerCompiler().compile(FluentIterable.from(additionalSources).append(module));
152-
assertThat(compilation).failed();
153-
assertThat(compilation)
154-
.hadErrorContaining(errorSubstring)
155-
.inFile(module)
156-
.onLine(methodLine(source));
141+
Source module = CompilerTests.javaSource("test.TestModule", source);
142+
CompilerTests.daggerCompiler(
143+
ImmutableList.<Source>builder().add(module).addAll(additionalSources).build())
144+
.compile(
145+
subject ->
146+
subject
147+
.hasErrorContaining(errorSubstring)
148+
.onSource(module)
149+
.onLine(methodLine(source)));
157150
}
158151

159152
private int methodLine(String source) {
@@ -180,5 +173,4 @@ private String moduleSource() {
180173
writer.println();
181174
return stringWriter.toString();
182175
}
183-
184176
}

javatests/dagger/internal/codegen/ModuleFactoryGeneratorTest.java

Lines changed: 43 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,8 @@
2323
import androidx.room.compiler.processing.util.Source;
2424
import com.google.common.collect.ImmutableList;
2525
import com.google.common.collect.ImmutableMap;
26-
import com.google.testing.compile.JavaFileObjects;
2726
import dagger.testing.compile.CompilerTests;
2827
import dagger.testing.golden.GoldenFileRule;
29-
import javax.tools.JavaFileObject;
3028
import org.junit.Rule;
3129
import org.junit.Test;
3230
import org.junit.runner.RunWith;
@@ -119,13 +117,14 @@ public void providesMethodReturnsProduced() {
119117

120118
@Test public void modulesWithTypeParamsMustBeAbstract() {
121119
Source moduleFile =
122-
CompilerTests.javaSource("test.TestModule",
123-
"package test;",
124-
"",
125-
"import dagger.Module;",
126-
"",
127-
"@Module",
128-
"final class TestModule<A> {}");
120+
CompilerTests.javaSource(
121+
"test.TestModule",
122+
"package test;",
123+
"",
124+
"import dagger.Module;",
125+
"",
126+
"@Module",
127+
"final class TestModule<A> {}");
129128
CompilerTests.daggerCompiler(moduleFile)
130129
.compile(
131130
subject -> {
@@ -137,16 +136,18 @@ public void providesMethodReturnsProduced() {
137136
}
138137

139138
@Test public void provideOverriddenByNoProvide() {
140-
JavaFileObject parent = JavaFileObjects.forSourceLines("test.Parent",
141-
"package test;",
142-
"",
143-
"import dagger.Module;",
144-
"import dagger.Provides;",
145-
"",
146-
"@Module",
147-
"class Parent {",
148-
" @Provides String foo() { return null; }",
149-
"}");
139+
Source parent =
140+
CompilerTests.javaSource(
141+
"test.Parent",
142+
"package test;",
143+
"",
144+
"import dagger.Module;",
145+
"import dagger.Provides;",
146+
"",
147+
"@Module",
148+
"class Parent {",
149+
" @Provides String foo() { return null; }",
150+
"}");
150151
assertThatModuleMethod("String foo() { return null; }")
151152
.withDeclaration("@Module class %s extends Parent { %s }")
152153
.withAdditionalSources(parent)
@@ -156,16 +157,18 @@ public void providesMethodReturnsProduced() {
156157
}
157158

158159
@Test public void provideOverriddenByProvide() {
159-
JavaFileObject parent = JavaFileObjects.forSourceLines("test.Parent",
160-
"package test;",
161-
"",
162-
"import dagger.Module;",
163-
"import dagger.Provides;",
164-
"",
165-
"@Module",
166-
"class Parent {",
167-
" @Provides String foo() { return null; }",
168-
"}");
160+
Source parent =
161+
CompilerTests.javaSource(
162+
"test.Parent",
163+
"package test;",
164+
"",
165+
"import dagger.Module;",
166+
"import dagger.Provides;",
167+
"",
168+
"@Module",
169+
"class Parent {",
170+
" @Provides String foo() { return null; }",
171+
"}");
169172
assertThatModuleMethod("@Provides String foo() { return null; }")
170173
.withDeclaration("@Module class %s extends Parent { %s }")
171174
.withAdditionalSources(parent)
@@ -175,15 +178,17 @@ public void providesMethodReturnsProduced() {
175178
}
176179

177180
@Test public void providesOverridesNonProvides() {
178-
JavaFileObject parent = JavaFileObjects.forSourceLines("test.Parent",
179-
"package test;",
180-
"",
181-
"import dagger.Module;",
182-
"",
183-
"@Module",
184-
"class Parent {",
185-
" String foo() { return null; }",
186-
"}");
181+
Source parent =
182+
CompilerTests.javaSource(
183+
"test.Parent",
184+
"package test;",
185+
"",
186+
"import dagger.Module;",
187+
"",
188+
"@Module",
189+
"class Parent {",
190+
" String foo() { return null; }",
191+
"}");
187192
assertThatModuleMethod("@Provides String foo() { return null; }")
188193
.withDeclaration("@Module class %s extends Parent { %s }")
189194
.withAdditionalSources(parent)

0 commit comments

Comments
 (0)