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.subcomponent.repeat
18 
19 import com.google.common.truth.Truth.assertThat
20 import org.junit.Assert
21 import org.junit.Before
22 import org.junit.Test
23 import org.junit.runner.RunWith
24 import org.junit.runners.JUnit4
25 
26 @RunWith(JUnit4::class)
27 class RepeatedModuleTest {
28   private lateinit var parentComponent: ParentComponent
29 
30   @Before
initializeParentComponentnull31   fun initializeParentComponent() {
32     parentComponent = DaggerParentComponent.builder().build()
33   }
34 
35   @Test
repeatedModuleHasSameStateInSubcomponentnull36   fun repeatedModuleHasSameStateInSubcomponent() {
37     val childComponent = parentComponent.newChildComponentBuilder().build()
38     assertThat(parentComponent.state()).isSameInstanceAs(childComponent.state())
39   }
40 
41   @Test
repeatedModuleHasSameStateInGrandchildSubcomponentnull42   fun repeatedModuleHasSameStateInGrandchildSubcomponent() {
43     val childComponent = parentComponent.newChildComponentWithoutRepeatedModule()
44     val grandchildComponent: SubcomponentWithRepeatedModule =
45       childComponent.newGrandchildBuilder().build()
46     assertThat(parentComponent.state()).isSameInstanceAs(grandchildComponent.state())
47   }
48 
49   @Test
repeatedModuleBuilderThrowsInSubcomponentnull50   fun repeatedModuleBuilderThrowsInSubcomponent() {
51     val childComponentBuilder = parentComponent.newChildComponentBuilder()
52     try {
53       childComponentBuilder.repeatedModule(RepeatedModule())
54       Assert.fail()
55     } catch (expected: UnsupportedOperationException) {
56       assertThat(expected)
57         .hasMessageThat()
58         .isEqualTo(
59           "dagger.functional.kotlinsrc.subcomponent.repeat.RepeatedModule cannot be set " +
60             "because it is inherited from the enclosing component"
61         )
62     }
63   }
64 
65   @Test
repeatedModuleBuilderThrowsInGrandchildSubcomponentnull66   fun repeatedModuleBuilderThrowsInGrandchildSubcomponent() {
67     val childComponent = parentComponent.newChildComponentWithoutRepeatedModule()
68     val grandchildComponentBuilder = childComponent.newGrandchildBuilder()
69     try {
70       grandchildComponentBuilder.repeatedModule(RepeatedModule())
71       Assert.fail()
72     } catch (expected: UnsupportedOperationException) {
73       assertThat(expected)
74         .hasMessageThat()
75         .isEqualTo(
76           "dagger.functional.kotlinsrc.subcomponent.repeat.RepeatedModule cannot be set " +
77             "because it is inherited from the enclosing component"
78         )
79     }
80   }
81 }
82