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// 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. 38func 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 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. 47func 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 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. 56func 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 63// AssertPathRelativeToTopEquals checks if the expected value is equal to the result of calling 64// PathRelativeToTop on the actual Path. 65func 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. 72func 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. 79func AssertStringPathRelativeToTopEquals(t *testing.T, message string, config Config, expected string, actual string) { 80 t.Helper() 81 AssertStringEquals(t, message, expected, StringPathRelativeToTop(config.soongOutDir, actual)) 82} 83 84// AssertStringPathsRelativeToTopEquals checks if the expected value is equal to the result of 85// calling StringPathsRelativeToTop on the actual string paths. 86func AssertStringPathsRelativeToTopEquals(t *testing.T, message string, config Config, expected []string, actual []string) { 87 t.Helper() 88 AssertDeepEquals(t, message, expected, StringPathsRelativeToTop(config.soongOutDir, actual)) 89} 90 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. 94func 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. 106func 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. 114func 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. 123func 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 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. 133func 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 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. 143func 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: %s does not match regular expression %s", message, s, expectedRex) 152 } 153} 154 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. 158func AssertStringListContains(t *testing.T, message string, list []string, s string) { 159 t.Helper() 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. 167func 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. 177func AssertStringListContainsEquals(t *testing.T, message string, list []string, s string, expected bool) { 178 t.Helper() 179 if expected { 180 AssertStringListContains(t, message, list, s) 181 } else { 182 AssertStringListDoesNotContain(t, message, list, s) 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. 188func 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 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. 206func 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 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. 222func 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 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. 231func AssertPanicMessageContains(t *testing.T, message, expectedMessageContents string, funcThatShouldPanic func()) { 232 t.Helper() 233 panicked := false 234 var recovered interface{} 235 func() { 236 defer func() { 237 if recovered = recover(); recovered != nil { 238 panicked = true 239 } 240 }() 241 funcThatShouldPanic() 242 }() 243 if !panicked { 244 t.Errorf("%s: did not panic", message) 245 } 246 247 panicMessage := fmt.Sprintf("%s", recovered) 248 AssertStringDoesContain(t, fmt.Sprintf("%s: panic message", message), panicMessage, expectedMessageContents) 249} 250