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 androidx.room.compiler.processing.util.Source; 20 import com.google.common.collect.ImmutableCollection; 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 AssistedErrorsTest { 29 @Parameters(name = "{0}") parameters()30 public static ImmutableCollection<Object[]> parameters() { 31 return CompilerMode.TEST_PARAMETERS; 32 } 33 34 private final CompilerMode compilerMode; 35 AssistedErrorsTest(CompilerMode compilerMode)36 public AssistedErrorsTest(CompilerMode compilerMode) { 37 this.compilerMode = compilerMode; 38 } 39 40 @Test testAssistedNotWithAssistedInjectionConstructor()41 public void testAssistedNotWithAssistedInjectionConstructor() { 42 Source foo = 43 CompilerTests.javaSource( 44 "test.Foo", 45 "package test;", 46 "", 47 "import dagger.assisted.Assisted;", 48 "", 49 "final class Foo {", 50 " Foo(", 51 " @Assisted String str", 52 " ) {}", 53 "", 54 " void someMethod(", 55 " @Assisted int i", 56 " ) {}", 57 "}"); 58 CompilerTests.daggerCompiler(foo) 59 .withProcessingOptions(compilerMode.processorOptions()) 60 .compile( 61 subject -> { 62 subject.hasErrorCount(2); 63 subject.hasErrorContaining( 64 "@Assisted parameters can only be used within an @AssistedInject-annotated " 65 + "constructor") 66 .onSource(foo) 67 .onLine(7); 68 }); 69 } 70 71 @Test testNestedFactoryNotStatic()72 public void testNestedFactoryNotStatic() { 73 Source foo = 74 CompilerTests.javaSource( 75 "test.Foo", 76 "package test;", 77 "", 78 "import dagger.assisted.Assisted;", 79 "import dagger.assisted.AssistedInject;", 80 "import javax.inject.Qualifier;", 81 "", 82 "class Foo {", 83 " @Qualifier @interface FooQualifier {}", 84 "", 85 " @AssistedInject", 86 " Foo(", 87 " @FooQualifier @Assisted int i", 88 " ) {}", 89 "}"); 90 CompilerTests.daggerCompiler(foo) 91 .withProcessingOptions(compilerMode.processorOptions()) 92 .compile( 93 subject -> { 94 subject.hasErrorCount(1); 95 subject.hasErrorContaining("Qualifiers cannot be used with @Assisted parameters.") 96 .onSource(foo) 97 .onLine(12); 98 }); 99 } 100 } 101