Skip to content

Commit

Permalink
Add a test for Kotlin nullable provides methods.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 677905874
  • Loading branch information
java-team-github-bot authored and Dagger Team committed Sep 23, 2024
1 parent b2eb30f commit c3f2b94
Showing 1 changed file with 74 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static dagger.internal.codegen.DaggerModuleMethodSubject.Factory.assertThatMethodInUnannotatedClass;
import static dagger.internal.codegen.DaggerModuleMethodSubject.Factory.assertThatModuleMethod;

import androidx.room.compiler.processing.XProcessingEnv;
import androidx.room.compiler.processing.util.Source;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
Expand Down Expand Up @@ -304,7 +305,79 @@ public void nonTypeUseNullableProvides() {
}

@Test
public void multipleProvidesMethods() {
public void kotlinNullableProvides() {
Source moduleFile =
CompilerTests.kotlinSource(
"TestModule.kt",
"package test",
"",
"import dagger.Module;",
"import dagger.Provides;",
"",
"@Module",
"class TestModule {",
" @Provides fun provideString(): String? { return null; }",
"}");
CompilerTests.daggerCompiler(moduleFile)
.compile(
subject -> {
subject.hasErrorCount(0);
boolean isJavac = CompilerTests.backend(subject) == XProcessingEnv.Backend.JAVAC;
subject.generatedSource(
CompilerTests.javaSource(
"test.TestModule_ProvideStringFactory",
"package test;",
"",
"import dagger.internal.DaggerGenerated;",
"import dagger.internal.Factory;",
"import dagger.internal.QualifierMetadata;",
"import dagger.internal.ScopeMetadata;",
"import javax.annotation.processing.Generated;",
isJavac ? "import org.jetbrains.annotations.Nullable;\n" : "",
"@ScopeMetadata",
"@QualifierMetadata",
"@DaggerGenerated",
"@Generated(",
" value = \"dagger.internal.codegen.ComponentProcessor\",",
" comments = \"https://dagger.dev\"",
")",
"@SuppressWarnings({",
" \"unchecked\",",
" \"rawtypes\",",
" \"KotlinInternal\",",
" \"KotlinInternalInJava\",",
" \"cast\",",
" \"deprecation\"",
"})",
"public final class TestModule_ProvideStringFactory implements"
+ " Factory<String> {",
" private final TestModule module;",
"",
" public TestModule_ProvideStringFactory(TestModule module) {",
" this.module = module;",
" }",
"",
// TODO(b/368129744): KSP should output the @Nullable annotation after this
// bug is fixed.
isJavac ? " @Override\n @Nullable" : " @Override",
" public String get() {",
" return provideString(module);",
" }",
"",
" public static TestModule_ProvideStringFactory create(TestModule module) {",
" return new TestModule_ProvideStringFactory(module);",
" }",
// TODO(b/368129744): KSP should output the @Nullable annotation after this
// bug is fixed.
isJavac ? "\n @Nullable" : "",
" public static String provideString(TestModule instance) {",
" return instance.provideString();",
" }",
"}"));
});
}

@Test public void multipleProvidesMethods() {
Source classXFile =
CompilerTests.javaSource("test.X",
"package test;",
Expand Down

0 comments on commit c3f2b94

Please sign in to comment.