• 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;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 import static dagger.testing.compile.CompilerTests.kotlinCompiler;
21 
22 import com.google.common.collect.ImmutableList;
23 import com.tschuchort.compiletesting.KotlinCompilation;
24 import com.tschuchort.compiletesting.KotlinCompilation.ExitCode;
25 import com.tschuchort.compiletesting.SourceFile;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.junit.runners.JUnit4;
29 
30 @RunWith(JUnit4.class)
31 public final class ComponentValidationKtTest {
32   @Test
creatorMethodNameIsJavaKeyword_compilationError()33   public void creatorMethodNameIsJavaKeyword_compilationError() {
34     SourceFile componentSrc =
35         SourceFile.Companion.kotlin(
36             "FooComponent.kt",
37             String.join(
38                 "\n",
39                 "package test",
40                 "",
41                 "import dagger.BindsInstance",
42                 "import dagger.Component",
43                 "",
44                 "@Component",
45                 "interface FooComponent {",
46                 "  @Component.Builder",
47                 "  interface Builder {",
48                 "    @BindsInstance public fun int(str: Int): Builder",
49                 "    public fun build(): FooComponent",
50                 "  }",
51                 "}"),
52             false);
53     KotlinCompilation compilation = kotlinCompiler();
54     compilation.setSources(ImmutableList.of(componentSrc));
55 
56     KotlinCompilation.Result result = compilation.compile();
57 
58     // TODO(b/192396673): Add error count when the feature request is fulfilled.
59     assertThat(result.getExitCode()).isEqualTo(ExitCode.COMPILATION_ERROR);
60     assertThat(result.getMessages())
61         .contains("Can not use a Java keyword as method name: int(I)Ltest/FooComponent$Builder");
62   }
63 
64   @Test
componentMethodNameIsJavaKeyword_compilationError()65   public void componentMethodNameIsJavaKeyword_compilationError() {
66     SourceFile componentSrc =
67         SourceFile.Companion.kotlin(
68             "FooComponent.kt",
69             String.join(
70                 "\n",
71                 "package test",
72                 "",
73                 "import dagger.BindsInstance",
74                 "import dagger.Component",
75                 "",
76                 "@Component(modules = [TestModule::class])",
77                 "interface FooComponent {",
78                 "  fun int(str: Int): String",
79                 "}"),
80             false);
81     SourceFile moduleSrc =
82         SourceFile.Companion.kotlin(
83             "TestModule.kt",
84             String.join(
85                 "\n",
86                 "package test",
87                 "",
88                 "import dagger.Module",
89                 "",
90                 "@Module",
91                 "interface TestModule {",
92                 "  fun providesString(): String {",
93                 "    return \"test\"",
94                 "  }",
95                 "}"),
96             false);
97     KotlinCompilation compilation = kotlinCompiler();
98     compilation.setSources(ImmutableList.of(componentSrc, moduleSrc));
99 
100     KotlinCompilation.Result result = compilation.compile();
101 
102     assertThat(result.getExitCode()).isEqualTo(ExitCode.COMPILATION_ERROR);
103     assertThat(result.getMessages())
104         .contains("Can not use a Java keyword as method name: int(I)Ljava/lang/String");
105   }
106 }
107