• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.assertThat
21 import dagger.functional.kotlinsrc.multibindings.NestedAnnotationContainer.NestedWrappedKey
22 import dagger.multibindings.ClassKey
23 import dagger.multibindings.StringKey
24 import java.math.BigDecimal
25 import java.math.BigInteger
26 import org.junit.Test
27 import org.junit.runner.RunWith
28 import org.junit.runners.JUnit4
29 
30 /** Tests for [MultibindingComponent]. */
31 @RunWith(JUnit4::class)
32 class MultibindingTest {
33   private val multibindingComponent =
<lambda>null34     DaggerMultibindingComponent.builder().multibindingDependency { 0.0 }.build()
35 
36   @Test
mapnull37   fun map() {
38     val map = multibindingComponent.map()
39     assertThat(map).hasSize(2)
40     assertThat(map).containsEntry("foo", "foo value")
41     assertThat(map).containsEntry("bar", "bar value")
42   }
43 
44   @Test
mapOfArraysnull45   fun mapOfArrays() {
46     val map = multibindingComponent.mapOfArrays()
47     assertThat(map).hasSize(2)
48     assertThat(map).containsKey("foo")
49     assertThat(map["foo"]).asList().containsExactly("foo1", "foo2").inOrder()
50     assertThat(map).containsKey("bar")
51     assertThat(map["bar"]).asList().containsExactly("bar1", "bar2").inOrder()
52   }
53 
54   @Test
mapOfProvidersnull55   fun mapOfProviders() {
56     val mapOfProviders = multibindingComponent.mapOfProviders()
57     assertThat(mapOfProviders).hasSize(2)
58     assertThat(mapOfProviders["foo"]!!.get()).isEqualTo("foo value")
59     assertThat(mapOfProviders["bar"]!!.get()).isEqualTo("bar value")
60   }
61 
62   @Test
mapKeysAndValuesnull63   fun mapKeysAndValues() {
64     assertThat(multibindingComponent.mapKeys()).containsExactly("foo", "bar")
65     assertThat(multibindingComponent.mapValues()).containsExactly("foo value", "bar value")
66   }
67 
68   @Test
nestedKeyMapnull69   fun nestedKeyMap() {
70     assertThat(multibindingComponent.nestedKeyMap())
71       .containsExactly(
72         AutoAnnotationHolder.nestedWrappedKey(java.lang.Integer::class.java),
73         "integer",
74         AutoAnnotationHolder.nestedWrappedKey(java.lang.Long::class.java),
75         "long"
76       )
77   }
78 
79   @Test
unwrappedAnnotationKeyMapnull80   fun unwrappedAnnotationKeyMap() {
81     assertThat(multibindingComponent.unwrappedAnnotationKeyMap())
82       .containsExactly(AutoAnnotationHolder.testStringKey("foo\n"), "foo annotation")
83   }
84 
85   @Test
wrappedAnnotationKeyMapnull86   fun wrappedAnnotationKeyMap() {
87     assertThat(multibindingComponent.wrappedAnnotationKeyMap())
88       .containsExactly(
89         AutoAnnotationHolder.testWrappedAnnotationKey(
90           AutoAnnotationHolder.testStringKey("foo"),
91           intArrayOf(1, 2, 3),
92           arrayOf(),
93           arrayOf(java.lang.Long::class.java, java.lang.Integer::class.java)
94         ),
95         "wrapped foo annotation"
96       )
97   }
98 
99   @Test
booleanKeyMapnull100   fun booleanKeyMap() {
101     assertThat(multibindingComponent.booleanKeyMap()).containsExactly(true, "true")
102   }
103 
104   @Test
byteKeyMapnull105   fun byteKeyMap() {
106     assertThat(multibindingComponent.byteKeyMap()).containsExactly(100.toByte(), "100 byte")
107   }
108 
109   @Test
charKeyMapnull110   fun charKeyMap() {
111     assertThat(multibindingComponent.characterKeyMap())
112       .containsExactly('a', "a char", '\n', "newline char")
113   }
114 
115   @Test
classKeyMapnull116   fun classKeyMap() {
117     assertThat(multibindingComponent.classKeyMap())
118       .containsExactly(java.lang.Integer::class.java, "integer", java.lang.Long::class.java, "long")
119   }
120 
121   @Test
numberClassKeyMapnull122   fun numberClassKeyMap() {
123     assertThat(multibindingComponent.numberClassKeyMap())
124       .containsExactly(BigDecimal::class.java, "bigdecimal", BigInteger::class.java, "biginteger")
125   }
126 
127   @Test
intKeyMapnull128   fun intKeyMap() {
129     assertThat(multibindingComponent.integerKeyMap()).containsExactly(100, "100 int")
130   }
131 
132   @Test
longKeyMapnull133   fun longKeyMap() {
134     assertThat(multibindingComponent.longKeyMap()).containsExactly(100.toLong(), "100 long")
135   }
136 
137   @Test
shortKeyMapnull138   fun shortKeyMap() {
139     assertThat(multibindingComponent.shortKeyMap()).containsExactly(100.toShort(), "100 short")
140   }
141 
142   @Test
setBindingsnull143   fun setBindings() {
144     assertThat(multibindingComponent.set())
145       .containsExactly(-90, -17, -1, 5, 6, 832, 1742, -101, -102)
146   }
147 
148   @Test
complexQualifierSetnull149   fun complexQualifierSet() {
150     assertThat(multibindingComponent.complexQualifierStringSet()).containsExactly("foo")
151   }
152 
153   @Test
emptySetnull154   fun emptySet() {
155     assertThat(multibindingComponent.emptySet()).isEmpty()
156   }
157 
158   @Test
emptyQualifiedSetnull159   fun emptyQualifiedSet() {
160     assertThat(multibindingComponent.emptyQualifiedSet()).isEmpty()
161   }
162 
163   @Test
emptyMapnull164   fun emptyMap() {
165     assertThat(multibindingComponent.emptyMap()).isEmpty()
166   }
167 
168   @Test
emptyQualifiedMapnull169   fun emptyQualifiedMap() {
170     assertThat(multibindingComponent.emptyQualifiedMap()).isEmpty()
171   }
172 
173   @Test
maybeEmptySetnull174   fun maybeEmptySet() {
175     assertThat(multibindingComponent.maybeEmptySet()).containsExactly("foo")
176   }
177 
178   @Test
maybeEmptyQualifiedSetnull179   fun maybeEmptyQualifiedSet() {
180     assertThat(multibindingComponent.maybeEmptyQualifiedSet()).containsExactly("qualified foo")
181   }
182 
183   @Test
maybeEmptyMapnull184   fun maybeEmptyMap() {
185     assertThat(multibindingComponent.maybeEmptyMap()).containsEntry("key", "foo value")
186   }
187 
188   @Test
maybeEmptyQualifiedMapnull189   fun maybeEmptyQualifiedMap() {
190     assertThat(multibindingComponent.maybeEmptyQualifiedMap())
191       .containsEntry("key", "qualified foo value")
192   }
193 
194   // Note: @AutoAnnotation requires a static method. Normally, we would just use a companion object
195   // but that generates both a static and non-static method so we need to use a normal object.
196   object AutoAnnotationHolder {
197     @JvmStatic
198     @AutoAnnotation
testStringKeynull199     fun testStringKey(value: String): StringKey {
200       return AutoAnnotation_MultibindingTest_AutoAnnotationHolder_testStringKey(value)
201     }
202 
203     @JvmStatic
204     @AutoAnnotation
nestedWrappedKeynull205     fun nestedWrappedKey(value: Class<*>): NestedWrappedKey {
206       return AutoAnnotation_MultibindingTest_AutoAnnotationHolder_nestedWrappedKey(value)
207     }
208 
209     @JvmStatic
210     @AutoAnnotation
testWrappedAnnotationKeynull211     fun testWrappedAnnotationKey(
212       value: StringKey,
213       integers: IntArray,
214       annotations: Array<ClassKey>,
215       classes: Array<Class<out Number>>
216     ): WrappedAnnotationKey {
217       return AutoAnnotation_MultibindingTest_AutoAnnotationHolder_testWrappedAnnotationKey(
218         value,
219         integers,
220         annotations,
221         classes
222       )
223     }
224   }
225 }
226