1// Copyright (C) 2020 The Android Open Source Project 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 sdk 16 17import ( 18 "testing" 19 20 "android/soong/android" 21 22 "github.com/google/blueprint/proptools" 23) 24 25func propertySetFixture() interface{} { 26 set := newPropertySet() 27 set.AddProperty("x", "taxi") 28 set.AddPropertyWithTag("y", 1729, "tag_y") 29 subset := set.AddPropertySet("sub") 30 subset.AddPropertyWithTag("x", "taxi", "tag_x") 31 subset.AddProperty("y", 1729) 32 return set 33} 34 35func intPtr(i int) *int { return &i } 36 37type propertyStruct struct { 38 X *string 39 Y *int 40 Unset *bool 41 Sub struct { 42 X *string 43 Y *int 44 Unset *bool 45 } 46} 47 48func propertyStructFixture() interface{} { 49 str := &propertyStruct{} 50 str.X = proptools.StringPtr("taxi") 51 str.Y = intPtr(1729) 52 str.Sub.X = proptools.StringPtr("taxi") 53 str.Sub.Y = intPtr(1729) 54 return str 55} 56 57func checkPropertySetFixture(t *testing.T, val interface{}, hasTags bool) { 58 set := val.(*bpPropertySet) 59 android.AssertDeepEquals(t, "wrong x value", "taxi", set.getValue("x")) 60 android.AssertDeepEquals(t, "wrong y value", 1729, set.getValue("y")) 61 62 subset := set.getValue("sub").(*bpPropertySet) 63 android.AssertDeepEquals(t, "wrong sub.x value", "taxi", subset.getValue("x")) 64 android.AssertDeepEquals(t, "wrong sub.y value", 1729, subset.getValue("y")) 65 66 if hasTags { 67 android.AssertDeepEquals(t, "wrong y tag", "tag_y", set.getTag("y")) 68 android.AssertDeepEquals(t, "wrong sub.x tag", "tag_x", subset.getTag("x")) 69 } else { 70 android.AssertDeepEquals(t, "wrong y tag", nil, set.getTag("y")) 71 android.AssertDeepEquals(t, "wrong sub.x tag", nil, subset.getTag("x")) 72 } 73} 74 75func TestAddPropertySimple(t *testing.T) { 76 set := newPropertySet() 77 for name, val := range map[string]interface{}{ 78 "x": "taxi", 79 "y": 1729, 80 "t": true, 81 "f": false, 82 "arr": []string{"a", "b", "c"}, 83 } { 84 set.AddProperty(name, val) 85 android.AssertDeepEquals(t, "wrong value", val, set.getValue(name)) 86 } 87 android.AssertPanicMessageContains(t, "adding x again should panic", `Property "x" already exists in property set`, 88 func() { set.AddProperty("x", "taxi") }) 89 android.AssertPanicMessageContains(t, "adding arr again should panic", `Property "arr" already exists in property set`, 90 func() { set.AddProperty("arr", []string{"d"}) }) 91} 92 93func TestAddPropertySubset(t *testing.T) { 94 getFixtureMap := map[string]func() interface{}{ 95 "property set": propertySetFixture, 96 "property struct": propertyStructFixture, 97 } 98 99 t.Run("add new subset", func(t *testing.T) { 100 for name, getFixture := range getFixtureMap { 101 t.Run(name, func(t *testing.T) { 102 set := propertySetFixture().(*bpPropertySet) 103 set.AddProperty("new", getFixture()) 104 checkPropertySetFixture(t, set, true) 105 checkPropertySetFixture(t, set.getValue("new"), name == "property set") 106 }) 107 } 108 }) 109 110 t.Run("merge existing subset", func(t *testing.T) { 111 for name, getFixture := range getFixtureMap { 112 t.Run(name, func(t *testing.T) { 113 set := newPropertySet() 114 subset := set.AddPropertySet("sub") 115 subset.AddProperty("flag", false) 116 subset.AddPropertySet("sub") 117 set.AddProperty("sub", getFixture()) 118 merged := set.getValue("sub").(*bpPropertySet) 119 android.AssertDeepEquals(t, "wrong flag value", false, merged.getValue("flag")) 120 checkPropertySetFixture(t, merged, name == "property set") 121 }) 122 } 123 }) 124 125 t.Run("add conflicting subset", func(t *testing.T) { 126 set := propertySetFixture().(*bpPropertySet) 127 android.AssertPanicMessageContains(t, "adding x again should panic", `Property "x" already exists in property set`, 128 func() { set.AddProperty("x", propertySetFixture()) }) 129 }) 130 131 t.Run("add non-pointer struct", func(t *testing.T) { 132 set := propertySetFixture().(*bpPropertySet) 133 str := propertyStructFixture().(*propertyStruct) 134 android.AssertPanicMessageContains(t, "adding a non-pointer struct should panic", "Value is a struct, not a pointer to one:", 135 func() { set.AddProperty("new", *str) }) 136 }) 137} 138 139func TestAddPropertySetNew(t *testing.T) { 140 set := newPropertySet() 141 subset := set.AddPropertySet("sub") 142 subset.AddProperty("new", "d^^b") 143 android.AssertDeepEquals(t, "wrong sub.new value", "d^^b", set.getValue("sub").(*bpPropertySet).getValue("new")) 144} 145 146func TestAddPropertySetExisting(t *testing.T) { 147 set := propertySetFixture().(*bpPropertySet) 148 subset := set.AddPropertySet("sub") 149 subset.AddProperty("new", "d^^b") 150 android.AssertDeepEquals(t, "wrong sub.new value", "d^^b", set.getValue("sub").(*bpPropertySet).getValue("new")) 151} 152 153type removeFredTransformation struct { 154 identityTransformation 155} 156 157func (t removeFredTransformation) transformProperty(name string, value interface{}, tag android.BpPropertyTag) (interface{}, android.BpPropertyTag) { 158 if name == "fred" { 159 return nil, nil 160 } 161 return value, tag 162} 163 164func (t removeFredTransformation) transformPropertySetBeforeContents(name string, propertySet *bpPropertySet, tag android.BpPropertyTag) (*bpPropertySet, android.BpPropertyTag) { 165 if name == "fred" { 166 return nil, nil 167 } 168 return propertySet, tag 169} 170 171func (t removeFredTransformation) transformPropertySetAfterContents(name string, propertySet *bpPropertySet, tag android.BpPropertyTag) (*bpPropertySet, android.BpPropertyTag) { 172 if len(propertySet.properties) == 0 { 173 return nil, nil 174 } 175 return propertySet, tag 176} 177 178func TestTransformRemoveProperty(t *testing.T) { 179 set := newPropertySet() 180 set.AddProperty("name", "name") 181 set.AddProperty("fred", "12") 182 183 set.transformContents(removeFredTransformation{}) 184 185 contents := &generatedContents{} 186 outputPropertySet(contents, set) 187 android.AssertTrimmedStringEquals(t, "removing property failed", "name: \"name\",\n", contents.content.String()) 188} 189 190func TestTransformRemovePropertySet(t *testing.T) { 191 set := newPropertySet() 192 set.AddProperty("name", "name") 193 set.AddPropertySet("fred") 194 195 set.transformContents(removeFredTransformation{}) 196 197 contents := &generatedContents{} 198 outputPropertySet(contents, set) 199 android.AssertTrimmedStringEquals(t, "removing property set failed", "name: \"name\",\n", contents.content.String()) 200} 201