diff options
Diffstat (limited to 'sdk/bp_test.go')
-rw-r--r-- | sdk/bp_test.go | 134 |
1 files changed, 134 insertions, 0 deletions
diff --git a/sdk/bp_test.go b/sdk/bp_test.go index c630c2524..e1edc5131 100644 --- a/sdk/bp_test.go +++ b/sdk/bp_test.go @@ -18,8 +18,142 @@ import ( "testing" "android/soong/android" + + "github.com/google/blueprint/proptools" ) +func propertySetFixture() interface{} { + set := newPropertySet() + set.AddProperty("x", "taxi") + set.AddPropertyWithTag("y", 1729, "tag_y") + subset := set.AddPropertySet("sub") + subset.AddPropertyWithTag("x", "taxi", "tag_x") + subset.AddProperty("y", 1729) + return set +} + +func intPtr(i int) *int { return &i } + +type propertyStruct struct { + X *string + Y *int + Unset *bool + Sub struct { + X *string + Y *int + Unset *bool + } +} + +func propertyStructFixture() interface{} { + str := &propertyStruct{} + str.X = proptools.StringPtr("taxi") + str.Y = intPtr(1729) + str.Sub.X = proptools.StringPtr("taxi") + str.Sub.Y = intPtr(1729) + return str +} + +func checkPropertySetFixture(h *TestHelper, val interface{}, hasTags bool) { + set := val.(*bpPropertySet) + h.AssertDeepEquals("wrong x value", "taxi", set.getValue("x")) + h.AssertDeepEquals("wrong y value", 1729, set.getValue("y")) + + subset := set.getValue("sub").(*bpPropertySet) + h.AssertDeepEquals("wrong sub.x value", "taxi", subset.getValue("x")) + h.AssertDeepEquals("wrong sub.y value", 1729, subset.getValue("y")) + + if hasTags { + h.AssertDeepEquals("wrong y tag", "tag_y", set.getTag("y")) + h.AssertDeepEquals("wrong sub.x tag", "tag_x", subset.getTag("x")) + } else { + h.AssertDeepEquals("wrong y tag", nil, set.getTag("y")) + h.AssertDeepEquals("wrong sub.x tag", nil, subset.getTag("x")) + } +} + +func TestAddPropertySimple(t *testing.T) { + h := &TestHelper{t} + set := newPropertySet() + for name, val := range map[string]interface{}{ + "x": "taxi", + "y": 1729, + "t": true, + "f": false, + "arr": []string{"a", "b", "c"}, + } { + set.AddProperty(name, val) + h.AssertDeepEquals("wrong value", val, set.getValue(name)) + } + h.AssertPanic("adding x again should panic", + func() { set.AddProperty("x", "taxi") }) + h.AssertPanic("adding arr again should panic", + func() { set.AddProperty("arr", []string{"d"}) }) +} + +func TestAddPropertySubset(t *testing.T) { + h := &TestHelper{t} + getFixtureMap := map[string]func() interface{}{ + "property set": propertySetFixture, + "property struct": propertyStructFixture, + } + + t.Run("add new subset", func(t *testing.T) { + for name, getFixture := range getFixtureMap { + t.Run(name, func(t *testing.T) { + set := propertySetFixture().(*bpPropertySet) + set.AddProperty("new", getFixture()) + checkPropertySetFixture(h, set, true) + checkPropertySetFixture(h, set.getValue("new"), name == "property set") + }) + } + }) + + t.Run("merge existing subset", func(t *testing.T) { + for name, getFixture := range getFixtureMap { + t.Run(name, func(t *testing.T) { + set := newPropertySet() + subset := set.AddPropertySet("sub") + subset.AddProperty("flag", false) + subset.AddPropertySet("sub") + set.AddProperty("sub", getFixture()) + merged := set.getValue("sub").(*bpPropertySet) + h.AssertDeepEquals("wrong flag value", false, merged.getValue("flag")) + checkPropertySetFixture(h, merged, name == "property set") + }) + } + }) + + t.Run("add conflicting subset", func(t *testing.T) { + set := propertySetFixture().(*bpPropertySet) + h.AssertPanic("adding x again should panic", + func() { set.AddProperty("x", propertySetFixture()) }) + }) + + t.Run("add non-pointer struct", func(t *testing.T) { + set := propertySetFixture().(*bpPropertySet) + str := propertyStructFixture().(*propertyStruct) + h.AssertPanic("adding a non-pointer struct should panic", + func() { set.AddProperty("new", *str) }) + }) +} + +func TestAddPropertySetNew(t *testing.T) { + h := &TestHelper{t} + set := newPropertySet() + subset := set.AddPropertySet("sub") + subset.AddProperty("new", "d^^b") + h.AssertDeepEquals("wrong sub.new value", "d^^b", set.getValue("sub").(*bpPropertySet).getValue("new")) +} + +func TestAddPropertySetExisting(t *testing.T) { + h := &TestHelper{t} + set := propertySetFixture().(*bpPropertySet) + subset := set.AddPropertySet("sub") + subset.AddProperty("new", "d^^b") + h.AssertDeepEquals("wrong sub.new value", "d^^b", set.getValue("sub").(*bpPropertySet).getValue("new")) +} + type removeFredTransformation struct { identityTransformation } |