test_asserts.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. // Copyright 2021 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 android
  15. import (
  16. "fmt"
  17. "reflect"
  18. "regexp"
  19. "strings"
  20. "testing"
  21. )
  22. // This file contains general purpose test assert functions.
  23. // AssertSame checks if the expected and actual values are equal and if they are not then
  24. // it reports an error prefixed with the supplied message and including a reason for why it failed.
  25. func AssertSame(t *testing.T, message string, expected interface{}, actual interface{}) {
  26. t.Helper()
  27. if actual != expected {
  28. t.Errorf("%s: expected:\n%#v\nactual:\n%#v", message, expected, actual)
  29. }
  30. }
  31. // AssertBoolEquals checks if the expected and actual values are equal and if they are not then it
  32. // reports an error prefixed with the supplied message and including a reason for why it failed.
  33. func AssertBoolEquals(t *testing.T, message string, expected bool, actual bool) {
  34. t.Helper()
  35. if actual != expected {
  36. t.Errorf("%s: expected %t, actual %t", message, expected, actual)
  37. }
  38. }
  39. // AssertIntEquals checks if the expected and actual values are equal and if they are not then it
  40. // reports an error prefixed with the supplied message and including a reason for why it failed.
  41. func AssertIntEquals(t *testing.T, message string, expected int, actual int) {
  42. t.Helper()
  43. if actual != expected {
  44. t.Errorf("%s: expected %d, actual %d", message, expected, actual)
  45. }
  46. }
  47. // AssertStringEquals checks if the expected and actual values are equal and if they are not then
  48. // it reports an error prefixed with the supplied message and including a reason for why it failed.
  49. func AssertStringEquals(t *testing.T, message string, expected string, actual string) {
  50. t.Helper()
  51. if actual != expected {
  52. t.Errorf("%s: expected %s, actual %s", message, expected, actual)
  53. }
  54. }
  55. // AssertPathRelativeToTopEquals checks if the expected value is equal to the result of calling
  56. // PathRelativeToTop on the actual Path.
  57. func AssertPathRelativeToTopEquals(t *testing.T, message string, expected string, actual Path) {
  58. t.Helper()
  59. AssertStringEquals(t, message, expected, PathRelativeToTop(actual))
  60. }
  61. // AssertPathsRelativeToTopEquals checks if the expected value is equal to the result of calling
  62. // PathsRelativeToTop on the actual Paths.
  63. func AssertPathsRelativeToTopEquals(t *testing.T, message string, expected []string, actual Paths) {
  64. t.Helper()
  65. AssertDeepEquals(t, message, expected, PathsRelativeToTop(actual))
  66. }
  67. // AssertStringPathRelativeToTopEquals checks if the expected value is equal to the result of calling
  68. // StringPathRelativeToTop on the actual string path.
  69. func AssertStringPathRelativeToTopEquals(t *testing.T, message string, config Config, expected string, actual string) {
  70. t.Helper()
  71. AssertStringEquals(t, message, expected, StringPathRelativeToTop(config.soongOutDir, actual))
  72. }
  73. // AssertStringPathsRelativeToTopEquals checks if the expected value is equal to the result of
  74. // calling StringPathsRelativeToTop on the actual string paths.
  75. func AssertStringPathsRelativeToTopEquals(t *testing.T, message string, config Config, expected []string, actual []string) {
  76. t.Helper()
  77. AssertDeepEquals(t, message, expected, StringPathsRelativeToTop(config.soongOutDir, actual))
  78. }
  79. // AssertErrorMessageEquals checks if the error is not nil and has the expected message. If it does
  80. // not then this reports an error prefixed with the supplied message and including a reason for why
  81. // it failed.
  82. func AssertErrorMessageEquals(t *testing.T, message string, expected string, actual error) {
  83. t.Helper()
  84. if actual == nil {
  85. t.Errorf("Expected error but was nil")
  86. } else if actual.Error() != expected {
  87. t.Errorf("%s: expected %s, actual %s", message, expected, actual.Error())
  88. }
  89. }
  90. // AssertTrimmedStringEquals checks if the expected and actual values are the same after trimming
  91. // leading and trailing spaces from them both. If they are not then it reports an error prefixed
  92. // with the supplied message and including a reason for why it failed.
  93. func AssertTrimmedStringEquals(t *testing.T, message string, expected string, actual string) {
  94. t.Helper()
  95. AssertStringEquals(t, message, strings.TrimSpace(expected), strings.TrimSpace(actual))
  96. }
  97. // AssertStringDoesContain checks if the string contains the expected substring. If it does not
  98. // then it reports an error prefixed with the supplied message and including a reason for why it
  99. // failed.
  100. func AssertStringDoesContain(t *testing.T, message string, s string, expectedSubstring string) {
  101. t.Helper()
  102. if !strings.Contains(s, expectedSubstring) {
  103. t.Errorf("%s: could not find %q within %q", message, expectedSubstring, s)
  104. }
  105. }
  106. // AssertStringDoesNotContain checks if the string contains the expected substring. If it does then
  107. // it reports an error prefixed with the supplied message and including a reason for why it failed.
  108. func AssertStringDoesNotContain(t *testing.T, message string, s string, unexpectedSubstring string) {
  109. t.Helper()
  110. if strings.Contains(s, unexpectedSubstring) {
  111. t.Errorf("%s: unexpectedly found %q within %q", message, unexpectedSubstring, s)
  112. }
  113. }
  114. // AssertStringContainsEquals checks if the string contains or does not contain the substring, given
  115. // the value of the expected bool. If the expectation does not hold it reports an error prefixed with
  116. // the supplied message and including a reason for why it failed.
  117. func AssertStringContainsEquals(t *testing.T, message string, s string, substring string, expected bool) {
  118. if expected {
  119. AssertStringDoesContain(t, message, s, substring)
  120. } else {
  121. AssertStringDoesNotContain(t, message, s, substring)
  122. }
  123. }
  124. // AssertStringMatches checks if the string matches the given regular expression. If it does not match,
  125. // then an error is reported with the supplied message including a reason for why it failed.
  126. func AssertStringMatches(t *testing.T, message, s, expectedRex string) {
  127. t.Helper()
  128. ok, err := regexp.MatchString(expectedRex, s)
  129. if err != nil {
  130. t.Fatalf("regexp failure trying to match %s against `%s` expression: %s", s, expectedRex, err)
  131. return
  132. }
  133. if !ok {
  134. t.Errorf("%s does not match regular expression %s", s, expectedRex)
  135. }
  136. }
  137. // AssertStringListContains checks if the list of strings contains the expected string. If it does
  138. // not then it reports an error prefixed with the supplied message and including a reason for why it
  139. // failed.
  140. func AssertStringListContains(t *testing.T, message string, list []string, s string) {
  141. t.Helper()
  142. if !InList(s, list) {
  143. t.Errorf("%s: could not find %q within %q", message, s, list)
  144. }
  145. }
  146. // AssertStringListDoesNotContain checks if the list of strings contains the expected string. If it does
  147. // then it reports an error prefixed with the supplied message and including a reason for why it failed.
  148. func AssertStringListDoesNotContain(t *testing.T, message string, list []string, s string) {
  149. t.Helper()
  150. if InList(s, list) {
  151. t.Errorf("%s: unexpectedly found %q within %q", message, s, list)
  152. }
  153. }
  154. // AssertStringContainsEquals checks if the string contains or does not contain the substring, given
  155. // the value of the expected bool. If the expectation does not hold it reports an error prefixed with
  156. // the supplied message and including a reason for why it failed.
  157. func AssertStringListContainsEquals(t *testing.T, message string, list []string, s string, expected bool) {
  158. t.Helper()
  159. if expected {
  160. AssertStringListContains(t, message, list, s)
  161. } else {
  162. AssertStringListDoesNotContain(t, message, list, s)
  163. }
  164. }
  165. // AssertArrayString checks if the expected and actual values are equal and if they are not then it
  166. // reports an error prefixed with the supplied message and including a reason for why it failed.
  167. func AssertArrayString(t *testing.T, message string, expected, actual []string) {
  168. t.Helper()
  169. if len(actual) != len(expected) {
  170. t.Errorf("%s: expected %d (%q), actual (%d) %q", message, len(expected), expected, len(actual), actual)
  171. return
  172. }
  173. for i := range actual {
  174. if actual[i] != expected[i] {
  175. t.Errorf("%s: expected %d-th, %q (%q), actual %q (%q)",
  176. message, i, expected[i], expected, actual[i], actual)
  177. return
  178. }
  179. }
  180. }
  181. // Asserts that each of the Paths in actual end with the corresponding string
  182. // from expected. Useful to test that output paths contain expected items without
  183. // hard-coding where intermediate files might be located.
  184. func AssertPathsEndWith(t *testing.T, message string, expected []string, actual []Path) {
  185. t.Helper()
  186. if len(expected) != len(actual) {
  187. t.Errorf("%s (length): expected %d, actual %d", message, len(expected), len(actual))
  188. return
  189. }
  190. for i := range expected {
  191. if !strings.HasSuffix(actual[i].String(), expected[i]) {
  192. t.Errorf("%s (item %d): expected '%s', actual '%s'", message, i, expected[i], actual[i].String())
  193. }
  194. }
  195. }
  196. // AssertDeepEquals checks if the expected and actual values are equal using reflect.DeepEqual and
  197. // if they are not then it reports an error prefixed with the supplied message and including a
  198. // reason for why it failed.
  199. func AssertDeepEquals(t *testing.T, message string, expected interface{}, actual interface{}) {
  200. t.Helper()
  201. if !reflect.DeepEqual(actual, expected) {
  202. t.Errorf("%s: expected:\n %#v\n got:\n %#v", message, expected, actual)
  203. }
  204. }
  205. // AssertPanicMessageContains checks that the supplied function panics as expected and the message
  206. // obtained by formatting the recovered value as a string contains the expected contents.
  207. func AssertPanicMessageContains(t *testing.T, message, expectedMessageContents string, funcThatShouldPanic func()) {
  208. t.Helper()
  209. panicked := false
  210. var recovered interface{}
  211. func() {
  212. defer func() {
  213. if recovered = recover(); recovered != nil {
  214. panicked = true
  215. }
  216. }()
  217. funcThatShouldPanic()
  218. }()
  219. if !panicked {
  220. t.Errorf("%s: did not panic", message)
  221. }
  222. panicMessage := fmt.Sprintf("%s", recovered)
  223. AssertStringDoesContain(t, fmt.Sprintf("%s: panic message", message), panicMessage, expectedMessageContents)
  224. }