• 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.originatingelement;
18 
19 import static dagger.hilt.android.testing.compile.HiltCompilerTests.hiltCompiler;
20 
21 import androidx.room.compiler.processing.util.Source;
22 import dagger.hilt.android.testing.compile.HiltCompilerTests;
23 import org.junit.Test;
24 import org.junit.runner.RunWith;
25 import org.junit.runners.JUnit4;
26 
27 @RunWith(JUnit4.class)
28 public class OriginatingElementProcessorTest {
29 
30   @Test
originatingElementOnInnerClass_fails()31   public void originatingElementOnInnerClass_fails() {
32     Source outer1 =
33         HiltCompilerTests.javaSource("test.Outer1", "package test;", "", "class Outer1 {}");
34     Source outer2 =
35         HiltCompilerTests.javaSource(
36             "test.Outer2",
37             "package test;",
38             "",
39             "import dagger.hilt.codegen.OriginatingElement;",
40             "",
41             "class Outer2 {",
42             "  @OriginatingElement(topLevelClass = Outer1.class)",
43             "  static class Inner {}",
44             "}");
45     hiltCompiler(outer1, outer2)
46         .compile(
47             subject -> {
48               subject.hasErrorCount(1);
49               subject.hasErrorContaining(
50                   "@OriginatingElement should only be used to annotate top-level types, but found: "
51                       + "test.Outer2.Inner");
52             });
53   }
54 
55   @Test
originatingElementValueWithInnerClass_fails()56   public void originatingElementValueWithInnerClass_fails() {
57     Source outer1 =
58         HiltCompilerTests.javaSource(
59             "test.Outer1", "package test;", "", "class Outer1 {", "  static class Inner {}", "}");
60     Source outer2 =
61         HiltCompilerTests.javaSource(
62             "test.Outer2",
63             "package test;",
64             "",
65             "import dagger.hilt.codegen.OriginatingElement;",
66             "",
67             "@OriginatingElement(topLevelClass = Outer1.Inner.class)",
68             "class Outer2 {}");
69     hiltCompiler(outer1, outer2)
70         .compile(
71             subject -> {
72               subject.hasErrorCount(1);
73               subject.hasErrorContaining(
74                   "OriginatingElement.topLevelClass value should be a top-level class, but found: "
75                       + "test.Outer1.Inner");
76             });
77   }
78 }
79