blob: 695549a9d6a1ff8534614a7fef7e5b2618027e40 [file] [log] [blame]
// 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 proptools
import (
"reflect"
"testing"
)
type Named struct {
A *string `keep:"true"`
B *string
}
type NamedAllFiltered struct {
A *string
}
type NamedNoneFiltered struct {
A *string `keep:"true"`
}
func TestFilterPropertyStruct(t *testing.T) {
tests := []struct {
name string
in interface{}
out interface{}
filtered bool
}{
// Property tests
{
name: "basic",
in: &struct {
A *string `keep:"true"`
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 `keep:"true"`
}{},
out: &struct {
A *string `keep:"true"`
}{},
filtered: false,
},
// Sub-struct tests
{
name: "substruct",
in: &struct {
A struct {
A *string `keep:"true"`
B *string
} `keep:"true"`
}{},
out: &struct {
A struct {
A *string
}
}{},
filtered: true,
},
{
name: "substruct all filtered",
in: &struct {
A struct {
A *string
} `keep:"true"`
}{},
out: nil,
filtered: true,
},
{
name: "substruct none filtered",
in: &struct {
A struct {
A *string `keep:"true"`
} `keep:"true"`
}{},
out: &struct {
A struct {
A *string `keep:"true"`
} `keep:"true"`
}{},
filtered: false,
},
// Named sub-struct tests
{
name: "named substruct",
in: &struct {
A Named `keep:"true"`
}{},
out: &struct {
A struct {
A *string
}
}{},
filtered: true,
},
{
name: "substruct all filtered",
in: &struct {
A NamedAllFiltered `keep:"true"`
}{},
out: nil,
filtered: true,
},
{
name: "substruct none filtered",
in: &struct {
A NamedNoneFiltered `keep:"true"`
}{},
out: &struct {
A NamedNoneFiltered `keep:"true"`
}{},
filtered: false,
},
// Pointer to sub-struct tests
{
name: "pointer substruct",
in: &struct {
A *struct {
A *string `keep:"true"`
B *string
} `keep:"true"`
}{},
out: &struct {
A *struct {
A *string
}
}{},
filtered: true,
},
{
name: "pointer substruct all filtered",
in: &struct {
A *struct {
A *string
} `keep:"true"`
}{},
out: nil,
filtered: true,
},
{
name: "pointer substruct none filtered",
in: &struct {
A *struct {
A *string `keep:"true"`
} `keep:"true"`
}{},
out: &struct {
A *struct {
A *string `keep:"true"`
} `keep:"true"`
}{},
filtered: false,
},
// Pointer to named sub-struct tests
{
name: "pointer named substruct",
in: &struct {
A *Named `keep:"true"`
}{},
out: &struct {
A *struct {
A *string
}
}{},
filtered: true,
},
{
name: "pointer substruct all filtered",
in: &struct {
A *NamedAllFiltered `keep:"true"`
}{},
out: nil,
filtered: true,
},
{
name: "pointer substruct none filtered",
in: &struct {
A *NamedNoneFiltered `keep:"true"`
}{},
out: &struct {
A *NamedNoneFiltered `keep:"true"`
}{},
filtered: false,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
out, filtered := FilterPropertyStruct(reflect.TypeOf(test.in),
func(field reflect.StructField, prefix string) (bool, reflect.StructField) {
if HasTag(field, "keep", "true") {
field.Tag = ""
return true, field
}
return false, field
})
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)
}
})
}
}