xref: /aosp_15_r20/external/dagger2/javatests/dagger/internal/codegen/HjarTest.java (revision f585d8a307d0621d6060bd7e80091fdcbf94fe27)
1 /*
2  * Copyright (C) 2014 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.ImmutableMap;
21 import dagger.testing.compile.CompilerTests;
22 import dagger.testing.compile.CompilerTests.DaggerCompiler;
23 import org.junit.Test;
24 import org.junit.runner.RunWith;
25 import org.junit.runners.JUnit4;
26 
27 /** Tests compilation with the {@code experimental_turbine_hjar} flag enabled. */
28 @RunWith(JUnit4.class)
29 public class HjarTest {
30   /** Returns a {@link DaggerCompiler} with hjar generation enabled. */
daggerCompiler(Source... sources)31   private static DaggerCompiler daggerCompiler(Source... sources) {
32     return CompilerTests.daggerCompiler(sources)
33         .withProcessingOptions(ImmutableMap.of("experimental_turbine_hjar", ""));
34   }
35 
36   @Test
componentTest()37   public void componentTest() {
38     Source component =
39         CompilerTests.javaSource(
40             "test.MyComponent",
41             "package test;",
42             "",
43             "import dagger.Component;",
44             "",
45             "@Component",
46             "interface MyComponent {",
47             "  String getString();",
48             "  int getInt();",
49             "  void inject(String str);",
50             "}");
51     daggerCompiler(component).compile(subject -> subject.hasErrorCount(0));
52   }
53 
54   @Test
moduleTest()55   public void moduleTest() {
56     Source module =
57         CompilerTests.javaSource(
58             "test.MyModule",
59             "package test;",
60             "",
61             "import dagger.Module;",
62             "import dagger.Provides;",
63             "",
64             "@Module",
65             "interface MyModule {",
66             "  @Provides static int provideInt() { return 0; }",
67             "  @Provides static String provideString() { return null; }",
68             "  @Provides static String[] provideStringArray() { return null; }",
69             "  @Provides static int[] provideIntArray() { return null; }",
70             "  @Provides static boolean provideBoolean() { return false; }",
71             "}");
72     daggerCompiler(module).compile(subject -> subject.hasErrorCount(0));
73   }
74 
75   @Test
producerModuleTest()76   public void producerModuleTest() {
77     Source module =
78         CompilerTests.javaSource(
79             "test.MyModule",
80             "package test;",
81             "",
82             "import com.google.common.util.concurrent.ListenableFuture;",
83             "import dagger.producers.ProducerModule;",
84             "import dagger.producers.Produces;",
85             "",
86             "@ProducerModule",
87             "interface MyModule {",
88             "  @Produces static ListenableFuture<String> produceString() { return null; }",
89             "}");
90     daggerCompiler(module).compile(subject -> subject.hasErrorCount(0));
91   }
92 }
93