blob: 4143f150ddabb84356a2d7459221d356505bc033 [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"
20 "strings"
21 "testing"
22)
23
24// This file contains general purpose test assert functions.
25
Paul Duffin3d119612021-03-16 19:30:32 +000026// AssertSame checks if the expected and actual values are equal and if they are not then
27// it reports an error prefixed with the supplied message and including a reason for why it failed.
28func AssertSame(t *testing.T, message string, expected interface{}, actual interface{}) {
29 t.Helper()
30 if actual != expected {
31 t.Errorf("%s: expected:\n%#v\nactual:\n%#v", message, expected, actual)
32 }
33}
34
Paul Duffin9dcf2532021-03-12 11:50:43 +000035// AssertBoolEquals checks if the expected and actual values are equal and if they are not then it
36// reports an error prefixed with the supplied message and including a reason for why it failed.
37func AssertBoolEquals(t *testing.T, message string, expected bool, actual bool) {
38 t.Helper()
39 if actual != expected {
40 t.Errorf("%s: expected %t, actual %t", message, expected, actual)
41 }
42}
43
Paul Duffin64d37182021-03-15 23:57:11 +000044// AssertIntEquals checks if the expected and actual values are equal and if they are not then it
45// reports an error prefixed with the supplied message and including a reason for why it failed.
46func AssertIntEquals(t *testing.T, message string, expected int, actual int) {
47 t.Helper()
48 if actual != expected {
49 t.Errorf("%s: expected %d, actual %d", message, expected, actual)
50 }
51}
52
Paul Duffin9dcf2532021-03-12 11:50:43 +000053// AssertStringEquals checks if the expected and actual values are equal and if they are not then
54// it reports an error prefixed with the supplied message and including a reason for why it failed.
55func AssertStringEquals(t *testing.T, message string, expected string, actual string) {
56 t.Helper()
57 if actual != expected {
58 t.Errorf("%s: expected %s, actual %s", message, expected, actual)
59 }
60}
61
Paul Duffin567465d2021-03-16 01:21:34 +000062// AssertPathRelativeToTopEquals checks if the expected value is equal to the result of calling
63// PathRelativeToTop on the actual Path.
64func AssertPathRelativeToTopEquals(t *testing.T, message string, expected string, actual Path) {
65 t.Helper()
66 AssertStringEquals(t, message, expected, PathRelativeToTop(actual))
67}
68
69// AssertPathsRelativeToTopEquals checks if the expected value is equal to the result of calling
70// PathsRelativeToTop on the actual Paths.
71func AssertPathsRelativeToTopEquals(t *testing.T, message string, expected []string, actual Paths) {
72 t.Helper()
73 AssertDeepEquals(t, message, expected, PathsRelativeToTop(actual))
74}
75
76// AssertStringPathRelativeToTopEquals checks if the expected value is equal to the result of calling
77// StringPathRelativeToTop on the actual string path.
78func AssertStringPathRelativeToTopEquals(t *testing.T, message string, config Config, expected string, actual string) {
79 t.Helper()
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +020080 AssertStringEquals(t, message, expected, StringPathRelativeToTop(config.soongOutDir, actual))
Paul Duffin567465d2021-03-16 01:21:34 +000081}
82
83// AssertStringPathsRelativeToTopEquals checks if the expected value is equal to the result of
84// calling StringPathsRelativeToTop on the actual string paths.
85func AssertStringPathsRelativeToTopEquals(t *testing.T, message string, config Config, expected []string, actual []string) {
86 t.Helper()
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +020087 AssertDeepEquals(t, message, expected, StringPathsRelativeToTop(config.soongOutDir, actual))
Paul Duffin567465d2021-03-16 01:21:34 +000088}
89
Paul Duffin9dcf2532021-03-12 11:50:43 +000090// AssertErrorMessageEquals checks if the error is not nil and has the expected message. If it does
91// not then this reports an error prefixed with the supplied message and including a reason for why
92// it failed.
93func AssertErrorMessageEquals(t *testing.T, message string, expected string, actual error) {
94 t.Helper()
95 if actual == nil {
96 t.Errorf("Expected error but was nil")
97 } else if actual.Error() != expected {
98 t.Errorf("%s: expected %s, actual %s", message, expected, actual.Error())
99 }
100}
101
102// AssertTrimmedStringEquals checks if the expected and actual values are the same after trimming
103// leading and trailing spaces from them both. If they are not then it reports an error prefixed
104// with the supplied message and including a reason for why it failed.
105func AssertTrimmedStringEquals(t *testing.T, message string, expected string, actual string) {
106 t.Helper()
107 AssertStringEquals(t, message, strings.TrimSpace(expected), strings.TrimSpace(actual))
108}
109
110// AssertStringDoesContain checks if the string contains the expected substring. If it does not
111// then it reports an error prefixed with the supplied message and including a reason for why it
112// failed.
113func AssertStringDoesContain(t *testing.T, message string, s string, expectedSubstring string) {
114 t.Helper()
115 if !strings.Contains(s, expectedSubstring) {
116 t.Errorf("%s: could not find %q within %q", message, expectedSubstring, s)
117 }
118}
119
120// AssertStringDoesNotContain checks if the string contains the expected substring. If it does then
121// it reports an error prefixed with the supplied message and including a reason for why it failed.
122func AssertStringDoesNotContain(t *testing.T, message string, s string, unexpectedSubstring string) {
123 t.Helper()
124 if strings.Contains(s, unexpectedSubstring) {
125 t.Errorf("%s: unexpectedly found %q within %q", message, unexpectedSubstring, s)
126 }
127}
128
Anton Hanssondae54cd2021-04-21 16:30:10 +0100129// AssertStringContainsEquals checks if the string contains or does not contain the substring, given
130// the value of the expected bool. If the expectation does not hold it reports an error prefixed with
131// the supplied message and including a reason for why it failed.
132func AssertStringContainsEquals(t *testing.T, message string, s string, substring string, expected bool) {
133 if expected {
134 AssertStringDoesContain(t, message, s, substring)
135 } else {
136 AssertStringDoesNotContain(t, message, s, substring)
137 }
138}
139
Paul Duffin9dcf2532021-03-12 11:50:43 +0000140// AssertStringListContains checks if the list of strings contains the expected string. If it does
141// not then it reports an error prefixed with the supplied message and including a reason for why it
142// failed.
Anton Hanssondae54cd2021-04-21 16:30:10 +0100143func AssertStringListContains(t *testing.T, message string, list []string, s string) {
Paul Duffin9dcf2532021-03-12 11:50:43 +0000144 t.Helper()
Anton Hanssondae54cd2021-04-21 16:30:10 +0100145 if !InList(s, list) {
146 t.Errorf("%s: could not find %q within %q", message, s, list)
147 }
148}
149
150// AssertStringListDoesNotContain checks if the list of strings contains the expected string. If it does
151// then it reports an error prefixed with the supplied message and including a reason for why it failed.
152func AssertStringListDoesNotContain(t *testing.T, message string, list []string, s string) {
153 t.Helper()
154 if InList(s, list) {
155 t.Errorf("%s: unexpectedly found %q within %q", message, s, list)
156 }
157}
158
159// AssertStringContainsEquals checks if the string contains or does not contain the substring, given
160// the value of the expected bool. If the expectation does not hold it reports an error prefixed with
161// the supplied message and including a reason for why it failed.
162func AssertStringListContainsEquals(t *testing.T, message string, list []string, s string, expected bool) {
Jooyung Han4973d452023-03-23 14:22:46 +0900163 t.Helper()
Anton Hanssondae54cd2021-04-21 16:30:10 +0100164 if expected {
165 AssertStringListContains(t, message, list, s)
166 } else {
167 AssertStringListDoesNotContain(t, message, list, s)
Paul Duffin9dcf2532021-03-12 11:50:43 +0000168 }
169}
170
171// AssertArrayString checks if the expected and actual values are equal and if they are not then it
172// reports an error prefixed with the supplied message and including a reason for why it failed.
173func AssertArrayString(t *testing.T, message string, expected, actual []string) {
174 t.Helper()
175 if len(actual) != len(expected) {
176 t.Errorf("%s: expected %d (%q), actual (%d) %q", message, len(expected), expected, len(actual), actual)
177 return
178 }
179 for i := range actual {
180 if actual[i] != expected[i] {
181 t.Errorf("%s: expected %d-th, %q (%q), actual %q (%q)",
182 message, i, expected[i], expected, actual[i], actual)
183 return
184 }
185 }
186}
187
188// AssertDeepEquals checks if the expected and actual values are equal using reflect.DeepEqual and
189// if they are not then it reports an error prefixed with the supplied message and including a
190// reason for why it failed.
191func AssertDeepEquals(t *testing.T, message string, expected interface{}, actual interface{}) {
192 t.Helper()
193 if !reflect.DeepEqual(actual, expected) {
194 t.Errorf("%s: expected:\n %#v\n got:\n %#v", message, expected, actual)
195 }
196}
197
Paul Duffin9f4b3bb2021-03-16 13:47:36 +0000198// AssertPanicMessageContains checks that the supplied function panics as expected and the message
199// obtained by formatting the recovered value as a string contains the expected contents.
200func AssertPanicMessageContains(t *testing.T, message, expectedMessageContents string, funcThatShouldPanic func()) {
Paul Duffin9dcf2532021-03-12 11:50:43 +0000201 t.Helper()
202 panicked := false
Paul Duffin9f4b3bb2021-03-16 13:47:36 +0000203 var recovered interface{}
Paul Duffin9dcf2532021-03-12 11:50:43 +0000204 func() {
205 defer func() {
Paul Duffin9f4b3bb2021-03-16 13:47:36 +0000206 if recovered = recover(); recovered != nil {
Paul Duffin9dcf2532021-03-12 11:50:43 +0000207 panicked = true
208 }
209 }()
210 funcThatShouldPanic()
211 }()
212 if !panicked {
Paul Duffin9f4b3bb2021-03-16 13:47:36 +0000213 t.Errorf("%s: did not panic", message)
Paul Duffin9dcf2532021-03-12 11:50:43 +0000214 }
Paul Duffin9f4b3bb2021-03-16 13:47:36 +0000215
216 panicMessage := fmt.Sprintf("%s", recovered)
217 AssertStringDoesContain(t, fmt.Sprintf("%s: panic message", message), panicMessage, expectedMessageContents)
Paul Duffin9dcf2532021-03-12 11:50:43 +0000218}