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