Skip to content

Commit

Permalink
Reorganize functional multibinding tests into separate targets.
Browse files Browse the repository at this point in the history
This is part of an on-going refactor to clean up Dagger's tests. The new structure follows Bazel best practices and has a number of benefits such as:

  * Makes it clear which classes go with a particular test class just by reading the BUILD file
  * Makes error messages clearer because a problematic class will only cause its corresponding test to fail and not unrelated tests.
  * Speeds up builds, as tests don't need to wait for unrelated classes to build.

RELNOTES=N/A
PiperOrigin-RevId: 660590373
  • Loading branch information
bcorso authored and Dagger Team committed Aug 8, 2024
1 parent 486be28 commit 78be2f5
Show file tree
Hide file tree
Showing 7 changed files with 248 additions and 41 deletions.
83 changes: 80 additions & 3 deletions javatests/dagger/functional/kotlinsrc/multibindings/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,24 @@ load("//:test_defs.bzl", "GenKtTests")
package(default_visibility = ["//:src"])

GenKtTests(
name = "multibindings",
srcs = glob(["*.kt"]),
name = "MultibindingTest",
srcs = [
"BooleanKey.kt",
"ByteKey.kt",
"CharKey.kt",
"MultibindingComponent.kt",
"MultibindingDependency.kt",
"MultibindingModule.kt",
"MultibindingTest.kt",
"MultibindsModule.kt",
"NestedAnnotationContainer.kt",
"NumberClassKey.kt",
"ShortKey.kt",
"UnwrappedAnnotationKey.kt",
"WrappedAnnotationKey.kt",
],
gen_library_deps = [
"//javatests/dagger/functional/kotlinsrc/multibindings/subpackage",
"//javatests/dagger/functional/kotlinsrc/multibindings/subpackage:ContributionsModule",
],
javacopts = DOCLINT_HTML_AND_SYNTAX + DOCLINT_REFERENCES,
deps = [
Expand All @@ -38,3 +52,66 @@ GenKtTests(
"//third_party/java/truth",
],
)

GenKtTests(
name = "BindsInaccessibleMapKeyTest",
srcs = ["BindsInaccessibleMapKeyTest.kt"],
gen_library_deps = [
"//javatests/dagger/functional/kotlinsrc/multibindings/subpackage:BindsInaccessibleMapKeyModule",
],
javacopts = DOCLINT_HTML_AND_SYNTAX + DOCLINT_REFERENCES,
deps = [
"//:dagger_with_compiler",
"//third_party/java/junit",
"//third_party/java/truth",
],
)

GenKtTests(
name = "ClassKeyWithGenericsTest",
srcs = ["ClassKeyWithGenericsTest.kt"],
javacopts = DOCLINT_HTML_AND_SYNTAX + DOCLINT_REFERENCES,
deps = [
"//:dagger_with_compiler",
"//third_party/java/junit",
"//third_party/java/truth",
],
)

GenKtTests(
name = "ComplexMapKeysInDifferentOrderTest",
srcs = ["ComplexMapKeysInDifferentOrderTest.kt"],
javacopts = DOCLINT_HTML_AND_SYNTAX + DOCLINT_REFERENCES,
deps = [
"//:dagger_with_compiler",
"//third_party/java/auto:value",
"//third_party/java/junit",
"//third_party/java/truth",
],
)

GenKtTests(
name = "MapKeyWithDefaultTest",
srcs = ["MapKeyWithDefaultTest.kt"],
javacopts = DOCLINT_HTML_AND_SYNTAX + DOCLINT_REFERENCES,
deps = [
"//:dagger_with_compiler",
"//third_party/java/auto:value",
"//third_party/java/junit",
"//third_party/java/truth",
],
)

GenKtTests(
name = "LazyMapsTest",
srcs = [
"LazyMaps.kt",
"LazyMapsTest.kt",
],
javacopts = DOCLINT_HTML_AND_SYNTAX + DOCLINT_REFERENCES,
deps = [
"//:dagger_with_compiler",
"//third_party/java/junit",
"//third_party/java/truth",
],
)
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2023 The Dagger Authors.
* Copyright (C) 2024 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.
Expand All @@ -16,11 +16,26 @@

package dagger.functional.kotlinsrc.multibindings

import com.google.common.truth.Truth.assertThat
import dagger.Component
import dagger.functional.kotlinsrc.multibindings.subpackage.BindsInaccessibleMapKeyModule
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4

// b/73820357
@Component(modules = [BindsInaccessibleMapKeyModule::class])
internal interface BindsInaccessibleMapKey {
fun mapWithAnInaccessibleMapKey(): Map<Class<*>, Any>
@RunWith(JUnit4::class)
class BindsInaccessibleMapKeyTest {
@Component(modules = [BindsInaccessibleMapKeyModule::class])
internal interface TestComponent {
fun mapWithAnInaccessibleMapKey(): Map<Class<*>, Any>
}

@Test
fun test() {
val map = DaggerBindsInaccessibleMapKeyTest_TestComponent.create().mapWithAnInaccessibleMapKey()
assertThat(map).hasSize(1)
assertThat(map.keys.single().canonicalName)
.isEqualTo("dagger.functional.kotlinsrc.multibindings.subpackage.Inaccessible");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,17 @@ load("//:test_defs.bzl", "GenKtLibrary")
package(default_visibility = ["//:src"])

GenKtLibrary(
name = "subpackage",
srcs = glob(["*.kt"]),
name = "ContributionsModule",
srcs = ["ContributionsModule.kt"],
javacopts = DOCLINT_HTML_AND_SYNTAX + DOCLINT_REFERENCES,
deps = [
"//:dagger_with_compiler",
],
)

GenKtLibrary(
name = "BindsInaccessibleMapKeyModule",
srcs = ["BindsInaccessibleMapKeyModule.kt"],
javacopts = DOCLINT_HTML_AND_SYNTAX + DOCLINT_REFERENCES,
deps = [
"//:dagger_with_compiler",
Expand Down
83 changes: 80 additions & 3 deletions javatests/dagger/functional/multibindings/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,94 @@ load("//:test_defs.bzl", "GenJavaTests")
package(default_visibility = ["//:src"])

GenJavaTests(
name = "multibindings",
srcs = glob(["*.java"]),
name = "MultibindingTest",
srcs = [
"BooleanKey.java",
"ByteKey.java",
"CharKey.java",
"MultibindingComponent.java",
"MultibindingDependency.java",
"MultibindingModule.java",
"MultibindingTest.java",
"MultibindsModule.java",
"NestedAnnotationContainer.java",
"NumberClassKey.java",
"ShortKey.java",
"UnwrappedAnnotationKey.java",
"WrappedAnnotationKey.java",
],
gen_library_deps = [
"//javatests/dagger/functional/multibindings/subpackage",
"//javatests/dagger/functional/multibindings/subpackage:ContributionsModule",
],
javacopts = DOCLINT_HTML_AND_SYNTAX + DOCLINT_REFERENCES,
deps = [
"//:dagger_with_compiler",
"//third_party/java/auto:value",
"//third_party/java/junit",
"//third_party/java/truth",
],
)

GenJavaTests(
name = "BindsInaccessibleMapKeyTest",
srcs = ["BindsInaccessibleMapKeyTest.java"],
gen_library_deps = [
"//javatests/dagger/functional/multibindings/subpackage:BindsInaccessibleMapKeyModule",
],
javacopts = DOCLINT_HTML_AND_SYNTAX + DOCLINT_REFERENCES,
deps = [
"//:dagger_with_compiler",
"//third_party/java/guava/collect",
"//third_party/java/junit",
"//third_party/java/truth",
],
)

GenJavaTests(
name = "ClassKeyWithGenericsTest",
srcs = ["ClassKeyWithGenericsTest.java"],
javacopts = DOCLINT_HTML_AND_SYNTAX + DOCLINT_REFERENCES,
deps = [
"//:dagger_with_compiler",
"//third_party/java/junit",
"//third_party/java/truth",
],
)

GenJavaTests(
name = "ComplexMapKeysInDifferentOrderTest",
srcs = ["ComplexMapKeysInDifferentOrderTest.java"],
javacopts = DOCLINT_HTML_AND_SYNTAX + DOCLINT_REFERENCES,
deps = [
"//:dagger_with_compiler",
"//third_party/java/auto:value",
"//third_party/java/junit",
"//third_party/java/truth",
],
)

GenJavaTests(
name = "MapKeyWithDefaultTest",
srcs = ["MapKeyWithDefaultTest.java"],
javacopts = DOCLINT_HTML_AND_SYNTAX + DOCLINT_REFERENCES,
deps = [
"//:dagger_with_compiler",
"//third_party/java/auto:value",
"//third_party/java/junit",
"//third_party/java/truth",
],
)

GenJavaTests(
name = "LazyMapsTest",
srcs = [
"LazyMaps.java",
"LazyMapsTest.java",
],
javacopts = DOCLINT_HTML_AND_SYNTAX + DOCLINT_REFERENCES,
deps = [
"//:dagger_with_compiler",
"//third_party/java/junit",
"//third_party/java/truth",
],
)

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (C) 2018 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.multibindings;

import static com.google.common.collect.Iterables.getOnlyElement;
import static com.google.common.truth.Truth.assertThat;

import dagger.Component;
import dagger.functional.multibindings.subpackage.BindsInaccessibleMapKeyModule;
import java.util.Map;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

// b/73820357
@RunWith(JUnit4.class)
public class BindsInaccessibleMapKeyTest {
@Component(modules = BindsInaccessibleMapKeyModule.class)
interface TestComponent {
Map<Class<?>, Object> mapWithAnInaccessibleMapKey();
}

@Test
public void test() {
Map<Class<?>, Object> map =
DaggerBindsInaccessibleMapKeyTest_TestComponent.create().mapWithAnInaccessibleMapKey();
assertThat(map).hasSize(1);
assertThat(getOnlyElement(map.keySet()).getCanonicalName())
.isEqualTo(
"dagger.functional.multibindings.subpackage."
+ "BindsInaccessibleMapKeyModule.Inaccessible");
}
}
13 changes: 11 additions & 2 deletions javatests/dagger/functional/multibindings/subpackage/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,17 @@ load("//:test_defs.bzl", "GenJavaLibrary")
package(default_visibility = ["//:src"])

GenJavaLibrary(
name = "subpackage",
srcs = glob(["*.java"]),
name = "ContributionsModule",
srcs = ["ContributionsModule.java"],
javacopts = DOCLINT_HTML_AND_SYNTAX + DOCLINT_REFERENCES,
deps = [
"//:dagger_with_compiler",
],
)

GenJavaLibrary(
name = "BindsInaccessibleMapKeyModule",
srcs = ["BindsInaccessibleMapKeyModule.java"],
javacopts = DOCLINT_HTML_AND_SYNTAX + DOCLINT_REFERENCES,
deps = [
"//:dagger_with_compiler",
Expand Down

0 comments on commit 78be2f5

Please sign in to comment.