blob: e1edc513176020164d516a73b76f3abee0d4359a [file] [log] [blame]
Paul Duffin047fdca2020-02-21 16:06:25 +00001// 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"
Martin Stjernholm191c25f2020-09-10 00:40:37 +010021
22 "github.com/google/blueprint/proptools"
Paul Duffin047fdca2020-02-21 16:06:25 +000023)
24
Martin Stjernholm191c25f2020-09-10 00:40:37 +010025func 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(h *TestHelper, val interface{}, hasTags bool) {
58 set := val.(*bpPropertySet)
59 h.AssertDeepEquals("wrong x value", "taxi", set.getValue("x"))
60 h.AssertDeepEquals("wrong y value", 1729, set.getValue("y"))
61
62 subset := set.getValue("sub").(*bpPropertySet)
63 h.AssertDeepEquals("wrong sub.x value", "taxi", subset.getValue("x"))
64 h.AssertDeepEquals("wrong sub.y value", 1729, subset.getValue("y"))
65
66 if hasTags {
67 h.AssertDeepEquals("wrong y tag", "tag_y", set.getTag("y"))
68 h.AssertDeepEquals("wrong sub.x tag", "tag_x", subset.getTag("x"))
69 } else {
70 h.AssertDeepEquals("wrong y tag", nil, set.getTag("y"))
71 h.AssertDeepEquals("wrong sub.x tag", nil, subset.getTag("x"))
72 }
73}
74
75func TestAddPropertySimple(t *testing.T) {
76 h := &TestHelper{t}
77 set := newPropertySet()
78 for name, val := range map[string]interface{}{
79 "x": "taxi",
80 "y": 1729,
81 "t": true,
82 "f": false,
83 "arr": []string{"a", "b", "c"},
84 } {
85 set.AddProperty(name, val)
86 h.AssertDeepEquals("wrong value", val, set.getValue(name))
87 }
88 h.AssertPanic("adding x again should panic",
89 func() { set.AddProperty("x", "taxi") })
90 h.AssertPanic("adding arr again should panic",
91 func() { set.AddProperty("arr", []string{"d"}) })
92}
93
94func TestAddPropertySubset(t *testing.T) {
95 h := &TestHelper{t}
96 getFixtureMap := map[string]func() interface{}{
97 "property set": propertySetFixture,
98 "property struct": propertyStructFixture,
99 }
100
101 t.Run("add new subset", func(t *testing.T) {
102 for name, getFixture := range getFixtureMap {
103 t.Run(name, func(t *testing.T) {
104 set := propertySetFixture().(*bpPropertySet)
105 set.AddProperty("new", getFixture())
106 checkPropertySetFixture(h, set, true)
107 checkPropertySetFixture(h, set.getValue("new"), name == "property set")
108 })
109 }
110 })
111
112 t.Run("merge existing subset", func(t *testing.T) {
113 for name, getFixture := range getFixtureMap {
114 t.Run(name, func(t *testing.T) {
115 set := newPropertySet()
116 subset := set.AddPropertySet("sub")
117 subset.AddProperty("flag", false)
118 subset.AddPropertySet("sub")
119 set.AddProperty("sub", getFixture())
120 merged := set.getValue("sub").(*bpPropertySet)
121 h.AssertDeepEquals("wrong flag value", false, merged.getValue("flag"))
122 checkPropertySetFixture(h, merged, name == "property set")
123 })
124 }
125 })
126
127 t.Run("add conflicting subset", func(t *testing.T) {
128 set := propertySetFixture().(*bpPropertySet)
129 h.AssertPanic("adding x again should panic",
130 func() { set.AddProperty("x", propertySetFixture()) })
131 })
132
133 t.Run("add non-pointer struct", func(t *testing.T) {
134 set := propertySetFixture().(*bpPropertySet)
135 str := propertyStructFixture().(*propertyStruct)
136 h.AssertPanic("adding a non-pointer struct should panic",
137 func() { set.AddProperty("new", *str) })
138 })
139}
140
141func TestAddPropertySetNew(t *testing.T) {
142 h := &TestHelper{t}
143 set := newPropertySet()
144 subset := set.AddPropertySet("sub")
145 subset.AddProperty("new", "d^^b")
146 h.AssertDeepEquals("wrong sub.new value", "d^^b", set.getValue("sub").(*bpPropertySet).getValue("new"))
147}
148
149func TestAddPropertySetExisting(t *testing.T) {
150 h := &TestHelper{t}
151 set := propertySetFixture().(*bpPropertySet)
152 subset := set.AddPropertySet("sub")
153 subset.AddProperty("new", "d^^b")
154 h.AssertDeepEquals("wrong sub.new value", "d^^b", set.getValue("sub").(*bpPropertySet).getValue("new"))
155}
156
Paul Duffin047fdca2020-02-21 16:06:25 +0000157type removeFredTransformation struct {
158 identityTransformation
159}
160
161func (t removeFredTransformation) transformProperty(name string, value interface{}, tag android.BpPropertyTag) (interface{}, android.BpPropertyTag) {
162 if name == "fred" {
163 return nil, nil
164 }
165 return value, tag
166}
167
Paul Duffin180a0062020-02-21 16:06:25 +0000168func (t removeFredTransformation) transformPropertySetBeforeContents(name string, propertySet *bpPropertySet, tag android.BpPropertyTag) (*bpPropertySet, android.BpPropertyTag) {
Paul Duffin047fdca2020-02-21 16:06:25 +0000169 if name == "fred" {
170 return nil, nil
171 }
172 return propertySet, tag
173}
174
Paul Duffin180a0062020-02-21 16:06:25 +0000175func (t removeFredTransformation) transformPropertySetAfterContents(name string, propertySet *bpPropertySet, tag android.BpPropertyTag) (*bpPropertySet, android.BpPropertyTag) {
176 if len(propertySet.properties) == 0 {
177 return nil, nil
178 }
179 return propertySet, tag
180}
181
Paul Duffin047fdca2020-02-21 16:06:25 +0000182func TestTransformRemoveProperty(t *testing.T) {
183
184 helper := &TestHelper{t}
185
186 set := newPropertySet()
187 set.AddProperty("name", "name")
188 set.AddProperty("fred", "12")
189
190 set.transformContents(removeFredTransformation{})
191
192 contents := &generatedContents{}
193 outputPropertySet(contents, set)
Paul Duffin11108272020-05-11 22:59:25 +0100194 helper.AssertTrimmedStringEquals("removing property failed", "name: \"name\",\n", contents.content.String())
Paul Duffin047fdca2020-02-21 16:06:25 +0000195}
196
197func TestTransformRemovePropertySet(t *testing.T) {
198
199 helper := &TestHelper{t}
200
201 set := newPropertySet()
202 set.AddProperty("name", "name")
203 set.AddPropertySet("fred")
204
205 set.transformContents(removeFredTransformation{})
206
207 contents := &generatedContents{}
208 outputPropertySet(contents, set)
Paul Duffin11108272020-05-11 22:59:25 +0100209 helper.AssertTrimmedStringEquals("removing property set failed", "name: \"name\",\n", contents.content.String())
Paul Duffin047fdca2020-02-21 16:06:25 +0000210}