• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 The Dagger Authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package dagger.internal.codegen.kotlin;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 import static dagger.testing.compile.CompilerTests.compileWithKapt;
21 
22 import androidx.room.compiler.processing.util.DiagnosticMessage;
23 import androidx.room.compiler.processing.util.Source;
24 import com.google.common.collect.ImmutableList;
25 import java.util.List;
26 import javax.tools.Diagnostic.Kind;
27 import org.junit.Rule;
28 import org.junit.Test;
29 import org.junit.rules.TemporaryFolder;
30 import org.junit.runner.RunWith;
31 import org.junit.runners.JUnit4;
32 
33 @RunWith(JUnit4.class)
34 public final class ComponentValidationKtTest {
35 
36   @Rule
37   public TemporaryFolder tempFolderRule = new TemporaryFolder();
38 
39   @Test
creatorMethodNameIsJavaKeyword_compilationError()40   public void creatorMethodNameIsJavaKeyword_compilationError() {
41     Source componentSrc =
42         Source.Companion.kotlin(
43             "FooComponent.kt",
44             String.join(
45                 "\n",
46                 "package test",
47                 "",
48                 "import dagger.BindsInstance",
49                 "import dagger.Component",
50                 "",
51                 "@Component",
52                 "interface FooComponent {",
53                 "  @Component.Builder",
54                 "  interface Builder {",
55                 "    @BindsInstance public fun int(str: Int): Builder",
56                 "    public fun build(): FooComponent",
57                 "  }",
58                 "}"));
59 
60     compileWithKapt(
61         ImmutableList.of(componentSrc),
62         tempFolderRule,
63         result -> {
64           // TODO(b/192396673): Add error count when the feature request is fulfilled.
65           assertThat(result.getSuccess()).isFalse();
66           List<DiagnosticMessage> errors = result.getDiagnostics().get(Kind.ERROR);
67           assertThat(errors).hasSize(1);
68           assertThat(errors.get(0).getMsg())
69               .contains(
70                   "Can not use a Java keyword as method name: int(I)Ltest/FooComponent$Builder");
71         });
72   }
73 
74   @Test
componentMethodNameIsJavaKeyword_compilationError()75   public void componentMethodNameIsJavaKeyword_compilationError() {
76     Source componentSrc =
77         Source.Companion.kotlin(
78             "FooComponent.kt",
79             String.join(
80                 "\n",
81                 "package test",
82                 "",
83                 "import dagger.BindsInstance",
84                 "import dagger.Component",
85                 "",
86                 "@Component(modules = [TestModule::class])",
87                 "interface FooComponent {",
88                 "  fun int(str: Int): String",
89                 "}"));
90     Source moduleSrc =
91         Source.Companion.kotlin(
92             "TestModule.kt",
93             String.join(
94                 "\n",
95                 "package test",
96                 "",
97                 "import dagger.Module",
98                 "",
99                 "@Module",
100                 "interface TestModule {",
101                 "  fun providesString(): String {",
102                 "    return \"test\"",
103                 "  }",
104                 "}"));
105 
106     compileWithKapt(
107         ImmutableList.of(componentSrc, moduleSrc),
108         tempFolderRule,
109         result -> {
110           assertThat(result.getSuccess()).isFalse();
111           List<DiagnosticMessage> errors = result.getDiagnostics().get(Kind.ERROR);
112           assertThat(errors).hasSize(1);
113           assertThat(errors.get(0).getMsg())
114               .contains("Can not use a Java keyword as method name: int(I)Ljava/lang/String");
115         });
116   }
117 }
118