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 15package android 16 17import ( 18 "fmt" 19 "reflect" 20 "regexp" 21 "strings" 22 "testing" 23) 24 25// This file contains general purpose test assert functions. 26 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. 29func 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 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. 38func 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 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. 55func 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 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. 64func 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 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. 73func 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 80// AssertPathRelativeToTopEquals checks if the expected value is equal to the result of calling 81// PathRelativeToTop on the actual Path. 82func 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. 89func 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. 96func AssertStringPathRelativeToTopEquals(t *testing.T, message string, config Config, expected string, actual string) { 97 t.Helper() 98 AssertStringEquals(t, message, expected, StringPathRelativeToTop(config.soongOutDir, actual)) 99} 100 101// AssertStringPathsRelativeToTopEquals checks if the expected value is equal to the result of 102// calling StringPathsRelativeToTop on the actual string paths. 103func AssertStringPathsRelativeToTopEquals(t *testing.T, message string, config Config, expected []string, actual []string) { 104 t.Helper() 105 AssertDeepEquals(t, message, expected, StringPathsRelativeToTop(config.soongOutDir, actual)) 106} 107 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. 111func 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. 123func 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. 131func 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. 140func 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 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. 150func 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 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. 160func 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 { 168 t.Errorf("%s: %s does not match regular expression %s", message, s, expectedRex) 169 } 170} 171 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. 175func AssertStringListContains(t *testing.T, message string, list []string, s string) { 176 t.Helper() 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. 184func 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. 194func AssertStringListContainsEquals(t *testing.T, message string, list []string, s string, expected bool) { 195 t.Helper() 196 if expected { 197 AssertStringListContains(t, message, list, s) 198 } else { 199 AssertStringListDoesNotContain(t, message, list, s) 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. 205func 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 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. 223func 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 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. 239func 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 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. 248func AssertPanicMessageContains(t *testing.T, message, expectedMessageContents string, funcThatShouldPanic func()) { 249 t.Helper() 250 panicked := false 251 var recovered interface{} 252 func() { 253 defer func() { 254 if recovered = recover(); recovered != nil { 255 panicked = true 256 } 257 }() 258 funcThatShouldPanic() 259 }() 260 if !panicked { 261 t.Errorf("%s: did not panic", message) 262 } 263 264 panicMessage := fmt.Sprintf("%s", recovered) 265 AssertStringDoesContain(t, fmt.Sprintf("%s: panic message", message), panicMessage, expectedMessageContents) 266} 267