blob: 419001670709366301aa1727c914cf01653cc043 [file] [log] [blame]
Colin Cross9d34f352019-11-22 16:03:51 -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 soongconfig
16
17import (
18 "reflect"
19 "testing"
20)
21
22func Test_CanonicalizeToProperty(t *testing.T) {
23 tests := []struct {
24 name string
25 arg string
26 want string
27 }{
28 {
29 name: "lowercase",
30 arg: "board",
31 want: "board",
32 },
33 {
34 name: "uppercase",
35 arg: "BOARD",
36 want: "BOARD",
37 },
38 {
39 name: "numbers",
40 arg: "BOARD123",
41 want: "BOARD123",
42 },
43 {
44 name: "underscore",
45 arg: "TARGET_BOARD",
46 want: "TARGET_BOARD",
47 },
48 {
49 name: "dash",
50 arg: "TARGET-BOARD",
51 want: "TARGET_BOARD",
52 },
53 {
54 name: "unicode",
55 arg: "boardĪ»",
56 want: "board_",
57 },
58 }
59 for _, tt := range tests {
60 t.Run(tt.name, func(t *testing.T) {
61 if got := CanonicalizeToProperty(tt.arg); got != tt.want {
62 t.Errorf("canonicalizeToProperty() = %v, want %v", got, tt.want)
63 }
64 })
65 }
66}
67
68func Test_typeForPropertyFromPropertyStruct(t *testing.T) {
69 tests := []struct {
70 name string
71 ps interface{}
72 property string
73 want string
74 }{
75 {
76 name: "string",
77 ps: struct {
78 A string
79 }{},
80 property: "a",
81 want: "string",
82 },
83 {
84 name: "list",
85 ps: struct {
86 A []string
87 }{},
88 property: "a",
89 want: "[]string",
90 },
91 {
92 name: "missing",
93 ps: struct {
94 A []string
95 }{},
96 property: "b",
97 want: "",
98 },
99 {
100 name: "nested",
101 ps: struct {
102 A struct {
103 B string
104 }
105 }{},
106 property: "a.b",
107 want: "string",
108 },
109 {
110 name: "missing nested",
111 ps: struct {
112 A struct {
113 B string
114 }
115 }{},
116 property: "a.c",
117 want: "",
118 },
119 {
120 name: "not a struct",
121 ps: struct {
122 A string
123 }{},
124 property: "a.b",
125 want: "",
126 },
127 {
128 name: "nested pointer",
129 ps: struct {
130 A *struct {
131 B string
132 }
133 }{},
134 property: "a.b",
135 want: "string",
136 },
137 {
138 name: "nested interface",
139 ps: struct {
140 A interface{}
141 }{
142 A: struct {
143 B string
144 }{},
145 },
146 property: "a.b",
147 want: "string",
148 },
149 {
150 name: "nested interface pointer",
151 ps: struct {
152 A interface{}
153 }{
154 A: &struct {
155 B string
156 }{},
157 },
158 property: "a.b",
159 want: "string",
160 },
161 {
162 name: "nested interface nil pointer",
163 ps: struct {
164 A interface{}
165 }{
166 A: (*struct {
167 B string
168 })(nil),
169 },
170 property: "a.b",
171 want: "string",
172 },
173 }
174 for _, tt := range tests {
175 t.Run(tt.name, func(t *testing.T) {
176 typ := typeForPropertyFromPropertyStruct(tt.ps, tt.property)
177 got := ""
178 if typ != nil {
179 got = typ.String()
180 }
181 if got != tt.want {
182 t.Errorf("typeForPropertyFromPropertyStruct() = %v, want %v", got, tt.want)
183 }
184 })
185 }
186}
187
188func Test_createAffectablePropertiesType(t *testing.T) {
189 tests := []struct {
190 name string
191 affectableProperties []string
192 factoryProps interface{}
193 want string
194 }{
195 {
196 name: "string",
197 affectableProperties: []string{"cflags"},
198 factoryProps: struct {
199 Cflags string
200 }{},
201 want: "*struct { Cflags string }",
202 },
203 {
204 name: "list",
205 affectableProperties: []string{"cflags"},
206 factoryProps: struct {
207 Cflags []string
208 }{},
209 want: "*struct { Cflags []string }",
210 },
211 {
212 name: "string pointer",
213 affectableProperties: []string{"cflags"},
214 factoryProps: struct {
215 Cflags *string
216 }{},
217 want: "*struct { Cflags *string }",
218 },
219 {
220 name: "subset",
221 affectableProperties: []string{"cflags"},
222 factoryProps: struct {
223 Cflags string
224 Ldflags string
225 }{},
226 want: "*struct { Cflags string }",
227 },
228 {
229 name: "none",
230 affectableProperties: []string{"cflags"},
231 factoryProps: struct {
232 Ldflags string
233 }{},
234 want: "",
235 },
236 }
237 for _, tt := range tests {
238 t.Run(tt.name, func(t *testing.T) {
239 typ := createAffectablePropertiesType(tt.affectableProperties, []interface{}{tt.factoryProps})
240 got := ""
241 if typ != nil {
242 got = typ.String()
243 }
244 if !reflect.DeepEqual(got, tt.want) {
245 t.Errorf("createAffectablePropertiesType() = %v, want %v", got, tt.want)
246 }
247 })
248 }
249}