Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 1 | // Copyright 2021 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 | |
Sam Delmerico | 7f88956 | 2022-03-25 14:55:40 +0000 | [diff] [blame] | 15 | package android |
Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 16 | |
| 17 | import ( |
Sam Delmerico | 7f88956 | 2022-03-25 14:55:40 +0000 | [diff] [blame] | 18 | "android/soong/bazel" |
Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 19 | "testing" |
| 20 | ) |
| 21 | |
| 22 | func TestExpandVars(t *testing.T) { |
Sam Delmerico | 7f88956 | 2022-03-25 14:55:40 +0000 | [diff] [blame] | 23 | android_arm64_config := TestConfig("out", nil, "", nil) |
| 24 | android_arm64_config.BuildOS = Android |
| 25 | android_arm64_config.BuildArch = Arm64 |
Chris Parsons | 3b1f83d | 2021-10-14 14:08:38 -0400 | [diff] [blame] | 26 | |
Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 27 | testCases := []struct { |
Jingwen Chen | 51a1e1c | 2021-05-20 13:40:14 +0000 | [diff] [blame] | 28 | description string |
Sam Delmerico | 7f88956 | 2022-03-25 14:55:40 +0000 | [diff] [blame] | 29 | config Config |
| 30 | stringScope ExportedStringVariables |
| 31 | stringListScope ExportedStringListVariables |
| 32 | configVars ExportedConfigDependingVariables |
Jingwen Chen | 51a1e1c | 2021-05-20 13:40:14 +0000 | [diff] [blame] | 33 | toExpand string |
| 34 | expectedValues []string |
Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 35 | }{ |
| 36 | { |
Jingwen Chen | 51a1e1c | 2021-05-20 13:40:14 +0000 | [diff] [blame] | 37 | description: "no expansion for non-interpolated value", |
| 38 | toExpand: "foo", |
| 39 | expectedValues: []string{"foo"}, |
| 40 | }, |
| 41 | { |
| 42 | description: "single level expansion for string var", |
Sam Delmerico | 7f88956 | 2022-03-25 14:55:40 +0000 | [diff] [blame] | 43 | stringScope: ExportedStringVariables{ |
Jingwen Chen | 51a1e1c | 2021-05-20 13:40:14 +0000 | [diff] [blame] | 44 | "foo": "bar", |
Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 45 | }, |
| 46 | toExpand: "${foo}", |
| 47 | expectedValues: []string{"bar"}, |
| 48 | }, |
| 49 | { |
Liz Kammer | e8303bd | 2022-02-16 09:02:48 -0500 | [diff] [blame] | 50 | description: "single level expansion with short-name for string var", |
Sam Delmerico | 7f88956 | 2022-03-25 14:55:40 +0000 | [diff] [blame] | 51 | stringScope: ExportedStringVariables{ |
Liz Kammer | e8303bd | 2022-02-16 09:02:48 -0500 | [diff] [blame] | 52 | "foo": "bar", |
| 53 | }, |
| 54 | toExpand: "${config.foo}", |
| 55 | expectedValues: []string{"bar"}, |
| 56 | }, |
| 57 | { |
Jingwen Chen | 51a1e1c | 2021-05-20 13:40:14 +0000 | [diff] [blame] | 58 | description: "single level expansion string list var", |
Sam Delmerico | 7f88956 | 2022-03-25 14:55:40 +0000 | [diff] [blame] | 59 | stringListScope: ExportedStringListVariables{ |
Jingwen Chen | 51a1e1c | 2021-05-20 13:40:14 +0000 | [diff] [blame] | 60 | "foo": []string{"bar"}, |
| 61 | }, |
| 62 | toExpand: "${foo}", |
| 63 | expectedValues: []string{"bar"}, |
| 64 | }, |
| 65 | { |
| 66 | description: "mixed level expansion for string list var", |
Sam Delmerico | 7f88956 | 2022-03-25 14:55:40 +0000 | [diff] [blame] | 67 | stringScope: ExportedStringVariables{ |
Jingwen Chen | 51a1e1c | 2021-05-20 13:40:14 +0000 | [diff] [blame] | 68 | "foo": "${bar}", |
| 69 | "qux": "hello", |
| 70 | }, |
Sam Delmerico | 7f88956 | 2022-03-25 14:55:40 +0000 | [diff] [blame] | 71 | stringListScope: ExportedStringListVariables{ |
Jingwen Chen | 51a1e1c | 2021-05-20 13:40:14 +0000 | [diff] [blame] | 72 | "bar": []string{"baz", "${qux}"}, |
| 73 | }, |
| 74 | toExpand: "${foo}", |
Chris Parsons | 3b1f83d | 2021-10-14 14:08:38 -0400 | [diff] [blame] | 75 | expectedValues: []string{"baz hello"}, |
Jingwen Chen | 51a1e1c | 2021-05-20 13:40:14 +0000 | [diff] [blame] | 76 | }, |
| 77 | { |
Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 78 | description: "double level expansion", |
Sam Delmerico | 7f88956 | 2022-03-25 14:55:40 +0000 | [diff] [blame] | 79 | stringListScope: ExportedStringListVariables{ |
Jingwen Chen | 51a1e1c | 2021-05-20 13:40:14 +0000 | [diff] [blame] | 80 | "foo": []string{"${bar}"}, |
| 81 | "bar": []string{"baz"}, |
Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 82 | }, |
| 83 | toExpand: "${foo}", |
| 84 | expectedValues: []string{"baz"}, |
| 85 | }, |
| 86 | { |
| 87 | description: "double level expansion with a literal", |
Sam Delmerico | 7f88956 | 2022-03-25 14:55:40 +0000 | [diff] [blame] | 88 | stringListScope: ExportedStringListVariables{ |
Jingwen Chen | 51a1e1c | 2021-05-20 13:40:14 +0000 | [diff] [blame] | 89 | "a": []string{"${b}", "c"}, |
| 90 | "b": []string{"d"}, |
Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 91 | }, |
| 92 | toExpand: "${a}", |
Chris Parsons | 3b1f83d | 2021-10-14 14:08:38 -0400 | [diff] [blame] | 93 | expectedValues: []string{"d c"}, |
Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 94 | }, |
| 95 | { |
| 96 | description: "double level expansion, with two variables in a string", |
Sam Delmerico | 7f88956 | 2022-03-25 14:55:40 +0000 | [diff] [blame] | 97 | stringListScope: ExportedStringListVariables{ |
Jingwen Chen | 51a1e1c | 2021-05-20 13:40:14 +0000 | [diff] [blame] | 98 | "a": []string{"${b} ${c}"}, |
| 99 | "b": []string{"d"}, |
| 100 | "c": []string{"e"}, |
Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 101 | }, |
| 102 | toExpand: "${a}", |
Chris Parsons | 3b1f83d | 2021-10-14 14:08:38 -0400 | [diff] [blame] | 103 | expectedValues: []string{"d e"}, |
Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 104 | }, |
| 105 | { |
| 106 | description: "triple level expansion with two variables in a string", |
Sam Delmerico | 7f88956 | 2022-03-25 14:55:40 +0000 | [diff] [blame] | 107 | stringListScope: ExportedStringListVariables{ |
Jingwen Chen | 51a1e1c | 2021-05-20 13:40:14 +0000 | [diff] [blame] | 108 | "a": []string{"${b} ${c}"}, |
| 109 | "b": []string{"${c}", "${d}"}, |
| 110 | "c": []string{"${d}"}, |
| 111 | "d": []string{"foo"}, |
Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 112 | }, |
| 113 | toExpand: "${a}", |
Chris Parsons | 3b1f83d | 2021-10-14 14:08:38 -0400 | [diff] [blame] | 114 | expectedValues: []string{"foo foo foo"}, |
| 115 | }, |
| 116 | { |
| 117 | description: "expansion with config depending vars", |
Sam Delmerico | 7f88956 | 2022-03-25 14:55:40 +0000 | [diff] [blame] | 118 | configVars: ExportedConfigDependingVariables{ |
| 119 | "a": func(c Config) string { return c.BuildOS.String() }, |
| 120 | "b": func(c Config) string { return c.BuildArch.String() }, |
Chris Parsons | 3b1f83d | 2021-10-14 14:08:38 -0400 | [diff] [blame] | 121 | }, |
| 122 | config: android_arm64_config, |
| 123 | toExpand: "${a}-${b}", |
| 124 | expectedValues: []string{"android-arm64"}, |
| 125 | }, |
| 126 | { |
| 127 | description: "double level multi type expansion", |
Sam Delmerico | 7f88956 | 2022-03-25 14:55:40 +0000 | [diff] [blame] | 128 | stringListScope: ExportedStringListVariables{ |
Chris Parsons | 3b1f83d | 2021-10-14 14:08:38 -0400 | [diff] [blame] | 129 | "platform": []string{"${os}-${arch}"}, |
| 130 | "const": []string{"const"}, |
| 131 | }, |
Sam Delmerico | 7f88956 | 2022-03-25 14:55:40 +0000 | [diff] [blame] | 132 | configVars: ExportedConfigDependingVariables{ |
| 133 | "os": func(c Config) string { return c.BuildOS.String() }, |
| 134 | "arch": func(c Config) string { return c.BuildArch.String() }, |
| 135 | "foo": func(c Config) string { return "foo" }, |
Chris Parsons | 3b1f83d | 2021-10-14 14:08:38 -0400 | [diff] [blame] | 136 | }, |
| 137 | config: android_arm64_config, |
| 138 | toExpand: "${const}/${platform}/${foo}", |
| 139 | expectedValues: []string{"const/android-arm64/foo"}, |
Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 140 | }, |
| 141 | } |
| 142 | |
| 143 | for _, testCase := range testCases { |
| 144 | t.Run(testCase.description, func(t *testing.T) { |
Chris Parsons | 3b1f83d | 2021-10-14 14:08:38 -0400 | [diff] [blame] | 145 | output, _ := expandVar(testCase.config, testCase.toExpand, testCase.stringScope, testCase.stringListScope, testCase.configVars) |
Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 146 | if len(output) != len(testCase.expectedValues) { |
| 147 | t.Errorf("Expected %d values, got %d", len(testCase.expectedValues), len(output)) |
| 148 | } |
| 149 | for i, actual := range output { |
| 150 | expectedValue := testCase.expectedValues[i] |
| 151 | if actual != expectedValue { |
| 152 | t.Errorf("Actual value '%s' doesn't match expected value '%s'", actual, expectedValue) |
| 153 | } |
| 154 | } |
| 155 | }) |
| 156 | } |
| 157 | } |
Liz Kammer | 82ad8cc | 2021-08-02 10:41:48 -0400 | [diff] [blame] | 158 | |
| 159 | func TestBazelToolchainVars(t *testing.T) { |
| 160 | testCases := []struct { |
| 161 | name string |
Sam Delmerico | 7f88956 | 2022-03-25 14:55:40 +0000 | [diff] [blame] | 162 | config Config |
| 163 | vars ExportedVariables |
Liz Kammer | 82ad8cc | 2021-08-02 10:41:48 -0400 | [diff] [blame] | 164 | expectedOut string |
| 165 | }{ |
| 166 | { |
| 167 | name: "exports strings", |
Sam Delmerico | 7f88956 | 2022-03-25 14:55:40 +0000 | [diff] [blame] | 168 | vars: ExportedVariables{ |
| 169 | exportedStringVars: ExportedStringVariables{ |
Liz Kammer | 82ad8cc | 2021-08-02 10:41:48 -0400 | [diff] [blame] | 170 | "a": "b", |
| 171 | "c": "d", |
| 172 | }, |
| 173 | }, |
Sam Delmerico | 7f88956 | 2022-03-25 14:55:40 +0000 | [diff] [blame] | 174 | expectedOut: bazel.GeneratedBazelFileWarning + ` |
Liz Kammer | 82ad8cc | 2021-08-02 10:41:48 -0400 | [diff] [blame] | 175 | |
| 176 | _a = "b" |
| 177 | |
| 178 | _c = "d" |
| 179 | |
| 180 | constants = struct( |
| 181 | a = _a, |
| 182 | c = _c, |
| 183 | )`, |
| 184 | }, |
| 185 | { |
| 186 | name: "exports string lists", |
Sam Delmerico | 7f88956 | 2022-03-25 14:55:40 +0000 | [diff] [blame] | 187 | vars: ExportedVariables{ |
| 188 | exportedStringListVars: ExportedStringListVariables{ |
Liz Kammer | 82ad8cc | 2021-08-02 10:41:48 -0400 | [diff] [blame] | 189 | "a": []string{"b1", "b2"}, |
| 190 | "c": []string{"d1", "d2"}, |
| 191 | }, |
| 192 | }, |
Sam Delmerico | 7f88956 | 2022-03-25 14:55:40 +0000 | [diff] [blame] | 193 | expectedOut: bazel.GeneratedBazelFileWarning + ` |
Liz Kammer | 82ad8cc | 2021-08-02 10:41:48 -0400 | [diff] [blame] | 194 | |
| 195 | _a = [ |
| 196 | "b1", |
| 197 | "b2", |
| 198 | ] |
| 199 | |
| 200 | _c = [ |
| 201 | "d1", |
| 202 | "d2", |
| 203 | ] |
| 204 | |
| 205 | constants = struct( |
| 206 | a = _a, |
| 207 | c = _c, |
| 208 | )`, |
| 209 | }, |
| 210 | { |
| 211 | name: "exports string lists dicts", |
Sam Delmerico | 7f88956 | 2022-03-25 14:55:40 +0000 | [diff] [blame] | 212 | vars: ExportedVariables{ |
| 213 | exportedStringListDictVars: ExportedStringListDictVariables{ |
| 214 | "a": map[string][]string{"b1": {"b2"}}, |
| 215 | "c": map[string][]string{"d1": {"d2"}}, |
Liz Kammer | 82ad8cc | 2021-08-02 10:41:48 -0400 | [diff] [blame] | 216 | }, |
| 217 | }, |
Sam Delmerico | 7f88956 | 2022-03-25 14:55:40 +0000 | [diff] [blame] | 218 | expectedOut: bazel.GeneratedBazelFileWarning + ` |
Liz Kammer | 82ad8cc | 2021-08-02 10:41:48 -0400 | [diff] [blame] | 219 | |
| 220 | _a = { |
Liz Kammer | 72beb34 | 2022-02-03 08:42:10 -0500 | [diff] [blame] | 221 | "b1": ["b2"], |
Liz Kammer | 82ad8cc | 2021-08-02 10:41:48 -0400 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | _c = { |
Liz Kammer | 72beb34 | 2022-02-03 08:42:10 -0500 | [diff] [blame] | 225 | "d1": ["d2"], |
Liz Kammer | 82ad8cc | 2021-08-02 10:41:48 -0400 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | constants = struct( |
| 229 | a = _a, |
| 230 | c = _c, |
| 231 | )`, |
| 232 | }, |
| 233 | { |
Liz Kammer | e8303bd | 2022-02-16 09:02:48 -0500 | [diff] [blame] | 234 | name: "exports dict with var refs", |
Sam Delmerico | 7f88956 | 2022-03-25 14:55:40 +0000 | [diff] [blame] | 235 | vars: ExportedVariables{ |
| 236 | exportedVariableReferenceDictVars: ExportedVariableReferenceDictVariables{ |
Liz Kammer | e8303bd | 2022-02-16 09:02:48 -0500 | [diff] [blame] | 237 | "a": map[string]string{"b1": "${b2}"}, |
| 238 | "c": map[string]string{"d1": "${config.d2}"}, |
| 239 | }, |
| 240 | }, |
Sam Delmerico | 7f88956 | 2022-03-25 14:55:40 +0000 | [diff] [blame] | 241 | expectedOut: bazel.GeneratedBazelFileWarning + ` |
Liz Kammer | e8303bd | 2022-02-16 09:02:48 -0500 | [diff] [blame] | 242 | |
| 243 | _a = { |
| 244 | "b1": _b2, |
| 245 | } |
| 246 | |
| 247 | _c = { |
| 248 | "d1": _d2, |
| 249 | } |
| 250 | |
| 251 | constants = struct( |
| 252 | a = _a, |
| 253 | c = _c, |
| 254 | )`, |
| 255 | }, |
| 256 | { |
| 257 | name: "sorts across types with variable references last", |
Sam Delmerico | 7f88956 | 2022-03-25 14:55:40 +0000 | [diff] [blame] | 258 | vars: ExportedVariables{ |
| 259 | exportedStringVars: ExportedStringVariables{ |
Liz Kammer | 82ad8cc | 2021-08-02 10:41:48 -0400 | [diff] [blame] | 260 | "b": "b-val", |
| 261 | "d": "d-val", |
| 262 | }, |
Sam Delmerico | 7f88956 | 2022-03-25 14:55:40 +0000 | [diff] [blame] | 263 | exportedStringListVars: ExportedStringListVariables{ |
Liz Kammer | 82ad8cc | 2021-08-02 10:41:48 -0400 | [diff] [blame] | 264 | "c": []string{"c-val"}, |
| 265 | "e": []string{"e-val"}, |
| 266 | }, |
Sam Delmerico | 7f88956 | 2022-03-25 14:55:40 +0000 | [diff] [blame] | 267 | exportedStringListDictVars: ExportedStringListDictVariables{ |
| 268 | "a": map[string][]string{"a1": {"a2"}}, |
| 269 | "f": map[string][]string{"f1": {"f2"}}, |
Liz Kammer | 82ad8cc | 2021-08-02 10:41:48 -0400 | [diff] [blame] | 270 | }, |
Sam Delmerico | 7f88956 | 2022-03-25 14:55:40 +0000 | [diff] [blame] | 271 | exportedVariableReferenceDictVars: ExportedVariableReferenceDictVariables{ |
Liz Kammer | e8303bd | 2022-02-16 09:02:48 -0500 | [diff] [blame] | 272 | "aa": map[string]string{"b1": "${b}"}, |
| 273 | "cc": map[string]string{"d1": "${config.d}"}, |
| 274 | }, |
Liz Kammer | 82ad8cc | 2021-08-02 10:41:48 -0400 | [diff] [blame] | 275 | }, |
Sam Delmerico | 7f88956 | 2022-03-25 14:55:40 +0000 | [diff] [blame] | 276 | expectedOut: bazel.GeneratedBazelFileWarning + ` |
Liz Kammer | 82ad8cc | 2021-08-02 10:41:48 -0400 | [diff] [blame] | 277 | |
| 278 | _a = { |
Liz Kammer | 72beb34 | 2022-02-03 08:42:10 -0500 | [diff] [blame] | 279 | "a1": ["a2"], |
Liz Kammer | 82ad8cc | 2021-08-02 10:41:48 -0400 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | _b = "b-val" |
| 283 | |
Liz Kammer | 72beb34 | 2022-02-03 08:42:10 -0500 | [diff] [blame] | 284 | _c = ["c-val"] |
Liz Kammer | 82ad8cc | 2021-08-02 10:41:48 -0400 | [diff] [blame] | 285 | |
| 286 | _d = "d-val" |
| 287 | |
Liz Kammer | 72beb34 | 2022-02-03 08:42:10 -0500 | [diff] [blame] | 288 | _e = ["e-val"] |
Liz Kammer | 82ad8cc | 2021-08-02 10:41:48 -0400 | [diff] [blame] | 289 | |
| 290 | _f = { |
Liz Kammer | 72beb34 | 2022-02-03 08:42:10 -0500 | [diff] [blame] | 291 | "f1": ["f2"], |
Liz Kammer | 82ad8cc | 2021-08-02 10:41:48 -0400 | [diff] [blame] | 292 | } |
| 293 | |
Liz Kammer | e8303bd | 2022-02-16 09:02:48 -0500 | [diff] [blame] | 294 | _aa = { |
| 295 | "b1": _b, |
| 296 | } |
| 297 | |
| 298 | _cc = { |
| 299 | "d1": _d, |
| 300 | } |
| 301 | |
Liz Kammer | 82ad8cc | 2021-08-02 10:41:48 -0400 | [diff] [blame] | 302 | constants = struct( |
| 303 | a = _a, |
| 304 | b = _b, |
| 305 | c = _c, |
| 306 | d = _d, |
| 307 | e = _e, |
| 308 | f = _f, |
Liz Kammer | e8303bd | 2022-02-16 09:02:48 -0500 | [diff] [blame] | 309 | aa = _aa, |
| 310 | cc = _cc, |
Liz Kammer | 82ad8cc | 2021-08-02 10:41:48 -0400 | [diff] [blame] | 311 | )`, |
| 312 | }, |
| 313 | } |
| 314 | |
| 315 | for _, tc := range testCases { |
| 316 | t.Run(tc.name, func(t *testing.T) { |
Sam Delmerico | 7f88956 | 2022-03-25 14:55:40 +0000 | [diff] [blame] | 317 | out := BazelToolchainVars(tc.config, tc.vars) |
Liz Kammer | 82ad8cc | 2021-08-02 10:41:48 -0400 | [diff] [blame] | 318 | if out != tc.expectedOut { |
| 319 | t.Errorf("Expected \n%s, got \n%s", tc.expectedOut, out) |
| 320 | } |
| 321 | }) |
| 322 | } |
| 323 | } |
Sam Delmerico | 932c01c | 2022-03-25 16:33:26 +0000 | [diff] [blame] | 324 | |
| 325 | func TestSplitStringKeepingQuotedSubstring(t *testing.T) { |
| 326 | testCases := []struct { |
| 327 | description string |
| 328 | s string |
| 329 | delimiter byte |
| 330 | split []string |
| 331 | }{ |
| 332 | { |
| 333 | description: "empty string returns single empty string", |
| 334 | s: "", |
| 335 | delimiter: ' ', |
| 336 | split: []string{ |
| 337 | "", |
| 338 | }, |
| 339 | }, |
| 340 | { |
| 341 | description: "string with single space returns two empty strings", |
| 342 | s: " ", |
| 343 | delimiter: ' ', |
| 344 | split: []string{ |
| 345 | "", |
| 346 | "", |
| 347 | }, |
| 348 | }, |
| 349 | { |
| 350 | description: "string with two spaces returns three empty strings", |
| 351 | s: " ", |
| 352 | delimiter: ' ', |
| 353 | split: []string{ |
| 354 | "", |
| 355 | "", |
| 356 | "", |
| 357 | }, |
| 358 | }, |
| 359 | { |
| 360 | description: "string with four words returns four word string", |
| 361 | s: "hello world with words", |
| 362 | delimiter: ' ', |
| 363 | split: []string{ |
| 364 | "hello", |
| 365 | "world", |
| 366 | "with", |
| 367 | "words", |
| 368 | }, |
| 369 | }, |
| 370 | { |
| 371 | description: "string with words and nested quote returns word strings and quote string", |
| 372 | s: `hello "world with" words`, |
| 373 | delimiter: ' ', |
| 374 | split: []string{ |
| 375 | "hello", |
| 376 | `"world with"`, |
| 377 | "words", |
| 378 | }, |
| 379 | }, |
| 380 | { |
| 381 | description: "string with escaped quote inside real quotes", |
| 382 | s: `hello \"world "with\" words"`, |
| 383 | delimiter: ' ', |
| 384 | split: []string{ |
| 385 | "hello", |
| 386 | `"world`, |
| 387 | `"with" words"`, |
| 388 | }, |
| 389 | }, |
| 390 | { |
| 391 | description: "string with words and escaped quotes returns word strings", |
| 392 | s: `hello \"world with\" words`, |
| 393 | delimiter: ' ', |
| 394 | split: []string{ |
| 395 | "hello", |
| 396 | `"world`, |
| 397 | `with"`, |
| 398 | "words", |
| 399 | }, |
| 400 | }, |
| 401 | { |
| 402 | description: "string which is single quoted substring returns only substring", |
| 403 | s: `"hello world with words"`, |
| 404 | delimiter: ' ', |
| 405 | split: []string{ |
| 406 | `"hello world with words"`, |
| 407 | }, |
| 408 | }, |
| 409 | { |
| 410 | description: "string starting with quote returns quoted string", |
| 411 | s: `"hello world with" words`, |
| 412 | delimiter: ' ', |
| 413 | split: []string{ |
| 414 | `"hello world with"`, |
| 415 | "words", |
| 416 | }, |
| 417 | }, |
| 418 | { |
| 419 | description: "string with starting quote and no ending quote returns quote to end of string", |
| 420 | s: `hello "world with words`, |
| 421 | delimiter: ' ', |
| 422 | split: []string{ |
| 423 | "hello", |
| 424 | `"world with words`, |
| 425 | }, |
| 426 | }, |
| 427 | { |
| 428 | description: "quoted string is treated as a single \"word\" unless separated by delimiter", |
| 429 | s: `hello "world"with words`, |
| 430 | delimiter: ' ', |
| 431 | split: []string{ |
| 432 | "hello", |
| 433 | `"world"with`, |
| 434 | "words", |
| 435 | }, |
| 436 | }, |
| 437 | } |
| 438 | |
| 439 | for _, tc := range testCases { |
| 440 | t.Run(tc.description, func(t *testing.T) { |
| 441 | split := splitStringKeepingQuotedSubstring(tc.s, tc.delimiter) |
| 442 | if len(split) != len(tc.split) { |
| 443 | t.Fatalf("number of split string elements (%d) differs from expected (%d): split string (%v), expected (%v)", |
| 444 | len(split), len(tc.split), split, tc.split, |
| 445 | ) |
| 446 | } |
| 447 | for i := range split { |
| 448 | if split[i] != tc.split[i] { |
| 449 | t.Errorf("split string element (%d), %v, differs from expected, %v", i, split[i], tc.split[i]) |
| 450 | } |
| 451 | } |
| 452 | }) |
| 453 | } |
| 454 | } |