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: 474306355
  • Loading branch information
bcorso authored and Dagger Team committed Sep 14, 2022
1 parent 714c3cb commit 96e2b8a
Show file tree
Hide file tree
Showing 3 changed files with 461 additions and 432 deletions.
103 changes: 53 additions & 50 deletions javatests/dagger/internal/codegen/RawTypeInjectionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,8 @@

package dagger.internal.codegen;

import static com.google.testing.compile.CompilationSubject.assertThat;
import static dagger.internal.codegen.Compilers.daggerCompiler;

import com.google.testing.compile.Compilation;
import com.google.testing.compile.JavaFileObjects;
import javax.tools.JavaFileObject;
import androidx.room.compiler.processing.util.Source;
import dagger.testing.compile.CompilerTests;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
Expand All @@ -30,8 +26,8 @@
public class RawTypeInjectionTest {
@Test
public void rawEntryPointTest() {
JavaFileObject component =
JavaFileObjects.forSourceLines(
Source component =
CompilerTests.javaSource(
"test.TestComponent",
"package test;",
"",
Expand All @@ -41,8 +37,8 @@ public void rawEntryPointTest() {
"interface TestComponent {",
" Foo foo();", // Fail: requesting raw type
"}");
JavaFileObject foo =
JavaFileObjects.forSourceLines(
Source foo =
CompilerTests.javaSource(
"test.Foo",
"package test;",
"",
Expand All @@ -52,18 +48,21 @@ public void rawEntryPointTest() {
" @Inject Foo() {}",
"}");

Compilation compilation = daggerCompiler().compile(component, foo);
assertThat(compilation).failed();
assertThat(compilation)
.hadErrorContaining("Foo cannot be provided without an @Provides-annotated method.")
.inFile(component)
.onLine(6);
CompilerTests.daggerCompiler(component, foo)
.compile(
subject -> {
subject.hasErrorCount(1);
subject.hasErrorContaining(
"Foo cannot be provided without an @Provides-annotated method.")
.onSource(component)
.onLine(6);
});
}

@Test
public void rawProvidesRequestTest() {
JavaFileObject component =
JavaFileObjects.forSourceLines(
Source component =
CompilerTests.javaSource(
"test.TestComponent",
"package test;",
"",
Expand All @@ -73,8 +72,8 @@ public void rawProvidesRequestTest() {
"interface TestComponent {",
" int integer();",
"}");
JavaFileObject foo =
JavaFileObjects.forSourceLines(
Source foo =
CompilerTests.javaSource(
"test.Foo",
"package test;",
"",
Expand All @@ -83,8 +82,8 @@ public void rawProvidesRequestTest() {
"class Foo<T> {",
" @Inject Foo() {}",
"}");
JavaFileObject module =
JavaFileObjects.forSourceLines(
Source module =
CompilerTests.javaSource(
"test.TestModule",
"package test;",
"",
Expand All @@ -99,19 +98,21 @@ public void rawProvidesRequestTest() {
" }",
"}");


Compilation compilation = daggerCompiler().compile(component, foo, module);
assertThat(compilation).failed();
assertThat(compilation)
.hadErrorContaining("Foo cannot be provided without an @Provides-annotated method.")
.inFile(component)
.onLine(6);
CompilerTests.daggerCompiler(component, foo, module)
.compile(
subject -> {
subject.hasErrorCount(1);
subject.hasErrorContaining(
"Foo cannot be provided without an @Provides-annotated method.")
.onSource(component)
.onLine(6);
});
}

@Test
public void rawInjectConstructorRequestTest() {
JavaFileObject component =
JavaFileObjects.forSourceLines(
Source component =
CompilerTests.javaSource(
"test.TestComponent",
"package test;",
"",
Expand All @@ -121,8 +122,8 @@ public void rawInjectConstructorRequestTest() {
"interface TestComponent {",
" Foo foo();",
"}");
JavaFileObject foo =
JavaFileObjects.forSourceLines(
Source foo =
CompilerTests.javaSource(
"test.Foo",
"package test;",
"",
Expand All @@ -131,8 +132,8 @@ public void rawInjectConstructorRequestTest() {
"class Foo<T> {",
" @Inject Foo() {}",
"}");
JavaFileObject bar =
JavaFileObjects.forSourceLines(
Source bar =
CompilerTests.javaSource(
"test.Bar",
"package test;",
"",
Expand All @@ -142,19 +143,21 @@ public void rawInjectConstructorRequestTest() {
" @Inject Bar(Foo foo) {}", // Fail: requesting raw type
"}");


Compilation compilation = daggerCompiler().compile(component, foo, bar);
assertThat(compilation).failed();
assertThat(compilation)
.hadErrorContaining("Foo cannot be provided without an @Provides-annotated method.")
.inFile(component)
.onLine(6);
CompilerTests.daggerCompiler(component, foo, bar)
.compile(
subject -> {
subject.hasErrorCount(1);
subject.hasErrorContaining(
"Foo cannot be provided without an @Provides-annotated method.")
.onSource(component)
.onLine(6);
});
}

@Test
public void rawProvidesReturnTest() {
JavaFileObject component =
JavaFileObjects.forSourceLines(
Source component =
CompilerTests.javaSource(
"test.TestComponent",
"package test;",
"",
Expand All @@ -165,8 +168,8 @@ public void rawProvidesReturnTest() {
// Test that we can request the raw type if it's provided by a module.
" Foo foo();",
"}");
JavaFileObject foo =
JavaFileObjects.forSourceLines(
Source foo =
CompilerTests.javaSource(
"test.Foo",
"package test;",
"",
Expand All @@ -175,8 +178,8 @@ public void rawProvidesReturnTest() {
"class Foo<T> {",
" @Inject Foo() {}",
"}");
JavaFileObject module =
JavaFileObjects.forSourceLines(
Source module =
CompilerTests.javaSource(
"test.TestModule",
"package test;",
"",
Expand All @@ -198,7 +201,7 @@ public void rawProvidesReturnTest() {
" }",
"}");

Compilation compilation = daggerCompiler().compile(component, foo, module);
assertThat(compilation).succeeded();
CompilerTests.daggerCompiler(component, foo, module)
.compile(subject -> subject.hasErrorCount(0));
}
}
62 changes: 28 additions & 34 deletions javatests/dagger/internal/codegen/RepeatedModuleValidationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,16 @@

package dagger.internal.codegen;

import static com.google.testing.compile.CompilationSubject.assertThat;
import static dagger.internal.codegen.Compilers.daggerCompiler;

import com.google.testing.compile.Compilation;
import com.google.testing.compile.JavaFileObjects;
import javax.tools.JavaFileObject;
import androidx.room.compiler.processing.util.Source;
import dagger.testing.compile.CompilerTests;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public class RepeatedModuleValidationTest {
private static final JavaFileObject MODULE_FILE =
JavaFileObjects.forSourceLines(
private static final Source MODULE_FILE =
CompilerTests.javaSource(
"test.TestModule",
"package test;",
"",
Expand All @@ -40,8 +36,8 @@ public class RepeatedModuleValidationTest {

@Test
public void moduleRepeatedInSubcomponentFactoryMethod() {
JavaFileObject subcomponentFile =
JavaFileObjects.forSourceLines(
Source subcomponentFile =
CompilerTests.javaSource(
"test.TestSubcomponent",
"package test;",
"",
Expand All @@ -50,8 +46,8 @@ public void moduleRepeatedInSubcomponentFactoryMethod() {
"@Subcomponent(modules = TestModule.class)",
"interface TestSubcomponent {",
"}");
JavaFileObject componentFile =
JavaFileObjects.forSourceLines(
Source componentFile =
CompilerTests.javaSource(
"test.TestComponent",
"package test;",
"",
Expand All @@ -61,20 +57,20 @@ public void moduleRepeatedInSubcomponentFactoryMethod() {
"interface TestComponent {",
" TestSubcomponent newTestSubcomponent(TestModule module);",
"}");
Compilation compilation =
daggerCompiler().compile(MODULE_FILE, subcomponentFile, componentFile);
assertThat(compilation).failed();
assertThat(compilation)
.hadErrorContaining("TestModule is present in test.TestComponent.")
.inFile(componentFile)
.onLine(7)
.atColumn(51);
CompilerTests.daggerCompiler(MODULE_FILE, subcomponentFile, componentFile)
.compile(
subject -> {
subject.hasErrorCount(1);
subject.hasErrorContaining("TestModule is present in test.TestComponent.")
.onSource(componentFile)
.onLine(7);
});
}

@Test
public void moduleRepeatedInSubcomponentBuilderMethod() {
JavaFileObject subcomponentFile =
JavaFileObjects.forSourceLines(
Source subcomponentFile =
CompilerTests.javaSource(
"test.TestSubcomponent",
"package test;",
"",
Expand All @@ -88,8 +84,8 @@ public void moduleRepeatedInSubcomponentBuilderMethod() {
" TestSubcomponent build();",
" }",
"}");
JavaFileObject componentFile =
JavaFileObjects.forSourceLines(
Source componentFile =
CompilerTests.javaSource(
"test.TestComponent",
"package test;",
"",
Expand All @@ -99,16 +95,15 @@ public void moduleRepeatedInSubcomponentBuilderMethod() {
"interface TestComponent {",
" TestSubcomponent.Builder newTestSubcomponentBuilder();",
"}");
Compilation compilation =
daggerCompiler().compile(MODULE_FILE, subcomponentFile, componentFile);
assertThat(compilation).succeeded();
CompilerTests.daggerCompiler(MODULE_FILE, subcomponentFile, componentFile)
.compile(subject -> subject.hasErrorCount(0));
// TODO(gak): assert about the warning when we have that ability
}

@Test
public void moduleRepeatedButNotPassed() {
JavaFileObject subcomponentFile =
JavaFileObjects.forSourceLines(
Source subcomponentFile =
CompilerTests.javaSource(
"test.TestSubcomponent",
"package test;",
"",
Expand All @@ -117,8 +112,8 @@ public void moduleRepeatedButNotPassed() {
"@Subcomponent(modules = TestModule.class)",
"interface TestSubcomponent {",
"}");
JavaFileObject componentFile =
JavaFileObjects.forSourceLines(
Source componentFile =
CompilerTests.javaSource(
"test.TestComponent",
"package test;",
"",
Expand All @@ -128,8 +123,7 @@ public void moduleRepeatedButNotPassed() {
"interface TestComponent {",
" TestSubcomponent newTestSubcomponent();",
"}");
Compilation compilation =
daggerCompiler().compile(MODULE_FILE, subcomponentFile, componentFile);
assertThat(compilation).succeeded();
CompilerTests.daggerCompiler(MODULE_FILE, subcomponentFile, componentFile)
.compile(subject -> subject.hasErrorCount(0));
}
}

0 comments on commit 96e2b8a

Please sign in to comment.