bp_test.go 6.2 KB

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