|
86 | 86 | import java.io.IOException; |
87 | 87 | import java.util.Arrays; |
88 | 88 | import java.util.List; |
| 89 | +import java.util.Map; |
89 | 90 | import java.util.Optional; |
90 | 91 | import java.util.stream.Stream; |
91 | 92 | import javax.annotation.Nullable; |
@@ -2993,6 +2994,73 @@ public void initializer_basic() throws Exception { |
2993 | 2994 | assertThat((List<String>) info.getValue("deps")).containsExactly("@@//:initial", "@@//:added"); |
2994 | 2995 | } |
2995 | 2996 |
|
| 2997 | + @Test |
| 2998 | + @SuppressWarnings("unchecked") |
| 2999 | + public void initializer_stringListDict() throws Exception { |
| 3000 | + scratch.file( |
| 3001 | + "initializer_testing/b.bzl", |
| 3002 | + "def initializer(**kwargs):", |
| 3003 | + " return {}", |
| 3004 | + "MyInfo = provider()", |
| 3005 | + "def impl(ctx): ", |
| 3006 | + " return [MyInfo(dict = ctx.attr.dict)]", |
| 3007 | + "my_rule = rule(impl,", |
| 3008 | + " initializer = initializer,", |
| 3009 | + " attrs = {", |
| 3010 | + " 'dict': attr.string_list_dict(),", |
| 3011 | + " })"); |
| 3012 | + scratch.file( |
| 3013 | + "initializer_testing/BUILD", // |
| 3014 | + "load(':b.bzl','my_rule')", |
| 3015 | + "my_rule(name = 'my_target', dict = {'k': ['val']})"); |
| 3016 | + |
| 3017 | + ConfiguredTarget myTarget = getConfiguredTarget("//initializer_testing:my_target"); |
| 3018 | + StructImpl info = |
| 3019 | + (StructImpl) |
| 3020 | + myTarget.get( |
| 3021 | + new StarlarkProvider.Key( |
| 3022 | + Label.parseCanonical("//initializer_testing:b.bzl"), "MyInfo")); |
| 3023 | + |
| 3024 | + assertThat(((Map<String, List<String>>) info.getValue("dict")).keySet()).containsExactly("k"); |
| 3025 | + assertThat(((Map<String, List<String>>) info.getValue("dict")).get("k")).containsExactly("val"); |
| 3026 | + } |
| 3027 | + |
| 3028 | + @Test |
| 3029 | + @SuppressWarnings("unchecked") |
| 3030 | + public void initializer_labelKeyedStringDict() throws Exception { |
| 3031 | + scratch.file( |
| 3032 | + "BUILD", // |
| 3033 | + "filegroup(name = 'key')"); |
| 3034 | + scratch.file( |
| 3035 | + "initializer_testing/b.bzl", |
| 3036 | + "def initializer(**kwargs):", |
| 3037 | + " return {}", |
| 3038 | + "MyInfo = provider()", |
| 3039 | + "def impl(ctx): ", |
| 3040 | + " return [MyInfo(dict = ctx.attr.dict)]", |
| 3041 | + "my_rule = rule(impl,", |
| 3042 | + " initializer = initializer,", |
| 3043 | + " attrs = {", |
| 3044 | + " 'dict': attr.label_keyed_string_dict(),", |
| 3045 | + " })"); |
| 3046 | + scratch.file( |
| 3047 | + "initializer_testing/BUILD", // |
| 3048 | + "load(':b.bzl','my_rule')", |
| 3049 | + "my_rule(name = 'my_target', dict = {'//:key': 'val'})"); |
| 3050 | + |
| 3051 | + ConfiguredTarget myTarget = getConfiguredTarget("//initializer_testing:my_target"); |
| 3052 | + ConfiguredTarget key = getConfiguredTarget("//:key"); |
| 3053 | + StructImpl info = |
| 3054 | + (StructImpl) |
| 3055 | + myTarget.get( |
| 3056 | + new StarlarkProvider.Key( |
| 3057 | + Label.parseCanonical("//initializer_testing:b.bzl"), "MyInfo")); |
| 3058 | + |
| 3059 | + assertThat(((Map<ConfiguredTarget, String>) info.getValue("dict")).keySet()) |
| 3060 | + .containsExactly(key); |
| 3061 | + assertThat(((Map<ConfiguredTarget, String>) info.getValue("dict")).get(key)).isEqualTo("val"); |
| 3062 | + } |
| 3063 | + |
2996 | 3064 | @Test |
2997 | 3065 | public void initializer_legacyAnyType() throws Exception { |
2998 | 3066 | scratch.file( |
|
0 commit comments