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.Component 22 import dagger.MapKey 23 import dagger.Module 24 import dagger.Provides 25 import dagger.multibindings.IntoMap 26 import org.junit.Test 27 import org.junit.runner.RunWith 28 import org.junit.runners.JUnit4 29 30 @RunWith(JUnit4::class) 31 class MapKeyWithDefaultTest { 32 @MapKey(unwrapValue = false) 33 annotation class MapKeyWithDefault(val hasDefault: Boolean = true, val required: Boolean) 34 35 @Module 36 internal interface TestModule { 37 companion object { justRequirednull38 @Provides @IntoMap @MapKeyWithDefault(required = false) fun justRequired(): Int = 1 39 40 @Provides 41 @IntoMap 42 @MapKeyWithDefault(required = false, hasDefault = false) 43 fun both(): Int = 2 44 } 45 } 46 47 @Component(modules = [TestModule::class]) 48 internal interface TestComponent { 49 fun map(): Map<MapKeyWithDefault, Int> 50 } 51 52 @Test testnull53 fun test() { 54 val map = DaggerMapKeyWithDefaultTest_TestComponent.create().map() 55 assertThat(map).hasSize(2) 56 assertThat(map[AutoAnnotationHolder.mapKey(true, false)]).isEqualTo(1) 57 assertThat(map[AutoAnnotationHolder.mapKey(false, false)]).isEqualTo(2) 58 } 59 60 // Note: @AutoAnnotation requires a static method. Normally, we would just use a companion object 61 // but that generates both a static and non-static method so we need to use a normal object. 62 object AutoAnnotationHolder { 63 @JvmStatic 64 @AutoAnnotation mapKeynull65 fun mapKey(hasDefault: Boolean, required: Boolean): MapKeyWithDefault { 66 return AutoAnnotation_MapKeyWithDefaultTest_AutoAnnotationHolder_mapKey(hasDefault, required) 67 } 68 } 69 } 70