1 /*
2  * Copyright (C) 2022 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.membersinject
18 
19 import com.google.common.truth.Truth.assertThat
20 import dagger.Binds
21 import dagger.Component
22 import dagger.Module
23 import dagger.Provides
24 import dagger.functional.kotlinsrc.membersinject.subpackage.MembersWithSameNameParent
25 import org.junit.Test
26 import org.junit.runner.RunWith
27 import org.junit.runners.JUnit4
28 
29 // https://github.com/google/dagger/issues/755
30 @RunWith(JUnit4::class)
31 class MembersWithSameNameTest {
32   @Test
parentInjectsMaskedMembersnull33   fun parentInjectsMaskedMembers() {
34     val parent = MembersWithSameNameParent()
35     val component = DaggerMembersWithSameNameTest_TestComponent.create()
36     component.injectParent(parent)
37     assertThat(parent.parentSameName()).isNotNull()
38     assertThat(parent.parentSameNameStringWasInvoked()).isTrue()
39     assertThat(parent.parentSameNameObjectWasInvoked()).isTrue()
40   }
41 
42   @Test
childInjectsMaskedMembersnull43   fun childInjectsMaskedMembers() {
44     val child = MembersWithSameNameChild()
45     val component = DaggerMembersWithSameNameTest_TestComponent.create()
46     component.injectChild(child)
47     assertThat(child.parentSameName()).isNotNull()
48     assertThat(child.parentSameNameStringWasInvoked()).isTrue()
49     assertThat(child.parentSameNameObjectWasInvoked()).isTrue()
50     assertThat(child.childSameName()).isNotNull()
51     assertThat(child.childSameNameStringWasInvoked()).isTrue()
52     assertThat(child.childSameNameObjectWasInvoked()).isTrue()
53   }
54 
55   @Module
56   internal abstract class TestModule {
bindObjectnull57     @Binds abstract fun bindObject(string: String): Any
58 
59     companion object {
60       @Provides fun provideString(): String = ""
61     }
62   }
63 
64   @Component(modules = [TestModule::class])
65   internal interface TestComponent {
injectParentnull66     fun injectParent(parent: MembersWithSameNameParent)
67     fun injectChild(child: MembersWithSameNameChild)
68   }
69 }
70