/* * Copyright (C) 2022 The Dagger Authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package dagger.functional.kotlinsrc.builder import com.google.common.truth.Truth.assertThat import dagger.Component import dagger.Module import dagger.Provides import org.junit.Test import org.junit.runner.RunWith import org.junit.runners.JUnit4 @RunWith(JUnit4::class) class PrivateConstructorsTest { // In Kotlin, object classes can't have constructors @Module internal object M1 { @Provides fun provideString(): String = "str" } // We suppress the warning to test the case of a "static" provides method in a module with a // private constructor. This is about as close as we can get since object classes can't have // constructors themselves. @SuppressWarnings("ClassShouldBeObject") @Module class M2 private constructor() { companion object { @Provides fun provideString(): Int = 13 } } @Component(modules = [M1::class, M2::class]) internal interface C { fun string(): String fun i(): Int @Component.Builder interface Builder { // M2 should not be required, even though its constructor is inaccessible fun build(): C } } @Test fun componentTest() { val component = DaggerPrivateConstructorsTest_C.builder().build() assertThat(component.string()).isEqualTo("str") assertThat(component.i()).isEqualTo(13) } }