• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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.hilt.processor.internal.uninstallmodules;
18 
19 import dagger.hilt.android.testing.compile.HiltCompilerTests;
20 import org.junit.Test;
21 import org.junit.runner.RunWith;
22 import org.junit.runners.JUnit4;
23 
24 @RunWith(JUnit4.class)
25 public class UninstallModulesProcessorTest {
26 
27   @Test
testInvalidModuleNoInstallIn_fails()28   public void testInvalidModuleNoInstallIn_fails() {
29     HiltCompilerTests.hiltCompiler(
30             HiltCompilerTests.javaSource(
31                 "test.MyTest",
32                 "package test;",
33                 "",
34                 "import dagger.hilt.android.testing.HiltAndroidTest;",
35                 "import dagger.hilt.android.testing.UninstallModules;",
36                 "",
37                 "@UninstallModules(InvalidModule.class)",
38                 "@HiltAndroidTest",
39                 "public class MyTest {}"),
40             HiltCompilerTests.javaSource(
41                 "test.InvalidModule",
42                 "package test;",
43                 "",
44                 "import dagger.Module;",
45                 "import dagger.hilt.migration.DisableInstallInCheck;",
46                 "",
47                 "@DisableInstallInCheck",
48                 "@Module",
49                 "public class InvalidModule {}"))
50         .compile(
51             subject -> {
52               subject.hasErrorCount(1);
53               subject.hasErrorContaining(
54                   "@UninstallModules should only include modules annotated with both @Module and "
55                       + "@InstallIn, but found: [test.InvalidModule].");
56             });
57   }
58 
59   @Test
testInvalidModuleNoModule_fails()60   public void testInvalidModuleNoModule_fails() {
61     HiltCompilerTests.hiltCompiler(
62             HiltCompilerTests.javaSource(
63                 "test.MyTest",
64                 "package test;",
65                 "",
66                 "import dagger.hilt.android.testing.HiltAndroidTest;",
67                 "import dagger.hilt.android.testing.UninstallModules;",
68                 "",
69                 "@UninstallModules(InvalidModule.class)",
70                 "@HiltAndroidTest",
71                 "public class MyTest {}"),
72             HiltCompilerTests.javaSource(
73                 "test.InvalidModule", "package test;", "", "public class InvalidModule {", "}"))
74         .compile(
75             subject -> {
76               subject.hasErrorCount(1);
77               subject.hasErrorContaining(
78                   "@UninstallModules should only include modules annotated with both @Module and "
79                       + "@InstallIn, but found: [test.InvalidModule].");
80             });
81   }
82 
83   @Test
testInvalidTest_fails()84   public void testInvalidTest_fails() {
85     HiltCompilerTests.hiltCompiler(
86             HiltCompilerTests.javaSource(
87                 "test.InvalidTest",
88                 "package test;",
89                 "",
90                 "import dagger.hilt.android.testing.UninstallModules;",
91                 "",
92                 "@UninstallModules(ValidModule.class)",
93                 "public class InvalidTest {}"),
94             HiltCompilerTests.javaSource(
95                 "test.ValidModule",
96                 "package test;",
97                 "",
98                 "import dagger.Module;",
99                 "import dagger.hilt.InstallIn;",
100                 "import dagger.hilt.components.SingletonComponent;",
101                 "",
102                 "@Module",
103                 "@InstallIn(SingletonComponent.class)",
104                 "public class ValidModule {}"))
105         .compile(
106             subject -> {
107               subject.hasErrorCount(1);
108               subject.hasErrorContaining(
109                   "@UninstallModules should only be used on test classes annotated with"
110                       + " @HiltAndroidTest, but found: test.InvalidTest");
111             });
112   }
113 
114   @Test
testInvalidTestModule_fails()115   public void testInvalidTestModule_fails() {
116     HiltCompilerTests.hiltCompiler(
117             HiltCompilerTests.javaSource(
118                 "test.MyTest",
119                 "package test;",
120                 "",
121                 "import dagger.Module;",
122                 "import dagger.hilt.InstallIn;",
123                 "import dagger.hilt.components.SingletonComponent;",
124                 "import dagger.hilt.android.testing.HiltAndroidTest;",
125                 "import dagger.hilt.android.testing.UninstallModules;",
126                 "",
127                 "@UninstallModules({",
128                 "    MyTest.PkgPrivateInvalidModule.class,",
129                 "    MyTest.PublicInvalidModule.class,",
130                 "})",
131                 "@HiltAndroidTest",
132                 "public class MyTest {",
133                 "  @Module",
134                 "  @InstallIn(SingletonComponent.class)",
135                 "  interface PkgPrivateInvalidModule {}",
136                 "",
137                 "  @Module",
138                 "  @InstallIn(SingletonComponent.class)",
139                 "  public interface PublicInvalidModule {}",
140                 "}"))
141         .compile(
142             subject -> {
143               subject.hasErrorCount(1);
144               // TODO(bcorso): Consider unwrapping pkg-private modules before reporting the error.
145               subject.hasErrorContaining(
146                   "@UninstallModules should not contain test modules, but found: "
147                       + "[test.MyTest.PkgPrivateInvalidModule, test.MyTest.PublicInvalidModule]");
148             });
149   }
150 }
151