1 /* 2 * Copyright (C) 2022 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.builderbinds 18 19 import com.google.common.collect.ImmutableList 20 import com.google.common.truth.Truth.assertThat 21 import java.lang.IllegalStateException 22 import org.junit.Assert.fail 23 import org.junit.Test 24 import org.junit.runner.RunWith 25 import org.junit.runners.JUnit4 26 27 @RunWith(JUnit4::class) 28 class BuilderBindsTest { 29 @Test builderBindsnull30 fun builderBinds() { 31 val builder = 32 DaggerTestComponent.builder() 33 .count(5) 34 .l(10L) 35 .input("foo") 36 .nullableInput("bar") 37 .listOfString(listOf("x", "y", "z")) 38 builder.boundInSubtype(20) 39 val component = builder.build() 40 assertThat(component.count()).isEqualTo(5) 41 assertThat(component.input()).isEqualTo("foo") 42 assertThat(component.nullableInput()).isEqualTo("bar") 43 assertThat(component.listOfString()).containsExactly("x", "y", "z").inOrder() 44 } 45 46 @Test builderBindsNullableWithNullnull47 fun builderBindsNullableWithNull() { 48 val builder = 49 DaggerTestComponent.builder() 50 .count(5) 51 .l(10L) 52 .input("foo") 53 .nullableInput(null) 54 .listOfString(ImmutableList.of()) 55 builder.boundInSubtype(20) 56 val component = builder.build() 57 assertThat(component.count()).isEqualTo(5) 58 assertThat(component.input()).isEqualTo("foo") 59 assertThat(component.nullableInput()).isNull() 60 assertThat(component.listOfString()).isEmpty() 61 } 62 63 @Test builderBindsPrimitiveNotSetnull64 fun builderBindsPrimitiveNotSet() { 65 try { 66 val builder = 67 DaggerTestComponent.builder() 68 .l(10L) 69 .input("foo") 70 .nullableInput("bar") 71 .listOfString(ImmutableList.of()) 72 builder.boundInSubtype(20) 73 builder.build() 74 fail("expected IllegalStateException") 75 } catch (expected: IllegalStateException) {} 76 } 77 78 @Test builderBindsNonNullableNotSetnull79 fun builderBindsNonNullableNotSet() { 80 try { 81 val builder = 82 DaggerTestComponent.builder() 83 .count(5) 84 .l(10L) 85 .nullableInput("foo") 86 .listOfString(ImmutableList.of()) 87 builder.boundInSubtype(20) 88 builder.build() 89 fail("expected IllegalStateException") 90 } catch (expected: IllegalStateException) {} 91 } 92 93 @Test builderBindsNullableNotSetnull94 fun builderBindsNullableNotSet() { 95 val builder = 96 DaggerTestComponent.builder().count(5).l(10L).input("foo").listOfString(ImmutableList.of()) 97 builder.boundInSubtype(20) 98 val component = builder.build() 99 assertThat(component.count()).isEqualTo(5) 100 assertThat(component.input()).isEqualTo("foo") 101 assertThat(component.nullableInput()).isNull() 102 assertThat(component.listOfString()).isEmpty() 103 } 104 } 105