1 /* 2 * Copyright (C) 2023 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.functional.kotlinsrc.assisted 18 19 import com.google.common.truth.Truth.assertThat 20 import dagger.Binds 21 import dagger.Component 22 import dagger.Module 23 import dagger.assisted.Assisted 24 import dagger.assisted.AssistedFactory 25 import dagger.assisted.AssistedInject 26 import javax.inject.Inject 27 import org.junit.Test 28 import org.junit.runner.RunWith 29 import org.junit.runners.JUnit4 30 31 @RunWith(JUnit4::class) 32 internal class AssistedFactoryBindsTest { 33 @Component(modules = [FooFactoryModule::class]) 34 interface ParentComponent { 35 // Test using @Binds where Foo => FooImpl and FooFactory => FooFactoryImpl fooFactorynull36 fun fooFactory(): FooFactory 37 } 38 39 @Module 40 interface FooFactoryModule { 41 @Binds fun bind(impl: FooFactoryImpl): FooFactory 42 } 43 44 interface Foo 45 46 class FooImpl 47 @AssistedInject 48 constructor(val dep: Dep, @Assisted val assistedDep: AssistedDep) : Foo 49 50 interface FooFactory { createnull51 fun create(assistedDep: AssistedDep): Foo 52 } 53 54 @AssistedFactory 55 interface FooFactoryImpl : FooFactory { 56 override fun create(assistedDep: AssistedDep): FooImpl 57 } 58 59 class AssistedDep 60 61 class Dep @Inject constructor() 62 63 @Test testFooFactorynull64 fun testFooFactory() { 65 val fooFactory = DaggerAssistedFactoryBindsTest_ParentComponent.create().fooFactory() 66 assertThat(fooFactory).isInstanceOf(FooFactoryImpl::class.java) 67 val assistedDep = AssistedDep() 68 val foo = fooFactory.create(assistedDep) 69 assertThat(foo).isInstanceOf(FooImpl::class.java) 70 val fooImpl = foo as FooImpl 71 assertThat(fooImpl.dep).isNotNull() 72 assertThat(fooImpl.assistedDep).isEqualTo(assistedDep) 73 } 74 } 75