xref: /aosp_15_r20/external/dagger2/javatests/dagger/internal/codegen/GenericMethodsTest.java (revision f585d8a307d0621d6060bd7e80091fdcbf94fe27)
1 /*
2  * Copyright (C) 2017 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 androidx.room.compiler.processing.util.Source;
20 import com.google.common.collect.ImmutableList;
21 import dagger.testing.compile.CompilerTests;
22 import org.junit.Test;
23 import org.junit.runner.RunWith;
24 import org.junit.runners.Parameterized;
25 import org.junit.runners.Parameterized.Parameters;
26 
27 @RunWith(Parameterized.class)
28 public class GenericMethodsTest {
29   @Parameters(name = "{0}")
parameters()30   public static ImmutableList<Object[]> parameters() {
31     return CompilerMode.TEST_PARAMETERS;
32   }
33 
34   private final CompilerMode compilerMode;
35 
GenericMethodsTest(CompilerMode compilerMode)36   public GenericMethodsTest(CompilerMode compilerMode) {
37     this.compilerMode = compilerMode;
38   }
39 
40   @Test
parameterizedComponentMethods()41   public void parameterizedComponentMethods() {
42     Source component =
43         CompilerTests.javaSource(
44             "test.TestComponent",
45             "package test;",
46             "",
47             "import dagger.Component;",
48             "import dagger.MembersInjector;",
49             "import java.util.Set;",
50             "",
51             "@Component",
52             "interface TestComponent {",
53             "  <T1> void injectTypeVariable(T1 type);",
54             "  <T2> MembersInjector<T2> membersInjector();",
55             "  <T3> Set<T3> setOfT();",
56             "  <UNUSED> TestComponent unused();",
57             "}");
58     CompilerTests.daggerCompiler(component)
59         .withProcessingOptions(compilerMode.processorOptions())
60         .compile(
61             subject -> {
62               subject.hasErrorCount(6);
63               subject.hasErrorContaining("cannot have type variables")
64                   .onSource(component)
65                   .onLineContaining("<T1>");
66               subject.hasErrorContaining("cannot have type variables")
67                   .onSource(component)
68                   .onLineContaining("<T2>");
69               subject.hasErrorContaining("cannot have type variables")
70                   .onSource(component)
71                   .onLineContaining("<T3>");
72               subject.hasErrorContaining("cannot have type variables")
73                   .onSource(component)
74                   .onLineContaining("<UNUSED>");
75             });
76   }
77 }
78