| Paul Duffin | 047fdca | 2020-02-21 16:06:25 +0000 | [diff] [blame] | 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 |  | 
|  | 15 | package sdk | 
|  | 16 |  | 
|  | 17 | import ( | 
|  | 18 | "testing" | 
|  | 19 |  | 
|  | 20 | "android/soong/android" | 
| Martin Stjernholm | 191c25f | 2020-09-10 00:40:37 +0100 | [diff] [blame] | 21 |  | 
|  | 22 | "github.com/google/blueprint/proptools" | 
| Paul Duffin | 047fdca | 2020-02-21 16:06:25 +0000 | [diff] [blame] | 23 | ) | 
|  | 24 |  | 
| Martin Stjernholm | 191c25f | 2020-09-10 00:40:37 +0100 | [diff] [blame] | 25 | func 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 |  | 
|  | 35 | func intPtr(i int) *int { return &i } | 
|  | 36 |  | 
|  | 37 | type 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 |  | 
|  | 48 | func 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 |  | 
| Paul Duffin | 36474d3 | 2021-03-12 12:19:43 +0000 | [diff] [blame] | 57 | func checkPropertySetFixture(t *testing.T, val interface{}, hasTags bool) { | 
| Martin Stjernholm | 191c25f | 2020-09-10 00:40:37 +0100 | [diff] [blame] | 58 | set := val.(*bpPropertySet) | 
| Paul Duffin | 36474d3 | 2021-03-12 12:19:43 +0000 | [diff] [blame] | 59 | android.AssertDeepEquals(t, "wrong x value", "taxi", set.getValue("x")) | 
|  | 60 | android.AssertDeepEquals(t, "wrong y value", 1729, set.getValue("y")) | 
| Martin Stjernholm | 191c25f | 2020-09-10 00:40:37 +0100 | [diff] [blame] | 61 |  | 
|  | 62 | subset := set.getValue("sub").(*bpPropertySet) | 
| Paul Duffin | 36474d3 | 2021-03-12 12:19:43 +0000 | [diff] [blame] | 63 | android.AssertDeepEquals(t, "wrong sub.x value", "taxi", subset.getValue("x")) | 
|  | 64 | android.AssertDeepEquals(t, "wrong sub.y value", 1729, subset.getValue("y")) | 
| Martin Stjernholm | 191c25f | 2020-09-10 00:40:37 +0100 | [diff] [blame] | 65 |  | 
|  | 66 | if hasTags { | 
| Paul Duffin | 36474d3 | 2021-03-12 12:19:43 +0000 | [diff] [blame] | 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")) | 
| Martin Stjernholm | 191c25f | 2020-09-10 00:40:37 +0100 | [diff] [blame] | 69 | } else { | 
| Paul Duffin | 36474d3 | 2021-03-12 12:19:43 +0000 | [diff] [blame] | 70 | android.AssertDeepEquals(t, "wrong y tag", nil, set.getTag("y")) | 
|  | 71 | android.AssertDeepEquals(t, "wrong sub.x tag", nil, subset.getTag("x")) | 
| Martin Stjernholm | 191c25f | 2020-09-10 00:40:37 +0100 | [diff] [blame] | 72 | } | 
|  | 73 | } | 
|  | 74 |  | 
|  | 75 | func TestAddPropertySimple(t *testing.T) { | 
| Martin Stjernholm | 191c25f | 2020-09-10 00:40:37 +0100 | [diff] [blame] | 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) | 
| Paul Duffin | 36474d3 | 2021-03-12 12:19:43 +0000 | [diff] [blame] | 85 | android.AssertDeepEquals(t, "wrong value", val, set.getValue(name)) | 
| Martin Stjernholm | 191c25f | 2020-09-10 00:40:37 +0100 | [diff] [blame] | 86 | } | 
| Paul Duffin | 9f4b3bb | 2021-03-16 13:47:36 +0000 | [diff] [blame] | 87 | android.AssertPanicMessageContains(t, "adding x again should panic", `Property "x" already exists in property set`, | 
| Martin Stjernholm | 191c25f | 2020-09-10 00:40:37 +0100 | [diff] [blame] | 88 | func() { set.AddProperty("x", "taxi") }) | 
| Paul Duffin | 9f4b3bb | 2021-03-16 13:47:36 +0000 | [diff] [blame] | 89 | android.AssertPanicMessageContains(t, "adding arr again should panic", `Property "arr" already exists in property set`, | 
| Martin Stjernholm | 191c25f | 2020-09-10 00:40:37 +0100 | [diff] [blame] | 90 | func() { set.AddProperty("arr", []string{"d"}) }) | 
|  | 91 | } | 
|  | 92 |  | 
|  | 93 | func TestAddPropertySubset(t *testing.T) { | 
| Martin Stjernholm | 191c25f | 2020-09-10 00:40:37 +0100 | [diff] [blame] | 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()) | 
| Paul Duffin | 36474d3 | 2021-03-12 12:19:43 +0000 | [diff] [blame] | 104 | checkPropertySetFixture(t, set, true) | 
|  | 105 | checkPropertySetFixture(t, set.getValue("new"), name == "property set") | 
| Martin Stjernholm | 191c25f | 2020-09-10 00:40:37 +0100 | [diff] [blame] | 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) | 
| Paul Duffin | 36474d3 | 2021-03-12 12:19:43 +0000 | [diff] [blame] | 119 | android.AssertDeepEquals(t, "wrong flag value", false, merged.getValue("flag")) | 
|  | 120 | checkPropertySetFixture(t, merged, name == "property set") | 
| Martin Stjernholm | 191c25f | 2020-09-10 00:40:37 +0100 | [diff] [blame] | 121 | }) | 
|  | 122 | } | 
|  | 123 | }) | 
|  | 124 |  | 
|  | 125 | t.Run("add conflicting subset", func(t *testing.T) { | 
|  | 126 | set := propertySetFixture().(*bpPropertySet) | 
| Paul Duffin | 9f4b3bb | 2021-03-16 13:47:36 +0000 | [diff] [blame] | 127 | android.AssertPanicMessageContains(t, "adding x again should panic", `Property "x" already exists in property set`, | 
| Martin Stjernholm | 191c25f | 2020-09-10 00:40:37 +0100 | [diff] [blame] | 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) | 
| Paul Duffin | 9f4b3bb | 2021-03-16 13:47:36 +0000 | [diff] [blame] | 134 | android.AssertPanicMessageContains(t, "adding a non-pointer struct should panic", "Value is a struct, not a pointer to one:", | 
| Martin Stjernholm | 191c25f | 2020-09-10 00:40:37 +0100 | [diff] [blame] | 135 | func() { set.AddProperty("new", *str) }) | 
|  | 136 | }) | 
|  | 137 | } | 
|  | 138 |  | 
|  | 139 | func TestAddPropertySetNew(t *testing.T) { | 
| Martin Stjernholm | 191c25f | 2020-09-10 00:40:37 +0100 | [diff] [blame] | 140 | set := newPropertySet() | 
|  | 141 | subset := set.AddPropertySet("sub") | 
|  | 142 | subset.AddProperty("new", "d^^b") | 
| Paul Duffin | 36474d3 | 2021-03-12 12:19:43 +0000 | [diff] [blame] | 143 | android.AssertDeepEquals(t, "wrong sub.new value", "d^^b", set.getValue("sub").(*bpPropertySet).getValue("new")) | 
| Martin Stjernholm | 191c25f | 2020-09-10 00:40:37 +0100 | [diff] [blame] | 144 | } | 
|  | 145 |  | 
|  | 146 | func TestAddPropertySetExisting(t *testing.T) { | 
| Martin Stjernholm | 191c25f | 2020-09-10 00:40:37 +0100 | [diff] [blame] | 147 | set := propertySetFixture().(*bpPropertySet) | 
|  | 148 | subset := set.AddPropertySet("sub") | 
|  | 149 | subset.AddProperty("new", "d^^b") | 
| Paul Duffin | 36474d3 | 2021-03-12 12:19:43 +0000 | [diff] [blame] | 150 | android.AssertDeepEquals(t, "wrong sub.new value", "d^^b", set.getValue("sub").(*bpPropertySet).getValue("new")) | 
| Martin Stjernholm | 191c25f | 2020-09-10 00:40:37 +0100 | [diff] [blame] | 151 | } | 
|  | 152 |  | 
| Paul Duffin | 047fdca | 2020-02-21 16:06:25 +0000 | [diff] [blame] | 153 | type removeFredTransformation struct { | 
|  | 154 | identityTransformation | 
|  | 155 | } | 
|  | 156 |  | 
|  | 157 | func (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 |  | 
| Paul Duffin | 180a006 | 2020-02-21 16:06:25 +0000 | [diff] [blame] | 164 | func (t removeFredTransformation) transformPropertySetBeforeContents(name string, propertySet *bpPropertySet, tag android.BpPropertyTag) (*bpPropertySet, android.BpPropertyTag) { | 
| Paul Duffin | 047fdca | 2020-02-21 16:06:25 +0000 | [diff] [blame] | 165 | if name == "fred" { | 
|  | 166 | return nil, nil | 
|  | 167 | } | 
|  | 168 | return propertySet, tag | 
|  | 169 | } | 
|  | 170 |  | 
| Paul Duffin | 180a006 | 2020-02-21 16:06:25 +0000 | [diff] [blame] | 171 | func (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 |  | 
| Paul Duffin | 047fdca | 2020-02-21 16:06:25 +0000 | [diff] [blame] | 178 | func TestTransformRemoveProperty(t *testing.T) { | 
| Paul Duffin | 047fdca | 2020-02-21 16:06:25 +0000 | [diff] [blame] | 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) | 
| Paul Duffin | 36474d3 | 2021-03-12 12:19:43 +0000 | [diff] [blame] | 187 | android.AssertTrimmedStringEquals(t, "removing property failed", "name: \"name\",\n", contents.content.String()) | 
| Paul Duffin | 047fdca | 2020-02-21 16:06:25 +0000 | [diff] [blame] | 188 | } | 
|  | 189 |  | 
|  | 190 | func TestTransformRemovePropertySet(t *testing.T) { | 
| Paul Duffin | 047fdca | 2020-02-21 16:06:25 +0000 | [diff] [blame] | 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) | 
| Paul Duffin | 36474d3 | 2021-03-12 12:19:43 +0000 | [diff] [blame] | 199 | android.AssertTrimmedStringEquals(t, "removing property set failed", "name: \"name\",\n", contents.content.String()) | 
| Paul Duffin | 047fdca | 2020-02-21 16:06:25 +0000 | [diff] [blame] | 200 | } |