blob: 22472c5c2cf9d2ed39f26290771498d9a1ff2997 [file] [log] [blame]
Paul Duffin9dcf2532021-03-12 11:50:43 +00001// 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 (
Paul Duffin9f4b3bb2021-03-16 13:47:36 +000018 "fmt"
Paul Duffin9dcf2532021-03-12 11:50:43 +000019 "reflect"
Sam Delmericob1daccd2023-05-25 14:45:30 -040020 "regexp"
Paul Duffin9dcf2532021-03-12 11:50:43 +000021 "strings"
22 "testing"
23)
24
25// This file contains general purpose test assert functions.
26
Paul Duffin3d119612021-03-16 19:30:32 +000027// 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
Jiyong Park76834912024-12-30 16:00:22 +090036// 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
Paul Duffin9dcf2532021-03-12 11:50:43 +000053// 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
Paul Duffin64d37182021-03-15 23:57:11 +000062// 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
Paul Duffin9dcf2532021-03-12 11:50:43 +000071// 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
Paul Duffin567465d2021-03-16 01:21:34 +000080// 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()
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +020098 AssertStringEquals(t, message, expected, StringPathRelativeToTop(config.soongOutDir, actual))
Paul Duffin567465d2021-03-16 01:21:34 +000099}
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()
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +0200105 AssertDeepEquals(t, message, expected, StringPathsRelativeToTop(config.soongOutDir, actual))
Paul Duffin567465d2021-03-16 01:21:34 +0000106}
107
Paul Duffin9dcf2532021-03-12 11:50:43 +0000108// 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
Anton Hanssondae54cd2021-04-21 16:30:10 +0100147// 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
Sam Delmericob1daccd2023-05-25 14:45:30 -0400158// 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 {
Alix141ab6a2023-10-04 20:19:54 +0000168 t.Errorf("%s: %s does not match regular expression %s", message, s, expectedRex)
Sam Delmericob1daccd2023-05-25 14:45:30 -0400169 }
170}
171
Paul Duffin9dcf2532021-03-12 11:50:43 +0000172// 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 Hanssondae54cd2021-04-21 16:30:10 +0100175func AssertStringListContains(t *testing.T, message string, list []string, s string) {
Paul Duffin9dcf2532021-03-12 11:50:43 +0000176 t.Helper()
Anton Hanssondae54cd2021-04-21 16:30:10 +0100177 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) {
Jooyung Han4973d452023-03-23 14:22:46 +0900195 t.Helper()
Anton Hanssondae54cd2021-04-21 16:30:10 +0100196 if expected {
197 AssertStringListContains(t, message, list, s)
198 } else {
199 AssertStringListDoesNotContain(t, message, list, s)
Paul Duffin9dcf2532021-03-12 11:50:43 +0000200 }
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
Joe Onorato175073c2023-06-01 14:42:59 -0700220// 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
Paul Duffin9dcf2532021-03-12 11:50:43 +0000236// 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
Paul Duffin9f4b3bb2021-03-16 13:47:36 +0000246// 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()) {
Paul Duffin9dcf2532021-03-12 11:50:43 +0000249 t.Helper()
250 panicked := false
Paul Duffin9f4b3bb2021-03-16 13:47:36 +0000251 var recovered interface{}
Paul Duffin9dcf2532021-03-12 11:50:43 +0000252 func() {
253 defer func() {
Paul Duffin9f4b3bb2021-03-16 13:47:36 +0000254 if recovered = recover(); recovered != nil {
Paul Duffin9dcf2532021-03-12 11:50:43 +0000255 panicked = true
256 }
257 }()
258 funcThatShouldPanic()
259 }()
260 if !panicked {
Paul Duffin9f4b3bb2021-03-16 13:47:36 +0000261 t.Errorf("%s: did not panic", message)
Paul Duffin9dcf2532021-03-12 11:50:43 +0000262 }
Paul Duffin9f4b3bb2021-03-16 13:47:36 +0000263
264 panicMessage := fmt.Sprintf("%s", recovered)
265 AssertStringDoesContain(t, fmt.Sprintf("%s: panic message", message), panicMessage, expectedMessageContents)
Paul Duffin9dcf2532021-03-12 11:50:43 +0000266}