bp_test.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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(t *testing.T, val interface{}, hasTags bool) {
  49. set := val.(*bpPropertySet)
  50. android.AssertDeepEquals(t, "wrong x value", "taxi", set.getValue("x"))
  51. android.AssertDeepEquals(t, "wrong y value", 1729, set.getValue("y"))
  52. subset := set.getValue("sub").(*bpPropertySet)
  53. android.AssertDeepEquals(t, "wrong sub.x value", "taxi", subset.getValue("x"))
  54. android.AssertDeepEquals(t, "wrong sub.y value", 1729, subset.getValue("y"))
  55. if hasTags {
  56. android.AssertDeepEquals(t, "wrong y tag", "tag_y", set.getTag("y"))
  57. android.AssertDeepEquals(t, "wrong sub.x tag", "tag_x", subset.getTag("x"))
  58. } else {
  59. android.AssertDeepEquals(t, "wrong y tag", nil, set.getTag("y"))
  60. android.AssertDeepEquals(t, "wrong sub.x tag", nil, subset.getTag("x"))
  61. }
  62. }
  63. func TestAddPropertySimple(t *testing.T) {
  64. set := newPropertySet()
  65. for name, val := range map[string]interface{}{
  66. "x": "taxi",
  67. "y": 1729,
  68. "t": true,
  69. "f": false,
  70. "arr": []string{"a", "b", "c"},
  71. } {
  72. set.AddProperty(name, val)
  73. android.AssertDeepEquals(t, "wrong value", val, set.getValue(name))
  74. }
  75. android.AssertPanicMessageContains(t, "adding x again should panic", `Property "x" already exists in property set`,
  76. func() { set.AddProperty("x", "taxi") })
  77. android.AssertPanicMessageContains(t, "adding arr again should panic", `Property "arr" already exists in property set`,
  78. func() { set.AddProperty("arr", []string{"d"}) })
  79. }
  80. func TestAddPropertySubset(t *testing.T) {
  81. getFixtureMap := map[string]func() interface{}{
  82. "property set": propertySetFixture,
  83. "property struct": propertyStructFixture,
  84. }
  85. t.Run("add new subset", func(t *testing.T) {
  86. for name, getFixture := range getFixtureMap {
  87. t.Run(name, func(t *testing.T) {
  88. set := propertySetFixture().(*bpPropertySet)
  89. set.AddProperty("new", getFixture())
  90. checkPropertySetFixture(t, set, true)
  91. checkPropertySetFixture(t, set.getValue("new"), name == "property set")
  92. })
  93. }
  94. })
  95. t.Run("merge existing subset", func(t *testing.T) {
  96. for name, getFixture := range getFixtureMap {
  97. t.Run(name, func(t *testing.T) {
  98. set := newPropertySet()
  99. subset := set.AddPropertySet("sub")
  100. subset.AddProperty("flag", false)
  101. subset.AddPropertySet("sub")
  102. set.AddProperty("sub", getFixture())
  103. merged := set.getValue("sub").(*bpPropertySet)
  104. android.AssertDeepEquals(t, "wrong flag value", false, merged.getValue("flag"))
  105. checkPropertySetFixture(t, merged, name == "property set")
  106. })
  107. }
  108. })
  109. t.Run("add conflicting subset", func(t *testing.T) {
  110. set := propertySetFixture().(*bpPropertySet)
  111. android.AssertPanicMessageContains(t, "adding x again should panic", `Property "x" already exists in property set`,
  112. func() { set.AddProperty("x", propertySetFixture()) })
  113. })
  114. t.Run("add non-pointer struct", func(t *testing.T) {
  115. set := propertySetFixture().(*bpPropertySet)
  116. str := propertyStructFixture().(*propertyStruct)
  117. android.AssertPanicMessageContains(t, "adding a non-pointer struct should panic", "Value is a struct, not a pointer to one:",
  118. func() { set.AddProperty("new", *str) })
  119. })
  120. }
  121. func TestAddPropertySetNew(t *testing.T) {
  122. set := newPropertySet()
  123. subset := set.AddPropertySet("sub")
  124. subset.AddProperty("new", "d^^b")
  125. android.AssertDeepEquals(t, "wrong sub.new value", "d^^b", set.getValue("sub").(*bpPropertySet).getValue("new"))
  126. }
  127. func TestAddPropertySetExisting(t *testing.T) {
  128. set := propertySetFixture().(*bpPropertySet)
  129. subset := set.AddPropertySet("sub")
  130. subset.AddProperty("new", "d^^b")
  131. android.AssertDeepEquals(t, "wrong sub.new value", "d^^b", set.getValue("sub").(*bpPropertySet).getValue("new"))
  132. }
  133. type removeFredTransformation struct {
  134. identityTransformation
  135. }
  136. func (t removeFredTransformation) transformProperty(name string, value interface{}, tag android.BpPropertyTag) (interface{}, android.BpPropertyTag) {
  137. if name == "fred" {
  138. return nil, nil
  139. }
  140. return value, tag
  141. }
  142. func (t removeFredTransformation) transformPropertySetBeforeContents(name string, propertySet *bpPropertySet, tag android.BpPropertyTag) (*bpPropertySet, android.BpPropertyTag) {
  143. if name == "fred" {
  144. return nil, nil
  145. }
  146. return propertySet, tag
  147. }
  148. func (t removeFredTransformation) transformPropertySetAfterContents(name string, propertySet *bpPropertySet, tag android.BpPropertyTag) (*bpPropertySet, android.BpPropertyTag) {
  149. if len(propertySet.properties) == 0 {
  150. return nil, nil
  151. }
  152. return propertySet, tag
  153. }
  154. func TestTransformRemoveProperty(t *testing.T) {
  155. set := newPropertySet()
  156. set.AddProperty("name", "name")
  157. set.AddProperty("fred", "12")
  158. set.transformContents(removeFredTransformation{})
  159. contents := &generatedContents{}
  160. outputPropertySet(contents, set)
  161. android.AssertTrimmedStringEquals(t, "removing property failed", "name: \"name\",\n", contents.content.String())
  162. }
  163. func TestTransformRemovePropertySet(t *testing.T) {
  164. set := newPropertySet()
  165. set.AddProperty("name", "name")
  166. set.AddPropertySet("fred")
  167. set.transformContents(removeFredTransformation{})
  168. contents := &generatedContents{}
  169. outputPropertySet(contents, set)
  170. android.AssertTrimmedStringEquals(t, "removing property set failed", "name: \"name\",\n", contents.content.String())
  171. }