blob: d3eaafebafa757309e326745b2d92523f13a705d [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) {
Colin Crossaba8cd92025-02-05 16:39:18 -080076 t.Parallel()
Martin Stjernholm191c25f2020-09-10 00:40:37 +010077 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)
Paul Duffin36474d32021-03-12 12:19:43 +000086 android.AssertDeepEquals(t, "wrong value", val, set.getValue(name))
Martin Stjernholm191c25f2020-09-10 00:40:37 +010087 }
Paul Duffin9f4b3bb2021-03-16 13:47:36 +000088 android.AssertPanicMessageContains(t, "adding x again should panic", `Property "x" already exists in property set`,
Martin Stjernholm191c25f2020-09-10 00:40:37 +010089 func() { set.AddProperty("x", "taxi") })
Paul Duffin9f4b3bb2021-03-16 13:47:36 +000090 android.AssertPanicMessageContains(t, "adding arr again should panic", `Property "arr" already exists in property set`,
Martin Stjernholm191c25f2020-09-10 00:40:37 +010091 func() { set.AddProperty("arr", []string{"d"}) })
92}
93
94func TestAddPropertySubset(t *testing.T) {
Colin Crossaba8cd92025-02-05 16:39:18 -080095 t.Parallel()
Martin Stjernholm191c25f2020-09-10 00:40:37 +010096 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) {
Colin Crossaba8cd92025-02-05 16:39:18 -0800102 t.Parallel()
Martin Stjernholm191c25f2020-09-10 00:40:37 +0100103 for name, getFixture := range getFixtureMap {
104 t.Run(name, func(t *testing.T) {
Colin Crossaba8cd92025-02-05 16:39:18 -0800105 t.Parallel()
Martin Stjernholm191c25f2020-09-10 00:40:37 +0100106 set := propertySetFixture().(*bpPropertySet)
107 set.AddProperty("new", getFixture())
Paul Duffin36474d32021-03-12 12:19:43 +0000108 checkPropertySetFixture(t, set, true)
109 checkPropertySetFixture(t, set.getValue("new"), name == "property set")
Martin Stjernholm191c25f2020-09-10 00:40:37 +0100110 })
111 }
112 })
113
114 t.Run("merge existing subset", func(t *testing.T) {
Colin Crossaba8cd92025-02-05 16:39:18 -0800115 t.Parallel()
Martin Stjernholm191c25f2020-09-10 00:40:37 +0100116 for name, getFixture := range getFixtureMap {
117 t.Run(name, func(t *testing.T) {
Colin Crossaba8cd92025-02-05 16:39:18 -0800118 t.Parallel()
Martin Stjernholm191c25f2020-09-10 00:40:37 +0100119 set := newPropertySet()
120 subset := set.AddPropertySet("sub")
121 subset.AddProperty("flag", false)
122 subset.AddPropertySet("sub")
123 set.AddProperty("sub", getFixture())
124 merged := set.getValue("sub").(*bpPropertySet)
Paul Duffin36474d32021-03-12 12:19:43 +0000125 android.AssertDeepEquals(t, "wrong flag value", false, merged.getValue("flag"))
126 checkPropertySetFixture(t, merged, name == "property set")
Martin Stjernholm191c25f2020-09-10 00:40:37 +0100127 })
128 }
129 })
130
131 t.Run("add conflicting subset", func(t *testing.T) {
Colin Crossaba8cd92025-02-05 16:39:18 -0800132 t.Parallel()
Martin Stjernholm191c25f2020-09-10 00:40:37 +0100133 set := propertySetFixture().(*bpPropertySet)
Paul Duffin9f4b3bb2021-03-16 13:47:36 +0000134 android.AssertPanicMessageContains(t, "adding x again should panic", `Property "x" already exists in property set`,
Martin Stjernholm191c25f2020-09-10 00:40:37 +0100135 func() { set.AddProperty("x", propertySetFixture()) })
136 })
137
138 t.Run("add non-pointer struct", func(t *testing.T) {
Colin Crossaba8cd92025-02-05 16:39:18 -0800139 t.Parallel()
Martin Stjernholm191c25f2020-09-10 00:40:37 +0100140 set := propertySetFixture().(*bpPropertySet)
141 str := propertyStructFixture().(*propertyStruct)
Paul Duffin9f4b3bb2021-03-16 13:47:36 +0000142 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 +0100143 func() { set.AddProperty("new", *str) })
144 })
145}
146
147func TestAddPropertySetNew(t *testing.T) {
Colin Crossaba8cd92025-02-05 16:39:18 -0800148 t.Parallel()
Martin Stjernholm191c25f2020-09-10 00:40:37 +0100149 set := newPropertySet()
150 subset := set.AddPropertySet("sub")
151 subset.AddProperty("new", "d^^b")
Paul Duffin36474d32021-03-12 12:19:43 +0000152 android.AssertDeepEquals(t, "wrong sub.new value", "d^^b", set.getValue("sub").(*bpPropertySet).getValue("new"))
Martin Stjernholm191c25f2020-09-10 00:40:37 +0100153}
154
155func TestAddPropertySetExisting(t *testing.T) {
Colin Crossaba8cd92025-02-05 16:39:18 -0800156 t.Parallel()
Martin Stjernholm191c25f2020-09-10 00:40:37 +0100157 set := propertySetFixture().(*bpPropertySet)
158 subset := set.AddPropertySet("sub")
159 subset.AddProperty("new", "d^^b")
Paul Duffin36474d32021-03-12 12:19:43 +0000160 android.AssertDeepEquals(t, "wrong sub.new value", "d^^b", set.getValue("sub").(*bpPropertySet).getValue("new"))
Martin Stjernholm191c25f2020-09-10 00:40:37 +0100161}
162
Paul Duffin047fdca2020-02-21 16:06:25 +0000163type removeFredTransformation struct {
164 identityTransformation
165}
166
167func (t removeFredTransformation) transformProperty(name string, value interface{}, tag android.BpPropertyTag) (interface{}, android.BpPropertyTag) {
168 if name == "fred" {
169 return nil, nil
170 }
171 return value, tag
172}
173
Paul Duffin180a0062020-02-21 16:06:25 +0000174func (t removeFredTransformation) transformPropertySetBeforeContents(name string, propertySet *bpPropertySet, tag android.BpPropertyTag) (*bpPropertySet, android.BpPropertyTag) {
Paul Duffin047fdca2020-02-21 16:06:25 +0000175 if name == "fred" {
176 return nil, nil
177 }
178 return propertySet, tag
179}
180
Paul Duffin180a0062020-02-21 16:06:25 +0000181func (t removeFredTransformation) transformPropertySetAfterContents(name string, propertySet *bpPropertySet, tag android.BpPropertyTag) (*bpPropertySet, android.BpPropertyTag) {
182 if len(propertySet.properties) == 0 {
183 return nil, nil
184 }
185 return propertySet, tag
186}
187
Paul Duffin047fdca2020-02-21 16:06:25 +0000188func TestTransformRemoveProperty(t *testing.T) {
Colin Crossaba8cd92025-02-05 16:39:18 -0800189 t.Parallel()
Paul Duffin047fdca2020-02-21 16:06:25 +0000190 set := newPropertySet()
191 set.AddProperty("name", "name")
192 set.AddProperty("fred", "12")
193
194 set.transformContents(removeFredTransformation{})
195
196 contents := &generatedContents{}
197 outputPropertySet(contents, set)
Paul Duffin36474d32021-03-12 12:19:43 +0000198 android.AssertTrimmedStringEquals(t, "removing property failed", "name: \"name\",\n", contents.content.String())
Paul Duffin047fdca2020-02-21 16:06:25 +0000199}
200
201func TestTransformRemovePropertySet(t *testing.T) {
Colin Crossaba8cd92025-02-05 16:39:18 -0800202 t.Parallel()
Paul Duffin047fdca2020-02-21 16:06:25 +0000203 set := newPropertySet()
204 set.AddProperty("name", "name")
205 set.AddPropertySet("fred")
206
207 set.transformContents(removeFredTransformation{})
208
209 contents := &generatedContents{}
210 outputPropertySet(contents, set)
Paul Duffin36474d32021-03-12 12:19:43 +0000211 android.AssertTrimmedStringEquals(t, "removing property set failed", "name: \"name\",\n", contents.content.String())
Paul Duffin047fdca2020-02-21 16:06:25 +0000212}