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.multibindings
18 
19 import com.google.auto.value.AutoAnnotation
20 import com.google.common.truth.Truth
21 import com.google.common.truth.Truth.assertThat
22 import dagger.Component
23 import dagger.MapKey
24 import dagger.Module
25 import dagger.Provides
26 import dagger.multibindings.IntoMap
27 import org.junit.Test
28 import org.junit.runner.RunWith
29 import org.junit.runners.JUnit4
30 
31 @RunWith(JUnit4::class)
32 class ComplexMapKeysInDifferentOrderTest {
33   @MapKey(unwrapValue = false) annotation class ComplexMapKey(val i: Int, val j: Int)
34 
35   @Module
36   internal interface TestModule {
37     companion object {
inOrdernull38       @Provides @IntoMap @ComplexMapKey(i = 1, j = 2) fun inOrder(): Int = 3
39 
40       @Provides @IntoMap @ComplexMapKey(j = 4, i = 5) fun backwardsOrder(): Int = 6
41     }
42   }
43 
44   @Component(modules = [TestModule::class])
45   internal interface TestComponent {
46     fun map(): Map<ComplexMapKey, Int>
47   }
48 
49   @Test
testnull50   fun test() {
51     val map = DaggerComplexMapKeysInDifferentOrderTest_TestComponent.create().map()
52     assertThat(map[AutoAnnotationHolder.mapKey(1, 2)]).isEqualTo(3)
53     assertThat(map[AutoAnnotationHolder.mapKey(5, 4)]).isEqualTo(6)
54   }
55 
56   // Note: @AutoAnnotation requires a static method. Normally, we would just use a companion object
57   // but that generates both a static and non-static method so we need to use a normal object.
58   object AutoAnnotationHolder {
59     @JvmStatic
60     @AutoAnnotation
mapKeynull61     fun mapKey(i: Int, j: Int): ComplexMapKey {
62       return AutoAnnotation_ComplexMapKeysInDifferentOrderTest_AutoAnnotationHolder_mapKey(i, j)
63     }
64   }
65 }
66