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