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.nullables 18 19 import com.google.common.truth.Truth.assertThat 20 import dagger.Component 21 import dagger.Module 22 import dagger.Provides 23 import org.jspecify.annotations.Nullable 24 import org.junit.Test 25 import org.junit.runner.RunWith 26 import org.junit.runners.JUnit4 27 28 // This is a regression test for b/290632844. 29 @RunWith(JUnit4::class) 30 public final class JspecifyNullableTest { 31 @Component(modules = [MyIntComponent.MyModule::class]) 32 interface MyIntComponent { getIntnull33 fun getInt(): Int 34 35 @Module 36 class MyModule(val intValue: Int) { 37 // Check that using @Nullable on the type is ignored (matching KAPT). 38 @Provides fun provideInt(): @Nullable Int = intValue 39 } 40 } 41 42 @Component(modules = [MyNullableStringComponent.MyModule::class]) 43 interface MyNullableStringComponent { getStringnull44 fun getString(): String? 45 46 @Module 47 class MyModule(val stringValue: String?) { 48 // Check that using @Nullable on the type is ignored (matching KAPT). 49 @Provides fun provideString(): @Nullable String? = stringValue 50 } 51 } 52 53 @Test testIntWithValuenull54 public fun testIntWithValue() { 55 val component = 56 DaggerJspecifyNullableTest_MyIntComponent.builder() 57 .myModule(MyIntComponent.MyModule(15)) 58 .build() 59 assertThat(component.getInt()).isEqualTo(15) 60 } 61 62 @Test testStringWithValuenull63 public fun testStringWithValue() { 64 val component = 65 DaggerJspecifyNullableTest_MyNullableStringComponent.builder() 66 .myModule(MyNullableStringComponent.MyModule("TEST_VALUE")) 67 .build() 68 assertThat(component.getString()).isEqualTo("TEST_VALUE") 69 } 70 71 @Test testStringWithNullnull72 public fun testStringWithNull() { 73 val component = 74 DaggerJspecifyNullableTest_MyNullableStringComponent.builder() 75 .myModule(MyNullableStringComponent.MyModule(null)) 76 .build() 77 assertThat(component.getString()).isNull() 78 } 79 } 80