| Cole Faust | 5a231bd | 2024-02-07 09:43:59 -0800 | [diff] [blame] | 1 | // Copyright 2024 Google Inc. All rights reserved. | 
|  | 2 | // | 
|  | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 4 | // you may not use this file except in compliance with the License. | 
|  | 5 | // You may obtain a copy of the License at | 
|  | 6 | // | 
|  | 7 | //     http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 8 | // | 
|  | 9 | // Unless required by applicable law or agreed to in writing, software | 
|  | 10 | // distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 12 | // See the License for the specific language governing permissions and | 
|  | 13 | // limitations under the License. | 
|  | 14 |  | 
|  | 15 | package android | 
|  | 16 |  | 
|  | 17 | import ( | 
|  | 18 | "fmt" | 
|  | 19 | "reflect" | 
|  | 20 | "testing" | 
|  | 21 |  | 
|  | 22 | "github.com/google/blueprint" | 
|  | 23 | "github.com/google/blueprint/proptools" | 
|  | 24 | ) | 
|  | 25 |  | 
|  | 26 | func TestSelects(t *testing.T) { | 
|  | 27 | testCases := []struct { | 
|  | 28 | name          string | 
|  | 29 | bp            string | 
|  | 30 | provider      selectsTestProvider | 
|  | 31 | vendorVars    map[string]map[string]string | 
|  | 32 | expectedError string | 
|  | 33 | }{ | 
|  | 34 | { | 
|  | 35 | name: "basic string list", | 
|  | 36 | bp: ` | 
|  | 37 | my_module_type { | 
|  | 38 | name: "foo", | 
|  | 39 | my_string_list: select(soong_config_variable("my_namespace", "my_variable"), { | 
|  | 40 | "a": ["a.cpp"], | 
|  | 41 | "b": ["b.cpp"], | 
| Cole Faust | 683316a | 2024-04-02 16:45:54 -0700 | [diff] [blame] | 42 | default: ["c.cpp"], | 
| Cole Faust | 5a231bd | 2024-02-07 09:43:59 -0800 | [diff] [blame] | 43 | }), | 
|  | 44 | } | 
|  | 45 | `, | 
|  | 46 | provider: selectsTestProvider{ | 
|  | 47 | my_string_list: &[]string{"c.cpp"}, | 
|  | 48 | }, | 
|  | 49 | }, | 
|  | 50 | { | 
|  | 51 | name: "basic string", | 
|  | 52 | bp: ` | 
|  | 53 | my_module_type { | 
|  | 54 | name: "foo", | 
|  | 55 | my_string: select(soong_config_variable("my_namespace", "my_variable"), { | 
|  | 56 | "a": "a.cpp", | 
|  | 57 | "b": "b.cpp", | 
| Cole Faust | 683316a | 2024-04-02 16:45:54 -0700 | [diff] [blame] | 58 | default: "c.cpp", | 
| Cole Faust | 5a231bd | 2024-02-07 09:43:59 -0800 | [diff] [blame] | 59 | }), | 
|  | 60 | } | 
|  | 61 | `, | 
|  | 62 | provider: selectsTestProvider{ | 
|  | 63 | my_string: proptools.StringPtr("c.cpp"), | 
|  | 64 | }, | 
|  | 65 | }, | 
|  | 66 | { | 
|  | 67 | name: "basic bool", | 
|  | 68 | bp: ` | 
|  | 69 | my_module_type { | 
|  | 70 | name: "foo", | 
|  | 71 | my_bool: select(soong_config_variable("my_namespace", "my_variable"), { | 
|  | 72 | "a": true, | 
|  | 73 | "b": false, | 
| Cole Faust | 683316a | 2024-04-02 16:45:54 -0700 | [diff] [blame] | 74 | default: true, | 
| Cole Faust | 5a231bd | 2024-02-07 09:43:59 -0800 | [diff] [blame] | 75 | }), | 
|  | 76 | } | 
|  | 77 | `, | 
|  | 78 | provider: selectsTestProvider{ | 
|  | 79 | my_bool: proptools.BoolPtr(true), | 
|  | 80 | }, | 
|  | 81 | }, | 
|  | 82 | { | 
| Cole Faust | bdd8aee | 2024-03-14 14:33:02 -0700 | [diff] [blame] | 83 | name: "basic paths", | 
|  | 84 | bp: ` | 
|  | 85 | my_module_type { | 
|  | 86 | name: "foo", | 
|  | 87 | my_paths: select(soong_config_variable("my_namespace", "my_variable"), { | 
|  | 88 | "a": ["foo.txt"], | 
|  | 89 | "b": ["bar.txt"], | 
| Cole Faust | 683316a | 2024-04-02 16:45:54 -0700 | [diff] [blame] | 90 | default: ["baz.txt"], | 
| Cole Faust | bdd8aee | 2024-03-14 14:33:02 -0700 | [diff] [blame] | 91 | }), | 
|  | 92 | } | 
|  | 93 | `, | 
|  | 94 | provider: selectsTestProvider{ | 
|  | 95 | my_paths: &[]string{"baz.txt"}, | 
|  | 96 | }, | 
|  | 97 | }, | 
|  | 98 | { | 
|  | 99 | name: "paths with module references", | 
|  | 100 | bp: ` | 
|  | 101 | my_module_type { | 
|  | 102 | name: "foo", | 
|  | 103 | my_paths: select(soong_config_variable("my_namespace", "my_variable"), { | 
|  | 104 | "a": [":a"], | 
|  | 105 | "b": [":b"], | 
| Cole Faust | 683316a | 2024-04-02 16:45:54 -0700 | [diff] [blame] | 106 | default: [":c"], | 
| Cole Faust | bdd8aee | 2024-03-14 14:33:02 -0700 | [diff] [blame] | 107 | }), | 
|  | 108 | } | 
|  | 109 | `, | 
|  | 110 | expectedError: `"foo" depends on undefined module "c"`, | 
|  | 111 | }, | 
|  | 112 | { | 
| Cole Faust | 5a231bd | 2024-02-07 09:43:59 -0800 | [diff] [blame] | 113 | name: "Differing types", | 
|  | 114 | bp: ` | 
|  | 115 | my_module_type { | 
|  | 116 | name: "foo", | 
|  | 117 | my_string: select(soong_config_variable("my_namespace", "my_variable"), { | 
|  | 118 | "a": "a.cpp", | 
|  | 119 | "b": true, | 
| Cole Faust | 683316a | 2024-04-02 16:45:54 -0700 | [diff] [blame] | 120 | default: "c.cpp", | 
| Cole Faust | 5a231bd | 2024-02-07 09:43:59 -0800 | [diff] [blame] | 121 | }), | 
|  | 122 | } | 
|  | 123 | `, | 
| Cole Faust | 12c8ed4 | 2024-03-28 16:26:59 -0700 | [diff] [blame] | 124 | expectedError: `Android.bp:8:5: Found select statement with differing types "string" and "bool" in its cases`, | 
|  | 125 | }, | 
|  | 126 | { | 
|  | 127 | name: "Select type doesn't match property type", | 
|  | 128 | bp: ` | 
|  | 129 | my_module_type { | 
|  | 130 | name: "foo", | 
|  | 131 | my_string: select(soong_config_variable("my_namespace", "my_variable"), { | 
|  | 132 | "a": false, | 
|  | 133 | "b": true, | 
| Cole Faust | 683316a | 2024-04-02 16:45:54 -0700 | [diff] [blame] | 134 | default: true, | 
| Cole Faust | 12c8ed4 | 2024-03-28 16:26:59 -0700 | [diff] [blame] | 135 | }), | 
|  | 136 | } | 
|  | 137 | `, | 
|  | 138 | expectedError: `can't assign bool value to string property "my_string\[0\]"`, | 
| Cole Faust | 5a231bd | 2024-02-07 09:43:59 -0800 | [diff] [blame] | 139 | }, | 
|  | 140 | { | 
|  | 141 | name: "String list non-default", | 
|  | 142 | bp: ` | 
|  | 143 | my_module_type { | 
|  | 144 | name: "foo", | 
|  | 145 | my_string_list: select(soong_config_variable("my_namespace", "my_variable"), { | 
|  | 146 | "a": ["a.cpp"], | 
|  | 147 | "b": ["b.cpp"], | 
| Cole Faust | 683316a | 2024-04-02 16:45:54 -0700 | [diff] [blame] | 148 | default: ["c.cpp"], | 
| Cole Faust | 5a231bd | 2024-02-07 09:43:59 -0800 | [diff] [blame] | 149 | }), | 
|  | 150 | } | 
|  | 151 | `, | 
|  | 152 | provider: selectsTestProvider{ | 
|  | 153 | my_string_list: &[]string{"a.cpp"}, | 
|  | 154 | }, | 
|  | 155 | vendorVars: map[string]map[string]string{ | 
|  | 156 | "my_namespace": { | 
|  | 157 | "my_variable": "a", | 
|  | 158 | }, | 
|  | 159 | }, | 
|  | 160 | }, | 
|  | 161 | { | 
|  | 162 | name: "String list append", | 
|  | 163 | bp: ` | 
|  | 164 | my_module_type { | 
|  | 165 | name: "foo", | 
|  | 166 | my_string_list: select(soong_config_variable("my_namespace", "my_variable"), { | 
|  | 167 | "a": ["a.cpp"], | 
|  | 168 | "b": ["b.cpp"], | 
| Cole Faust | 683316a | 2024-04-02 16:45:54 -0700 | [diff] [blame] | 169 | default: ["c.cpp"], | 
| Cole Faust | 5a231bd | 2024-02-07 09:43:59 -0800 | [diff] [blame] | 170 | }) + select(soong_config_variable("my_namespace", "my_variable_2"), { | 
|  | 171 | "a2": ["a2.cpp"], | 
|  | 172 | "b2": ["b2.cpp"], | 
| Cole Faust | 683316a | 2024-04-02 16:45:54 -0700 | [diff] [blame] | 173 | default: ["c2.cpp"], | 
| Cole Faust | 5a231bd | 2024-02-07 09:43:59 -0800 | [diff] [blame] | 174 | }), | 
|  | 175 | } | 
|  | 176 | `, | 
|  | 177 | provider: selectsTestProvider{ | 
|  | 178 | my_string_list: &[]string{"a.cpp", "c2.cpp"}, | 
|  | 179 | }, | 
|  | 180 | vendorVars: map[string]map[string]string{ | 
|  | 181 | "my_namespace": { | 
|  | 182 | "my_variable": "a", | 
|  | 183 | }, | 
|  | 184 | }, | 
|  | 185 | }, | 
|  | 186 | { | 
|  | 187 | name: "String list prepend literal", | 
|  | 188 | bp: ` | 
|  | 189 | my_module_type { | 
|  | 190 | name: "foo", | 
|  | 191 | my_string_list: ["literal.cpp"] + select(soong_config_variable("my_namespace", "my_variable"), { | 
|  | 192 | "a2": ["a2.cpp"], | 
|  | 193 | "b2": ["b2.cpp"], | 
| Cole Faust | 683316a | 2024-04-02 16:45:54 -0700 | [diff] [blame] | 194 | default: ["c2.cpp"], | 
| Cole Faust | 5a231bd | 2024-02-07 09:43:59 -0800 | [diff] [blame] | 195 | }), | 
|  | 196 | } | 
|  | 197 | `, | 
|  | 198 | provider: selectsTestProvider{ | 
|  | 199 | my_string_list: &[]string{"literal.cpp", "c2.cpp"}, | 
|  | 200 | }, | 
|  | 201 | }, | 
|  | 202 | { | 
|  | 203 | name: "String list append literal", | 
|  | 204 | bp: ` | 
|  | 205 | my_module_type { | 
|  | 206 | name: "foo", | 
|  | 207 | my_string_list: select(soong_config_variable("my_namespace", "my_variable"), { | 
|  | 208 | "a2": ["a2.cpp"], | 
|  | 209 | "b2": ["b2.cpp"], | 
| Cole Faust | 683316a | 2024-04-02 16:45:54 -0700 | [diff] [blame] | 210 | default: ["c2.cpp"], | 
| Cole Faust | 5a231bd | 2024-02-07 09:43:59 -0800 | [diff] [blame] | 211 | }) + ["literal.cpp"], | 
|  | 212 | } | 
|  | 213 | `, | 
|  | 214 | provider: selectsTestProvider{ | 
|  | 215 | my_string_list: &[]string{"c2.cpp", "literal.cpp"}, | 
|  | 216 | }, | 
|  | 217 | }, | 
|  | 218 | { | 
| Cole Faust | 74ef465 | 2024-03-27 16:45:41 -0700 | [diff] [blame] | 219 | name: "true + false = true", | 
| Cole Faust | 5a231bd | 2024-02-07 09:43:59 -0800 | [diff] [blame] | 220 | bp: ` | 
|  | 221 | my_module_type { | 
|  | 222 | name: "foo", | 
|  | 223 | my_bool: select(soong_config_variable("my_namespace", "my_variable"), { | 
|  | 224 | "a": true, | 
|  | 225 | "b": false, | 
| Cole Faust | 683316a | 2024-04-02 16:45:54 -0700 | [diff] [blame] | 226 | default: true, | 
| Cole Faust | 5a231bd | 2024-02-07 09:43:59 -0800 | [diff] [blame] | 227 | }) + false, | 
|  | 228 | } | 
|  | 229 | `, | 
| Cole Faust | 74ef465 | 2024-03-27 16:45:41 -0700 | [diff] [blame] | 230 | provider: selectsTestProvider{ | 
|  | 231 | my_bool: proptools.BoolPtr(true), | 
|  | 232 | }, | 
|  | 233 | }, | 
|  | 234 | { | 
|  | 235 | name: "false + false = false", | 
|  | 236 | bp: ` | 
|  | 237 | my_module_type { | 
|  | 238 | name: "foo", | 
|  | 239 | my_bool: select(soong_config_variable("my_namespace", "my_variable"), { | 
|  | 240 | "a": true, | 
|  | 241 | "b": false, | 
| Cole Faust | 683316a | 2024-04-02 16:45:54 -0700 | [diff] [blame] | 242 | default: true, | 
| Cole Faust | 74ef465 | 2024-03-27 16:45:41 -0700 | [diff] [blame] | 243 | }) + false, | 
|  | 244 | } | 
|  | 245 | `, | 
|  | 246 | vendorVars: map[string]map[string]string{ | 
|  | 247 | "my_namespace": { | 
|  | 248 | "my_variable": "b", | 
|  | 249 | }, | 
|  | 250 | }, | 
|  | 251 | provider: selectsTestProvider{ | 
|  | 252 | my_bool: proptools.BoolPtr(false), | 
|  | 253 | }, | 
| Cole Faust | 5a231bd | 2024-02-07 09:43:59 -0800 | [diff] [blame] | 254 | }, | 
|  | 255 | { | 
|  | 256 | name: "Append string", | 
|  | 257 | bp: ` | 
|  | 258 | my_module_type { | 
|  | 259 | name: "foo", | 
|  | 260 | my_string: select(soong_config_variable("my_namespace", "my_variable"), { | 
|  | 261 | "a": "a", | 
|  | 262 | "b": "b", | 
| Cole Faust | 683316a | 2024-04-02 16:45:54 -0700 | [diff] [blame] | 263 | default: "c", | 
| Cole Faust | 5a231bd | 2024-02-07 09:43:59 -0800 | [diff] [blame] | 264 | }) + ".cpp", | 
|  | 265 | } | 
|  | 266 | `, | 
|  | 267 | provider: selectsTestProvider{ | 
|  | 268 | my_string: proptools.StringPtr("c.cpp"), | 
|  | 269 | }, | 
|  | 270 | }, | 
| Cole Faust | 0aa21cc | 2024-03-20 12:28:03 -0700 | [diff] [blame] | 271 | { | 
| Cole Faust | fc57d40 | 2024-04-11 12:09:44 -0700 | [diff] [blame] | 272 | name: "Select on arch", | 
| Cole Faust | 0aa21cc | 2024-03-20 12:28:03 -0700 | [diff] [blame] | 273 | bp: ` | 
|  | 274 | my_module_type { | 
|  | 275 | name: "foo", | 
| Cole Faust | fc57d40 | 2024-04-11 12:09:44 -0700 | [diff] [blame] | 276 | my_string: select(arch(), { | 
| Cole Faust | 0aa21cc | 2024-03-20 12:28:03 -0700 | [diff] [blame] | 277 | "x86": "my_x86", | 
|  | 278 | "x86_64": "my_x86_64", | 
|  | 279 | "arm": "my_arm", | 
|  | 280 | "arm64": "my_arm64", | 
| Cole Faust | 683316a | 2024-04-02 16:45:54 -0700 | [diff] [blame] | 281 | default: "my_default", | 
| Cole Faust | 0aa21cc | 2024-03-20 12:28:03 -0700 | [diff] [blame] | 282 | }), | 
|  | 283 | } | 
|  | 284 | `, | 
|  | 285 | provider: selectsTestProvider{ | 
|  | 286 | my_string: proptools.StringPtr("my_arm64"), | 
|  | 287 | }, | 
|  | 288 | }, | 
| Cole Faust | 12c8ed4 | 2024-03-28 16:26:59 -0700 | [diff] [blame] | 289 | { | 
| Cole Faust | fc57d40 | 2024-04-11 12:09:44 -0700 | [diff] [blame] | 290 | name: "Select on os", | 
|  | 291 | bp: ` | 
|  | 292 | my_module_type { | 
|  | 293 | name: "foo", | 
|  | 294 | my_string: select(os(), { | 
|  | 295 | "android": "my_android", | 
|  | 296 | "linux": "my_linux", | 
|  | 297 | default: "my_default", | 
|  | 298 | }), | 
|  | 299 | } | 
|  | 300 | `, | 
|  | 301 | provider: selectsTestProvider{ | 
|  | 302 | my_string: proptools.StringPtr("my_android"), | 
|  | 303 | }, | 
|  | 304 | }, | 
|  | 305 | { | 
| Cole Faust | 12c8ed4 | 2024-03-28 16:26:59 -0700 | [diff] [blame] | 306 | name: "Unset value", | 
|  | 307 | bp: ` | 
|  | 308 | my_module_type { | 
|  | 309 | name: "foo", | 
|  | 310 | my_string: select(soong_config_variable("my_namespace", "my_variable"), { | 
|  | 311 | "a": unset, | 
|  | 312 | "b": "b", | 
| Cole Faust | 683316a | 2024-04-02 16:45:54 -0700 | [diff] [blame] | 313 | default: "c", | 
| Cole Faust | 12c8ed4 | 2024-03-28 16:26:59 -0700 | [diff] [blame] | 314 | }) | 
|  | 315 | } | 
|  | 316 | `, | 
|  | 317 | vendorVars: map[string]map[string]string{ | 
|  | 318 | "my_namespace": { | 
|  | 319 | "my_variable": "a", | 
|  | 320 | }, | 
|  | 321 | }, | 
|  | 322 | provider: selectsTestProvider{}, | 
|  | 323 | }, | 
|  | 324 | { | 
|  | 325 | name: "Unset value on different branch", | 
|  | 326 | bp: ` | 
|  | 327 | my_module_type { | 
|  | 328 | name: "foo", | 
|  | 329 | my_string: select(soong_config_variable("my_namespace", "my_variable"), { | 
|  | 330 | "a": unset, | 
|  | 331 | "b": "b", | 
| Cole Faust | 683316a | 2024-04-02 16:45:54 -0700 | [diff] [blame] | 332 | default: "c", | 
| Cole Faust | 12c8ed4 | 2024-03-28 16:26:59 -0700 | [diff] [blame] | 333 | }) | 
|  | 334 | } | 
|  | 335 | `, | 
|  | 336 | provider: selectsTestProvider{ | 
|  | 337 | my_string: proptools.StringPtr("c"), | 
|  | 338 | }, | 
|  | 339 | }, | 
|  | 340 | { | 
|  | 341 | name: "unset + unset = unset", | 
|  | 342 | bp: ` | 
|  | 343 | my_module_type { | 
|  | 344 | name: "foo", | 
|  | 345 | my_string: select(soong_config_variable("my_namespace", "my_variable"), { | 
| Cole Faust | fdbf5d4 | 2024-04-10 15:01:23 -0700 | [diff] [blame] | 346 | "foo": "bar", | 
| Cole Faust | 683316a | 2024-04-02 16:45:54 -0700 | [diff] [blame] | 347 | default: unset, | 
| Cole Faust | 12c8ed4 | 2024-03-28 16:26:59 -0700 | [diff] [blame] | 348 | }) + select(soong_config_variable("my_namespace", "my_variable2"), { | 
| Cole Faust | fdbf5d4 | 2024-04-10 15:01:23 -0700 | [diff] [blame] | 349 | "baz": "qux", | 
| Cole Faust | 683316a | 2024-04-02 16:45:54 -0700 | [diff] [blame] | 350 | default: unset, | 
| Cole Faust | 12c8ed4 | 2024-03-28 16:26:59 -0700 | [diff] [blame] | 351 | }) | 
|  | 352 | } | 
|  | 353 | `, | 
|  | 354 | provider: selectsTestProvider{}, | 
|  | 355 | }, | 
|  | 356 | { | 
|  | 357 | name: "unset + string = string", | 
|  | 358 | bp: ` | 
|  | 359 | my_module_type { | 
|  | 360 | name: "foo", | 
|  | 361 | my_string: select(soong_config_variable("my_namespace", "my_variable"), { | 
| Cole Faust | fdbf5d4 | 2024-04-10 15:01:23 -0700 | [diff] [blame] | 362 | "foo": "bar", | 
| Cole Faust | 683316a | 2024-04-02 16:45:54 -0700 | [diff] [blame] | 363 | default: unset, | 
| Cole Faust | 12c8ed4 | 2024-03-28 16:26:59 -0700 | [diff] [blame] | 364 | }) + select(soong_config_variable("my_namespace", "my_variable2"), { | 
| Cole Faust | 683316a | 2024-04-02 16:45:54 -0700 | [diff] [blame] | 365 | default: "a", | 
| Cole Faust | 12c8ed4 | 2024-03-28 16:26:59 -0700 | [diff] [blame] | 366 | }) | 
|  | 367 | } | 
|  | 368 | `, | 
|  | 369 | provider: selectsTestProvider{ | 
|  | 370 | my_string: proptools.StringPtr("a"), | 
|  | 371 | }, | 
|  | 372 | }, | 
|  | 373 | { | 
|  | 374 | name: "unset + bool = bool", | 
|  | 375 | bp: ` | 
|  | 376 | my_module_type { | 
|  | 377 | name: "foo", | 
|  | 378 | my_bool: select(soong_config_variable("my_namespace", "my_variable"), { | 
|  | 379 | "a": true, | 
| Cole Faust | 683316a | 2024-04-02 16:45:54 -0700 | [diff] [blame] | 380 | default: unset, | 
| Cole Faust | 12c8ed4 | 2024-03-28 16:26:59 -0700 | [diff] [blame] | 381 | }) + select(soong_config_variable("my_namespace", "my_variable2"), { | 
| Cole Faust | 683316a | 2024-04-02 16:45:54 -0700 | [diff] [blame] | 382 | default: true, | 
| Cole Faust | 12c8ed4 | 2024-03-28 16:26:59 -0700 | [diff] [blame] | 383 | }) | 
|  | 384 | } | 
|  | 385 | `, | 
|  | 386 | provider: selectsTestProvider{ | 
|  | 387 | my_bool: proptools.BoolPtr(true), | 
|  | 388 | }, | 
|  | 389 | }, | 
| Cole Faust | 02dd6e5 | 2024-04-03 17:04:57 -0700 | [diff] [blame] | 390 | { | 
|  | 391 | name: "defaults with lists are appended", | 
|  | 392 | bp: ` | 
|  | 393 | my_module_type { | 
|  | 394 | name: "foo", | 
|  | 395 | defaults: ["bar"], | 
|  | 396 | my_string_list: select(soong_config_variable("my_namespace", "my_variable"), { | 
|  | 397 | "a": ["a1"], | 
|  | 398 | default: ["b1"], | 
|  | 399 | }), | 
|  | 400 | } | 
|  | 401 | my_defaults { | 
|  | 402 | name: "bar", | 
|  | 403 | my_string_list: select(soong_config_variable("my_namespace", "my_variable2"), { | 
|  | 404 | "a": ["a2"], | 
|  | 405 | default: ["b2"], | 
|  | 406 | }), | 
|  | 407 | } | 
|  | 408 | `, | 
|  | 409 | provider: selectsTestProvider{ | 
|  | 410 | my_string_list: &[]string{"b2", "b1"}, | 
|  | 411 | }, | 
|  | 412 | }, | 
|  | 413 | { | 
|  | 414 | name: "Replacing string list", | 
|  | 415 | bp: ` | 
|  | 416 | my_module_type { | 
|  | 417 | name: "foo", | 
|  | 418 | defaults: ["bar"], | 
|  | 419 | replacing_string_list: select(soong_config_variable("my_namespace", "my_variable"), { | 
|  | 420 | "a": ["a1"], | 
|  | 421 | default: ["b1"], | 
|  | 422 | }), | 
|  | 423 | } | 
|  | 424 | my_defaults { | 
|  | 425 | name: "bar", | 
|  | 426 | replacing_string_list: select(soong_config_variable("my_namespace", "my_variable2"), { | 
|  | 427 | "a": ["a2"], | 
|  | 428 | default: ["b2"], | 
|  | 429 | }), | 
|  | 430 | } | 
|  | 431 | `, | 
|  | 432 | provider: selectsTestProvider{ | 
|  | 433 | replacing_string_list: &[]string{"b1"}, | 
|  | 434 | }, | 
|  | 435 | }, | 
| Cole Faust | fdbf5d4 | 2024-04-10 15:01:23 -0700 | [diff] [blame] | 436 | { | 
|  | 437 | name: "Multi-condition string 1", | 
|  | 438 | bp: ` | 
|  | 439 | my_module_type { | 
|  | 440 | name: "foo", | 
|  | 441 | my_string: select(( | 
|  | 442 | soong_config_variable("my_namespace", "my_variable"), | 
|  | 443 | soong_config_variable("my_namespace", "my_variable2"), | 
|  | 444 | ), { | 
|  | 445 | ("a", "b"): "a+b", | 
|  | 446 | ("a", default): "a+default", | 
|  | 447 | (default, default): "default", | 
|  | 448 | }), | 
|  | 449 | } | 
|  | 450 | `, | 
|  | 451 | vendorVars: map[string]map[string]string{ | 
|  | 452 | "my_namespace": { | 
|  | 453 | "my_variable":  "a", | 
|  | 454 | "my_variable2": "b", | 
|  | 455 | }, | 
|  | 456 | }, | 
|  | 457 | provider: selectsTestProvider{ | 
|  | 458 | my_string: proptools.StringPtr("a+b"), | 
|  | 459 | }, | 
|  | 460 | }, | 
|  | 461 | { | 
|  | 462 | name: "Multi-condition string 2", | 
|  | 463 | bp: ` | 
|  | 464 | my_module_type { | 
|  | 465 | name: "foo", | 
|  | 466 | my_string: select(( | 
|  | 467 | soong_config_variable("my_namespace", "my_variable"), | 
|  | 468 | soong_config_variable("my_namespace", "my_variable2"), | 
|  | 469 | ), { | 
|  | 470 | ("a", "b"): "a+b", | 
|  | 471 | ("a", default): "a+default", | 
|  | 472 | (default, default): "default", | 
|  | 473 | }), | 
|  | 474 | } | 
|  | 475 | `, | 
|  | 476 | vendorVars: map[string]map[string]string{ | 
|  | 477 | "my_namespace": { | 
|  | 478 | "my_variable":  "a", | 
|  | 479 | "my_variable2": "c", | 
|  | 480 | }, | 
|  | 481 | }, | 
|  | 482 | provider: selectsTestProvider{ | 
|  | 483 | my_string: proptools.StringPtr("a+default"), | 
|  | 484 | }, | 
|  | 485 | }, | 
|  | 486 | { | 
|  | 487 | name: "Multi-condition string 3", | 
|  | 488 | bp: ` | 
|  | 489 | my_module_type { | 
|  | 490 | name: "foo", | 
|  | 491 | my_string: select(( | 
|  | 492 | soong_config_variable("my_namespace", "my_variable"), | 
|  | 493 | soong_config_variable("my_namespace", "my_variable2"), | 
|  | 494 | ), { | 
|  | 495 | ("a", "b"): "a+b", | 
|  | 496 | ("a", default): "a+default", | 
|  | 497 | (default, default): "default", | 
|  | 498 | }), | 
|  | 499 | } | 
|  | 500 | `, | 
|  | 501 | vendorVars: map[string]map[string]string{ | 
|  | 502 | "my_namespace": { | 
|  | 503 | "my_variable":  "c", | 
|  | 504 | "my_variable2": "b", | 
|  | 505 | }, | 
|  | 506 | }, | 
|  | 507 | provider: selectsTestProvider{ | 
|  | 508 | my_string: proptools.StringPtr("default"), | 
|  | 509 | }, | 
|  | 510 | }, | 
|  | 511 | { | 
|  | 512 | name: "Select on boolean", | 
|  | 513 | bp: ` | 
|  | 514 | my_module_type { | 
|  | 515 | name: "foo", | 
|  | 516 | my_string: select(boolean_var_for_testing(), { | 
|  | 517 | true: "t", | 
|  | 518 | false: "f", | 
|  | 519 | }), | 
|  | 520 | } | 
|  | 521 | `, | 
|  | 522 | vendorVars: map[string]map[string]string{ | 
|  | 523 | "boolean_var": { | 
|  | 524 | "for_testing": "true", | 
|  | 525 | }, | 
|  | 526 | }, | 
|  | 527 | provider: selectsTestProvider{ | 
|  | 528 | my_string: proptools.StringPtr("t"), | 
|  | 529 | }, | 
|  | 530 | }, | 
|  | 531 | { | 
|  | 532 | name: "Select on boolean false", | 
|  | 533 | bp: ` | 
|  | 534 | my_module_type { | 
|  | 535 | name: "foo", | 
|  | 536 | my_string: select(boolean_var_for_testing(), { | 
|  | 537 | true: "t", | 
|  | 538 | false: "f", | 
|  | 539 | }), | 
|  | 540 | } | 
|  | 541 | `, | 
|  | 542 | vendorVars: map[string]map[string]string{ | 
|  | 543 | "boolean_var": { | 
|  | 544 | "for_testing": "false", | 
|  | 545 | }, | 
|  | 546 | }, | 
|  | 547 | provider: selectsTestProvider{ | 
|  | 548 | my_string: proptools.StringPtr("f"), | 
|  | 549 | }, | 
|  | 550 | }, | 
|  | 551 | { | 
|  | 552 | name: "Select on boolean undefined", | 
|  | 553 | bp: ` | 
|  | 554 | my_module_type { | 
|  | 555 | name: "foo", | 
|  | 556 | my_string: select(boolean_var_for_testing(), { | 
|  | 557 | true: "t", | 
|  | 558 | false: "f", | 
|  | 559 | }), | 
|  | 560 | } | 
|  | 561 | `, | 
|  | 562 | expectedError: "foo", | 
|  | 563 | }, | 
|  | 564 | { | 
|  | 565 | name: "Select on boolean undefined with default", | 
|  | 566 | bp: ` | 
|  | 567 | my_module_type { | 
|  | 568 | name: "foo", | 
|  | 569 | my_string: select(boolean_var_for_testing(), { | 
|  | 570 | true: "t", | 
|  | 571 | false: "f", | 
|  | 572 | default: "default", | 
|  | 573 | }), | 
|  | 574 | } | 
|  | 575 | `, | 
|  | 576 | provider: selectsTestProvider{ | 
|  | 577 | my_string: proptools.StringPtr("default"), | 
|  | 578 | }, | 
|  | 579 | }, | 
|  | 580 | { | 
|  | 581 | name: "Mismatched condition types", | 
|  | 582 | bp: ` | 
|  | 583 | my_module_type { | 
|  | 584 | name: "foo", | 
|  | 585 | my_string: select(boolean_var_for_testing(), { | 
|  | 586 | "true": "t", | 
|  | 587 | "false": "f", | 
|  | 588 | default: "default", | 
|  | 589 | }), | 
|  | 590 | } | 
|  | 591 | `, | 
|  | 592 | vendorVars: map[string]map[string]string{ | 
|  | 593 | "boolean_var": { | 
|  | 594 | "for_testing": "false", | 
|  | 595 | }, | 
|  | 596 | }, | 
|  | 597 | expectedError: "Expected all branches of a select on condition boolean_var_for_testing\\(\\) to have type bool, found string", | 
|  | 598 | }, | 
| Cole Faust | 5a231bd | 2024-02-07 09:43:59 -0800 | [diff] [blame] | 599 | } | 
|  | 600 |  | 
|  | 601 | for _, tc := range testCases { | 
|  | 602 | t.Run(tc.name, func(t *testing.T) { | 
|  | 603 | fixtures := GroupFixturePreparers( | 
| Cole Faust | 02dd6e5 | 2024-04-03 17:04:57 -0700 | [diff] [blame] | 604 | PrepareForTestWithDefaults, | 
| Cole Faust | 0aa21cc | 2024-03-20 12:28:03 -0700 | [diff] [blame] | 605 | PrepareForTestWithArchMutator, | 
| Cole Faust | 5a231bd | 2024-02-07 09:43:59 -0800 | [diff] [blame] | 606 | FixtureRegisterWithContext(func(ctx RegistrationContext) { | 
|  | 607 | ctx.RegisterModuleType("my_module_type", newSelectsMockModule) | 
| Cole Faust | 02dd6e5 | 2024-04-03 17:04:57 -0700 | [diff] [blame] | 608 | ctx.RegisterModuleType("my_defaults", newSelectsMockModuleDefaults) | 
| Cole Faust | 5a231bd | 2024-02-07 09:43:59 -0800 | [diff] [blame] | 609 | }), | 
|  | 610 | FixtureModifyProductVariables(func(variables FixtureProductVariables) { | 
|  | 611 | variables.VendorVars = tc.vendorVars | 
|  | 612 | }), | 
|  | 613 | ) | 
|  | 614 | if tc.expectedError != "" { | 
|  | 615 | fixtures = fixtures.ExtendWithErrorHandler(FixtureExpectsOneErrorPattern(tc.expectedError)) | 
|  | 616 | } | 
|  | 617 | result := fixtures.RunTestWithBp(t, tc.bp) | 
|  | 618 |  | 
|  | 619 | if tc.expectedError == "" { | 
| Cole Faust | 0aa21cc | 2024-03-20 12:28:03 -0700 | [diff] [blame] | 620 | m := result.ModuleForTests("foo", "android_arm64_armv8-a") | 
|  | 621 | p, _ := OtherModuleProvider(result.testContext.OtherModuleProviderAdaptor(), m.Module(), selectsTestProviderKey) | 
| Cole Faust | 5a231bd | 2024-02-07 09:43:59 -0800 | [diff] [blame] | 622 | if !reflect.DeepEqual(p, tc.provider) { | 
|  | 623 | t.Errorf("Expected:\n  %q\ngot:\n  %q", tc.provider.String(), p.String()) | 
|  | 624 | } | 
|  | 625 | } | 
|  | 626 | }) | 
|  | 627 | } | 
|  | 628 | } | 
|  | 629 |  | 
|  | 630 | type selectsTestProvider struct { | 
| Cole Faust | 02dd6e5 | 2024-04-03 17:04:57 -0700 | [diff] [blame] | 631 | my_bool               *bool | 
|  | 632 | my_string             *string | 
|  | 633 | my_string_list        *[]string | 
|  | 634 | my_paths              *[]string | 
|  | 635 | replacing_string_list *[]string | 
| Cole Faust | 5a231bd | 2024-02-07 09:43:59 -0800 | [diff] [blame] | 636 | } | 
|  | 637 |  | 
|  | 638 | func (p *selectsTestProvider) String() string { | 
|  | 639 | myBoolStr := "nil" | 
|  | 640 | if p.my_bool != nil { | 
|  | 641 | myBoolStr = fmt.Sprintf("%t", *p.my_bool) | 
|  | 642 | } | 
|  | 643 | myStringStr := "nil" | 
|  | 644 | if p.my_string != nil { | 
|  | 645 | myStringStr = *p.my_string | 
|  | 646 | } | 
|  | 647 | return fmt.Sprintf(`selectsTestProvider { | 
|  | 648 | my_bool: %v, | 
|  | 649 | my_string: %s, | 
|  | 650 | my_string_list: %s, | 
| Cole Faust | bdd8aee | 2024-03-14 14:33:02 -0700 | [diff] [blame] | 651 | my_paths: %s, | 
| Cole Faust | 02dd6e5 | 2024-04-03 17:04:57 -0700 | [diff] [blame] | 652 | replacing_string_list %s, | 
|  | 653 | }`, myBoolStr, myStringStr, p.my_string_list, p.my_paths, p.replacing_string_list) | 
| Cole Faust | 5a231bd | 2024-02-07 09:43:59 -0800 | [diff] [blame] | 654 | } | 
|  | 655 |  | 
|  | 656 | var selectsTestProviderKey = blueprint.NewProvider[selectsTestProvider]() | 
|  | 657 |  | 
|  | 658 | type selectsMockModuleProperties struct { | 
| Cole Faust | 02dd6e5 | 2024-04-03 17:04:57 -0700 | [diff] [blame] | 659 | My_bool               proptools.Configurable[bool] | 
|  | 660 | My_string             proptools.Configurable[string] | 
|  | 661 | My_string_list        proptools.Configurable[[]string] | 
|  | 662 | My_paths              proptools.Configurable[[]string] `android:"path"` | 
|  | 663 | Replacing_string_list proptools.Configurable[[]string] `android:"replace_instead_of_append,arch_variant"` | 
| Cole Faust | 5a231bd | 2024-02-07 09:43:59 -0800 | [diff] [blame] | 664 | } | 
|  | 665 |  | 
|  | 666 | type selectsMockModule struct { | 
|  | 667 | ModuleBase | 
|  | 668 | DefaultableModuleBase | 
|  | 669 | properties selectsMockModuleProperties | 
|  | 670 | } | 
|  | 671 |  | 
|  | 672 | func (p *selectsMockModule) GenerateAndroidBuildActions(ctx ModuleContext) { | 
| Cole Faust | bdd8aee | 2024-03-14 14:33:02 -0700 | [diff] [blame] | 673 | SetProvider(ctx, selectsTestProviderKey, selectsTestProvider{ | 
| Cole Faust | b78ce43 | 2024-04-04 10:55:19 -0700 | [diff] [blame] | 674 | my_bool:               p.properties.My_bool.Get(ctx), | 
|  | 675 | my_string:             p.properties.My_string.Get(ctx), | 
|  | 676 | my_string_list:        p.properties.My_string_list.Get(ctx), | 
|  | 677 | my_paths:              p.properties.My_paths.Get(ctx), | 
|  | 678 | replacing_string_list: p.properties.Replacing_string_list.Get(ctx), | 
| Cole Faust | 5a231bd | 2024-02-07 09:43:59 -0800 | [diff] [blame] | 679 | }) | 
|  | 680 | } | 
|  | 681 |  | 
|  | 682 | func newSelectsMockModule() Module { | 
|  | 683 | m := &selectsMockModule{} | 
|  | 684 | m.AddProperties(&m.properties) | 
| Cole Faust | 0aa21cc | 2024-03-20 12:28:03 -0700 | [diff] [blame] | 685 | InitAndroidArchModule(m, HostAndDeviceSupported, MultilibFirst) | 
| Cole Faust | 5a231bd | 2024-02-07 09:43:59 -0800 | [diff] [blame] | 686 | InitDefaultableModule(m) | 
|  | 687 | return m | 
|  | 688 | } | 
| Cole Faust | 02dd6e5 | 2024-04-03 17:04:57 -0700 | [diff] [blame] | 689 |  | 
|  | 690 | type selectsMockModuleDefaults struct { | 
|  | 691 | ModuleBase | 
|  | 692 | DefaultsModuleBase | 
|  | 693 | } | 
|  | 694 |  | 
|  | 695 | func (d *selectsMockModuleDefaults) GenerateAndroidBuildActions(ctx ModuleContext) { | 
|  | 696 | } | 
|  | 697 |  | 
|  | 698 | func newSelectsMockModuleDefaults() Module { | 
|  | 699 | module := &selectsMockModuleDefaults{} | 
|  | 700 |  | 
|  | 701 | module.AddProperties( | 
|  | 702 | &selectsMockModuleProperties{}, | 
|  | 703 | ) | 
|  | 704 |  | 
|  | 705 | InitDefaultsModule(module) | 
|  | 706 |  | 
|  | 707 | return module | 
|  | 708 | } |