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