blob: c620ac2b80ebc536b887636edec2dd3bb13f6892 [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
Paul Duffin36474d32021-03-12 12:19:43 +000057func checkPropertySetFixture(t *testing.T, val interface{}, hasTags bool) {
Martin Stjernholm191c25f2020-09-10 00:40:37 +010058 set := val.(*bpPropertySet)
Paul Duffin36474d32021-03-12 12:19:43 +000059 android.AssertDeepEquals(t, "wrong x value", "taxi", set.getValue("x"))
60 android.AssertDeepEquals(t, "wrong y value", 1729, set.getValue("y"))
Martin Stjernholm191c25f2020-09-10 00:40:37 +010061
62 subset := set.getValue("sub").(*bpPropertySet)
Paul Duffin36474d32021-03-12 12:19:43 +000063 android.AssertDeepEquals(t, "wrong sub.x value", "taxi", subset.getValue("x"))
64 android.AssertDeepEquals(t, "wrong sub.y value", 1729, subset.getValue("y"))
Martin Stjernholm191c25f2020-09-10 00:40:37 +010065
66 if hasTags {
Paul Duffin36474d32021-03-12 12:19:43 +000067 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 Stjernholm191c25f2020-09-10 00:40:37 +010069 } else {
Paul Duffin36474d32021-03-12 12:19:43 +000070 android.AssertDeepEquals(t, "wrong y tag", nil, set.getTag("y"))
71 android.AssertDeepEquals(t, "wrong sub.x tag", nil, subset.getTag("x"))
Martin Stjernholm191c25f2020-09-10 00:40:37 +010072 }
73}
74
75func TestAddPropertySimple(t *testing.T) {
Martin Stjernholm191c25f2020-09-10 00:40:37 +010076 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 Duffin36474d32021-03-12 12:19:43 +000085 android.AssertDeepEquals(t, "wrong value", val, set.getValue(name))
Martin Stjernholm191c25f2020-09-10 00:40:37 +010086 }
Paul Duffin9f4b3bb2021-03-16 13:47:36 +000087 android.AssertPanicMessageContains(t, "adding x again should panic", `Property "x" already exists in property set`,
Martin Stjernholm191c25f2020-09-10 00:40:37 +010088 func() { set.AddProperty("x", "taxi") })
Paul Duffin9f4b3bb2021-03-16 13:47:36 +000089 android.AssertPanicMessageContains(t, "adding arr again should panic", `Property "arr" already exists in property set`,
Martin Stjernholm191c25f2020-09-10 00:40:37 +010090 func() { set.AddProperty("arr", []string{"d"}) })
91}
92
93func TestAddPropertySubset(t *testing.T) {
Martin Stjernholm191c25f2020-09-10 00:40:37 +010094 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 Duffin36474d32021-03-12 12:19:43 +0000104 checkPropertySetFixture(t, set, true)
105 checkPropertySetFixture(t, set.getValue("new"), name == "property set")
Martin Stjernholm191c25f2020-09-10 00:40:37 +0100106 })
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 Duffin36474d32021-03-12 12:19:43 +0000119 android.AssertDeepEquals(t, "wrong flag value", false, merged.getValue("flag"))
120 checkPropertySetFixture(t, merged, name == "property set")
Martin Stjernholm191c25f2020-09-10 00:40:37 +0100121 })
122 }
123 })
124
125 t.Run("add conflicting subset", func(t *testing.T) {
126 set := propertySetFixture().(*bpPropertySet)
Paul Duffin9f4b3bb2021-03-16 13:47:36 +0000127 android.AssertPanicMessageContains(t, "adding x again should panic", `Property "x" already exists in property set`,
Martin Stjernholm191c25f2020-09-10 00:40:37 +0100128 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 Duffin9f4b3bb2021-03-16 13:47:36 +0000134 android.AssertPanicMessageContains(t, "adding a non-pointer struct should panic", "Value is a struct, not a pointer to one:",
Martin Stjernholm191c25f2020-09-10 00:40:37 +0100135 func() { set.AddProperty("new", *str) })
136 })
137}
138
139func TestAddPropertySetNew(t *testing.T) {
Martin Stjernholm191c25f2020-09-10 00:40:37 +0100140 set := newPropertySet()
141 subset := set.AddPropertySet("sub")
142 subset.AddProperty("new", "d^^b")
Paul Duffin36474d32021-03-12 12:19:43 +0000143 android.AssertDeepEquals(t, "wrong sub.new value", "d^^b", set.getValue("sub").(*bpPropertySet).getValue("new"))
Martin Stjernholm191c25f2020-09-10 00:40:37 +0100144}
145
146func TestAddPropertySetExisting(t *testing.T) {
Martin Stjernholm191c25f2020-09-10 00:40:37 +0100147 set := propertySetFixture().(*bpPropertySet)
148 subset := set.AddPropertySet("sub")
149 subset.AddProperty("new", "d^^b")
Paul Duffin36474d32021-03-12 12:19:43 +0000150 android.AssertDeepEquals(t, "wrong sub.new value", "d^^b", set.getValue("sub").(*bpPropertySet).getValue("new"))
Martin Stjernholm191c25f2020-09-10 00:40:37 +0100151}
152
Paul Duffin047fdca2020-02-21 16:06:25 +0000153type 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
Paul Duffin180a0062020-02-21 16:06:25 +0000164func (t removeFredTransformation) transformPropertySetBeforeContents(name string, propertySet *bpPropertySet, tag android.BpPropertyTag) (*bpPropertySet, android.BpPropertyTag) {
Paul Duffin047fdca2020-02-21 16:06:25 +0000165 if name == "fred" {
166 return nil, nil
167 }
168 return propertySet, tag
169}
170
Paul Duffin180a0062020-02-21 16:06:25 +0000171func (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 Duffin047fdca2020-02-21 16:06:25 +0000178func TestTransformRemoveProperty(t *testing.T) {
Paul Duffin047fdca2020-02-21 16:06:25 +0000179 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 Duffin36474d32021-03-12 12:19:43 +0000187 android.AssertTrimmedStringEquals(t, "removing property failed", "name: \"name\",\n", contents.content.String())
Paul Duffin047fdca2020-02-21 16:06:25 +0000188}
189
190func TestTransformRemovePropertySet(t *testing.T) {
Paul Duffin047fdca2020-02-21 16:06:25 +0000191 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 Duffin36474d32021-03-12 12:19:43 +0000199 android.AssertTrimmedStringEquals(t, "removing property set failed", "name: \"name\",\n", contents.content.String())
Paul Duffin047fdca2020-02-21 16:06:25 +0000200}