summaryrefslogtreecommitdiff
path: root/android/arch_test.go
diff options
context:
space:
mode:
author Colin Cross <ccross@android.com> 2019-01-24 14:58:11 -0800
committer Colin Cross <ccross@android.com> 2019-01-25 21:26:53 +0000
commitcb9880786d611ebb44f28752c383b11c3c686b9c (patch)
tree49b3b7814c5f6402d4d0793a1a2727769808d095 /android/arch_test.go
parentea5c1f453cf797c90ccfae10d6f4af08263e1d6f (diff)
Optimize filterArchStruct when nothing is filtered
Runtime-created struct types have limit on the name of the type, which is the full text of the fields in the struct. Avoid creating runtime struct types when filtering produces an identical struct. Also add tests and godoc for filterArchStruct. Test: arch_test.go Change-Id: If7eb27ea9e72073d5252e93f2110b08889ed76b1
Diffstat (limited to 'android/arch_test.go')
-rw-r--r--android/arch_test.go232
1 files changed, 232 insertions, 0 deletions
diff --git a/android/arch_test.go b/android/arch_test.go
new file mode 100644
index 000000000..0589e6c3a
--- /dev/null
+++ b/android/arch_test.go
@@ -0,0 +1,232 @@
+// Copyright 2019 Google Inc. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package android
+
+import (
+ "reflect"
+ "testing"
+)
+
+type Named struct {
+ A *string `android:"arch_variant"`
+ B *string
+}
+
+type NamedAllFiltered struct {
+ A *string
+}
+
+type NamedNoneFiltered struct {
+ A *string `android:"arch_variant"`
+}
+
+func TestFilterArchStruct(t *testing.T) {
+ tests := []struct {
+ name string
+ in interface{}
+ out interface{}
+ filtered bool
+ }{
+ // Property tests
+ {
+ name: "basic",
+ in: &struct {
+ A *string `android:"arch_variant"`
+ B *string
+ }{},
+ out: &struct {
+ A *string
+ }{},
+ filtered: true,
+ },
+ {
+ name: "all filtered",
+ in: &struct {
+ A *string
+ }{},
+ out: nil,
+ filtered: true,
+ },
+ {
+ name: "none filtered",
+ in: &struct {
+ A *string `android:"arch_variant"`
+ }{},
+ out: &struct {
+ A *string `android:"arch_variant"`
+ }{},
+ filtered: false,
+ },
+
+ // Sub-struct tests
+ {
+ name: "substruct",
+ in: &struct {
+ A struct {
+ A *string `android:"arch_variant"`
+ B *string
+ } `android:"arch_variant"`
+ }{},
+ out: &struct {
+ A struct {
+ A *string
+ }
+ }{},
+ filtered: true,
+ },
+ {
+ name: "substruct all filtered",
+ in: &struct {
+ A struct {
+ A *string
+ } `android:"arch_variant"`
+ }{},
+ out: nil,
+ filtered: true,
+ },
+ {
+ name: "substruct none filtered",
+ in: &struct {
+ A struct {
+ A *string `android:"arch_variant"`
+ } `android:"arch_variant"`
+ }{},
+ out: &struct {
+ A struct {
+ A *string `android:"arch_variant"`
+ } `android:"arch_variant"`
+ }{},
+ filtered: false,
+ },
+
+ // Named sub-struct tests
+ {
+ name: "named substruct",
+ in: &struct {
+ A Named `android:"arch_variant"`
+ }{},
+ out: &struct {
+ A struct {
+ A *string
+ }
+ }{},
+ filtered: true,
+ },
+ {
+ name: "substruct all filtered",
+ in: &struct {
+ A NamedAllFiltered `android:"arch_variant"`
+ }{},
+ out: nil,
+ filtered: true,
+ },
+ {
+ name: "substruct none filtered",
+ in: &struct {
+ A NamedNoneFiltered `android:"arch_variant"`
+ }{},
+ out: &struct {
+ A NamedNoneFiltered `android:"arch_variant"`
+ }{},
+ filtered: false,
+ },
+
+ // Pointer to sub-struct tests
+ {
+ name: "pointer substruct",
+ in: &struct {
+ A *struct {
+ A *string `android:"arch_variant"`
+ B *string
+ } `android:"arch_variant"`
+ }{},
+ out: &struct {
+ A *struct {
+ A *string
+ }
+ }{},
+ filtered: true,
+ },
+ {
+ name: "pointer substruct all filtered",
+ in: &struct {
+ A *struct {
+ A *string
+ } `android:"arch_variant"`
+ }{},
+ out: nil,
+ filtered: true,
+ },
+ {
+ name: "pointer substruct none filtered",
+ in: &struct {
+ A *struct {
+ A *string `android:"arch_variant"`
+ } `android:"arch_variant"`
+ }{},
+ out: &struct {
+ A *struct {
+ A *string `android:"arch_variant"`
+ } `android:"arch_variant"`
+ }{},
+ filtered: false,
+ },
+
+ // Pointer to named sub-struct tests
+ {
+ name: "pointer named substruct",
+ in: &struct {
+ A *Named `android:"arch_variant"`
+ }{},
+ out: &struct {
+ A *struct {
+ A *string
+ }
+ }{},
+ filtered: true,
+ },
+ {
+ name: "pointer substruct all filtered",
+ in: &struct {
+ A *NamedAllFiltered `android:"arch_variant"`
+ }{},
+ out: nil,
+ filtered: true,
+ },
+ {
+ name: "pointer substruct none filtered",
+ in: &struct {
+ A *NamedNoneFiltered `android:"arch_variant"`
+ }{},
+ out: &struct {
+ A *NamedNoneFiltered `android:"arch_variant"`
+ }{},
+ filtered: false,
+ },
+ }
+
+ for _, test := range tests {
+ t.Run(test.name, func(t *testing.T) {
+ out, filtered := filterArchStruct(reflect.TypeOf(test.in))
+ if filtered != test.filtered {
+ t.Errorf("expected filtered %v, got %v", test.filtered, filtered)
+ }
+ expected := reflect.TypeOf(test.out)
+ if out != expected {
+ t.Errorf("expected type %v, got %v", expected, out)
+ }
+ })
+ }
+}