modules_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. // Copyright 2020 Google Inc. All rights reserved.
  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 soongconfig
  15. import (
  16. "reflect"
  17. "testing"
  18. )
  19. func Test_CanonicalizeToProperty(t *testing.T) {
  20. tests := []struct {
  21. name string
  22. arg string
  23. want string
  24. }{
  25. {
  26. name: "lowercase",
  27. arg: "board",
  28. want: "board",
  29. },
  30. {
  31. name: "uppercase",
  32. arg: "BOARD",
  33. want: "BOARD",
  34. },
  35. {
  36. name: "numbers",
  37. arg: "BOARD123",
  38. want: "BOARD123",
  39. },
  40. {
  41. name: "underscore",
  42. arg: "TARGET_BOARD",
  43. want: "TARGET_BOARD",
  44. },
  45. {
  46. name: "dash",
  47. arg: "TARGET-BOARD",
  48. want: "TARGET_BOARD",
  49. },
  50. {
  51. name: "unicode",
  52. arg: "boardλ",
  53. want: "board_",
  54. },
  55. }
  56. for _, tt := range tests {
  57. t.Run(tt.name, func(t *testing.T) {
  58. if got := CanonicalizeToProperty(tt.arg); got != tt.want {
  59. t.Errorf("canonicalizeToProperty() = %v, want %v", got, tt.want)
  60. }
  61. })
  62. }
  63. }
  64. func Test_typeForPropertyFromPropertyStruct(t *testing.T) {
  65. tests := []struct {
  66. name string
  67. ps interface{}
  68. property string
  69. want string
  70. }{
  71. {
  72. name: "string",
  73. ps: struct {
  74. A string
  75. }{},
  76. property: "a",
  77. want: "string",
  78. },
  79. {
  80. name: "list",
  81. ps: struct {
  82. A []string
  83. }{},
  84. property: "a",
  85. want: "[]string",
  86. },
  87. {
  88. name: "missing",
  89. ps: struct {
  90. A []string
  91. }{},
  92. property: "b",
  93. want: "",
  94. },
  95. {
  96. name: "nested",
  97. ps: struct {
  98. A struct {
  99. B string
  100. }
  101. }{},
  102. property: "a.b",
  103. want: "string",
  104. },
  105. {
  106. name: "missing nested",
  107. ps: struct {
  108. A struct {
  109. B string
  110. }
  111. }{},
  112. property: "a.c",
  113. want: "",
  114. },
  115. {
  116. name: "not a struct",
  117. ps: struct {
  118. A string
  119. }{},
  120. property: "a.b",
  121. want: "",
  122. },
  123. {
  124. name: "nested pointer",
  125. ps: struct {
  126. A *struct {
  127. B string
  128. }
  129. }{},
  130. property: "a.b",
  131. want: "string",
  132. },
  133. {
  134. name: "nested interface",
  135. ps: struct {
  136. A interface{}
  137. }{
  138. A: struct {
  139. B string
  140. }{},
  141. },
  142. property: "a.b",
  143. want: "string",
  144. },
  145. {
  146. name: "nested interface pointer",
  147. ps: struct {
  148. A interface{}
  149. }{
  150. A: &struct {
  151. B string
  152. }{},
  153. },
  154. property: "a.b",
  155. want: "string",
  156. },
  157. {
  158. name: "nested interface nil pointer",
  159. ps: struct {
  160. A interface{}
  161. }{
  162. A: (*struct {
  163. B string
  164. })(nil),
  165. },
  166. property: "a.b",
  167. want: "string",
  168. },
  169. }
  170. for _, tt := range tests {
  171. t.Run(tt.name, func(t *testing.T) {
  172. typ := typeForPropertyFromPropertyStruct(tt.ps, tt.property)
  173. got := ""
  174. if typ != nil {
  175. got = typ.String()
  176. }
  177. if got != tt.want {
  178. t.Errorf("typeForPropertyFromPropertyStruct() = %v, want %v", got, tt.want)
  179. }
  180. })
  181. }
  182. }
  183. func Test_createAffectablePropertiesType(t *testing.T) {
  184. tests := []struct {
  185. name string
  186. affectableProperties []string
  187. factoryProps interface{}
  188. want string
  189. }{
  190. {
  191. name: "string",
  192. affectableProperties: []string{"cflags"},
  193. factoryProps: struct {
  194. Cflags string
  195. }{},
  196. want: "*struct { Cflags string }",
  197. },
  198. {
  199. name: "list",
  200. affectableProperties: []string{"cflags"},
  201. factoryProps: struct {
  202. Cflags []string
  203. }{},
  204. want: "*struct { Cflags []string }",
  205. },
  206. {
  207. name: "string pointer",
  208. affectableProperties: []string{"cflags"},
  209. factoryProps: struct {
  210. Cflags *string
  211. }{},
  212. want: "*struct { Cflags *string }",
  213. },
  214. {
  215. name: "subset",
  216. affectableProperties: []string{"cflags"},
  217. factoryProps: struct {
  218. Cflags string
  219. Ldflags string
  220. }{},
  221. want: "*struct { Cflags string }",
  222. },
  223. {
  224. name: "none",
  225. affectableProperties: []string{"cflags"},
  226. factoryProps: struct {
  227. Ldflags string
  228. }{},
  229. want: "",
  230. },
  231. }
  232. for _, tt := range tests {
  233. t.Run(tt.name, func(t *testing.T) {
  234. typ := createAffectablePropertiesType(tt.affectableProperties, []interface{}{tt.factoryProps})
  235. got := ""
  236. if typ != nil {
  237. got = typ.String()
  238. }
  239. if !reflect.DeepEqual(got, tt.want) {
  240. t.Errorf("createAffectablePropertiesType() = %v, want %v", got, tt.want)
  241. }
  242. })
  243. }
  244. }