• 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.internal.codegen;
18 
19 import static com.google.testing.compile.CompilationSubject.assertThat;
20 import static dagger.internal.codegen.Compilers.compilerWithOptions;
21 
22 import com.google.common.collect.ImmutableCollection;
23 import com.google.testing.compile.Compilation;
24 import com.google.testing.compile.JavaFileObjects;
25 import javax.tools.JavaFileObject;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.junit.runners.Parameterized;
29 import org.junit.runners.Parameterized.Parameters;
30 
31 @RunWith(Parameterized.class)
32 public class AssistedErrorsTest {
33   @Parameters(name = "{0}")
parameters()34   public static ImmutableCollection<Object[]> parameters() {
35     return CompilerMode.TEST_PARAMETERS;
36   }
37 
38   private final CompilerMode compilerMode;
39 
AssistedErrorsTest(CompilerMode compilerMode)40   public AssistedErrorsTest(CompilerMode compilerMode) {
41     this.compilerMode = compilerMode;
42   }
43 
44   @Test
testAssistedNotWithAssistedInjectionConstructor()45   public void testAssistedNotWithAssistedInjectionConstructor() {
46     JavaFileObject foo =
47         JavaFileObjects.forSourceLines(
48             "test.Foo",
49             "package test;",
50             "",
51             "import dagger.assisted.Assisted;",
52             "",
53             "final class Foo {",
54             "  Foo(",
55             "      @Assisted String str",
56             "  ) {}",
57             "",
58             "  void someMethod(",
59             "      @Assisted int i",
60             "  ) {}",
61             "}");
62     Compilation compilation = compilerWithOptions(compilerMode.javacopts()).compile(foo);
63     assertThat(compilation).failed();
64     assertThat(compilation).hadErrorCount(2);
65     assertThat(compilation)
66         .hadErrorContaining(
67             "@Assisted parameters can only be used within an @AssistedInject-annotated constructor")
68         .inFile(foo)
69         .onLine(7);
70     assertThat(compilation)
71         .hadErrorContaining(
72             "@Assisted parameters can only be used within an @AssistedInject-annotated constructor")
73         .inFile(foo)
74         .onLine(11);
75   }
76 
77   @Test
testNestedFactoryNotStatic()78   public void testNestedFactoryNotStatic() {
79     JavaFileObject foo =
80         JavaFileObjects.forSourceLines(
81             "test.Foo",
82             "package test;",
83             "",
84             "import dagger.assisted.Assisted;",
85             "import dagger.assisted.AssistedInject;",
86             "import javax.inject.Qualifier;",
87             "",
88             "class Foo {",
89             "  @Qualifier @interface FooQualifier {}",
90             "",
91             "  @AssistedInject",
92             "  Foo(",
93             "      @FooQualifier @Assisted int i",
94             "  ) {}",
95             "}");
96     Compilation compilation = compilerWithOptions(compilerMode.javacopts()).compile(foo);
97     assertThat(compilation).failed();
98     assertThat(compilation).hadErrorCount(1);
99     assertThat(compilation)
100         .hadErrorContaining("Qualifiers cannot be used with @Assisted parameters.")
101         .inFile(foo)
102         .onLine(12);
103   }
104 }
105