blob: a92d439947b3b806dc812304944c18e4f88896c9 [file] [log] [blame]
Yo Chiangb138d492020-03-04 20:53:21 +08001// Copyright 2020 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
15package main
16
17import (
18 "strings"
19 "testing"
20
21 "github.com/google/blueprint/parser"
Colin Crossb1abbad2021-04-29 20:16:40 -070022 "github.com/google/blueprint/proptools"
Yo Chiangb138d492020-03-04 20:53:21 +080023)
24
25var testCases = []struct {
Colin Cross5ef7b662021-04-29 19:09:04 -070026 name string
Yo Chiangb138d492020-03-04 20:53:21 +080027 input string
28 output string
29 property string
30 addSet string
31 removeSet string
Colin Crossb1abbad2021-04-29 20:16:40 -070032 setString *string
Yo Chiangb138d492020-03-04 20:53:21 +080033}{
34 {
Colin Cross5ef7b662021-04-29 19:09:04 -070035 name: "add",
36 input: `
37 cc_foo {
38 name: "foo",
39 }
Yo Chiangb138d492020-03-04 20:53:21 +080040 `,
Colin Cross5ef7b662021-04-29 19:09:04 -070041 output: `
42 cc_foo {
43 name: "foo",
44 deps: ["bar"],
45 }
Yo Chiangb138d492020-03-04 20:53:21 +080046 `,
Colin Cross5ef7b662021-04-29 19:09:04 -070047 property: "deps",
48 addSet: "bar",
Yo Chiangb138d492020-03-04 20:53:21 +080049 },
50 {
Colin Cross5ef7b662021-04-29 19:09:04 -070051 name: "remove",
52 input: `
53 cc_foo {
54 name: "foo",
55 deps: ["bar"],
56 }
Yo Chiangb138d492020-03-04 20:53:21 +080057 `,
Colin Cross5ef7b662021-04-29 19:09:04 -070058 output: `
59 cc_foo {
60 name: "foo",
61 deps: [],
62 }
Yo Chiangb138d492020-03-04 20:53:21 +080063 `,
Colin Cross5ef7b662021-04-29 19:09:04 -070064 property: "deps",
65 removeSet: "bar",
Yo Chiangb138d492020-03-04 20:53:21 +080066 },
67 {
Colin Cross5ef7b662021-04-29 19:09:04 -070068 name: "nested add",
69 input: `
70 cc_foo {
71 name: "foo",
72 }
Yo Chiangb138d492020-03-04 20:53:21 +080073 `,
Colin Cross5ef7b662021-04-29 19:09:04 -070074 output: `
75 cc_foo {
76 name: "foo",
77 arch: {
78 arm: {
79 deps: [
80 "dep2",
81 "nested_dep",],
82 },
Yo Chiangb138d492020-03-04 20:53:21 +080083 },
Colin Cross5ef7b662021-04-29 19:09:04 -070084 }
Yo Chiangb138d492020-03-04 20:53:21 +080085 `,
Colin Cross5ef7b662021-04-29 19:09:04 -070086 property: "arch.arm.deps",
87 addSet: "nested_dep,dep2",
Yo Chiangb138d492020-03-04 20:53:21 +080088 },
89 {
Colin Cross5ef7b662021-04-29 19:09:04 -070090 name: "nested remove",
91 input: `
92 cc_foo {
93 name: "foo",
94 arch: {
95 arm: {
96 deps: [
97 "dep2",
98 "nested_dep",
99 ],
100 },
Yo Chiangb138d492020-03-04 20:53:21 +0800101 },
Colin Cross5ef7b662021-04-29 19:09:04 -0700102 }
Yo Chiangb138d492020-03-04 20:53:21 +0800103 `,
Colin Cross5ef7b662021-04-29 19:09:04 -0700104 output: `
105 cc_foo {
106 name: "foo",
107 arch: {
108 arm: {
109 deps: [
110 ],
111 },
Yo Chiangb138d492020-03-04 20:53:21 +0800112 },
Colin Cross5ef7b662021-04-29 19:09:04 -0700113 }
Yo Chiangb138d492020-03-04 20:53:21 +0800114 `,
Colin Cross5ef7b662021-04-29 19:09:04 -0700115 property: "arch.arm.deps",
116 removeSet: "nested_dep,dep2",
Yo Chiangb138d492020-03-04 20:53:21 +0800117 },
118 {
Colin Cross5ef7b662021-04-29 19:09:04 -0700119 name: "add existing",
120 input: `
121 cc_foo {
122 name: "foo",
123 arch: {
124 arm: {
125 deps: [
126 "nested_dep",
127 "dep2",
128 ],
129 },
Yo Chiangb138d492020-03-04 20:53:21 +0800130 },
Colin Cross5ef7b662021-04-29 19:09:04 -0700131 }
Yo Chiangb138d492020-03-04 20:53:21 +0800132 `,
Colin Cross5ef7b662021-04-29 19:09:04 -0700133 output: `
134 cc_foo {
135 name: "foo",
136 arch: {
137 arm: {
138 deps: [
139 "nested_dep",
140 "dep2",
141 ],
142 },
Yo Chiangb138d492020-03-04 20:53:21 +0800143 },
Colin Cross5ef7b662021-04-29 19:09:04 -0700144 }
Yo Chiangb138d492020-03-04 20:53:21 +0800145 `,
Colin Cross5ef7b662021-04-29 19:09:04 -0700146 property: "arch.arm.deps",
147 addSet: "dep2,dep2",
Yo Chiangb138d492020-03-04 20:53:21 +0800148 },
149 {
Colin Cross5ef7b662021-04-29 19:09:04 -0700150 name: "remove missing",
151 input: `
152 cc_foo {
153 name: "foo",
154 arch: {
155 arm: {
156 deps: [
157 "nested_dep",
158 "dep2",
159 ],
160 },
Yo Chiangb138d492020-03-04 20:53:21 +0800161 },
Colin Cross5ef7b662021-04-29 19:09:04 -0700162 }
Yo Chiangb138d492020-03-04 20:53:21 +0800163 `,
Colin Cross5ef7b662021-04-29 19:09:04 -0700164 output: `
165 cc_foo {
166 name: "foo",
167 arch: {
168 arm: {
169 deps: [
170 "nested_dep",
171 "dep2",
172 ],
173 },
Yo Chiangb138d492020-03-04 20:53:21 +0800174 },
Colin Cross5ef7b662021-04-29 19:09:04 -0700175 }
Yo Chiangb138d492020-03-04 20:53:21 +0800176 `,
Colin Cross5ef7b662021-04-29 19:09:04 -0700177 property: "arch.arm.deps",
178 removeSet: "dep3,dep4",
Yo Chiangb138d492020-03-04 20:53:21 +0800179 },
180 {
Colin Cross5ef7b662021-04-29 19:09:04 -0700181 name: "remove non existent",
182 input: `
183 cc_foo {
184 name: "foo",
185 }
Yo Chiangb138d492020-03-04 20:53:21 +0800186 `,
Colin Cross5ef7b662021-04-29 19:09:04 -0700187 output: `
188 cc_foo {
189 name: "foo",
190 }
Yo Chiangb138d492020-03-04 20:53:21 +0800191 `,
Colin Cross5ef7b662021-04-29 19:09:04 -0700192 property: "deps",
193 removeSet: "bar",
Yo Chiangb138d492020-03-04 20:53:21 +0800194 },
195 {
Colin Cross5ef7b662021-04-29 19:09:04 -0700196 name: "remove non existent nested",
197 input: `
198 cc_foo {
199 name: "foo",
200 arch: {},
201 }
Yo Chiangb138d492020-03-04 20:53:21 +0800202 `,
Colin Cross5ef7b662021-04-29 19:09:04 -0700203 output: `
204 cc_foo {
205 name: "foo",
206 arch: {},
207 }
Yo Chiangb138d492020-03-04 20:53:21 +0800208 `,
Colin Cross5ef7b662021-04-29 19:09:04 -0700209 property: "arch.arm.deps",
210 removeSet: "dep3,dep4",
Yo Chiangb138d492020-03-04 20:53:21 +0800211 },
Jooyung Han53e92a02020-12-15 04:30:48 +0900212 {
Colin Cross5ef7b662021-04-29 19:09:04 -0700213 name: "add numeric sorted",
214 input: `
215 cc_foo {
216 name: "foo",
217 versions: ["1", "2"],
218 }
Jooyung Han53e92a02020-12-15 04:30:48 +0900219 `,
Colin Cross5ef7b662021-04-29 19:09:04 -0700220 output: `
221 cc_foo {
222 name: "foo",
223 versions: [
224 "1",
225 "2",
226 "10",
227 ],
228 }
Jooyung Han53e92a02020-12-15 04:30:48 +0900229 `,
Colin Cross5ef7b662021-04-29 19:09:04 -0700230 property: "versions",
231 addSet: "10",
Jooyung Han53e92a02020-12-15 04:30:48 +0900232 },
233 {
Colin Cross5ef7b662021-04-29 19:09:04 -0700234 name: "add mixed sorted",
235 input: `
236 cc_foo {
237 name: "foo",
238 deps: ["bar-v1-bar", "bar-v2-bar"],
239 }
Jooyung Han53e92a02020-12-15 04:30:48 +0900240 `,
Colin Cross5ef7b662021-04-29 19:09:04 -0700241 output: `
242 cc_foo {
243 name: "foo",
244 deps: [
245 "bar-v1-bar",
246 "bar-v2-bar",
247 "bar-v10-bar",
248 ],
249 }
Jooyung Han53e92a02020-12-15 04:30:48 +0900250 `,
Colin Cross5ef7b662021-04-29 19:09:04 -0700251 property: "deps",
252 addSet: "bar-v10-bar",
Jooyung Han53e92a02020-12-15 04:30:48 +0900253 },
Colin Crossb1abbad2021-04-29 20:16:40 -0700254 {
255 name: "set string",
256 input: `
257 cc_foo {
258 name: "foo",
259 }
260 `,
261 output: `
262 cc_foo {
263 name: "foo",
264 foo: "bar",
265 }
266 `,
267 property: "foo",
268 setString: proptools.StringPtr("bar"),
269 },
270 {
271 name: "set existing string",
272 input: `
273 cc_foo {
274 name: "foo",
275 foo: "baz",
276 }
277 `,
278 output: `
279 cc_foo {
280 name: "foo",
281 foo: "bar",
282 }
283 `,
284 property: "foo",
285 setString: proptools.StringPtr("bar"),
286 },
Yo Chiangb138d492020-03-04 20:53:21 +0800287}
288
289func simplifyModuleDefinition(def string) string {
290 var result string
291 for _, line := range strings.Split(def, "\n") {
292 result += strings.TrimSpace(line)
293 }
294 return result
295}
296
297func TestProcessModule(t *testing.T) {
298 for i, testCase := range testCases {
Colin Cross5ef7b662021-04-29 19:09:04 -0700299 t.Run(testCase.name, func(t *testing.T) {
300 targetedProperty.Set(testCase.property)
301 addIdents.Set(testCase.addSet)
302 removeIdents.Set(testCase.removeSet)
Colin Crossb1abbad2021-04-29 20:16:40 -0700303 setString = testCase.setString
Yo Chiangb138d492020-03-04 20:53:21 +0800304
Colin Cross5ef7b662021-04-29 19:09:04 -0700305 inAst, errs := parser.ParseAndEval("", strings.NewReader(testCase.input), parser.NewScope(nil))
Yo Chiangb138d492020-03-04 20:53:21 +0800306 if len(errs) > 0 {
Yo Chiangb138d492020-03-04 20:53:21 +0800307 for _, err := range errs {
308 t.Errorf(" %s", err)
309 }
Colin Cross5ef7b662021-04-29 19:09:04 -0700310 t.Errorf("failed to parse:")
311 t.Errorf("%+v", testCase)
312 t.FailNow()
Yo Chiangb138d492020-03-04 20:53:21 +0800313 }
Colin Cross5ef7b662021-04-29 19:09:04 -0700314
315 if inModule, ok := inAst.Defs[0].(*parser.Module); !ok {
316 t.Fatalf(" input must only contain a single module definition: %s", testCase.input)
317 } else {
318 _, errs := processModule(inModule, "", inAst)
319 if len(errs) > 0 {
320 t.Errorf("test case %d:", i)
321 for _, err := range errs {
322 t.Errorf(" %s", err)
323 }
324 }
325 inModuleText, _ := parser.Print(inAst)
326 inModuleString := string(inModuleText)
327 if simplifyModuleDefinition(inModuleString) != simplifyModuleDefinition(testCase.output) {
328 t.Errorf("test case %d:", i)
329 t.Errorf("expected module definition:")
330 t.Errorf(" %s", testCase.output)
331 t.Errorf("actual module definition:")
332 t.Errorf(" %s", inModuleString)
333 }
Yo Chiangb138d492020-03-04 20:53:21 +0800334 }
Colin Cross5ef7b662021-04-29 19:09:04 -0700335 })
Yo Chiangb138d492020-03-04 20:53:21 +0800336 }
Colin Cross5ef7b662021-04-29 19:09:04 -0700337
Yo Chiangb138d492020-03-04 20:53:21 +0800338}