blob: 11edb4f1c41f0fae086bad0f3b94b04c18b5c22e [file] [log] [blame]
Colin Crosscb988072019-01-24 14:58:11 -08001// Copyright 2019 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 android
16
17import (
18 "reflect"
19 "testing"
Colin Cross74449102019-09-25 11:26:40 -070020
21 "github.com/google/blueprint/proptools"
Colin Crosscb988072019-01-24 14:58:11 -080022)
23
24type Named struct {
25 A *string `android:"arch_variant"`
26 B *string
27}
28
29type NamedAllFiltered struct {
30 A *string
31}
32
33type NamedNoneFiltered struct {
34 A *string `android:"arch_variant"`
35}
36
37func TestFilterArchStruct(t *testing.T) {
38 tests := []struct {
39 name string
40 in interface{}
41 out interface{}
42 filtered bool
43 }{
44 // Property tests
45 {
46 name: "basic",
47 in: &struct {
48 A *string `android:"arch_variant"`
49 B *string
50 }{},
51 out: &struct {
52 A *string
53 }{},
54 filtered: true,
55 },
56 {
57 name: "all filtered",
58 in: &struct {
59 A *string
60 }{},
61 out: nil,
62 filtered: true,
63 },
64 {
65 name: "none filtered",
66 in: &struct {
67 A *string `android:"arch_variant"`
68 }{},
69 out: &struct {
70 A *string `android:"arch_variant"`
71 }{},
72 filtered: false,
73 },
74
75 // Sub-struct tests
76 {
77 name: "substruct",
78 in: &struct {
79 A struct {
80 A *string `android:"arch_variant"`
81 B *string
82 } `android:"arch_variant"`
83 }{},
84 out: &struct {
85 A struct {
86 A *string
87 }
88 }{},
89 filtered: true,
90 },
91 {
92 name: "substruct all filtered",
93 in: &struct {
94 A struct {
95 A *string
96 } `android:"arch_variant"`
97 }{},
98 out: nil,
99 filtered: true,
100 },
101 {
102 name: "substruct none filtered",
103 in: &struct {
104 A struct {
105 A *string `android:"arch_variant"`
106 } `android:"arch_variant"`
107 }{},
108 out: &struct {
109 A struct {
110 A *string `android:"arch_variant"`
111 } `android:"arch_variant"`
112 }{},
113 filtered: false,
114 },
115
116 // Named sub-struct tests
117 {
118 name: "named substruct",
119 in: &struct {
120 A Named `android:"arch_variant"`
121 }{},
122 out: &struct {
123 A struct {
124 A *string
125 }
126 }{},
127 filtered: true,
128 },
129 {
130 name: "substruct all filtered",
131 in: &struct {
132 A NamedAllFiltered `android:"arch_variant"`
133 }{},
134 out: nil,
135 filtered: true,
136 },
137 {
138 name: "substruct none filtered",
139 in: &struct {
140 A NamedNoneFiltered `android:"arch_variant"`
141 }{},
142 out: &struct {
143 A NamedNoneFiltered `android:"arch_variant"`
144 }{},
145 filtered: false,
146 },
147
148 // Pointer to sub-struct tests
149 {
150 name: "pointer substruct",
151 in: &struct {
152 A *struct {
153 A *string `android:"arch_variant"`
154 B *string
155 } `android:"arch_variant"`
156 }{},
157 out: &struct {
158 A *struct {
159 A *string
160 }
161 }{},
162 filtered: true,
163 },
164 {
165 name: "pointer substruct all filtered",
166 in: &struct {
167 A *struct {
168 A *string
169 } `android:"arch_variant"`
170 }{},
171 out: nil,
172 filtered: true,
173 },
174 {
175 name: "pointer substruct none filtered",
176 in: &struct {
177 A *struct {
178 A *string `android:"arch_variant"`
179 } `android:"arch_variant"`
180 }{},
181 out: &struct {
182 A *struct {
183 A *string `android:"arch_variant"`
184 } `android:"arch_variant"`
185 }{},
186 filtered: false,
187 },
188
189 // Pointer to named sub-struct tests
190 {
191 name: "pointer named substruct",
192 in: &struct {
193 A *Named `android:"arch_variant"`
194 }{},
195 out: &struct {
196 A *struct {
197 A *string
198 }
199 }{},
200 filtered: true,
201 },
202 {
203 name: "pointer substruct all filtered",
204 in: &struct {
205 A *NamedAllFiltered `android:"arch_variant"`
206 }{},
207 out: nil,
208 filtered: true,
209 },
210 {
211 name: "pointer substruct none filtered",
212 in: &struct {
213 A *NamedNoneFiltered `android:"arch_variant"`
214 }{},
215 out: &struct {
216 A *NamedNoneFiltered `android:"arch_variant"`
217 }{},
218 filtered: false,
219 },
220 }
221
222 for _, test := range tests {
223 t.Run(test.name, func(t *testing.T) {
Colin Cross74449102019-09-25 11:26:40 -0700224 out, filtered := proptools.FilterPropertyStruct(reflect.TypeOf(test.in), filterArchStruct)
Colin Crosscb988072019-01-24 14:58:11 -0800225 if filtered != test.filtered {
226 t.Errorf("expected filtered %v, got %v", test.filtered, filtered)
227 }
228 expected := reflect.TypeOf(test.out)
229 if out != expected {
230 t.Errorf("expected type %v, got %v", expected, out)
231 }
232 })
233 }
234}